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.
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
18 if isinstance(str, unicode):
19 return str.encode("ascii", "ignore")
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.
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('"', "_")
43 def __init__(self, auth_hierarchy, 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
50 # record options into an OptionParser
51 def record_options (self, parser):
52 self.logger.info ("SfaImporter.record_options : to do")
55 def run (self, options):
56 self.logger.info ("SfaImporter.run : no options used")
57 self.create_top_level_records()
59 def create_top_level_records(self):
61 Create top level and interface records
63 # create root authority
64 interface_hrn = self.config.SFA_INTERFACE_HRN
65 self.create_top_level_auth_records(interface_hrn)
67 # create s user record for the slice manager
68 self.create_sm_client_record()
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()
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())
82 def create_top_level_auth_records(self, hrn):
84 Create top level db records (includes root and sub authorities (local/remote)
86 # make sure parent exists
87 parent_hrn = get_authority(hrn)
90 if not parent_hrn == hrn:
91 self.create_top_level_auth_records(parent_hrn)
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'
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)
105 self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)
107 def create_sm_client_record(self):
109 Create a user record for the Slicemanager service.
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("SfaImporter: creating Slice Manager user")
115 self.auth_hierarchy.create_auth(urn)
117 auth_info = self.auth_hierarchy.get_auth_info(hrn)
118 user_record = RegUser()
119 user_record.type='user'
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)
126 self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
128 def create_interface_records(self):
130 Create a record for each SFA interface
132 # just create certs for all sfa interfaces even if they
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)
150 self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
152 def delete_record(self, hrn, type):
154 for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
155 dbsession.delete(rec)