3 from sfa.util.prefixTree import prefixTree
4 from sfa.util.record import SfaRecord
5 from sfa.util.table import SfaTable
6 from sfa.util.record import SfaRecord
7 from sfa.trust.gid import GID
8 from sfa.util.namespace import *
9 from sfa.trust.credential import *
10 from sfa.trust.certificate import *
11 from sfa.util.faults import *
15 def get_credential(api, xrn, type, is_self=False):
18 hrn = urn_to_hrn(xrn)[0]
20 hrn, type = urn_to_hrn(xrn)
22 # Is this a root or sub authority
23 auth_hrn = api.auth.get_authority(hrn)
24 if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
27 auth_info = api.auth.get_auth_info(auth_hrn)
29 records = table.findObjects({'type': type, 'hrn': hrn})
31 raise RecordNotFound(hrn)
34 # verify_cancreate_credential requires that the member lists
35 # (researchers, pis, etc) be filled in
36 api.fill_record_info(record)
37 if record['type']=='user':
38 if not record['enabled']:
39 raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
42 # if this is a self cred the record's gid is the caller's gid
45 caller_gid = record.get_gid_object()
47 caller_gid = api.auth.client_cred.get_gid_caller()
48 caller_hrn = caller_gid.get_hrn()
50 object_hrn = record.get_gid_object().get_hrn()
51 rights = api.auth.determine_user_rights(caller_hrn, record)
52 # make sure caller has rights to this object
54 raise PermissionError(caller_hrn + " has no rights to " + record['name'])
56 object_gid = GID(string=record['gid'])
57 new_cred = Credential(subject = object_gid.get_subject())
58 new_cred.set_gid_caller(caller_gid)
59 new_cred.set_gid_object(object_gid)
60 new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
61 #new_cred.set_pubkey(object_gid.get_pubkey())
62 new_cred.set_privileges(rights)
63 new_cred.get_privileges().delegate_all_privileges(True)
64 auth_kind = "authority,ma,sa"
65 # Parent not necessary, verify with certs
66 #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
70 return new_cred.save_to_string(save_parents=True)
73 # The GENI GetVersion call
76 version['geni_api'] = 1
81 # The GENI resolve call
82 def Resolve(api, xrn, creds):
83 records = resolve(api, xrn)
89 if record.type == 'slice':
90 return {'geni_urn': xrn, 'geni_creator': " ".join(record.PI)}
91 if record.type == 'user':
92 return {'geni_urn': xrn, 'geni_certificate': record.gid}
96 def resolve(api, xrns, type=None, origin_hrn=None, full=True):
98 # load all know registry names into a prefix tree and attempt to find
99 # the longest matching prefix
100 if not isinstance(xrns, types.ListType):
102 hrns = [urn_to_hrn(xrn)[0] for xrn in xrns]
103 # create a dict whre key is an registry hrn and its value is a
104 # hrns at that registry (determined by the known prefix tree).
106 registries = api.registries
108 registry_hrns = registries.keys()
109 tree.load(registry_hrns)
111 registry_hrn = tree.best_match(urn_to_hrn(xrn)[0])
112 if registry_hrn not in xrn_dict:
113 xrn_dict[registry_hrn] = []
114 xrn_dict[registry_hrn].append(xrn)
117 for registry_hrn in xrn_dict:
118 # skip the hrn without a registry hrn
119 # XX should we let the user know the authority is unknown?
123 # if the best match (longest matching hrn) is not the local registry,
124 # forward the request
125 xrns = xrn_dict[registry_hrn]
126 if registry_hrn != api.hrn:
127 credential = api.getCredential()
128 peer_records = registries[registry_hrn].resolve(credential, xrns, origin_hrn)
129 records.extend([SfaRecord(dict=record).as_dict() for record in peer_records])
131 # try resolving the remaining unfound records at the local registry
132 remaining_hrns = set(hrns).difference([record['hrn'] for record in records])
133 # convert set to list
134 remaining_hrns = [hrn for hrn in remaining_hrns]
136 local_records = table.findObjects({'hrn': remaining_hrns})
138 api.fill_record_info(local_records)
140 # convert local record objects to dicts
141 records.extend([dict(record) for record in local_records])
143 raise RecordNotFound(str(hrns))
146 records = filter(lambda rec: rec['type'] in [type], records)
150 def list(api, xrn, origin_hrn=None):
151 hrn, type = urn_to_hrn(xrn)
152 # load all know registry names into a prefix tree and attempt to find
153 # the longest matching prefix
155 registries = api.registries
156 registry_hrns = registries.keys()
158 tree.load(registry_hrns)
159 registry_hrn = tree.best_match(hrn)
161 #if there was no match then this record belongs to an unknow registry
163 raise MissingAuthority(xrn)
165 # if the best match (longest matching hrn) is not the local registry,
166 # forward the request
168 if registry_hrn != api.hrn:
169 credential = api.getCredential()
170 record_list = registries[registry_hrn].list(credential, xrn, origin_hrn)
171 records = [SfaRecord(dict=record).as_dict() for record in record_list]
173 # if we still havnt found the record yet, try the local registry
175 if not api.auth.hierarchy.auth_exists(hrn):
176 raise MissingAuthority(hrn)
179 records = table.find({'authority': hrn})
184 def register(api, record):
186 hrn, type = record['hrn'], record['type']
187 urn = hrn_to_urn(hrn,type)
189 if type not in ['authority', 'slice', 'node', 'user']:
190 raise UnknownSfaType(type)
192 # check if record already exists
194 existing_records = table.find({'type': type, 'hrn': hrn})
196 raise ExistingRecord(hrn)
198 record = SfaRecord(dict = record)
199 record['authority'] = get_authority(record['hrn'])
200 type = record['type']
202 api.auth.verify_object_permission(hrn)
203 auth_info = api.auth.get_auth_info(record['authority'])
205 # make sure record has a gid
206 if 'gid' not in record:
208 pkey = Keypair(create=True)
209 if 'key' in record and record['key']:
210 if isinstance(record['key'], types.ListType):
211 pub_key = record['key'][0]
213 pub_key = record['key']
214 pkey = convert_public_key(pub_key)
216 gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
217 gid = gid_object.save_to_string(save_parents=True)
221 if type in ["authority"]:
223 if not api.auth.hierarchy.auth_exists(hrn):
224 api.auth.hierarchy.create_auth(hrn_to_urn(hrn,'authority'))
226 # get the GID from the newly created authority
227 gid = auth_info.get_gid_object()
228 record.set_gid(gid.save_to_string(save_parents=True))
229 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
230 sites = api.plshell.GetSites(api.plauth, [pl_record['login_base']])
232 pointer = api.plshell.AddSite(api.plauth, pl_record)
234 pointer = sites[0]['site_id']
236 record.set_pointer(pointer)
237 record['pointer'] = pointer
239 elif (type == "slice"):
240 acceptable_fields=['url', 'instantiation', 'name', 'description']
241 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
242 for key in pl_record.keys():
243 if key not in acceptable_fields:
245 slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
247 pointer = api.plshell.AddSlice(api.plauth, pl_record)
249 pointer = slices[0]['slice_id']
250 record.set_pointer(pointer)
251 record['pointer'] = pointer
253 elif (type == "user"):
254 persons = api.plshell.GetPersons(api.plauth, [record['email']])
256 pointer = api.plshell.AddPerson(api.plauth, dict(record))
258 pointer = persons[0]['person_id']
260 if 'enabled' in record and record['enabled']:
261 api.plshell.UpdatePerson(api.plauth, pointer, {'enabled': record['enabled']})
262 # add this persons to the site only if he is being added for the first
263 # time by sfa and doesont already exist in plc
264 if not persons or not persons[0]['site_ids']:
265 login_base = get_leaf(record['authority'])
266 api.plshell.AddPersonToSite(api.plauth, pointer, login_base)
268 # What roles should this user have?
269 api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
272 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
274 elif (type == "node"):
275 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
276 login_base = hrn_to_pl_login_base(record['authority'])
277 nodes = api.plshell.GetNodes(api.plauth, [pl_record['hostname']])
279 pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
281 pointer = nodes[0]['node_id']
283 record['pointer'] = pointer
284 record.set_pointer(pointer)
285 record_id = table.insert(record)
286 record['record_id'] = record_id
288 # update membership for researchers, pis, owners, operators
289 api.update_membership(None, record)
291 return record.get_gid_object().save_to_string(save_parents=True)
293 def update(api, record_dict):
294 new_record = SfaRecord(dict = record_dict)
295 type = new_record['type']
296 hrn = new_record['hrn']
297 urn = hrn_to_urn(hrn,type)
298 api.auth.verify_object_permission(hrn)
300 # make sure the record exists
301 records = table.findObjects({'type': type, 'hrn': hrn})
303 raise RecordNotFound(hrn)
305 record['last_updated'] = time.gmtime()
307 # Update_membership needs the membership lists in the existing record
308 # filled in, so it can see if members were added or removed
309 api.fill_record_info(record)
311 # Use the pointer from the existing record, not the one that the user
312 # gave us. This prevents the user from inserting a forged pointer
313 pointer = record['pointer']
314 # update the PLC information that was specified with the record
316 if (type == "authority"):
317 api.plshell.UpdateSite(api.plauth, pointer, new_record)
319 elif type == "slice":
320 pl_record=api.sfa_fields_to_pl_fields(type, hrn, new_record)
321 if 'name' in pl_record:
322 pl_record.pop('name')
323 api.plshell.UpdateSlice(api.plauth, pointer, pl_record)
326 # SMBAKER: UpdatePerson only allows a limited set of fields to be
327 # updated. Ideally we should have a more generic way of doing
328 # this. I copied the field names from UpdatePerson.py...
330 all_fields = new_record
331 for key in all_fields.keys():
332 if key in ['first_name', 'last_name', 'title', 'email',
333 'password', 'phone', 'url', 'bio', 'accepted_aup',
335 update_fields[key] = all_fields[key]
336 api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
338 if 'key' in new_record and new_record['key']:
339 # must check this key against the previous one if it exists
340 persons = api.plshell.GetPersons(api.plauth, [pointer], ['key_ids'])
342 keys = person['key_ids']
343 keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
345 if isinstance(new_record['key'], types.ListType):
346 new_key = new_record['key'][0]
348 new_key = new_record['key']
350 # Delete all stale keys
352 if new_record['key'] != key['key']:
353 api.plshell.DeleteKey(api.plauth, key['key_id'])
357 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
359 # update the openssl key and gid
360 pkey = convert_public_key(new_key)
362 gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
363 gid = gid_object.save_to_string(save_parents=True)
365 record = SfaRecord(dict=record)
369 api.plshell.UpdateNode(api.plauth, pointer, new_record)
372 raise UnknownSfaType(type)
374 # update membership for researchers, pis, owners, operators
375 api.update_membership(record, new_record)
379 def remove(api, xrn, type, origin_hrn=None):
382 hrn = urn_to_hrn(xrn)[0]
384 hrn, type = urn_to_hrn(xrn)
387 filter = {'hrn': hrn}
388 if type not in ['all', '*']:
389 filter['type'] = type
390 records = table.find(filter)
392 raise RecordNotFound(hrn)
394 type = record['type']
396 credential = api.getCredential()
397 registries = api.registries
399 # Try to remove the object from the PLCDB of federated agg.
400 # This is attempted before removing the object from the local agg's PLCDB and sfa table
401 if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']:
402 for registry in registries:
403 if registry not in [api.hrn]:
405 result=registries[registry].remove_peer_object(credential, record, origin_hrn)
409 persons = api.plshell.GetPersons(api.plauth, record['pointer'])
410 # only delete this person if he has site ids. if he doesnt, it probably means
411 # he was just removed from a site, not actually deleted
412 if persons and persons[0]['site_ids']:
413 api.plshell.DeletePerson(api.plauth, record['pointer'])
414 elif type == "slice":
415 if api.plshell.GetSlices(api.plauth, record['pointer']):
416 api.plshell.DeleteSlice(api.plauth, record['pointer'])
418 if api.plshell.GetNodes(api.plauth, record['pointer']):
419 api.plshell.DeleteNode(api.plauth, record['pointer'])
420 elif type == "authority":
421 if api.plshell.GetSites(api.plauth, record['pointer']):
422 api.plshell.DeleteSite(api.plauth, record['pointer'])
424 raise UnknownSfaType(type)
430 def remove_peer_object(api, record, origin_hrn=None):
433 def register_peer_object(api, record, origin_hrn=None):