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.trustedroots import *
28 from sfa.trust.hierarchy import *
29 from sfa.util.xrn import Xrn
30 from sfa.plc.api import *
31 from sfa.trust.gid import create_uuid
32 from sfa.plc.sfaImport import sfaImport, _cleanup_string
34 def process_options():
36 (options, args) = getopt.getopt(sys.argv[1:], '', [])
42 def load_keys(filename):
46 execfile(filename, tmp_dict)
47 if 'keys' in tmp_dict:
48 keys = tmp_dict['keys']
53 def save_keys(filename, keys):
54 f = open(filename, 'w')
55 f.write("keys = %s" % str(keys))
58 def _get_site_hrn(interface_hrn, site):
59 # Hardcode 'internet2' into the hrn for sites hosting
60 # internet2 nodes. This is a special operation for some vini
62 hrn = ".".join([interface_hrn, site['login_base']])
63 if ".vini" in interface_hrn and interface_hrn.endswith('vini'):
64 if site['login_base'].startswith("i2") or site['login_base'].startswith("nlr"):
65 hrn = ".".join([interface_hrn, "internet2", site['login_base']])
72 if not config.SFA_REGISTRY_ENABLED:
74 root_auth = config.SFA_REGISTRY_ROOT_AUTH
75 interface_hrn = config.SFA_INTERFACE_HRN
76 keys_filename = config.config_path + os.sep + 'person_keys.py'
77 sfaImporter = sfaImport()
78 if config.SFA_API_DEBUG: sfaImporter.logger.setLevelDebug()
79 shell = sfaImporter.shell
80 plc_auth = sfaImporter.plc_auth
82 # initialize registry db table
84 if not table.exists():
87 # create root authority
88 sfaImporter.create_top_level_auth_records(root_auth)
89 if not root_auth == interface_hrn:
90 sfaImporter.create_top_level_auth_records(interface_hrn)
92 # create interface records
93 sfaImporter.logger.info("Import: creating interface records")
94 sfaImporter.create_interface_records()
96 # add local root authority's cert to trusted list
97 sfaImporter.logger.info("Import: adding " + interface_hrn + " to trusted list")
98 authority = sfaImporter.AuthHierarchy.get_auth_info(interface_hrn)
99 sfaImporter.TrustedRoots.add_gid(authority.get_gid_object())
101 # special case for vini
102 if ".vini" in interface_hrn and interface_hrn.endswith('vini'):
103 # create a fake internet2 site first
104 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
105 'login_base': 'internet2', 'site_id': -1}
106 sfaImporter.import_site(interface_hrn, i2site)
108 # create dict of all existing sfa records
109 existing_records = {}
113 results = table.find()
114 for result in results:
115 existing_records[(result['hrn'], result['type'])] = result
116 existing_hrns.append(result['hrn'])
119 sites = shell.GetSites(plc_auth, {'peer_id': None})
122 sites_dict[site['login_base']] = site
125 persons = shell.GetPersons(plc_auth, {'peer_id': None, 'enabled': True}, ['person_id', 'email', 'key_ids', 'site_ids'])
127 for person in persons:
128 persons_dict[person['person_id']] = person
129 key_ids.extend(person['key_ids'])
131 # Get all public keys
132 keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
135 keys_dict[key['key_id']] = key['key']
137 # create a dict of person keys keyed on key_id
138 old_person_keys = load_keys(keys_filename)
139 for person in persons:
141 for key_id in person['key_ids']:
142 pubkeys.append(keys_dict[key_id])
143 person_keys[person['person_id']] = pubkeys
146 nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
149 nodes_dict[node['node_id']] = node
152 slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
155 slices_dict[slice['slice_id']] = slice
158 site_hrn = _get_site_hrn(interface_hrn, site)
159 sfaImporter.logger.info("Importing site: %s" % site_hrn)
161 # import if hrn is not in list of existing hrns or if the hrn exists
162 # but its not a site record
163 if site_hrn not in existing_hrns or \
164 (site_hrn, 'authority') not in existing_records:
165 sfaImporter.import_site(site_hrn, site)
167 # import node records
168 for node_id in site['node_ids']:
169 if node_id not in nodes_dict:
171 node = nodes_dict[node_id]
172 root_auth = get_authority(site_hrn)
173 site_name = get_leaf(site_hrn)
174 hrn = hostname_to_hrn(root_auth, site_name, node['hostname'])
175 if hrn not in existing_hrns or \
176 (hrn, 'node') not in existing_records:
177 sfaImporter.import_node(hrn, node)
180 for slice_id in site['slice_ids']:
181 if slice_id not in slices_dict:
183 slice = slices_dict[slice_id]
184 hrn = slicename_to_hrn(interface_hrn, slice['name'])
185 if hrn not in existing_hrns or \
186 (hrn, 'slice') not in existing_records:
187 sfaImporter.import_slice(site_hrn, slice)
190 for person_id in site['person_ids']:
191 if person_id not in persons_dict:
193 person = persons_dict[person_id]
194 hrn = email_to_hrn(site_hrn, person['email'])
197 if person_id in old_person_keys:
198 old_keys = old_person_keys[person_id]
199 if person_id in person_keys:
200 new_keys = person_keys[person_id]
201 update_record = False
203 if key not in old_keys:
206 if hrn not in existing_hrns or \
207 (hrn, 'user') not in existing_records or update_record:
208 sfaImporter.import_person(site_hrn, person)
210 # remove stale records
211 for (record_hrn, type) in existing_records.keys():
212 record = existing_records[(record_hrn, type)]
213 # if this is the interface name dont do anything
214 if record_hrn == interface_hrn or \
215 record_hrn == root_auth or \
216 record['peer_authority']:
218 # dont delete vini's internet2 placeholdder record
219 # normally this would be deleted becuase it does not have a plc record
220 if ".vini" in interface_hrn and interface_hrn.endswith('vini') and \
221 record_hrn.endswith("internet2"):
226 if type == 'authority':
228 site_hrn = interface_hrn + "." + site['login_base']
229 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
234 login_base = get_leaf(get_authority(record_hrn))
235 username = get_leaf(record_hrn)
236 if login_base in sites_dict:
237 site = sites_dict[login_base]
238 for person in persons:
239 tmp_username = person['email'].split("@")[0]
240 alt_username = person['email'].split("@")[0].replace(".", "_").replace("+", "_")
241 if username in [tmp_username, alt_username] and \
242 site['site_id'] in person['site_ids'] and \
243 person['person_id'] == record['pointer']:
247 elif type == 'slice':
248 slicename = hrn_to_pl_slicename(record_hrn)
250 if slicename == slice['name'] and \
251 slice['slice_id'] == record['pointer']:
256 login_base = get_leaf(get_authority(record_hrn))
257 nodename = Xrn.unescape(get_leaf(record_hrn))
258 if login_base in sites_dict:
259 site = sites_dict[login_base]
261 tmp_nodename = node['hostname']
262 if tmp_nodename == nodename and \
263 node['site_id'] == site['site_id'] and \
264 node['node_id'] == record['pointer']:
271 record_object = existing_records[(record_hrn, type)]
272 sfaImporter.delete_record(record_hrn, type)
275 sfaImporter.logger.info('Import: saving current pub keys')
276 save_keys(keys_filename, person_keys)
278 if __name__ == "__main__":