Merge branch 'master' into senslab2
[sfa.git] / sfa / managers / registry_manager_openstack.py
1 import types
2 # for get_key_from_incoming_ip
3 import tempfile
4 import os
5 import commands
6
7 from sfa.util.faults import RecordNotFound, AccountNotEnabled, PermissionError, MissingAuthority, \
8     UnknownSfaType, ExistingRecord, NonExistingRecord
9 from sfa.util.sfatime import utcparse, datetime_to_epoch
10 from sfa.util.prefixTree import prefixTree
11 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn, urn_to_hrn
12 from sfa.util.plxrn import hrn_to_pl_login_base
13 from sfa.util.version import version_core
14 from sfa.util.sfalogging import logger
15
16 from sfa.trust.gid import GID 
17 from sfa.trust.credential import Credential
18 from sfa.trust.certificate import Certificate, Keypair, convert_public_key
19 from sfa.trust.gid import create_uuid
20
21 from sfa.storage.model import make_record,RegRecord
22 from sfa.storage.alchemy import dbsession
23
24 from sfa.managers.registry_manager import RegistryManager
25
26 class RegistryManager(RegistryManager):
27
28     def GetCredential(self, api, xrn, type, caller_xrn = None):
29         # convert xrn to hrn     
30         if type:
31             hrn = urn_to_hrn(xrn)[0]
32         else:
33             hrn, type = urn_to_hrn(xrn)
34             
35         # Is this a root or sub authority
36         auth_hrn = api.auth.get_authority(hrn)
37         if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
38             auth_hrn = hrn
39         auth_info = api.auth.get_auth_info(auth_hrn)
40         # get record info
41         record=dbsession.query(RegRecord).filter_by(hrn=hrn).first()
42         if type:
43             record = record.filter_by(type=type)
44         if not record:
45             raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
46     
47         # verify_cancreate_credential requires that the member lists
48         # (researchers, pis, etc) be filled in
49         logger.debug("get credential before augment dict, keys=%s"%record.__dict__.keys())
50         self.driver.augment_records_with_testbed_info (record.__dict__)
51         logger.debug("get credential after augment dict, keys=%s"%record.__dict__.keys())
52         if not self.driver.is_enabled (record.__dict__):
53               raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record.email))
54     
55         # get the callers gid
56         # if caller_xrn is not specified assume the caller is the record
57         # object itself.  
58         if not caller_xrn:
59             caller_hrn = hrn
60             caller_gid = record.get_gid_object()
61         else:
62             caller_hrn, caller_type = urn_to_hrn(caller_xrn)
63             caller_record = dbsession.query(RegRecord).filter_by(hrn=caller_hrn).first()
64             if caller_type:
65                 caller_record = caller_record.filter_by(type=caller_type)
66             if not caller_record:
67                 raise RecordNotFound("Unable to associated caller (hrn=%s, type=%s) with credential for (hrn: %s, type: %s)"%(caller_hrn, caller_type, hrn, type))
68             caller_gid = GID(string=caller_record.gid) 
69         
70         object_hrn = record.get_gid_object().get_hrn()
71         rights = api.auth.determine_user_rights(caller_hrn, record.__dict__)
72         # make sure caller has rights to this object
73         if rights.is_empty():
74             raise PermissionError(caller_hrn + " has no rights to " + record.hrn)
75     
76         object_gid = GID(string=record.gid)
77         new_cred = Credential(subject = object_gid.get_subject())
78         new_cred.set_gid_caller(caller_gid)
79         new_cred.set_gid_object(object_gid)
80         new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
81         #new_cred.set_pubkey(object_gid.get_pubkey())
82         new_cred.set_privileges(rights)
83         new_cred.get_privileges().delegate_all_privileges(True)
84         if hasattr(record,'expires'):
85             date = utcparse(record.expires)
86             expires = datetime_to_epoch(date)
87             new_cred.set_expiration(int(expires))
88         auth_kind = "authority,ma,sa"
89         # Parent not necessary, verify with certs
90         #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
91         new_cred.encode()
92         new_cred.sign()
93     
94         return new_cred.save_to_string(save_parents=True)
95     
96     
97     # subject_record describes the subject of the relationships
98     # ref_record contains the target values for the various relationships we need to manage
99     # (to begin with, this is just the slice x person relationship)
100     def update_relations (self, subject_obj, ref_obj):
101         type=subject_obj.type
102         if type=='slice':
103             self.update_relation(subject_obj, 'researcher', ref_obj.researcher, 'user')
104         
105     # field_key is the name of one field in the record, typically 'researcher' for a 'slice' record
106     # hrns is the list of hrns that should be linked to the subject from now on
107     # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
108     def update_relation (self, record_obj, field_key, hrns, target_type):
109         # locate the linked objects in our db
110         subject_type=record_obj.type
111         subject_id=record_obj.pointer
112         # get the 'pointer' field of all matching records
113         link_id_tuples = dbsession.query(RegRecord.pointer).filter_by(type=target_type).filter(RegRecord.hrn.in_(hrns)).all()
114         # sqlalchemy returns named tuples for columns
115         link_ids = [ tuple.pointer for tuple in link_id_tuples ]
116         self.driver.update_relation (subject_type, target_type, subject_id, link_ids)
117