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.record import *
16 from sfa.util.genitable import GeniTable
17 from sfa.util.misc import *
18 from sfa.util.config import Config
19 from sfa.util.report import trace, error
21 from sfa.trust.certificate import convert_public_key, Keypair
22 from sfa.trust.trustedroot import *
23 from sfa.trust.hierarchy import *
24 from sfa.trust.gid import create_uuid
28 if isinstance(str, unicode):
29 return str.encode("ascii", "ignore")
33 def cleanup_string(str):
34 # pgsql has a fit with strings that have high ascii in them, so filter it
35 # out when generating the hrns.
43 str = str.replace(" ", "_")
44 str = str.replace(".", "_")
45 str = str.replace("(", "_")
46 str = str.replace("'", "_")
47 str = str.replace(")", "_")
48 str = str.replace('"', "_")
54 self.AuthHierarchy = Hierarchy()
55 self.TrustedRoots = TrustedRootList()
57 self.config = Config()
58 self.plc_auth = self.config.get_plc_auth()
59 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
60 self.level1_auth = self.config.SFA_REGISTRY_LEVEL1_AUTH
61 if not self.level1_auth or self.level1_auth in ['']:
62 self.level1_auth = None
64 # connect to planetlab
66 if "Url" in self.plc_auth:
67 from sfa.plc.remoteshell import RemoteShell
68 self.shell = RemoteShell()
71 self.shell = PLC.Shell.Shell(globals = globals())
74 def create_top_level_auth_records(self, hrn):
75 AuthHierarchy = self.AuthHierarchy
77 # if auth records for this hrn dont exist, create it
78 if not AuthHierarchy.auth_exists(hrn):
79 AuthHierarchy.create_auth(hrn)
82 # get the auth info of the newly created root auth (parent)
83 # or level1_auth if it exists
85 auth_info = AuthHierarchy.get_auth_info(hrn)
88 parent_hrn = get_authority(hrn)
91 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
94 auth_record = table.find({'type': 'authority', 'hrn': hrn})
97 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
98 trace(" inserting authority record for " + hrn)
99 table.insert(auth_record)
102 def import_person(self, parent_hrn, person):
103 AuthHierarchy = self.AuthHierarchy
104 hrn = email_to_hrn(parent_hrn, person['email'])
106 # ASN.1 will have problems with hrn's longer than 64 characters
110 trace("Import: importing person " + hrn)
112 if 'key_ids' in person and person['key_ids']:
113 key_ids = person["key_ids"]
114 # get the user's private key from the SSH keys they have uploaded
116 keys = self.shell.GetKeys(self.plc_auth, key_ids)
118 pkey = convert_public_key(key)
120 pkey = Keypair(create=True)
122 # the user has no keys
123 trace(" person " + hrn + " does not have a PL public key")
124 # if a key is unavailable, then we still need to put something in the
125 # user's GID. So make one up.
126 pkey = Keypair(create=True)
130 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
132 person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
134 table.insert(person_record)
136 trace("Import: %s exists, updating " % hrn)
137 existing_record = table.find(person_record)
138 person_record['record_id'] = existing_record['record_id']
139 table.update(person_record)
141 def import_slice(self, parent_hrn, slice):
142 AuthHierarchy = self.AuthHierarchy
143 slicename = slice['name'].split("_",1)[-1]
144 slicename = cleanup_string(slicename)
147 error("Import_Slice: failed to parse slice name " + slice['name'])
150 hrn = parent_hrn + "." + slicename
151 trace("Import: importing slice " + hrn)
153 pkey = Keypair(create=True)
154 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
155 slice_record = GeniRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
158 table.insert(slice_record)
160 trace("Import: %s exists, updating " % hrn)
161 existing_record = table.find(slice_record)
162 slice_record['record_id'] = existing_record['record_id']
163 table.update(slice_record)
165 def import_node(self, parent_hrn, node):
166 AuthHierarchy = self.AuthHierarchy
167 nodename = node['hostname'].split(".")[0]
168 nodename = cleanup_string(nodename)
171 error("Import_node: failed to parse node name " + node['hostname'])
174 hrn = parent_hrn + "." + nodename
175 trace("Import: importing node " + hrn)
176 # ASN.1 will have problems with hrn's longer than 64 characters
181 node_record = table.find({'type': 'node', 'hrn': hrn})
182 pkey = Keypair(create=True)
183 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
184 node_record = GeniRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
186 table.insert(node_record)
188 trace("Import: %s exists, updating " % hrn)
189 existing_record = table.find(node_record)
190 node_record['record_id'] = existing_record['record_id']
191 table.update(node_record)
194 def import_site(self, parent_hrn, site):
195 AuthHierarchy = self.AuthHierarchy
197 plc_auth = self.plc_auth
198 sitename = site['login_base']
199 sitename = cleanup_string(sitename)
201 hrn = parent_hrn + "." + sitename
203 # Hardcode 'internet2' into the hrn for sites hosting
204 # internet2 nodes. This is a special operation for some vini
206 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
207 if sitename.startswith("ii"):
208 sitename = sitename.replace("ii", "")
209 hrn = ".".join([parent_hrn, "internet2", sitename])
210 elif sitename.startswith("nlr"):
211 hrn = ".".join([parent_hrn, "internet2", sitename])
212 sitename = sitename.replace("nlr", "")
214 trace("Import_Site: importing site " + hrn)
216 # create the authority
217 if not AuthHierarchy.auth_exists(hrn):
218 AuthHierarchy.create_auth(hrn)
220 auth_info = AuthHierarchy.get_auth_info(hrn)
223 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
225 table.insert(auth_record)
227 trace("Import: %s exists, updating " % hrn)
228 existing_record = table.find(auth_record)
229 auth_record['record_id'] = existing_record['record_id']
230 table.update(auth_record)
232 #if 'person_ids' in site:
233 # for person_id in site['person_ids']:
234 # persons = shell.GetPersons(plc_auth, [person_id])
237 # self.import_person(hrn, persons[0])
238 # except Exception, e:
239 # trace("Failed to import: %s (%s)" % (persons[0], e))
240 #if 'slice_ids' in site:
241 # for slice_id in site['slice_ids']:
242 # slices = shell.GetSlices(plc_auth, [slice_id])
245 # self.import_slice(hrn, slices[0])
246 # except Exception, e:
247 # trace("Failed to import: %s (%s)" % (slices[0], e))
248 #if 'node_ids' in site:
249 # for node_id in site['node_ids']:
250 # nodes = shell.GetNodes(plc_auth, [node_id])
253 # self.import_node(hrn, nodes[0])
254 # except Exception, e:
255 # trace("Failed to import: %s (%s)" % (nodes[0], e))
257 def delete_record(self, parent_hrn, object, type):
261 if type in ['slice'] and 'name' in object and object['name']:
262 slice_name = object['name'].split("_")[0]
263 hrn = parent_hrn + "." + slice_name
264 elif type in ['user'] and 'email' in object and object['email']:
265 person_name = object['email'].split('@')[0]
266 hrn = parent_hrn + "." + person_name
267 elif type in ['node'] and 'hostname' in object and object['hostname']:
268 node_name = object['hostname'].replace('.','_')
269 hrn = parent_hrn + "." + node_name
270 elif type in ['site'] and 'login_base' in object and object['login_base']:
271 site_name = object['login_base']
273 parent_hrn = get_authority(hrn)
275 # delete all records whos authority is this site
276 records = table.find({'authority': hrn})
277 for record in records:
283 record_list = table.find({'type': type, 'hrn': hrn})
284 for record in record_list: