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 *
13 def get_credential(api, xrn, type, is_self=False):
16 hrn = urn_to_hrn(xrn)[0]
18 hrn, type = urn_to_hrn(xrn)
19 # Is this a root or sub authority
20 auth_hrn = api.auth.get_authority(hrn)
21 if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
24 auth_info = api.auth.get_auth_info(auth_hrn)
26 records = table.findObjects({'type': type, 'hrn': hrn})
28 raise RecordNotFound(hrn)
31 # verify_cancreate_credential requires that the member lists
32 # (researchers, pis, etc) be filled in
33 api.fill_record_info(record)
34 if record['type']=='user':
35 if not record['enabled']:
36 raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
39 # if this is a self cred the record's gid is the caller's gid
42 caller_gid = record.get_gid_object()
44 caller_gid = api.auth.client_cred.get_gid_caller()
45 caller_hrn = caller_gid.get_hrn()
47 object_hrn = record.get_gid_object().get_hrn()
48 rights = api.auth.determine_user_rights(caller_hrn, record)
49 # make sure caller has rights to this object
51 raise PermissionError(caller_hrn + " has no rights to " + record['name'])
53 object_gid = GID(string=record['gid'])
54 new_cred = Credential(subject = object_gid.get_subject())
55 new_cred.set_gid_caller(caller_gid)
56 new_cred.set_gid_object(object_gid)
57 new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
58 #new_cred.set_pubkey(object_gid.get_pubkey())
59 new_cred.set_privileges(rights)
60 new_cred.get_privileges().delegate_all_privileges(True)
61 auth_kind = "authority,ma,sa"
62 # Parent not necessary, verify with certs
63 #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
67 return new_cred.save_to_string(save_parents=True)
69 def resolve(api, xrns, type=None, origin_hrn=None, full=True):
71 # load all know registry names into a prefix tree and attempt to find
72 # the longest matching prefix
73 if not isinstance(xrns, types.ListType):
75 hrns = [urn_to_hrn(xrn)[0] for xrn in xrns]
76 # create a dict whre key is an registry hrn and its value is a
77 # hrns at that registry (determined by the known prefix tree).
79 registries = api.registries
81 registry_hrns = registries.keys()
82 tree.load(registry_hrns)
84 registry_hrn = tree.best_match(urn_to_hrn(xrn)[0])
85 if registry_hrn not in xrn_dict:
86 xrn_dict[registry_hrn] = []
87 xrn_dict[registry_hrn].append(xrn)
90 for registry_hrn in xrn_dict:
91 # skip the hrn without a registry hrn
92 # XX should we let the user know the authority is unknown?
96 # if the best match (longest matching hrn) is not the local registry,
98 xrns = xrn_dict[registry_hrn]
99 if registry_hrn != api.hrn:
100 credential = api.getCredential()
101 peer_records = registries[registry_hrn].resolve(credential, xrns, origin_hrn)
102 records.extend([SfaRecord(dict=record).as_dict() for record in peer_records])
104 # try resolving the remaining unfound records at the local registry
105 remaining_hrns = set(hrns).difference([record['hrn'] for record in records])
106 # convert set to list
107 remaining_hrns = [hrn for hrn in remaining_hrns]
109 local_records = table.findObjects({'hrn': remaining_hrns})
111 api.fill_record_info(local_records)
113 # convert local record objects to dicts
114 records.extend([dict(record) for record in local_records])
116 raise RecordNotFound(str(hrns))
119 records = filter(lambda rec: rec['type'] in [type], records)
123 def list(api, xrn, origin_hrn=None):
124 hrn, type = urn_to_hrn(xrn)
125 # load all know registry names into a prefix tree and attempt to find
126 # the longest matching prefix
128 registries = api.registries
129 registry_hrns = registries.keys()
131 tree.load(registry_hrns)
132 registry_hrn = tree.best_match(hrn)
134 #if there was no match then this record belongs to an unknow registry
136 raise MissingAuthority(xrn)
138 # if the best match (longest matching hrn) is not the local registry,
139 # forward the request
141 if registry_hrn != api.hrn:
142 credential = api.getCredential()
143 record_list = registries[registry_hrn].list(credential, xrn, origin_hrn)
144 records = [SfaRecord(dict=record).as_dict() for record in record_list]
146 # if we still havnt found the record yet, try the local registry
148 if not api.auth.hierarchy.auth_exists(hrn):
149 raise MissingAuthority(hrn)
152 records = table.find({'authority': hrn})
157 def register(api, record):
159 hrn, type = record['hrn'], record['type']
162 if type not in ['authority', 'slice', 'node', 'user']:
163 raise UnknownSfaType(type)
165 # check if record already exists
167 existing_records = table.find({'type': type, 'hrn': hrn})
169 raise ExistingRecord(hrn)
171 record = SfaRecord(dict = record)
172 record['authority'] = get_authority(record['hrn'])
173 type = record['type']
175 api.auth.verify_object_permission(hrn)
176 auth_info = api.auth.get_auth_info(record['authority'])
178 # make sure record has a gid
179 if 'gid' not in record:
181 pkey = Keypair(create=True)
182 if 'key' in record and record['key']:
183 if isinstance(record['key'], types.ListType):
184 pub_key = record['key'][0]
186 pub_key = record['key']
187 pkey = convert_public_key(pub_key)
189 gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
190 gid = gid_object.save_to_string(save_parents=True)
194 if type in ["authority"]:
196 if not api.auth.hierarchy.auth_exists(hrn):
197 api.auth.hierarchy.create_auth(hrn)
199 # get the GID from the newly created authority
200 gid = auth_info.get_gid_object()
201 record.set_gid(gid.save_to_string(save_parents=True))
202 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
203 sites = api.plshell.GetSites(api.plauth, [pl_record['login_base']])
205 pointer = api.plshell.AddSite(api.plauth, pl_record)
207 pointer = sites[0]['site_id']
209 record.set_pointer(pointer)
210 record['pointer'] = pointer
212 elif (type == "slice"):
213 acceptable_fields=['url', 'instantiation', 'name', 'description']
214 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
215 for key in pl_record.keys():
216 if key not in acceptable_fields:
218 slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
220 pointer = api.plshell.AddSlice(api.plauth, pl_record)
222 pointer = slices[0]['slice_id']
223 record.set_pointer(pointer)
224 record['pointer'] = pointer
226 elif (type == "user"):
227 persons = api.plshell.GetPersons(api.plauth, [record['email']])
229 pointer = api.plshell.AddPerson(api.plauth, dict(record))
231 pointer = persons[0]['person_id']
233 if 'enabled' in record and record['enabled']:
234 api.plshell.UpdatePerson(api.plauth, pointer, {'enabled': record['enabled']})
235 # add this persons to the site only if he is being added for the first
236 # time by sfa and doesont already exist in plc
237 if not persons or not persons[0]['site_ids']:
238 login_base = get_leaf(record['authority'])
239 api.plshell.AddPersonToSite(api.plauth, pointer, login_base)
241 # What roles should this user have?
242 api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
245 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
247 elif (type == "node"):
248 pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
249 login_base = hrn_to_pl_login_base(record['authority'])
250 nodes = api.plshell.GetNodes(api.plauth, [pl_record['hostname']])
252 pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
254 pointer = nodes[0]['node_id']
256 record['pointer'] = pointer
257 record.set_pointer(pointer)
258 record_id = table.insert(record)
259 record['record_id'] = record_id
261 # update membership for researchers, pis, owners, operators
262 api.update_membership(None, record)
264 return record.get_gid_object().save_to_string(save_parents=True)
266 def update(api, record_dict):
267 new_record = SfaRecord(dict = record_dict)
268 type = new_record['type']
269 hrn = new_record['hrn']
270 api.auth.verify_object_permission(hrn)
272 # make sure the record exists
273 records = table.findObjects({'type': type, 'hrn': hrn})
275 raise RecordNotFound(hrn)
277 record['last_updated'] = time.gmtime()
279 # Update_membership needs the membership lists in the existing record
280 # filled in, so it can see if members were added or removed
281 api.fill_record_info(record)
283 # Use the pointer from the existing record, not the one that the user
284 # gave us. This prevents the user from inserting a forged pointer
285 pointer = record['pointer']
286 # update the PLC information that was specified with the record
288 if (type == "authority"):
289 api.plshell.UpdateSite(api.plauth, pointer, new_record)
291 elif type == "slice":
292 pl_record=api.sfa_fields_to_pl_fields(type, hrn, new_record)
293 if 'name' in pl_record:
294 pl_record.pop('name')
295 api.plshell.UpdateSlice(api.plauth, pointer, pl_record)
298 # SMBAKER: UpdatePerson only allows a limited set of fields to be
299 # updated. Ideally we should have a more generic way of doing
300 # this. I copied the field names from UpdatePerson.py...
302 all_fields = new_record
303 for key in all_fields.keys():
304 if key in ['first_name', 'last_name', 'title', 'email',
305 'password', 'phone', 'url', 'bio', 'accepted_aup',
307 update_fields[key] = all_fields[key]
308 api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
310 if 'key' in new_record and new_record['key']:
311 # must check this key against the previous one if it exists
312 persons = api.plshell.GetPersons(api.plauth, [pointer], ['key_ids'])
314 keys = person['key_ids']
315 keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
317 if isinstance(new_record['key'], types.ListType):
318 new_key = new_record['key'][0]
320 new_key = new_record['key']
322 # Delete all stale keys
324 if new_record['key'] != key['key']:
325 api.plshell.DeleteKey(api.plauth, key['key_id'])
329 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
331 # update the openssl key and gid
332 pkey = convert_public_key(new_key)
334 gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
335 gid = gid_object.save_to_string(save_parents=True)
337 record = SfaRecord(dict=record)
341 api.plshell.UpdateNode(api.plauth, pointer, new_record)
344 raise UnknownSfaType(type)
346 # update membership for researchers, pis, owners, operators
347 api.update_membership(record, new_record)
351 def remove(api, xrn, type, origin_hrn=None):
354 hrn = urn_to_hrn(xrn)[0]
356 hrn, type = urn_to_hrn(xrn)
359 filter = {'hrn': hrn}
360 if type not in ['all', '*']:
361 filter['type'] = type
362 records = table.find(filter)
364 raise RecordNotFound(hrn)
366 type = record['type']
368 credential = api.getCredential()
369 registries = api.registries
371 # Try to remove the object from the PLCDB of federated agg.
372 # This is attempted before removing the object from the local agg's PLCDB and sfa table
373 if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']:
374 for registry in registries:
375 if registry not in [api.hrn]:
377 result=registries[registry].remove_peer_object(credential, record, origin_hrn)
381 persons = api.plshell.GetPersons(api.plauth, record['pointer'])
382 # only delete this person if he has site ids. if he doesnt, it probably means
383 # he was just removed from a site, not actually deleted
384 if persons and persons[0]['site_ids']:
385 api.plshell.DeletePerson(api.plauth, record['pointer'])
386 elif type == "slice":
387 if api.plshell.GetSlices(api.plauth, record['pointer']):
388 api.plshell.DeleteSlice(api.plauth, record['pointer'])
390 if api.plshell.GetNodes(api.plauth, record['pointer']):
391 api.plshell.DeleteNode(api.plauth, record['pointer'])
392 elif type == "authority":
393 if api.plshell.GetSites(api.plauth, record['pointer']):
394 api.plshell.DeleteSite(api.plauth, record['pointer'])
396 raise UnknownSfaType(type)
402 def remove_peer_object(api, record, origin_hrn=None):
405 def register_peer_object(api, record, origin_hrn=None):