create registry objects with kdws-style again, like RegAuthority(hrn=hrn)
[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 add_options (self, parser):
52        # no generic option
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("SfaImporter: 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("SfaImporter: 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(hrn=hrn, gid=auth_info.get_gid_object(),
98                                    authority=get_authority(hrn))
99         auth_record.just_created()
100         dbsession.add (auth_record)
101         dbsession.commit()
102         self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)
103
104     def create_sm_client_record(self):
105         """
106         Create a user record for the Slicemanager service.
107         """
108         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
109         urn = hrn_to_urn(hrn, 'user')
110         if not self.auth_hierarchy.auth_exists(urn):
111             self.logger.info("SfaImporter: creating Slice Manager user")
112             self.auth_hierarchy.create_auth(urn)
113
114         auth_info = self.auth_hierarchy.get_auth_info(hrn)
115         user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
116                               authority=get_authority(hrn))
117         user_record.just_created()
118         dbsession.add (user_record)
119         dbsession.commit()
120         self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
121
122     def create_interface_records(self):
123         """
124         Create a record for each SFA interface
125         """
126         # just create certs for all sfa interfaces even if they
127         # aren't enabled
128         hrn = self.config.SFA_INTERFACE_HRN
129         auth_info = self.auth_hierarchy.get_auth_info(hrn)
130         pkey = auth_info.get_pkey_object()
131         for type in  [ 'authority+sa', 'authority+am', 'authority+sm', ]:
132             urn = hrn_to_urn(hrn, type)
133             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
134             # xxx this should probably use a RegAuthority, or a to-be-defined RegPeer object
135             # but for now we have to preserve the authority+<> stuff
136             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
137                                             authority=get_authority(hrn))
138             interface_record.just_created()
139             dbsession.add (interface_record)
140             dbsession.commit()
141             self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
142              
143     def delete_record(self, hrn, type):
144         # delete the record
145         for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
146            dbsession.delete(rec)
147         dbsession.commit()