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.xrn import get_leaf, get_authority
24 from sfa.util.plxrn import hostname_to_hrn, slicename_to_hrn, email_to_hrn, hrn_to_pl_slicename
25 from sfa.util.config import Config
26 from sfa.trust.certificate import convert_public_key, Keypair
27 from sfa.trust.trustedroot import *
28 from sfa.trust.hierarchy import *
29 from sfa.plc.api import *
30 from sfa.trust.gid import create_uuid
31 from sfa.plc.sfaImport import sfaImport
33 def process_options():
35 (options, args) = getopt.getopt(sys.argv[1:], '', [])
41 def load_keys(filename):
45 execfile(filename, tmp_dict)
46 if 'keys' in tmp_dict:
47 keys = tmp_dict['keys']
52 def save_keys(filename, keys):
53 f = open(filename, 'w')
54 f.write("keys = %s" % str(keys))
61 if not config.SFA_REGISTRY_ENABLED:
63 root_auth = config.SFA_REGISTRY_ROOT_AUTH
64 interface_hrn = config.SFA_INTERFACE_HRN
65 keys_filename = config.config_path + os.sep + 'person_keys.py'
66 sfaImporter = sfaImport()
67 if config.SFA_API_DEBUG: sfaImporter.logger.setLevelDebug()
68 shell = sfaImporter.shell
69 plc_auth = sfaImporter.plc_auth
72 if not table.exists():
75 # create root authority
76 sfaImporter.create_top_level_auth_records(root_auth)
77 if not root_auth == interface_hrn:
78 sfaImporter.create_top_level_auth_records(interface_hrn)
80 sfaImporter.logger.info("Import: adding " + interface_hrn + " to trusted list")
81 authority = sfaImporter.AuthHierarchy.get_auth_info(interface_hrn)
82 sfaImporter.TrustedRoots.add_gid(authority.get_gid_object())
84 if ".vini" in interface_hrn and interface_hrn.endswith('vini'):
85 # create a fake internet2 site first
86 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
87 'login_base': 'internet2', 'site_id': -1}
88 sfaImporter.import_site(interface_hrn, i2site)
90 # create dict of all existing sfa records
95 results = table.find()
96 for result in results:
97 existing_records[(result['hrn'], result['type'])] = result
98 existing_hrns.append(result['hrn'])
101 sites = shell.GetSites(plc_auth, {'peer_id': None})
104 sites_dict[site['login_base']] = site
107 persons = shell.GetPersons(plc_auth, {'peer_id': None, 'enabled': True}, ['person_id', 'email', 'key_ids', 'site_ids'])
109 for person in persons:
110 persons_dict[person['person_id']] = person
111 key_ids.extend(person['key_ids'])
113 # Get all public keys
114 keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
117 keys_dict[key['key_id']] = key['key']
119 # create a dict of person keys keyed on key_id
120 old_person_keys = load_keys(keys_filename)
121 for person in persons:
123 for key_id in person['key_ids']:
124 pubkeys.append(keys_dict[key_id])
125 person_keys[person['person_id']] = pubkeys
128 nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
131 nodes_dict[node['node_id']] = node
134 slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
137 slices_dict[slice['slice_id']] = slice
140 site_hrn = interface_hrn + "." + site['login_base']
141 sfa_logger().info("Importing site: %s" % site_hrn)
143 # import if hrn is not in list of existing hrns or if the hrn exists
144 # but its not a site record
145 if site_hrn not in existing_hrns or \
146 (site_hrn, 'authority') not in existing_records:
147 site_hrn = sfaImporter.import_site(interface_hrn, site)
149 # import node records
150 for node_id in site['node_ids']:
151 if node_id not in nodes_dict:
153 node = nodes_dict[node_id]
154 hrn = hostname_to_hrn(interface_hrn, site['login_base'], node['hostname'])
155 if hrn not in existing_hrns or \
156 (hrn, 'node') not in existing_records:
157 sfaImporter.import_node(site_hrn, node)
160 for slice_id in site['slice_ids']:
161 if slice_id not in slices_dict:
163 slice = slices_dict[slice_id]
164 hrn = slicename_to_hrn(interface_hrn, slice['name'])
165 if hrn not in existing_hrns or \
166 (hrn, 'slice') not in existing_records:
167 sfaImporter.import_slice(site_hrn, slice)
170 for person_id in site['person_ids']:
171 if person_id not in persons_dict:
173 person = persons_dict[person_id]
174 hrn = email_to_hrn(site_hrn, person['email'])
177 if person_id in old_person_keys:
178 old_keys = old_person_keys[person_id]
179 if person_id in person_keys:
180 new_keys = person_keys[person_id]
181 update_record = False
183 if key not in old_keys:
186 if hrn not in existing_hrns or \
187 (hrn, 'user') not in existing_records or update_record:
188 sfaImporter.import_person(site_hrn, person)
190 # remove stale records
191 for (record_hrn, type) in existing_records.keys():
192 record = existing_records[(record_hrn, type)]
193 # if this is the interface name dont do anything
194 if record_hrn == interface_hrn or \
195 record_hrn == root_auth or \
196 record['peer_authority']:
198 # dont delete vini's internet2 placeholdder record
199 # normally this would be deleted becuase it does not have a plc record
200 if ".vini" in interface_hrn and interface_hrn.endswith('vini') and \
201 record_hrn.endswith("internet2"):
206 if type == 'authority':
208 site_hrn = interface_hrn + "." + site['login_base']
209 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
214 login_base = get_leaf(get_authority(record_hrn))
215 username = get_leaf(record_hrn)
216 if login_base in sites_dict:
217 site = sites_dict[login_base]
218 for person in persons:
219 tmp_username = person['email'].split("@")[0]
220 alt_username = person['email'].split("@")[0].replace(".", "_")
221 if username in [tmp_username, alt_username] and \
222 site['site_id'] in person['site_ids'] and \
223 person['person_id'] == record['pointer']:
227 elif type == 'slice':
228 slicename = hrn_to_pl_slicename(record_hrn)
230 if slicename == slice['name'] and \
231 slice['slice_id'] == record['pointer']:
236 login_base = get_leaf(get_authority(record_hrn))
237 nodename = get_leaf(record_hrn)
238 if login_base in sites_dict:
239 site = sites_dict[login_base]
241 tmp_nodename = node['hostname'].split(".")[0]
242 if tmp_nodename == nodename and \
243 node['site_id'] == site['site_id'] and \
244 node['node_id'] == record['pointer']:
251 record_object = existing_records[(record_hrn, type)]
252 sfaImporter.delete_record(record_hrn, type)
255 sfaImporter.logger.info('Import: saving current pub keys')
256 save_keys(keys_filename, person_keys)
258 if __name__ == "__main__":