ordering the for loop
[sfa.git] / sfa / managers / registry_manager_pl.py
1 import types
2 import time 
3 from sfa.server.registry import Registries
4 from sfa.util.prefixTree import prefixTree
5 from sfa.util.record import SfaRecord
6 from sfa.util.table import SfaTable
7 from sfa.util.record import SfaRecord
8 from sfa.trust.gid import GID 
9 from sfa.util.namespace import *
10 from sfa.trust.credential import *
11 from sfa.trust.certificate import *
12 from sfa.util.faults import *
13
14 def get_credential(api, xrn, type, is_self=False):
15     # convert xrn to hrn     
16     if type:
17         hrn = urn_to_hrn(xrn)[0]
18     else:
19         hrn, type = urn_to_hrn(xrn)
20     # Is this a root or sub authority
21     auth_hrn = api.auth.get_authority(hrn)
22     if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
23         auth_hrn = hrn
24     # get record info
25     auth_info = api.auth.get_auth_info(auth_hrn)
26     table = SfaTable()
27     records = table.findObjects({'type': type, 'hrn': hrn})
28     if not records:
29         raise RecordNotFound(hrn)
30     record = records[0]
31
32     # verify_cancreate_credential requires that the member lists
33     # (researchers, pis, etc) be filled in
34     api.fill_record_info(record)
35
36     # get the callers gid
37     # if this is a self cred the record's gid is the caller's gid
38     if is_self:
39         caller_hrn = hrn
40         caller_gid = record.get_gid_object()
41     else:
42         caller_gid = api.auth.client_cred.get_gid_caller() 
43         caller_hrn = caller_gid.get_hrn()
44     
45     object_hrn = record.get_gid_object().get_hrn()
46     rights = api.auth.determine_user_rights(caller_hrn, record)
47     # make sure caller has rights to this object
48     if rights.is_empty():
49         raise PermissionError(caller_hrn + " has no rights to " + record['name'])
50
51     object_gid = GID(string=record['gid'])
52     new_cred = Credential(subject = object_gid.get_subject())
53     new_cred.set_gid_caller(caller_gid)
54     new_cred.set_gid_object(object_gid)
55     new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
56     new_cred.set_pubkey(object_gid.get_pubkey())
57     new_cred.set_privileges(rights)
58     new_cred.set_delegate(True)
59     auth_kind = "authority,ma,sa"
60     new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
61     new_cred.encode()
62     new_cred.sign()
63
64     return new_cred.save_to_string(save_parents=True)
65
66 def resolve(api, xrns, type=None, origin_hrn=None):
67
68     # load all know registry names into a prefix tree and attempt to find
69     # the longest matching prefix
70     if not isinstance(xrns, types.ListType):
71         xrns = [xrns]
72     hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] 
73     # create a dict whre key is an registry hrn and its value is a
74     # hrns at that registry (determined by the known prefix tree).  
75     xrn_dict = {}
76     registries = Registries(api)
77     tree = prefixTree()
78     registry_hrns = registries.keys()
79     tree.load(registry_hrns)
80     for xrn in xrns:
81         registry_hrn = tree.best_match(urn_to_hrn(xrn)[0])
82         if registry_hrn not in xrn_dict:
83             xrn_dict[registry_hrn] = []
84         xrn_dict[registry_hrn].append(xrn)
85         
86     records = [] 
87     for registry_hrn in xrn_dict:
88         # skip the hrn without a registry hrn
89         # XX should we let the user know the authority is unknown?       
90         if not registry_hrn:
91             continue
92
93         # if the best match (longest matching hrn) is not the local registry,
94         # forward the request
95         xrns = xrn_dict[registry_hrn]
96         if registry_hrn != api.hrn:
97             credential = api.getCredential()
98             peer_records = registries[registry_hrn].resolve(credential, xrns, origin_hrn)
99             records.extend([SfaRecord(dict=record).as_dict() for record in peer_records])
100
101     # try resolving the remaining unfound records at the local registry
102     remaining_hrns = set(hrns).difference([record['hrn'] for record in records])
103     # convert set to list
104     remaining_hrns = [hrn for hrn in remaining_hrns] 
105     table = SfaTable()
106     local_records = table.findObjects({'hrn': remaining_hrns})
107     for record in local_records:
108         try:
109             api.fill_record_info(record)
110             records.append(dict(record))
111         except PlanetLabRecordDoesNotExist:
112             # silently drop the ones that are missing in PL
113             print >> log, "ignoring SFA record ", record['hrn'], \
114                               " because pl record does not exist"    
115             table.remove(record)
116
117     if not records:
118         raise RecordNotFound(str(hrns))
119
120     if type:
121         records = filter(lambda rec: rec['type'] in [type], records)
122
123     return records
124
125 def list(api, xrn):
126     hrn, type = urn_to_hrn(xrn)
127     # load all know registry names into a prefix tree and attempt to find
128     # the longest matching prefix
129     records = []
130     registries = Registries(api)
131     registry_hrns = registries.keys()
132     tree = prefixTree()
133     tree.load(registry_hrns)
134     registry_hrn = tree.best_match(hrn)
135     
136     #if there was no match then this record belongs to an unknow registry
137     if not registry_hrn:
138         raise MissingAuthority(xrn)
139     
140     # if the best match (longest matching hrn) is not the local registry,
141     # forward the request
142     records = []    
143     if registry_hrn != api.hrn:
144         credential = api.getCredential()
145         record_list = registries[registry_hrn].list(credential, xrn, origin_hrn)
146         records = [SfaRecord(dict=record).as_dict() for record in record_list]
147     
148     # if we still havnt found the record yet, try the local registry
149     if not records:
150         if not api.auth.hierarchy.auth_exists(hrn):
151             raise MissingAuthority(hrn)
152
153         table = SfaTable()
154         records = table.find({'authority': hrn})
155
156     return records
157
158
159 def register(api, record):
160
161     hrn, type = record['hrn'], record['type']
162
163     # validate the type
164     if type not in ['authority', 'slice', 'node', 'user']:
165         raise UnknownSfaType(type) 
166     
167     # check if record already exists
168     table = SfaTable()
169     existing_records = table.find({'type': type, 'hrn': hrn})
170     if existing_records:
171         raise ExistingRecord(hrn)
172        
173     record = SfaRecord(dict = record)
174     record['authority'] = get_authority(record['hrn'])
175     type = record['type']
176     hrn = record['hrn']
177     api.auth.verify_object_permission(hrn)
178     auth_info = api.auth.get_auth_info(record['authority'])
179     pub_key = None
180     # make sure record has a gid
181     if 'gid' not in record:
182         uuid = create_uuid()
183         pkey = Keypair(create=True)
184         if 'key' in record and record['key']:
185             if isinstance(record['key'], types.ListType):
186                 pub_key = record['key'][0]
187             else:
188                 pub_key = record['key']
189             pkey = convert_public_key(pub_key)
190
191         gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
192         gid = gid_object.save_to_string(save_parents=True)
193         record['gid'] = gid
194         record.set_gid(gid)
195
196     if type in ["authority"]:
197         # update the tree
198         if not api.auth.hierarchy.auth_exists(hrn):
199             api.auth.hierarchy.create_auth(hrn)
200
201         # get the GID from the newly created authority
202         gid = auth_info.get_gid_object()
203         record.set_gid(gid.save_to_string(save_parents=True))
204         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
205         sites = api.plshell.GetSites(api.plauth, [pl_record['login_base']])
206         if not sites:
207             pointer = api.plshell.AddSite(api.plauth, pl_record)
208         else:
209             pointer = sites[0]['site_id']
210
211         record.set_pointer(pointer)
212         record['pointer'] = pointer
213
214     elif (type == "slice"):
215         acceptable_fields=['url', 'instantiation', 'name', 'description']
216         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
217         for key in pl_record.keys():
218             if key not in acceptable_fields:
219                 pl_record.pop(key)
220         slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
221         if not slices:
222              pointer = api.plshell.AddSlice(api.plauth, pl_record)
223         else:
224              pointer = slices[0]['slice_id']
225         record.set_pointer(pointer)
226         record['pointer'] = pointer
227
228     elif  (type == "user"):
229         persons = api.plshell.GetPersons(api.plauth, [record['email']])
230         if not persons:
231             pointer = api.plshell.AddPerson(api.plauth, dict(record))
232         else:
233             pointer = persons[0]['person_id']
234
235         if 'enabled' in record and record['enabled']:
236             api.plshell.UpdatePerson(api.plauth, pointer, {'enabled': record['enabled']})
237         # add this persons to the site only if he is being added for the first
238         # time by sfa and doesont already exist in plc
239         if not persons or not persons[0]['site_ids']:
240             login_base = get_leaf(record['authority'])
241             api.plshell.AddPersonToSite(api.plauth, pointer, login_base)
242
243         # What roles should this user have?
244         api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
245         # Add the user's key
246         if pub_key:
247             api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
248
249     elif (type == "node"):
250         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
251         login_base = hrn_to_pl_login_base(record['authority'])
252         nodes = api.plshell.GetNodes(api.plauth, [pl_record['hostname']])
253         if not nodes:
254             pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
255         else:
256             pointer = nodes[0]['node_id']
257
258     record['pointer'] = pointer
259     record.set_pointer(pointer)
260     record_id = table.insert(record)
261     record['record_id'] = record_id
262
263     # update membership for researchers, pis, owners, operators
264     api.update_membership(None, record)
265
266     return record.get_gid_object().save_to_string(save_parents=True)
267
268 def update(api, record_dict):
269     new_record = SfaRecord(dict = record_dict)
270     type = new_record['type']
271     hrn = new_record['hrn']
272     api.auth.verify_object_permission(hrn)
273     table = SfaTable()
274     # make sure the record exists
275     records = table.findObjects({'type': type, 'hrn': hrn})
276     if not records:
277         raise RecordNotFound(hrn)
278     record = records[0]
279     record['last_updated'] = time.gmtime()
280
281     # Update_membership needs the membership lists in the existing record
282     # filled in, so it can see if members were added or removed
283     api.fill_record_info(record)
284
285     # Use the pointer from the existing record, not the one that the user
286     # gave us. This prevents the user from inserting a forged pointer
287     pointer = record['pointer']
288     # update the PLC information that was specified with the record
289
290     if (type == "authority"):
291         api.plshell.UpdateSite(api.plauth, pointer, new_record)
292
293     elif type == "slice":
294         pl_record=api.sfa_fields_to_pl_fields(type, hrn, new_record)
295         if 'name' in pl_record:
296             pl_record.pop('name')
297             api.plshell.UpdateSlice(api.plauth, pointer, pl_record)
298
299     elif type == "user":
300         # SMBAKER: UpdatePerson only allows a limited set of fields to be
301         #    updated. Ideally we should have a more generic way of doing
302         #    this. I copied the field names from UpdatePerson.py...
303         update_fields = {}
304         all_fields = new_record
305         for key in all_fields.keys():
306             if key in ['first_name', 'last_name', 'title', 'email',
307                        'password', 'phone', 'url', 'bio', 'accepted_aup',
308                        'enabled']:
309                 update_fields[key] = all_fields[key]
310         api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
311
312         if 'key' in new_record and new_record['key']:
313             # must check this key against the previous one if it exists
314             persons = api.plshell.GetPersons(api.plauth, [pointer], ['key_ids'])
315             person = persons[0]
316             keys = person['key_ids']
317             keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
318             key_exists = False
319             if isinstance(new_record['key'], types.ListType):
320                 new_key = new_record['key'][0]
321             else:
322                 new_key = new_record['key']
323             
324             # Delete all stale keys
325             for key in keys:
326                 if new_record['key'] != key['key']:
327                     api.plshell.DeleteKey(api.plauth, key['key_id'])
328                 else:
329                     key_exists = True
330             if not key_exists:
331                 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
332
333             # update the openssl key and gid
334             pkey = convert_public_key(new_key)
335             uuid = create_uuid()
336             gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
337             gid = gid_object.save_to_string(save_parents=True)
338             record['gid'] = gid
339             record = SfaRecord(dict=record)
340             table.update(record)
341
342     elif type == "node":
343         api.plshell.UpdateNode(api.plauth, pointer, new_record)
344
345     else:
346         raise UnknownSfaType(type)
347
348     # update membership for researchers, pis, owners, operators
349     api.update_membership(record, new_record)
350     
351     return 1 
352
353 def remove(api, xrn, type, origin_hrn=None):
354     # convert xrn to hrn     
355     if type:
356         hrn = urn_to_hrn(xrn)[0]
357     else:
358         hrn, type = urn_to_hrn(xrn)    
359
360     table = SfaTable()
361     filter = {'hrn': hrn}
362     if type not in ['all', '*']:
363         filter['type'] = type
364     records = table.find(filter)
365     if not records:
366         raise RecordNotFound(hrn)
367     record = records[0]
368     type = record['type']
369
370     credential = api.getCredential()
371     registries = Registries(api)
372
373     # Try to remove the object from the PLCDB of federated agg.
374     # This is attempted before removing the object from the local agg's PLCDB and sfa table
375     if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']:
376         for registry in registries:
377             if registry not in [api.hrn]:
378                 try:
379                     result=registries[registry].remove_peer_object(credential, record, origin_hrn)
380                 except:
381                     pass
382     if type == "user":
383         persons = api.plshell.GetPersons(api.plauth, record['pointer'])
384         # only delete this person if he has site ids. if he doesnt, it probably means
385         # he was just removed from a site, not actually deleted
386         if persons and persons[0]['site_ids']:
387             api.plshell.DeletePerson(api.plauth, record['pointer'])
388     elif type == "slice":
389         if api.plshell.GetSlices(api.plauth, record['pointer']):
390             api.plshell.DeleteSlice(api.plauth, record['pointer'])
391     elif type == "node":
392         if api.plshell.GetNodes(api.plauth, record['pointer']):
393             api.plshell.DeleteNode(api.plauth, record['pointer'])
394     elif type == "authority":
395         if api.plshell.GetSites(api.plauth, record['pointer']):
396             api.plshell.DeleteSite(api.plauth, record['pointer'])
397     else:
398         raise UnknownSfaType(type)
399
400     table.remove(record)
401
402     return 1
403
404 def remove_peer_object(api, record, origin_hrn=None):
405     pass
406
407 def register_peer_object(api, record, origin_hrn=None):
408     pass