importer revisited - sfa-import.py sfa-nuke.py (no more -plc)
[sfa.git] / sfa / importer / sfaimporter.py
1 #
2 # Public keys are extracted from the users' SSH keys automatically and used to
3 # create GIDs. This is relatively experimental as a custom tool had to be
4 # written to perform conversion from SSH to OpenSSL format. It only supports
5 # RSA keys at this time, not DSA keys.
6 ##
7
8 from sfa.util.xrn import get_authority, hrn_to_urn
9 from sfa.util.plxrn import email_to_hrn
10 from sfa.util.config import Config
11 from sfa.trust.certificate import convert_public_key, Keypair
12 from sfa.trust.trustedroots import TrustedRoots
13 from sfa.trust.gid import create_uuid
14 from sfa.storage.model import RegRecord, RegAuthority, RegUser
15 from sfa.storage.alchemy import dbsession
16
17 def _un_unicode(str):
18    if isinstance(str, unicode):
19        return str.encode("ascii", "ignore")
20    else:
21        return str
22
23 def _cleanup_string(str):
24     # pgsql has a fit with strings that have high ascii in them, so filter it
25     # out when generating the hrns.
26     tmp = ""
27     for c in str:
28         if ord(c) < 128:
29             tmp = tmp + c
30     str = tmp
31
32     str = _un_unicode(str)
33     str = str.replace(" ", "_")
34     str = str.replace(".", "_")
35     str = str.replace("(", "_")
36     str = str.replace("'", "_")
37     str = str.replace(")", "_")
38     str = str.replace('"', "_")
39     return str
40
41 class SfaImporter:
42
43     def __init__(self, auth_hierarchy, logger):
44        self.logger=logger
45        self.auth_hierarchy = auth_hierarchy
46        self.config = Config()
47        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
48        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
49
50     # record options into an OptionParser
51     def record_options (self, parser):
52        self.logger.info ("SfaImporter.record_options : to do")
53        pass
54
55     def run (self, options):
56        self.logger.info ("SfaImporter.run : no options used")
57        self.create_top_level_records()
58
59     def create_top_level_records(self):
60         """
61         Create top level and interface records
62         """
63         # create root authority
64         interface_hrn = self.config.SFA_INTERFACE_HRN
65         self.create_top_level_auth_records(interface_hrn)
66
67         # create s user record for the slice manager
68         self.create_sm_client_record()
69
70         # create interface records
71         self.logger.info("Import: creating interface records")
72 # xxx authority+ turning off the creation of authority+*
73 # in fact his is required - used in SfaApi._getCredentialRaw
74 # that tries to locate 'authority+sa'
75         self.create_interface_records()
76
77         # add local root authority's cert  to trusted list
78         self.logger.info("Import: adding " + interface_hrn + " to trusted list")
79         authority = self.auth_hierarchy.get_auth_info(interface_hrn)
80         self.TrustedRoots.add_gid(authority.get_gid_object())
81
82     def create_top_level_auth_records(self, hrn):
83         """
84         Create top level db records (includes root and sub authorities (local/remote)
85         """
86         # make sure parent exists
87         parent_hrn = get_authority(hrn)
88         if not parent_hrn:
89             parent_hrn = hrn
90         if not parent_hrn == hrn:
91             self.create_top_level_auth_records(parent_hrn)
92
93         # ensure key and cert exists:
94         self.auth_hierarchy.create_top_level_auth(hrn)    
95         # create the db record if it doesnt already exist    
96         auth_info = self.auth_hierarchy.get_auth_info(hrn)
97         auth_record = RegAuthority()
98         auth_record.type='authority'
99         auth_record.hrn=hrn
100         auth_record.gid=auth_info.get_gid_object()
101         auth_record.authority=get_authority(hrn)
102         auth_record.just_created()
103         dbsession.add (auth_record)
104         dbsession.commit()
105         self.logger.info("Import: imported authority (parent) %s " % auth_record)
106
107     def create_sm_client_record(self):
108         """
109         Create a user record for the Slicemanager service.
110         """
111         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
112         urn = hrn_to_urn(hrn, 'user')
113         if not self.auth_hierarchy.auth_exists(urn):
114             self.logger.info("Import: creating Slice Manager user")
115             self.auth_hierarchy.create_auth(urn)
116
117         auth_info = self.auth_hierarchy.get_auth_info(hrn)
118         user_record = RegUser()
119         user_record.type='user'
120         user_record.hrn=hrn
121         user_record.gid=auth_info.get_gid_object()
122         user_record.authority=get_authority(hrn)
123         user_record.just_created()
124         dbsession.add (user_record)
125         dbsession.commit()
126         self.logger.info("Import: importing user (slicemanager) %s " % user_record)
127
128     def create_interface_records(self):
129         """
130         Create a record for each SFA interface
131         """
132         # just create certs for all sfa interfaces even if they
133         # aren't enabled
134         hrn = self.config.SFA_INTERFACE_HRN
135         auth_info = self.auth_hierarchy.get_auth_info(hrn)
136         pkey = auth_info.get_pkey_object()
137         for type in  [ 'authority+sa', 'authority+am', 'authority+sm', ]:
138             urn = hrn_to_urn(hrn, type)
139             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
140             # xxx this should probably use a RegAuthority, or a to-be-defined RegPeer object
141             # but for now we have to preserve the authority+<> stuff
142             interface_record = RegAuthority()
143             interface_record.type=type
144             interface_record.hrn=hrn
145             interface_record.gid= gid
146             interface_record.authority=get_authority(hrn)
147             interface_record.just_created()
148             dbsession.add (interface_record)
149             dbsession.commit()
150             self.logger.info("Import: imported authority (%s) %s " % (type,interface_record))
151              
152     def delete_record(self, hrn, type):
153         # delete the record
154         for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
155            dbsession.delete(rec)
156         dbsession.commit()