merging with geni-api branch
[sfa.git] / sfa / managers / registry_manager_pl.py
1 import types
2 import time 
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 *
12
13     
14
15 def get_credential(api, xrn, type, is_self=False):
16     # convert xrn to hrn     
17     if type:
18         hrn = urn_to_hrn(xrn)[0]
19     else:
20         hrn, type = urn_to_hrn(xrn)
21         
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:
25         auth_hrn = hrn
26     # get record info
27     auth_info = api.auth.get_auth_info(auth_hrn)
28     table = SfaTable()
29     records = table.findObjects({'type': type, 'hrn': hrn})
30     if not records:
31         raise RecordNotFound(hrn)
32     record = records[0]
33
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']))
40
41     # get the callers gid
42     # if this is a self cred the record's gid is the caller's gid
43     if is_self:
44         caller_hrn = hrn
45         caller_gid = record.get_gid_object()
46     else:
47         caller_gid = api.auth.client_cred.get_gid_caller() 
48         caller_hrn = caller_gid.get_hrn()
49     
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
53     if rights.is_empty():
54         raise PermissionError(caller_hrn + " has no rights to " + record['name'])
55
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))
67     new_cred.encode()
68     new_cred.sign()
69
70     return new_cred.save_to_string(save_parents=True)
71
72
73 # The GENI GetVersion call
74 def GetVersion():
75     version = {}
76     version['geni_api'] = 1
77     return version
78
79
80
81 # The GENI resolve call
82 def Resolve(api, xrn, creds):
83     records = resolve(api, xrn)
84     
85     if len(records) == 0:
86         return {}
87     
88     record = records[0]
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}
93     
94     
95
96 def resolve(api, xrns, type=None, origin_hrn=None, full=True):
97
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):
101         xrns = [xrns]
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).  
105     xrn_dict = {}
106     registries = api.registries
107     tree = prefixTree()
108     registry_hrns = registries.keys()
109     tree.load(registry_hrns)
110     for xrn in xrns:
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)
115         
116     records = [] 
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?       
120         if not registry_hrn:
121             continue
122
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])
130
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] 
135     table = SfaTable()
136     local_records = table.findObjects({'hrn': remaining_hrns})
137     if full:
138         api.fill_record_info(local_records)
139     
140     # convert local record objects to dicts
141     records.extend([dict(record) for record in local_records])
142     if not records:
143         raise RecordNotFound(str(hrns))
144
145     if type:
146         records = filter(lambda rec: rec['type'] in [type], records)
147
148     return records
149
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
154     records = []
155     registries = api.registries
156     registry_hrns = registries.keys()
157     tree = prefixTree()
158     tree.load(registry_hrns)
159     registry_hrn = tree.best_match(hrn)
160    
161     #if there was no match then this record belongs to an unknow registry
162     if not registry_hrn:
163         raise MissingAuthority(xrn)
164     
165     # if the best match (longest matching hrn) is not the local registry,
166     # forward the request
167     records = []    
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]
172     
173     # if we still havnt found the record yet, try the local registry
174     if not records:
175         if not api.auth.hierarchy.auth_exists(hrn):
176             raise MissingAuthority(hrn)
177
178         table = SfaTable()
179         records = table.find({'authority': hrn})
180
181     return records
182
183
184 def register(api, record):
185
186     hrn, type = record['hrn'], record['type']
187     urn = hrn_to_urn(hrn,type)
188     # validate the type
189     if type not in ['authority', 'slice', 'node', 'user']:
190         raise UnknownSfaType(type) 
191     
192     # check if record already exists
193     table = SfaTable()
194     existing_records = table.find({'type': type, 'hrn': hrn})
195     if existing_records:
196         raise ExistingRecord(hrn)
197        
198     record = SfaRecord(dict = record)
199     record['authority'] = get_authority(record['hrn'])
200     type = record['type']
201     hrn = record['hrn']
202     api.auth.verify_object_permission(hrn)
203     auth_info = api.auth.get_auth_info(record['authority'])
204     pub_key = None
205     # make sure record has a gid
206     if 'gid' not in record:
207         uuid = create_uuid()
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]
212             else:
213                 pub_key = record['key']
214             pkey = convert_public_key(pub_key)
215
216         gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
217         gid = gid_object.save_to_string(save_parents=True)
218         record['gid'] = gid
219         record.set_gid(gid)
220
221     if type in ["authority"]:
222         # update the tree
223         if not api.auth.hierarchy.auth_exists(hrn):
224             api.auth.hierarchy.create_auth(hrn_to_urn(hrn,'authority'))
225
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']])
231         if not sites:
232             pointer = api.plshell.AddSite(api.plauth, pl_record)
233         else:
234             pointer = sites[0]['site_id']
235
236         record.set_pointer(pointer)
237         record['pointer'] = pointer
238
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:
244                 pl_record.pop(key)
245         slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
246         if not slices:
247              pointer = api.plshell.AddSlice(api.plauth, pl_record)
248         else:
249              pointer = slices[0]['slice_id']
250         record.set_pointer(pointer)
251         record['pointer'] = pointer
252
253     elif  (type == "user"):
254         persons = api.plshell.GetPersons(api.plauth, [record['email']])
255         if not persons:
256             pointer = api.plshell.AddPerson(api.plauth, dict(record))
257         else:
258             pointer = persons[0]['person_id']
259
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)
267
268         # What roles should this user have?
269         api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
270         # Add the user's key
271         if pub_key:
272             api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
273
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']])
278         if not nodes:
279             pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
280         else:
281             pointer = nodes[0]['node_id']
282
283     record['pointer'] = pointer
284     record.set_pointer(pointer)
285     record_id = table.insert(record)
286     record['record_id'] = record_id
287
288     # update membership for researchers, pis, owners, operators
289     api.update_membership(None, record)
290
291     return record.get_gid_object().save_to_string(save_parents=True)
292
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)
299     table = SfaTable()
300     # make sure the record exists
301     records = table.findObjects({'type': type, 'hrn': hrn})
302     if not records:
303         raise RecordNotFound(hrn)
304     record = records[0]
305     record['last_updated'] = time.gmtime()
306
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)
310
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
315
316     if (type == "authority"):
317         api.plshell.UpdateSite(api.plauth, pointer, new_record)
318
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)
324
325     elif type == "user":
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...
329         update_fields = {}
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',
334                        'enabled']:
335                 update_fields[key] = all_fields[key]
336         api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
337
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'])
341             person = persons[0]
342             keys = person['key_ids']
343             keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
344             key_exists = False
345             if isinstance(new_record['key'], types.ListType):
346                 new_key = new_record['key'][0]
347             else:
348                 new_key = new_record['key']
349             
350             # Delete all stale keys
351             for key in keys:
352                 if new_record['key'] != key['key']:
353                     api.plshell.DeleteKey(api.plauth, key['key_id'])
354                 else:
355                     key_exists = True
356             if not key_exists:
357                 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
358
359             # update the openssl key and gid
360             pkey = convert_public_key(new_key)
361             uuid = create_uuid()
362             gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey)
363             gid = gid_object.save_to_string(save_parents=True)
364             record['gid'] = gid
365             record = SfaRecord(dict=record)
366             table.update(record)
367
368     elif type == "node":
369         api.plshell.UpdateNode(api.plauth, pointer, new_record)
370
371     else:
372         raise UnknownSfaType(type)
373
374     # update membership for researchers, pis, owners, operators
375     api.update_membership(record, new_record)
376     
377     return 1 
378
379 def remove(api, xrn, type, origin_hrn=None):
380     # convert xrn to hrn     
381     if type:
382         hrn = urn_to_hrn(xrn)[0]
383     else:
384         hrn, type = urn_to_hrn(xrn)    
385
386     table = SfaTable()
387     filter = {'hrn': hrn}
388     if type not in ['all', '*']:
389         filter['type'] = type
390     records = table.find(filter)
391     if not records:
392         raise RecordNotFound(hrn)
393     record = records[0]
394     type = record['type']
395
396     credential = api.getCredential()
397     registries = api.registries
398
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]:
404                 try:
405                     result=registries[registry].remove_peer_object(credential, record, origin_hrn)
406                 except:
407                     pass
408     if type == "user":
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'])
417     elif type == "node":
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'])
423     else:
424         raise UnknownSfaType(type)
425
426     table.remove(record)
427
428     return 1
429
430 def remove_peer_object(api, record, origin_hrn=None):
431     pass
432
433 def register_peer_object(api, record, origin_hrn=None):
434     pass