7 # Import PLC records into the SFA database. It is indended that this tool be
8 # run once to create SFA 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.table import SfaTable
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.trust.gid import create_uuid
34 from sfa.plc.sfaImport import *
35 from sfa.util.report import trace, error
37 def process_options():
40 (options, args) = getopt.getopt(sys.argv[1:], '', [])
46 def load_keys(filename):
50 execfile(filename, tmp_dict)
51 if 'keys' in tmp_dict:
52 keys = tmp_dict['keys']
57 def save_keys(filename, keys):
58 f = open(filename, 'w')
59 f.write("keys = %s" % str(keys))
64 LOGFILE='/var/log/sfa_import_plc.log'
65 logging.basicConfig(level=logging.INFO,
66 format='%(asctime)s - %(message)s',
68 rotate_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=1000000, backupCount=5)
69 logger = logging.getLogger()
70 logger.addHandler(rotate_handler)
74 if not config.SFA_REGISTRY_ENABLED:
76 root_auth = config.SFA_REGISTRY_ROOT_AUTH
77 interface_hrn = config.SFA_INTERFACE_HRN
78 keys_filename = config.config_path + os.sep + 'person_keys.py'
79 sfaImporter = sfaImport(logger)
80 shell = sfaImporter.shell
81 plc_auth = sfaImporter.plc_auth
82 AuthHierarchy = sfaImporter.AuthHierarchy
83 TrustedRoots = sfaImporter.TrustedRoots
85 if not table.exists():
88 # create root authority
89 sfaImporter.create_top_level_auth_records(root_auth)
90 if not root_auth == interface_hrn:
91 sfaImporter.create_top_level_auth_records(interface_hrn)
93 trace("Import: adding " + interface_hrn + " to trusted list", logger)
94 authority = AuthHierarchy.get_auth_info(interface_hrn)
95 TrustedRoots.add_gid(authority.get_gid_object())
97 if ".vini" in interface_hrn and interface_hrn.endswith('vini'):
98 # create a fake internet2 site first
99 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
100 'login_base': 'internet2', 'site_id': -1}
101 sfaImporter.import_site(interface_hrn, i2site)
103 # create dict of all existing sfa records
104 existing_records = {}
108 results = table.find()
109 for result in results:
110 existing_records[(result['hrn'], result['type'])] = result
111 existing_hrns.append(result['hrn'])
114 sites = shell.GetSites(plc_auth, {'peer_id': None})
117 sites_dict[site['login_base']] = site
120 persons = shell.GetPersons(plc_auth, {'peer_id': None}, ['person_id', 'email', 'key_ids', 'site_ids'])
122 for person in persons:
123 persons_dict[person['person_id']] = person
124 key_ids.extend(person['key_ids'])
126 # Get all public keys
127 keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
130 keys_dict[key['key_id']] = key['key']
132 # create a dict of person keys keyed on key_id
133 old_person_keys = load_keys(keys_filename)
134 for person in persons:
136 for key_id in person['key_ids']:
137 pubkeys.append(keys_dict[key_id])
138 person_keys[person['person_id']] = pubkeys
141 nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
144 nodes_dict[node['node_id']] = node
147 slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
150 slices_dict[slice['slice_id']] = slice
154 site_hrn = interface_hrn + "." + site['login_base']
155 # import if hrn is not in list of existing hrns or if the hrn exists
156 # but its not a site record
157 if site_hrn not in existing_hrns or \
158 (site_hrn, 'authority') not in existing_records:
159 site_hrn = sfaImporter.import_site(interface_hrn, site)
161 # import node records
162 for node_id in site['node_ids']:
163 if node_id not in nodes_dict:
165 node = nodes_dict[node_id]
166 hrn = hostname_to_hrn(interface_hrn, site['login_base'], node['hostname'])
167 if hrn not in existing_hrns or \
168 (hrn, 'node') not in existing_records:
169 sfaImporter.import_node(site_hrn, node)
172 for slice_id in site['slice_ids']:
173 if slice_id not in slices_dict:
175 slice = slices_dict[slice_id]
176 hrn = slicename_to_hrn(interface_hrn, slice['name'])
177 if hrn not in existing_hrns or \
178 (hrn, 'slice') not in existing_records:
179 sfaImporter.import_slice(site_hrn, slice)
182 for person_id in site['person_ids']:
183 if person_id not in persons_dict:
185 person = persons_dict[person_id]
186 hrn = email_to_hrn(site_hrn, person['email'])
189 if person_id in old_person_keys:
190 old_keys = old_person_keys[person_id]
191 if person_id in person_keys:
192 new_keys = person_keys[person_id]
193 update_record = False
195 if key not in old_keys:
198 if hrn not in existing_hrns or \
199 (hrn, 'user') not in existing_records or update_record:
200 sfaImporter.import_person(site_hrn, person)
202 # remove stale records
203 for (record_hrn, type) in existing_records.keys():
204 record = existing_records[(record_hrn, type)]
205 # if this is the interface name dont do anything
206 if record_hrn == interface_hrn or \
207 record_hrn == root_auth or \
208 record['peer_authority']:
210 # dont delete vini's internet2 placeholdder record
211 # normally this would be deleted becuase it does not have a plc record
212 if ".vini" in interface_hrn and interface_hrn.endswith('vini') and \
213 record_hrn.endswith("internet2"):
218 if type == 'authority':
220 site_hrn = interface_hrn + "." + site['login_base']
221 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
226 login_base = get_leaf(get_authority(record_hrn))
227 username = get_leaf(record_hrn)
228 if login_base in sites_dict:
229 site = sites_dict[login_base]
230 for person in persons:
231 tmp_username = person['email'].split("@")[0]
232 alt_username = person['email'].split("@")[0].replace(".", "_")
233 if username in [tmp_username, alt_username] and \
234 site['site_id'] in person['site_ids'] and \
235 person['person_id'] == record['pointer']:
239 elif type == 'slice':
240 slicename = hrn_to_pl_slicename(record_hrn)
242 if slicename == slice['name'] and \
243 slice['slice_id'] == record['pointer']:
248 login_base = get_leaf(get_authority(record_hrn))
249 nodename = get_leaf(record_hrn)
250 if login_base in sites_dict:
251 site = sites_dict[login_base]
253 tmp_nodename = node['hostname'].split(".")[0]
254 if tmp_nodename == nodename and \
255 node['site_id'] == site['site_id'] and \
256 node['node_id'] == record['pointer']:
263 record_object = existing_records[(record_hrn, type)]
264 sfaImporter.delete_record(record_hrn, type)
267 trace('Import: saving current pub keys', logger)
268 save_keys(keys_filename, person_keys)
270 if __name__ == "__main__":