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.
15 from sfa.util.sfalogging import _SfaLogger
17 from sfa.util.record import *
18 from sfa.util.table import SfaTable
19 from sfa.util.xrn import get_authority, hrn_to_urn
20 from sfa.util.plxrn import email_to_hrn
21 from sfa.util.config import Config
22 from sfa.trust.certificate import convert_public_key, Keypair
23 from sfa.trust.trustedroots import TrustedRoots
24 from sfa.trust.hierarchy import *
25 from sfa.trust.gid import create_uuid
29 if isinstance(str, unicode):
30 return str.encode("ascii", "ignore")
34 def _cleanup_string(str):
35 # pgsql has a fit with strings that have high ascii in them, so filter it
36 # out when generating the hrns.
43 str = _un_unicode(str)
44 str = str.replace(" ", "_")
45 str = str.replace(".", "_")
46 str = str.replace("(", "_")
47 str = str.replace("'", "_")
48 str = str.replace(")", "_")
49 str = str.replace('"', "_")
55 self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
56 self.AuthHierarchy = Hierarchy()
57 self.config = Config()
58 self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
59 self.plc_auth = self.config.get_plc_auth()
60 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
62 # connect to planetlab
64 if "Url" in self.plc_auth:
65 from sfa.plc.remoteshell import RemoteShell
66 self.shell = RemoteShell(self.logger)
69 self.shell = PLC.Shell.Shell(globals = globals())
71 def create_top_level_auth_records(self, hrn):
73 Create top level records (includes root and sub authorities (local/remote)
75 urn = hrn_to_urn(hrn, 'authority')
76 # make sure parent exists
77 parent_hrn = get_authority(hrn)
80 if not parent_hrn == hrn:
81 self.create_top_level_auth_records(parent_hrn)
83 # create the authority if it doesnt already exist
84 if not self.AuthHierarchy.auth_exists(urn):
85 self.logger.info("Import: creating top level authorities")
86 self.AuthHierarchy.create_auth(urn)
88 # create the db record if it doesnt already exist
89 auth_info = self.AuthHierarchy.get_auth_info(hrn)
91 auth_record = table.find({'type': 'authority', 'hrn': hrn})
94 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
95 auth_record['authority'] = get_authority(auth_record['hrn'])
96 self.logger.info("Import: inserting authority record for %s"%hrn)
97 table.insert(auth_record)
99 def create_interface_records(self):
101 Create a record for each SFA interface
103 # just create certs for all sfa interfaces even if they
105 interface_hrn = self.config.SFA_INTERFACE_HRN
106 interfaces = ['authority+sa', 'authority+am', 'authority+sm']
108 auth_info = self.AuthHierarchy.get_auth_info(interface_hrn)
109 pkey = auth_info.get_pkey_object()
110 for interface in interfaces:
111 interface_record = table.find({'type': interface, 'hrn': interface_hrn})
112 if not interface_record:
113 self.logger.info("Import: interface %s %s " % (interface_hrn, interface))
114 urn = hrn_to_urn(interface_hrn, interface)
115 gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
116 record = SfaRecord(hrn=interface_hrn, gid=gid, type=interface, pointer=-1)
117 record['authority'] = get_authority(interface_hrn)
120 def import_person(self, parent_hrn, person):
122 Register a user record
124 hrn = email_to_hrn(parent_hrn, person['email'])
126 # ASN.1 will have problems with hrn's longer than 64 characters
130 self.logger.info("Import: person %s"%hrn)
132 if 'key_ids' in person and person['key_ids']:
133 key_ids = person["key_ids"]
134 # get the user's private key from the SSH keys they have uploaded
136 keys = self.shell.GetKeys(self.plc_auth, key_ids)
140 pkey = convert_public_key(key)
142 self.logger.warn('unable to convert public key for %s' % hrn)
144 pkey = Keypair(create=True)
146 # the user has no keys
147 self.logger.warn("Import: person %s does not have a PL public key"%hrn)
148 # if a key is unavailable, then we still need to put something in the
149 # user's GID. So make one up.
150 pkey = Keypair(create=True)
153 urn = hrn_to_urn(hrn, 'user')
154 person_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
156 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
157 person_record['authority'] = get_authority(person_record['hrn'])
158 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
159 if not existing_records:
160 table.insert(person_record)
162 self.logger.info("Import: %s exists, updating " % hrn)
163 existing_record = existing_records[0]
164 person_record['record_id'] = existing_record['record_id']
165 table.update(person_record)
167 def import_slice(self, parent_hrn, slice):
168 slicename = slice['name'].split("_",1)[-1]
169 slicename = _cleanup_string(slicename)
172 self.logger.error("Import: failed to parse slice name %s" %slice['name'])
175 hrn = parent_hrn + "." + slicename
176 self.logger.info("Import: slice %s"%hrn)
178 pkey = Keypair(create=True)
179 urn = hrn_to_urn(hrn, 'slice')
180 slice_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
181 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
182 slice_record['authority'] = get_authority(slice_record['hrn'])
184 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
185 if not existing_records:
186 table.insert(slice_record)
188 self.logger.info("Import: %s exists, updating " % hrn)
189 existing_record = existing_records[0]
190 slice_record['record_id'] = existing_record['record_id']
191 table.update(slice_record)
193 def import_node(self, hrn, node):
194 self.logger.info("Import: node %s" % hrn)
195 # ASN.1 will have problems with hrn's longer than 64 characters
200 node_record = table.find({'type': 'node', 'hrn': hrn})
201 pkey = Keypair(create=True)
202 urn = hrn_to_urn(hrn, 'node')
203 node_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
204 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
205 node_record['authority'] = get_authority(node_record['hrn'])
206 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
207 if not existing_records:
208 table.insert(node_record)
210 self.logger.info("Import: %s exists, updating " % hrn)
211 existing_record = existing_records[0]
212 node_record['record_id'] = existing_record['record_id']
213 table.update(node_record)
216 def import_site(self, hrn, site):
218 plc_auth = self.plc_auth
219 urn = hrn_to_urn(hrn, 'authority')
220 self.logger.info("Import: site %s"%hrn)
222 # create the authority
223 if not self.AuthHierarchy.auth_exists(urn):
224 self.AuthHierarchy.create_auth(urn)
226 auth_info = self.AuthHierarchy.get_auth_info(urn)
229 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
230 auth_record['authority'] = get_authority(auth_record['hrn'])
231 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
232 if not existing_records:
233 table.insert(auth_record)
235 self.logger.info("Import: %s exists, updating " % hrn)
236 existing_record = existing_records[0]
237 auth_record['record_id'] = existing_record['record_id']
238 table.update(auth_record)
243 def delete_record(self, hrn, type):
246 record_list = table.find({'type': type, 'hrn': hrn})
247 for record in record_list:
248 self.logger.info("Import: removing record %s %s" % (type, hrn))