Merge branch 'master' into senslab2
[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
15 from sfa.storage.alchemy import dbsession
16 from sfa.storage.model import RegRecord, RegAuthority, RegUser
17
18 def _un_unicode(str):
19    if isinstance(str, unicode):
20        return str.encode("ascii", "ignore")
21    else:
22        return str
23
24 def _cleanup_string(str):
25     # pgsql has a fit with strings that have high ascii in them, so filter it
26     # out when generating the hrns.
27     tmp = ""
28     for c in str:
29         if ord(c) < 128:
30             tmp = tmp + c
31     str = tmp
32
33     str = _un_unicode(str)
34     str = str.replace(" ", "_")
35     str = str.replace(".", "_")
36     str = str.replace("(", "_")
37     str = str.replace("'", "_")
38     str = str.replace(")", "_")
39     str = str.replace('"', "_")
40     return str
41
42 class SfaImporter:
43
44     def __init__(self, auth_hierarchy, logger):
45        self.logger=logger
46        self.auth_hierarchy = auth_hierarchy
47        config = Config()
48        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(config))
49        self.root_auth = config.SFA_REGISTRY_ROOT_AUTH
50        self.interface_hrn = config.SFA_INTERFACE_HRN
51
52     # check before creating a RegRecord entry as we run this over and over
53     def record_exists (self, type, hrn):
54        return dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0
55
56     # record options into an OptionParser
57     def add_options (self, parser):
58        # no generic option
59        pass
60
61     def run (self, options):
62        self.logger.info ("SfaImporter.run : no options used")
63        self.create_top_level_records()
64
65     def create_top_level_records(self):
66         """
67         Create top level and interface records
68         """
69         # create root authority
70         self.create_top_level_auth_records(self.interface_hrn)
71
72         # create s user record for the slice manager
73         self.create_sm_client_record()
74
75         # create interface records
76         # xxx turning off the creation of authority+*
77         # in fact his is required - used in SfaApi._getCredentialRaw
78         # that tries to locate 'authority+sa'
79         self.create_interface_records()
80
81         # add local root authority's cert  to trusted list
82         self.logger.info("SfaImporter: adding " + self.interface_hrn + " to trusted list")
83         authority = self.auth_hierarchy.get_auth_info(self.interface_hrn)
84         self.TrustedRoots.add_gid(authority.get_gid_object())
85
86     def create_top_level_auth_records(self, hrn):
87         """
88         Create top level db records (includes root and sub authorities (local/remote)
89         """
90         # make sure parent exists
91         parent_hrn = get_authority(hrn)
92         if not parent_hrn:
93             parent_hrn = hrn
94         if not parent_hrn == hrn:
95             self.create_top_level_auth_records(parent_hrn)
96
97         # ensure key and cert exists:
98         self.auth_hierarchy.create_top_level_auth(hrn)    
99         # create the db record if it doesnt already exist    
100         if self.record_exists ('authority',hrn): return
101         auth_info = self.auth_hierarchy.get_auth_info(hrn)
102         auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
103                                    authority=get_authority(hrn))
104         auth_record.just_created()
105         dbsession.add (auth_record)
106         dbsession.commit()
107         self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)
108
109     def create_sm_client_record(self):
110         """
111         Create a user record for the Slicemanager service.
112         """
113         hrn = self.interface_hrn + '.slicemanager'
114         urn = hrn_to_urn(hrn, 'user')
115         if not self.auth_hierarchy.auth_exists(urn):
116             self.logger.info("SfaImporter: creating Slice Manager user")
117             self.auth_hierarchy.create_auth(urn)
118
119         if self.record_exists ('user',hrn): return
120         auth_info = self.auth_hierarchy.get_auth_info(hrn)
121         user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
122                               authority=get_authority(hrn))
123         user_record.just_created()
124         dbsession.add (user_record)
125         dbsession.commit()
126         self.logger.info("SfaImporter: 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         auth_info = self.auth_hierarchy.get_auth_info(self.interface_hrn)
135         pkey = auth_info.get_pkey_object()
136         hrn=self.interface_hrn
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             # for now we have to preserve the authority+<> stuff
141             if self.record_exists (type,hrn): continue
142             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
143                                             authority=get_authority(hrn))
144             interface_record.just_created()
145             dbsession.add (interface_record)
146             dbsession.commit()
147             self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
148