7 # Import PLC records into the Geni database. It is indended that this tool be
8 # run once to create Geni records that reflect the current state of the
11 # The import tool assumes that the existing PLC hierarchy should all be part
12 # of "planetlab.us" (see the root_auth and level1_auth variables below).
14 # Public keys are extracted from the users' SSH keys automatically and used to
15 # create GIDs. This is relatively experimental as a custom tool had to be
16 # written to perform conversion from SSH to OpenSSL format. It only supports
17 # RSA keys at this time, not DSA keys.
23 import logging.handlers
25 from sfa.util.record import *
26 from sfa.util.genitable import GeniTable
27 from sfa.util.namespace import *
28 from sfa.util.config import Config
29 from sfa.trust.certificate import convert_public_key, Keypair
30 from sfa.trust.trustedroot import *
31 from sfa.trust.hierarchy import *
32 from sfa.plc.api import *
33 from sfa.util.geniclient import *
34 from sfa.trust.gid import create_uuid
35 from sfa.plc.sfaImport import *
36 from sfa.util.report import trace, error
38 def process_options():
41 (options, args) = getopt.getopt(sys.argv[1:], '', [])
47 def load_keys(filename):
51 execfile(filename, tmp_dict)
52 if 'keys' in tmp_dict:
53 keys = tmp_dict['keys']
58 def save_keys(filename, keys):
59 f = open(filename, 'w')
60 f.write("keys = %s" % str(keys))
65 LOGFILE='/var/log/sfa_import_plc.log'
66 logging.basicConfig(level=logging.INFO,
67 format='%(asctime)s - %(message)s',
69 rotate_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=1000000, backupCount=5)
70 logger = logging.getLogger()
71 logger.addHandler(rotate_handler)
75 if not config.SFA_REGISTRY_ENABLED:
77 root_auth = config.SFA_REGISTRY_ROOT_AUTH
78 level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH
79 keys_filename = config.config_path + os.sep + 'person_keys.py'
80 sfaImporter = sfaImport(logger)
81 shell = sfaImporter.shell
82 plc_auth = sfaImporter.plc_auth
83 AuthHierarchy = sfaImporter.AuthHierarchy
84 TrustedRoots = sfaImporter.TrustedRoots
86 if not table.exists():
89 if not level1_auth or level1_auth in ['']:
93 sfaImporter.create_top_level_auth_records(root_auth)
94 import_auth = root_auth
96 if not AuthHierarchy.auth_exists(level1_auth):
97 AuthHierarchy.create_auth(level1_auth)
98 sfaImporter.create_top_level_auth_records(level1_auth)
99 import_auth = level1_auth
101 trace("Import: adding" + import_auth + "to trusted list", logger)
102 authority = AuthHierarchy.get_auth_info(import_auth)
103 TrustedRoots.add_gid(authority.get_gid_object())
105 if ".vini" in import_auth and import_auth.endswith('vini'):
106 # create a fake internet2 site first
107 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
108 'login_base': 'internet2', 'site_id': -1}
109 sfaImporter.import_site(import_auth, i2site)
111 # create dict of all existing sfa records
112 existing_records = {}
116 results = table.find()
117 for result in results:
118 existing_records[(result['hrn'], result['type'])] = result
119 existing_hrns.append(result['hrn'])
122 sites = shell.GetSites(plc_auth, {'peer_id': None})
125 sites_dict[site['login_base']] = site
128 persons = shell.GetPersons(plc_auth, {'peer_id': None}, ['person_id', 'email', 'key_ids', 'site_ids'])
130 for person in persons:
131 persons_dict[person['person_id']] = person
132 key_ids.extend(person['key_ids'])
134 # Get all public keys
135 keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
138 keys_dict[key['key_id']] = key['key']
140 # create a dict of person keys keyed on key_id
141 old_person_keys = load_keys(keys_filename)
142 for person in persons:
144 for key_id in person['key_ids']:
145 pubkeys.append(keys_dict[key_id])
146 person_keys[person['person_id']] = pubkeys
149 nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
152 nodes_dict[node['node_id']] = node
155 slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
158 slices_dict[slice['slice_id']] = slice
162 site_hrn = import_auth + "." + site['login_base']
163 # import if hrn is not in list of existing hrns or if the hrn exists
164 # but its not a site record
165 if site_hrn not in existing_hrns or \
166 (site_hrn, 'authority') not in existing_records:
167 site_hrn = sfaImporter.import_site(import_auth, site)
169 # import node records
170 for node_id in site['node_ids']:
171 if node_id not in nodes_dict:
173 node = nodes_dict[node_id]
174 hrn = hostname_to_hrn(import_auth, site['login_base'], node['hostname'])
175 if hrn not in existing_hrns or \
176 (hrn, 'node') not in existing_records:
177 sfaImporter.import_node(site_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(import_auth, 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 == import_auth or record['peer_authority']:
216 # dont delete vini's internet2 placeholdder record
217 # normally this would be deleted becuase it does not have a plc record
218 if ".vini" in import_auth and import_auth.endswith('vini') and \
219 record_hrn.endswith("internet2"):
224 if type == 'authority':
226 site_hrn = import_auth + "." + site['login_base']
227 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
232 login_base = get_leaf(get_authority(record_hrn))
233 username = get_leaf(record_hrn)
234 if login_base in sites_dict:
235 site = sites_dict[login_base]
236 for person in persons:
237 tmp_username = person['email'].split("@")[0]
238 alt_username = person['email'].split("@")[0].replace(".", "_")
239 if username in [tmp_username, alt_username] and \
240 site['site_id'] in person['site_ids'] and \
241 person['person_id'] == record['pointer']:
245 elif type == 'slice':
246 slicename = hrn_to_pl_slicename(record_hrn)
248 if slicename == slice['name'] and \
249 slice['slice_id'] == record['pointer']:
254 login_base = get_leaf(get_authority(record_hrn))
255 nodename = get_leaf(record_hrn)
256 if login_base in sites_dict:
257 site = sites_dict[login_base]
259 tmp_nodename = node['hostname'].split(".")[0]
260 if tmp_nodename == nodename and \
261 node['site_id'] == site['site_id'] and \
262 node['node_id'] == record['pointer']:
269 record_object = existing_records[(record_hrn, type)]
270 sfaImporter.delete_record(record_hrn, type)
273 trace('Import: saving current pub keys', logger)
274 save_keys(keys_filename, person_keys)
276 if __name__ == "__main__":