4 # Import PLC records into the SFA database. It is indended that this tool be
5 # run once to create SFA records that reflect the current state of the
8 # The import tool assumes that the existing PLC hierarchy should all be part
9 # of "planetlab.us" (see the root_auth and level1_auth variables below).
11 # Public keys are extracted from the users' SSH keys automatically and used to
12 # create GIDs. This is relatively experimental as a custom tool had to be
13 # written to perform conversion from SSH to OpenSSL format. It only supports
14 # RSA keys at this time, not DSA keys.
21 from sfa.util.record import *
22 from sfa.util.table import SfaTable
23 from sfa.util.namespace import *
24 from sfa.util.config import Config
25 from sfa.trust.certificate import convert_public_key, Keypair
26 from sfa.trust.trustedroot import *
27 from sfa.trust.hierarchy import *
28 from sfa.plc.api import *
29 from sfa.trust.gid import create_uuid
30 from sfa.plc.sfaImport import sfaImport
32 def process_options():
34 (options, args) = getopt.getopt(sys.argv[1:], '', [])
40 def load_keys(filename):
44 execfile(filename, tmp_dict)
45 if 'keys' in tmp_dict:
46 keys = tmp_dict['keys']
51 def save_keys(filename, keys):
52 f = open(filename, 'w')
53 f.write("keys = %s" % str(keys))
60 if not config.SFA_REGISTRY_ENABLED:
62 root_auth = config.SFA_REGISTRY_ROOT_AUTH
63 interface_hrn = config.SFA_INTERFACE_HRN
64 keys_filename = config.config_path + os.sep + 'person_keys.py'
65 sfaImporter = sfaImport()
66 if config.SFA_API_DEBUG: sfaImporter.logger.setLevelDebug()
67 shell = sfaImporter.shell
68 plc_auth = sfaImporter.plc_auth
71 if not table.exists():
74 # create root authority
75 sfaImporter.create_top_level_auth_records(root_auth)
76 if not root_auth == interface_hrn:
77 sfaImporter.create_top_level_auth_records(interface_hrn)
79 sfaImporter.logger.info("Import: adding " + interface_hrn + " to trusted list")
80 authority = sfaImporter.AuthHierarchy.get_auth_info(interface_hrn)
81 sfaImporter.TrustedRoots.add_gid(authority.get_gid_object())
83 if ".vini" in interface_hrn and interface_hrn.endswith('vini'):
84 # create a fake internet2 site first
85 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
86 'login_base': 'internet2', 'site_id': -1}
87 sfaImporter.import_site(interface_hrn, i2site)
89 # create dict of all existing sfa records
94 results = table.find()
95 for result in results:
96 existing_records[(result['hrn'], result['type'])] = result
97 existing_hrns.append(result['hrn'])
100 sites = shell.GetSites(plc_auth, {'peer_id': None})
103 sites_dict[site['login_base']] = site
106 persons = shell.GetPersons(plc_auth, {'peer_id': None, 'enabled': True}, ['person_id', 'email', 'key_ids', 'site_ids'])
108 for person in persons:
109 persons_dict[person['person_id']] = person
110 key_ids.extend(person['key_ids'])
112 # Get all public keys
113 keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
116 keys_dict[key['key_id']] = key['key']
118 # create a dict of person keys keyed on key_id
119 old_person_keys = load_keys(keys_filename)
120 for person in persons:
122 for key_id in person['key_ids']:
123 pubkeys.append(keys_dict[key_id])
124 person_keys[person['person_id']] = pubkeys
127 nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
130 nodes_dict[node['node_id']] = node
133 slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
136 slices_dict[slice['slice_id']] = slice
139 site_hrn = interface_hrn + "." + site['login_base']
140 print "Importing site: %s" % site_hrn
142 # import if hrn is not in list of existing hrns or if the hrn exists
143 # but its not a site record
144 if site_hrn not in existing_hrns or \
145 (site_hrn, 'authority') not in existing_records:
146 site_hrn = sfaImporter.import_site(interface_hrn, site)
148 # import node records
149 for node_id in site['node_ids']:
150 if node_id not in nodes_dict:
152 node = nodes_dict[node_id]
153 hrn = hostname_to_hrn(interface_hrn, site['login_base'], node['hostname'])
154 if hrn not in existing_hrns or \
155 (hrn, 'node') not in existing_records:
156 sfaImporter.import_node(site_hrn, node)
159 for slice_id in site['slice_ids']:
160 if slice_id not in slices_dict:
162 slice = slices_dict[slice_id]
163 hrn = slicename_to_hrn(interface_hrn, slice['name'])
164 if hrn not in existing_hrns or \
165 (hrn, 'slice') not in existing_records:
166 sfaImporter.import_slice(site_hrn, slice)
169 for person_id in site['person_ids']:
170 if person_id not in persons_dict:
172 person = persons_dict[person_id]
173 hrn = email_to_hrn(site_hrn, person['email'])
176 if person_id in old_person_keys:
177 old_keys = old_person_keys[person_id]
178 if person_id in person_keys:
179 new_keys = person_keys[person_id]
180 update_record = False
182 if key not in old_keys:
185 if hrn not in existing_hrns or \
186 (hrn, 'user') not in existing_records or update_record:
187 sfaImporter.import_person(site_hrn, person)
189 # remove stale records
190 for (record_hrn, type) in existing_records.keys():
191 record = existing_records[(record_hrn, type)]
192 # if this is the interface name dont do anything
193 if record_hrn == interface_hrn or \
194 record_hrn == root_auth or \
195 record['peer_authority']:
197 # dont delete vini's internet2 placeholdder record
198 # normally this would be deleted becuase it does not have a plc record
199 if ".vini" in interface_hrn and interface_hrn.endswith('vini') and \
200 record_hrn.endswith("internet2"):
205 if type == 'authority':
207 site_hrn = interface_hrn + "." + site['login_base']
208 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
213 login_base = get_leaf(get_authority(record_hrn))
214 username = get_leaf(record_hrn)
215 if login_base in sites_dict:
216 site = sites_dict[login_base]
217 for person in persons:
218 tmp_username = person['email'].split("@")[0]
219 alt_username = person['email'].split("@")[0].replace(".", "_")
220 if username in [tmp_username, alt_username] and \
221 site['site_id'] in person['site_ids'] and \
222 person['person_id'] == record['pointer']:
226 elif type == 'slice':
227 slicename = hrn_to_pl_slicename(record_hrn)
229 if slicename == slice['name'] and \
230 slice['slice_id'] == record['pointer']:
235 login_base = get_leaf(get_authority(record_hrn))
236 nodename = get_leaf(record_hrn)
237 if login_base in sites_dict:
238 site = sites_dict[login_base]
240 tmp_nodename = node['hostname'].split(".")[0]
241 if tmp_nodename == nodename and \
242 node['site_id'] == site['site_id'] and \
243 node['node_id'] == record['pointer']:
250 record_object = existing_records[(record_hrn, type)]
251 sfaImporter.delete_record(record_hrn, type)
254 sfaImporter.logger.info('Import: saving current pub keys')
255 save_keys(keys_filename, person_keys)
257 if __name__ == "__main__":