From: Tony Mack Date: Wed, 16 Sep 2009 00:19:53 +0000 (+0000) Subject: reimport person records when the person's public key has been updated/changed. save... X-Git-Tag: sfa-0.9-2~4 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=360779d7a069ccccf753ed8d55c09576f486d5c0;p=sfa.git reimport person records when the person's public key has been updated/changed. save know public keys in /etc/sfa/person_keys.py --- diff --git a/sfa/plc/sfa-import-plc.py b/sfa/plc/sfa-import-plc.py index ade97907..16eddb6d 100755 --- a/sfa/plc/sfa-import-plc.py +++ b/sfa/plc/sfa-import-plc.py @@ -43,11 +43,29 @@ def process_options(): name = opt[0] val = opt[1] + +def load_keys(filename): + keys = {} + tmp_dict = {} + try: + execfile(filename, tmp_dict) + if 'keys' in tmp_dict: + keys = tmp_dict['keys'] + return keys + except: + return keys + +def save_keys(filename, keys): + f = open(filename, 'w') + f.write("keys = %s" % str(keys)) + f.close() + def main(): process_options() config = Config() root_auth = config.SFA_REGISTRY_ROOT_AUTH level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH + keys_filename = config.config_path + os.sep + 'person_keys.py' sfaImporter = sfaImport() shell = sfaImporter.shell plc_auth = sfaImporter.plc_auth @@ -83,6 +101,8 @@ def main(): # create dict of all existing sfa records existing_records = {} existing_hrns = [] + key_ids = [] + person_keys = {} results = table.find() for result in results: existing_records[(result['hrn'], result['type'])] = result @@ -99,6 +119,21 @@ def main(): persons_dict = {} for person in persons: persons_dict[person['person_id']] = person + key_ids.extend(person['key_ids']) + + # Get all public keys + keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids}) + keys_dict = {} + for key in keys: + keys_dict[key['key_id']] = key['key'] + + # create a dict of person keys keyed on key_id + old_person_keys = load_keys(keys_filename) + for person in persons: + pubkeys = [] + for key_id in person['key_ids']: + pubkeys.append(keys_dict[key_id]) + person_keys[person['person_id']] = pubkeys # Get all plc nodes nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id']) @@ -147,11 +182,22 @@ def main(): continue person = persons_dict[person_id] hrn = email_to_hrn(site_hrn, person['email']) + old_keys = [] + new_keys = [] + if person_id in old_person_keys: + old_keys = old_person_keys[person_id] + if person_id in person_keys: + new_keys = person_keys[person_id] + update_record = False + for key in new_keys: + if key not in old_keys: + update_record = True + if hrn not in existing_hrns or \ - (hrn, 'user') not in existing_records: + (hrn, 'user') not in existing_records or update_record: sfaImporter.import_person(site_hrn, person) - + # remove stale records for (record_hrn, type) in existing_records.keys(): found = False @@ -201,7 +247,9 @@ def main(): record_object = existing_records[(record_hrn, type)] sfaImporter.delete_record(record_hrn, type) - + # save pub keys + trace('saving current pub keys') + save_keys(keys_filename, person_keys) if __name__ == "__main__": main() diff --git a/sfa/plc/sfaImport.py b/sfa/plc/sfaImport.py index 79255210..fd8c08b9 100644 --- a/sfa/plc/sfaImport.py +++ b/sfa/plc/sfaImport.py @@ -129,11 +129,12 @@ class sfaImport: person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey) table = GeniTable() person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id']) - try: + existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']}) + if not existing_records: table.insert(person_record) - except: + else: trace("Import: %s exists, updating " % hrn) - existing_record = table.find(person_record) + existing_record = existing_records[0] person_record['record_id'] = existing_record['record_id'] table.update(person_record) @@ -153,11 +154,12 @@ class sfaImport: slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey) slice_record = GeniRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id']) table = GeniTable() - try: + existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']}) + if not existing_records: table.insert(slice_record) - except: + else: trace("Import: %s exists, updating " % hrn) - existing_record = table.find(slice_record) + existing_record = existing_records[0] slice_record['record_id'] = existing_record['record_id'] table.update(slice_record) @@ -181,11 +183,12 @@ class sfaImport: pkey = Keypair(create=True) node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey) node_record = GeniRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id']) - try: + existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']}) + if not existing_records: table.insert(node_record) - except: + else: trace("Import: %s exists, updating " % hrn) - existing_record = table.find(node_record) + existing_record = existing_records[0] node_record['record_id'] = existing_record['record_id'] table.update(node_record) @@ -220,11 +223,12 @@ class sfaImport: table = GeniTable() auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id']) - try: + existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']}) + if not existing_records: table.insert(auth_record) - except: + else: trace("Import: %s exists, updating " % hrn) - existing_record = table.find(auth_record) + existing_record = existing_record[0] auth_record['record_id'] = existing_record['record_id'] table.update(auth_record)