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
13 from sfa.util.record import SfaRecord
14 from sfa.util.table import SfaTable
15 from sfa.util.xrn import get_authority, hrn_to_urn
16 from sfa.util.plxrn import email_to_hrn
17 from sfa.util.config import Config
18 from sfa.trust.certificate import convert_public_key, Keypair
19 from sfa.trust.trustedroots import TrustedRoots
20 from sfa.trust.hierarchy import Hierarchy
21 from sfa.trust.gid import create_uuid
25 if isinstance(str, unicode):
26 return str.encode("ascii", "ignore")
30 def _cleanup_string(str):
31 # pgsql has a fit with strings that have high ascii in them, so filter it
32 # out when generating the hrns.
39 str = _un_unicode(str)
40 str = str.replace(" ", "_")
41 str = str.replace(".", "_")
42 str = str.replace("(", "_")
43 str = str.replace("'", "_")
44 str = str.replace(")", "_")
45 str = str.replace('"', "_")
51 self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
52 self.AuthHierarchy = Hierarchy()
53 self.config = Config()
54 self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
55 self.plc_auth = self.config.get_plc_auth()
56 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
58 # connect to planetlab
60 if "Url" in self.plc_auth:
61 from sfa.plc.remoteshell import RemoteShell
62 self.shell = RemoteShell(self.logger)
65 self.shell = PLC.Shell.Shell(globals = globals())
67 def create_top_level_auth_records(self, hrn):
69 Create top level records (includes root and sub authorities (local/remote)
71 urn = hrn_to_urn(hrn, 'authority')
72 # make sure parent exists
73 parent_hrn = get_authority(hrn)
76 if not parent_hrn == hrn:
77 self.create_top_level_auth_records(parent_hrn)
79 # create the authority if it doesnt already exist
80 if not self.AuthHierarchy.auth_exists(urn):
81 self.logger.info("Import: creating top level authorities")
82 self.AuthHierarchy.create_auth(urn)
84 # create the db record if it doesnt already exist
85 auth_info = self.AuthHierarchy.get_auth_info(hrn)
87 auth_record = table.find({'type': 'authority', 'hrn': hrn})
90 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
91 auth_record['authority'] = get_authority(auth_record['hrn'])
92 self.logger.info("Import: inserting authority record for %s"%hrn)
93 table.insert(auth_record)
95 def create_sm_client_record(self):
97 Create a user record for the Slicemanager service.
99 hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
100 urn = hrn_to_urn(hrn, 'user')
101 if not self.AuthHierarchy.auth_exists(urn):
102 self.logger.info("Import: creating Slice Manager user")
103 self.AuthHierarchy.create_auth(urn)
105 auth_info = self.AuthHierarchy.get_auth_info(hrn)
107 sm_user_record = table.find({'type': 'user', 'hrn': hrn})
108 if not sm_user_record:
109 record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="user", pointer=-1)
110 record['authority'] = get_authority(record['hrn'])
113 def create_interface_records(self):
115 Create a record for each SFA interface
117 # just create certs for all sfa interfaces even if they
119 interface_hrn = self.config.SFA_INTERFACE_HRN
120 interfaces = ['authority+sa', 'authority+am', 'authority+sm']
122 auth_info = self.AuthHierarchy.get_auth_info(interface_hrn)
123 pkey = auth_info.get_pkey_object()
124 for interface in interfaces:
125 interface_record = table.find({'type': interface, 'hrn': interface_hrn})
126 if not interface_record:
127 self.logger.info("Import: interface %s %s " % (interface_hrn, interface))
128 urn = hrn_to_urn(interface_hrn, interface)
129 gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
130 record = SfaRecord(hrn=interface_hrn, gid=gid, type=interface, pointer=-1)
131 record['authority'] = get_authority(interface_hrn)
136 def import_person(self, parent_hrn, person):
138 Register a user record
140 hrn = email_to_hrn(parent_hrn, person['email'])
142 # ASN.1 will have problems with hrn's longer than 64 characters
146 self.logger.info("Import: person %s"%hrn)
148 if 'key_ids' in person and person['key_ids']:
149 key_ids = person["key_ids"]
150 # get the user's private key from the SSH keys they have uploaded
152 keys = self.shell.GetKeys(self.plc_auth, key_ids)
156 pkey = convert_public_key(key)
158 self.logger.warn('unable to convert public key for %s' % hrn)
160 pkey = Keypair(create=True)
162 # the user has no keys
163 self.logger.warn("Import: person %s does not have a PL public key"%hrn)
164 # if a key is unavailable, then we still need to put something in the
165 # user's GID. So make one up.
166 pkey = Keypair(create=True)
169 urn = hrn_to_urn(hrn, 'user')
170 person_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
172 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
173 person_record['authority'] = get_authority(person_record['hrn'])
174 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
175 if not existing_records:
176 table.insert(person_record)
178 self.logger.info("Import: %s exists, updating " % hrn)
179 existing_record = existing_records[0]
180 person_record['record_id'] = existing_record['record_id']
181 table.update(person_record)
183 def import_slice(self, parent_hrn, slice):
184 slicename = slice['name'].split("_",1)[-1]
185 slicename = _cleanup_string(slicename)
188 self.logger.error("Import: failed to parse slice name %s" %slice['name'])
191 hrn = parent_hrn + "." + slicename
192 self.logger.info("Import: slice %s"%hrn)
194 pkey = Keypair(create=True)
195 urn = hrn_to_urn(hrn, 'slice')
196 slice_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
197 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
198 slice_record['authority'] = get_authority(slice_record['hrn'])
200 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
201 if not existing_records:
202 table.insert(slice_record)
204 self.logger.info("Import: %s exists, updating " % hrn)
205 existing_record = existing_records[0]
206 slice_record['record_id'] = existing_record['record_id']
207 table.update(slice_record)
209 def import_node(self, hrn, node):
210 self.logger.info("Import: node %s" % hrn)
211 # ASN.1 will have problems with hrn's longer than 64 characters
216 node_record = table.find({'type': 'node', 'hrn': hrn})
217 pkey = Keypair(create=True)
218 urn = hrn_to_urn(hrn, 'node')
219 node_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
220 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
221 node_record['authority'] = get_authority(node_record['hrn'])
222 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
223 if not existing_records:
224 table.insert(node_record)
226 self.logger.info("Import: %s exists, updating " % hrn)
227 existing_record = existing_records[0]
228 node_record['record_id'] = existing_record['record_id']
229 table.update(node_record)
232 def import_site(self, hrn, site):
233 urn = hrn_to_urn(hrn, 'authority')
234 self.logger.info("Import: site %s"%hrn)
236 # create the authority
237 if not self.AuthHierarchy.auth_exists(urn):
238 self.AuthHierarchy.create_auth(urn)
240 auth_info = self.AuthHierarchy.get_auth_info(urn)
243 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
244 auth_record['authority'] = get_authority(auth_record['hrn'])
245 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
246 if not existing_records:
247 table.insert(auth_record)
249 self.logger.info("Import: %s exists, updating " % hrn)
250 existing_record = existing_records[0]
251 auth_record['record_id'] = existing_record['record_id']
252 table.update(auth_record)
257 def delete_record(self, hrn, type):
260 record_list = table.find({'type': type, 'hrn': hrn})
261 for record in record_list:
262 self.logger.info("Import: removing record %s %s" % (type, hrn))