added support for urn name format. urn is the default name format used over the wire
[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(hrns, types.ListType):
71         xrns = [xrns]
72     
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 hrn_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     remaining_hrns = [hrn for hrn in remaining_hrns] 
104     table = SfaTable()
105     local_records = table.findObjects({'hrn': remaining_hrns})
106     for record in local_records:
107         try:
108             api.fill_record_info(record)
109             records.append(dict(record))
110         except PlanetLabRecordDoesNotExist:
111             # silently drop the ones that are missing in PL
112             print >> log, "ignoring SFA record ", record['hrn'], \
113                               " because pl record does not exist"    
114             table.remove(record)
115
116     if not records:
117         raise RecordNotFound(str(hrns))
118
119     if type:
120         records = filter(lambda rec: rec['type'] in [type], records)
121
122     return records
123
124 def list(api, xrn):
125     hrn, type = hrn_to_urn(xrn)
126     # load all know registry names into a prefix tree and attempt to find
127     # the longest matching prefix
128     records = []
129     registries = Registries(api)
130     registry_hrns = registries.keys()
131     tree = prefixTree()
132     tree.load(registry_hrns)
133     registry_hrn = tree.best_match(hrn)
134     
135     #if there was no match then this record belongs to an unknow registry
136     if not registry_hrn:
137         raise MissingAuthority(xrn)
138     
139     # if the best match (longest matching hrn) is not the local registry,
140     # forward the request
141     records = []    
142     if registry_hrn != api.hrn:
143         credential = api.getCredential()
144         record_list = registries[registry_hrn].list(credential, xrn, origin_hrn)
145         records = [SfaRecord(dict=record).as_dict() for record in record_list]
146     
147     # if we still havnt found the record yet, try the local registry
148     if not records:
149         if not api.auth.hierarchy.auth_exists(hrn):
150             raise MissingAuthority(hrn)
151
152         table = SfaTable()
153         records = table.find({'authority': hrn})
154
155     return records
156
157
158 def register(api, record):
159
160     hrn, type = record['hrn'], record['type']
161
162     # validate the type
163     if type not in ['authority', 'slice', 'node', 'user']:
164         raise UnknownSfaType(type) 
165     
166     # check if record already exists
167     table = SfaTable()
168     existing_records = table.find({'type': type, 'hrn': hrn})
169     if existing_records:
170         raise ExistingRecord(hrn)
171        
172     record = SfaRecord(dict = record)
173     record['authority'] = get_authority(record['hrn'])
174     type = record['type']
175     hrn = record['hrn']
176     api.auth.verify_object_permission(hrn)
177     auth_info = api.auth.get_auth_info(record['authority'])
178     pub_key = None
179     # make sure record has a gid
180     if 'gid' not in record:
181         uuid = create_uuid()
182         pkey = Keypair(create=True)
183         if 'key' in record and record['key']:
184             if isinstance(record['key'], types.ListType):
185                 pub_key = record['key'][0]
186             else:
187                 pub_key = record['key']
188             pkey = convert_public_key(pub_key)
189
190         gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
191         gid = gid_object.save_to_string(save_parents=True)
192         record['gid'] = gid
193         record.set_gid(gid)
194
195     if type in ["authority"]:
196         # update the tree
197         if not api.auth.hierarchy.auth_exists(hrn):
198             api.auth.hierarchy.create_auth(hrn)
199
200         # get the GID from the newly created authority
201         gid = auth_info.get_gid_object()
202         record.set_gid(gid.save_to_string(save_parents=True))
203         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
204         sites = api.plshell.GetSites(api.plauth, [pl_record['login_base']])
205         if not sites:
206             pointer = api.plshell.AddSite(api.plauth, pl_record)
207         else:
208             pointer = sites[0]['site_id']
209
210         record.set_pointer(pointer)
211         record['pointer'] = pointer
212
213     elif (type == "slice"):
214         acceptable_fields=['url', 'instantiation', 'name', 'description']
215         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
216         for key in pl_record.keys():
217             if key not in acceptable_fields:
218                 pl_record.pop(key)
219             slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
220             if not slices:
221                 pointer = api.plshell.AddSlice(api.plauth, pl_record)
222             else:
223                 pointer = slices[0]['slice_id']
224             record.set_pointer(pointer)
225             record['pointer'] = pointer
226
227     elif  (type == "user"):
228         persons = api.plshell.GetPersons(api.plauth, [record['email']])
229         if not persons:
230             pointer = api.plshell.AddPerson(api.plauth, dict(record))
231         else:
232             pointer = persons[0]['person_id']
233
234         if 'enabled' in record and record['enabled']:
235             api.plshell.UpdatePerson(api.plauth, pointer, {'enabled': record['enabled']})
236         # add this persons to the site only if he is being added for the first
237         # time by sfa and doesont already exist in plc
238         if not persons or not persons[0]['site_ids']:
239             login_base = get_leaf(record['authority'])
240             api.plshell.AddPersonToSite(api.plauth, pointer, login_base)
241
242         # What roles should this user have?
243         api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
244         # Add the user's key
245         if pub_key:
246             api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
247
248     elif (type == "node"):
249         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
250         login_base = hrn_to_pl_login_base(record['authority'])
251         nodes = api.plshell.GetNodes(api.plauth, [pl_record['hostname']])
252         if not nodes:
253             pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
254         else:
255             pointer = nodes[0]['node_id']
256
257     record['pointer'] = pointer
258     record.set_pointer(pointer)
259     record_id = table.insert(record)
260     record['record_id'] = record_id
261
262     # update membership for researchers, pis, owners, operators
263     api.update_membership(None, record)
264
265     return record.get_gid_object().save_to_string(save_parents=True)
266
267 def update(api, record_dict):
268     new_record = SfaRecord(dict = record_dict)
269     type = new_record['type']
270     hrn = new_record['hrn']
271     api.auth.verify_object_permission(hrn)
272     table = SfaTable()
273     # make sure the record exists
274     records = table.findObjects({'type': type, 'hrn': hrn})
275     if not records:
276         raise RecordNotFound(hrn)
277     record = records[0]
278     record['last_updated'] = time.gmtime()
279
280     # Update_membership needs the membership lists in the existing record
281     # filled in, so it can see if members were added or removed
282     api.fill_record_info(record)
283
284     # Use the pointer from the existing record, not the one that the user
285     # gave us. This prevents the user from inserting a forged pointer
286     pointer = record['pointer']
287     # update the PLC information that was specified with the record
288
289     if (type == "authority"):
290         api.plshell.UpdateSite(api.plauth, pointer, new_record)
291
292     elif type == "slice":
293         pl_record=api.sfa_fields_to_pl_fields(type, hrn, new_record)
294         if 'name' in pl_record:
295             pl_record.pop('name')
296             api.plshell.UpdateSlice(api.plauth, pointer, pl_record)
297
298     elif type == "user":
299         # SMBAKER: UpdatePerson only allows a limited set of fields to be
300         #    updated. Ideally we should have a more generic way of doing
301         #    this. I copied the field names from UpdatePerson.py...
302         update_fields = {}
303         all_fields = new_record
304         for key in all_fields.keys():
305             if key in ['first_name', 'last_name', 'title', 'email',
306                        'password', 'phone', 'url', 'bio', 'accepted_aup',
307                        'enabled']:
308                 update_fields[key] = all_fields[key]
309         api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
310
311         if 'key' in new_record and new_record['key']:
312             # must check this key against the previous one if it exists
313             persons = api.plshell.GetPersons(api.plauth, [pointer], ['key_ids'])
314             person = persons[0]
315             keys = person['key_ids']
316             keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
317             key_exists = False
318             if isinstance(new_record['key'], types.ListType):
319                 new_key = new_record['key'][0]
320             else:
321                 new_key = new_record['key']
322             
323             # Delete all stale keys
324             for key in keys:
325                 if new_record['key'] != key['key']:
326                     api.plshell.DeleteKey(api.plauth, key['key_id'])
327                 else:
328                     key_exists = True
329             if not key_exists:
330                 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
331
332             # update the openssl key and gid
333             pkey = convert_public_key(new_key)
334             uuid = create_uuid()
335             gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
336             gid = gid_object.save_to_string(save_parents=True)
337             record['gid'] = gid
338             record = SfaRecord(dict=record)
339             table.update(record)
340
341     elif type == "node":
342         api.plshell.UpdateNode(api.plauth, pointer, new_record)
343
344     else:
345         raise UnknownSfaType(type)
346
347     # update membership for researchers, pis, owners, operators
348     api.update_membership(record, new_record)
349     
350     return 1 
351
352 def remove(api, xrn, type, origin_hrn=None):
353     # convert xrn to hrn     
354     if type:
355         hrn = urn_to_hrn(xrn)[0]
356     else:
357         hrn, type = urn_to_hrn(xrn)    
358
359     table = SfaTable()
360     filter = {'hrn': hrn}
361     if type not in ['all', '*']:
362         filter['type'] = type
363     records = table.find(filter)
364     if not records:
365         raise RecordNotFound(hrn)
366     record = records[0]
367     type = record['type']
368
369     credential = api.getCredential()
370     registries = Registries(api)
371
372     # Try to remove the object from the PLCDB of federated agg.
373     # This is attempted before removing the object from the local agg's PLCDB and sfa table
374     if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']:
375         for registry in registries:
376             if registry not in [api.hrn]:
377                 try:
378                     result=registries[registry].remove_peer_object(credential, record, origin_hrn)
379                 except:
380                     pass
381     if type == "user":
382         persons = api.plshell.GetPersons(api.plauth, record['pointer'])
383         # only delete this person if he has site ids. if he doesnt, it probably means
384         # he was just removed from a site, not actually deleted
385         if persons and persons[0]['site_ids']:
386             api.plshell.DeletePerson(api.plauth, record['pointer'])
387     elif type == "slice":
388         if api.plshell.GetSlices(api.plauth, record['pointer']):
389             api.plshell.DeleteSlice(api.plauth, record['pointer'])
390     elif type == "node":
391         if api.plshell.GetNodes(api.plauth, record['pointer']):
392             api.plshell.DeleteNode(api.plauth, record['pointer'])
393     elif type == "authority":
394         if api.plshell.GetSites(api.plauth, record['pointer']):
395             api.plshell.DeleteSite(api.plauth, record['pointer'])
396     else:
397         raise UnknownSfaType(type)
398
399     table.remove(record)
400
401     return 1
402
403 def remove_peer_object(api, record, origin_hrn=None):
404     pass
405
406 def register_peer_object(api, record, origin_hrn=None):
407     pass