2 # The import tool assumes that the existing PLC hierarchy should all be part
3 # of "planetlab.us" (see the root_auth and level1_auth variables below).
5 # Public keys are extracted from the users' SSH keys automatically and used to
6 # create GIDs. This is relatively experimental as a custom tool had to be
7 # written to perform conversion from SSH to OpenSSL format. It only supports
8 # RSA keys at this time, not DSA keys.
11 from sfa.util.sfalogging import _SfaLogger
12 from sfa.util.xrn import get_authority, hrn_to_urn
13 from sfa.util.plxrn import email_to_hrn
14 from sfa.util.config import Config
15 from sfa.trust.certificate import convert_public_key, Keypair
16 from sfa.trust.trustedroots import TrustedRoots
17 from sfa.trust.hierarchy import Hierarchy
18 from sfa.trust.gid import create_uuid
19 from sfa.storage.model import RegRecord, RegAuthority, RegUser
20 from sfa.storage.alchemy import dbsession
23 if isinstance(str, unicode):
24 return str.encode("ascii", "ignore")
28 def _cleanup_string(str):
29 # pgsql has a fit with strings that have high ascii in them, so filter it
30 # out when generating the hrns.
37 str = _un_unicode(str)
38 str = str.replace(" ", "_")
39 str = str.replace(".", "_")
40 str = str.replace("(", "_")
41 str = str.replace("'", "_")
42 str = str.replace(")", "_")
43 str = str.replace('"', "_")
49 self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
50 self.AuthHierarchy = Hierarchy()
51 self.config = Config()
52 self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
53 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
55 def create_top_level_records(self):
57 Create top level and interface records
59 # create root authority
60 interface_hrn = self.config.SFA_INTERFACE_HRN
61 self.create_top_level_auth_records(interface_hrn)
63 # create s user record for the slice manager
64 self.create_sm_client_record()
66 # create interface records
67 self.logger.info("Import: creating interface records")
68 # xxx authority+ turning off the creation of authority+*
69 # in fact his is required - used in SfaApi._getCredentialRaw
70 # that tries to locate 'authority+sa'
71 self.create_interface_records()
73 # add local root authority's cert to trusted list
74 self.logger.info("Import: adding " + interface_hrn + " to trusted list")
75 authority = self.AuthHierarchy.get_auth_info(interface_hrn)
76 self.TrustedRoots.add_gid(authority.get_gid_object())
78 def create_top_level_auth_records(self, hrn):
80 Create top level db records (includes root and sub authorities (local/remote)
82 # make sure parent exists
83 parent_hrn = get_authority(hrn)
86 if not parent_hrn == hrn:
87 self.create_top_level_auth_records(parent_hrn)
89 # ensure key and cert exists:
90 self.AuthHierarchy.create_top_level_auth(hrn)
91 # create the db record if it doesnt already exist
92 auth_info = self.AuthHierarchy.get_auth_info(hrn)
93 auth_record = RegAuthority()
94 auth_record.type='authority'
96 auth_record.gid=auth_info.get_gid_object()
97 auth_record.authority=get_authority(hrn)
98 auth_record.just_created()
99 dbsession.add (auth_record)
101 self.logger.info("Import: imported authority (parent) %s " % auth_record)
103 def create_sm_client_record(self):
105 Create a user record for the Slicemanager service.
107 hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
108 urn = hrn_to_urn(hrn, 'user')
109 if not self.AuthHierarchy.auth_exists(urn):
110 self.logger.info("Import: creating Slice Manager user")
111 self.AuthHierarchy.create_auth(urn)
113 auth_info = self.AuthHierarchy.get_auth_info(hrn)
114 user_record = RegUser()
115 user_record.type='user'
117 user_record.gid=auth_info.get_gid_object()
118 user_record.authority=get_authority(hrn)
119 user_record.just_created()
120 dbsession.add (user_record)
122 self.logger.info("Import: importing user (slicemanager) %s " % user_record)
124 def create_interface_records(self):
126 Create a record for each SFA interface
128 # just create certs for all sfa interfaces even if they
130 hrn = self.config.SFA_INTERFACE_HRN
131 auth_info = self.AuthHierarchy.get_auth_info(hrn)
132 pkey = auth_info.get_pkey_object()
133 for type in [ 'authority+sa', 'authority+am', 'authority+sm', ]:
134 urn = hrn_to_urn(hrn, type)
135 gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
136 # xxx this should probably use a RegAuthority, or a to-be-defined RegPeer object
137 # but for now we have to preserve the authority+<> stuff
138 interface_record = RegAuthority()
139 interface_record.type=type
140 interface_record.hrn=hrn
141 interface_record.gid= gid
142 interface_record.authority=get_authority(hrn)
143 interface_record.just_created()
144 dbsession.add (interface_record)
146 self.logger.info("Import: imported authority (%s) %s " % (type,interface_record))
148 def delete_record(self, hrn, type):
150 for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
151 dbsession.delete(rec)