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 sfa_logger_goes_to_import,sfa_logger
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.trustedroot import *
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 sfa_logger_goes_to_import()
56 self.logger = sfa_logger()
57 self.AuthHierarchy = Hierarchy()
58 self.config = Config()
59 self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config))
60 self.plc_auth = self.config.get_plc_auth()
61 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
63 # connect to planetlab
65 if "Url" in self.plc_auth:
66 from sfa.plc.remoteshell import RemoteShell
67 self.shell = RemoteShell(self.logger)
70 self.shell = PLC.Shell.Shell(globals = globals())
72 def create_top_level_auth_records(self, hrn):
74 Create top level records (includes root and sub authorities (local/remote)
76 urn = hrn_to_urn(hrn, 'authority')
77 # make sure parent exists
78 parent_hrn = get_authority(hrn)
81 if not parent_hrn == hrn:
82 self.create_top_level_auth_records(parent_hrn)
84 # create the authority if it doesnt already exist
85 if not self.AuthHierarchy.auth_exists(urn):
86 self.logger.info("Import: creating top level authorities")
87 self.AuthHierarchy.create_auth(urn)
89 # create the db record if it doesnt already exist
90 auth_info = self.AuthHierarchy.get_auth_info(hrn)
92 auth_record = table.find({'type': 'authority', 'hrn': hrn})
95 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
96 auth_record['authority'] = get_authority(auth_record['hrn'])
97 self.logger.info("Import: inserting authority record for %s"%hrn)
98 table.insert(auth_record)
100 def create_interface_records(self):
102 Create a record for each SFA interface
104 # just create certs for all sfa interfaces even if they
106 interface_hrn = config.SFA_INTERFACE_HRN
107 interfaces = ['sa', 'am', 'sm']
109 auth_info = self.AuthHierarchy.get_auth_info(interface_hrn)
110 pkey = auth_info.get_pkey_object()
111 for interface in interfaces:
112 interface_record = table.find({'type': interface, 'hrn': interface_hrn})
113 if not interface_record:
114 self.logger.info("Import: interface %s %s " % (interface_hrn, interface))
115 urn = hrn_to_urn(interface_hrn, interface)
116 gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
117 record = SfaRecord(hrn=interface_hrn, gid=gid, type=interface, pointer=-1)
118 record['authority'] = get_authority(interface_hrn)
121 def import_person(self, parent_hrn, person):
123 Register a user record
125 hrn = email_to_hrn(parent_hrn, person['email'])
127 # ASN.1 will have problems with hrn's longer than 64 characters
131 self.logger.info("Import: person %s"%hrn)
133 if 'key_ids' in person and person['key_ids']:
134 key_ids = person["key_ids"]
135 # get the user's private key from the SSH keys they have uploaded
137 keys = self.shell.GetKeys(self.plc_auth, key_ids)
139 pkey = convert_public_key(key)
141 pkey = Keypair(create=True)
143 # the user has no keys
144 self.logger.warning("Import: person %s does not have a PL public key"%hrn)
145 # if a key is unavailable, then we still need to put something in the
146 # user's GID. So make one up.
147 pkey = Keypair(create=True)
150 urn = hrn_to_urn(hrn, 'user')
151 person_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
153 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
154 person_record['authority'] = get_authority(person_record['hrn'])
155 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
156 if not existing_records:
157 table.insert(person_record)
159 self.logger.info("Import: %s exists, updating " % hrn)
160 existing_record = existing_records[0]
161 person_record['record_id'] = existing_record['record_id']
162 table.update(person_record)
164 def import_slice(self, parent_hrn, slice):
165 slicename = slice['name'].split("_",1)[-1]
166 slicename = _cleanup_string(slicename)
169 self.logger.error("Import: failed to parse slice name %s" %slice['name'])
172 hrn = parent_hrn + "." + slicename
173 self.logger.info("Import: slice %s"%hrn)
175 pkey = Keypair(create=True)
176 urn = hrn_to_urn(hrn, 'slice')
177 slice_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
178 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
179 slice_record['authority'] = get_authority(slice_record['hrn'])
181 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
182 if not existing_records:
183 table.insert(slice_record)
185 self.logger.info("Import: %s exists, updating " % hrn)
186 existing_record = existing_records[0]
187 slice_record['record_id'] = existing_record['record_id']
188 table.update(slice_record)
190 def import_node(self, parent_hrn, node):
191 nodename = node['hostname'].split(".")[0]
192 nodename = _cleanup_string(nodename)
195 self.logger.error("Import: failed to parse node %s" %node['hostname'])
198 hrn = parent_hrn + "." + nodename
199 self.logger.info("Import: node %s" % hrn)
200 # ASN.1 will have problems with hrn's longer than 64 characters
205 node_record = table.find({'type': 'node', 'hrn': hrn})
206 pkey = Keypair(create=True)
207 urn = hrn_to_urn(hrn, 'node')
208 node_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
209 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
210 node_record['authority'] = get_authority(node_record['hrn'])
211 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
212 if not existing_records:
213 table.insert(node_record)
215 self.logger.info("Import: %s exists, updating " % hrn)
216 existing_record = existing_records[0]
217 node_record['record_id'] = existing_record['record_id']
218 table.update(node_record)
221 def import_site(self, parent_hrn, site):
223 plc_auth = self.plc_auth
224 sitename = site['login_base']
225 sitename = _cleanup_string(sitename)
226 hrn = parent_hrn + "." + sitename
227 # Hardcode 'internet2' into the hrn for sites hosting
228 # internet2 nodes. This is a special operation for some vini
230 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
231 if sitename.startswith("i2"):
232 #sitename = sitename.replace("ii", "")
233 hrn = ".".join([parent_hrn, "internet2", sitename])
234 elif sitename.startswith("nlr"):
235 #sitename = sitename.replace("nlr", "")
236 hrn = ".".join([parent_hrn, "internet2", sitename])
238 urn = hrn_to_urn(hrn, 'authority')
239 self.logger.info("Import: site %s"%hrn)
241 # create the authority
242 if not self.AuthHierarchy.auth_exists(urn):
243 self.AuthHierarchy.create_auth(urn)
245 auth_info = self.AuthHierarchy.get_auth_info(urn)
248 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
249 auth_record['authority'] = get_authority(auth_record['hrn'])
250 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
251 if not existing_records:
252 table.insert(auth_record)
254 self.logger.info("Import: %s exists, updating " % hrn)
255 existing_record = existing_records[0]
256 auth_record['record_id'] = existing_record['record_id']
257 table.update(auth_record)
262 def delete_record(self, hrn, type):
265 record_list = table.find({'type': type, 'hrn': hrn})
266 for record in record_list:
267 self.logger.info("Import: removing record %s %s" % (type, hrn))