2 # for get_key_from_incoming_ip
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
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
21 from sfa.storage.model import make_record,RegRecord
22 from sfa.storage.alchemy import dbsession
24 from sfa.managers.registry_manager import RegistryManager
26 class RegistryManager(RegistryManager):
28 def GetCredential(self, api, xrn, type, is_self=False):
31 hrn = urn_to_hrn(xrn)[0]
33 hrn, type = urn_to_hrn(xrn)
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:
39 auth_info = api.auth.get_auth_info(auth_hrn)
41 record=dbsession.query(RegRecord).filter_by(type=type,hrn=hrn).first()
43 raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
45 # verify_cancreate_credential requires that the member lists
46 # (researchers, pis, etc) be filled in
47 logger.debug("get credential before augment dict, keys=%s"%record.__dict__.keys())
48 self.driver.augment_records_with_testbed_info (record.__dict__)
49 logger.debug("get credential after augment dict, keys=%s"%record.__dict__.keys())
50 if not self.driver.is_enabled (record.__dict__):
51 raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record.email))
54 # if this is a self cred the record's gid is the caller's gid
57 caller_gid = record.get_gid_object()
59 caller_gid = api.auth.client_cred.get_gid_caller()
60 caller_hrn = caller_gid.get_hrn()
62 object_hrn = record.get_gid_object().get_hrn()
63 rights = api.auth.determine_user_rights(caller_hrn, record.__dict__)
64 # make sure caller has rights to this object
66 raise PermissionError(caller_hrn + " has no rights to " + record.hrn)
68 object_gid = GID(string=record.gid)
69 new_cred = Credential(subject = object_gid.get_subject())
70 new_cred.set_gid_caller(caller_gid)
71 new_cred.set_gid_object(object_gid)
72 new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
73 #new_cred.set_pubkey(object_gid.get_pubkey())
74 new_cred.set_privileges(rights)
75 new_cred.get_privileges().delegate_all_privileges(True)
76 if hasattr(record,'expires'):
77 date = utcparse(record.expires)
78 expires = datetime_to_epoch(date)
79 new_cred.set_expiration(int(expires))
80 auth_kind = "authority,ma,sa"
81 # Parent not necessary, verify with certs
82 #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
86 return new_cred.save_to_string(save_parents=True)
89 # subject_record describes the subject of the relationships
90 # ref_record contains the target values for the various relationships we need to manage
91 # (to begin with, this is just the slice x person relationship)
92 def update_relations (self, subject_obj, ref_obj):
95 self.update_relation(subject_obj, 'researcher', ref_obj.researcher, 'user')
97 # field_key is the name of one field in the record, typically 'researcher' for a 'slice' record
98 # hrns is the list of hrns that should be linked to the subject from now on
99 # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
100 def update_relation (self, record_obj, field_key, hrns, target_type):
101 # locate the linked objects in our db
102 subject_type=record_obj.type
103 subject_id=record_obj.pointer
104 # get the 'pointer' field of all matching records
105 link_id_tuples = dbsession.query(RegRecord.pointer).filter_by(type=target_type).filter(RegRecord.hrn.in_(hrns)).all()
106 # sqlalchemy returns named tuples for columns
107 link_ids = [ tuple.pointer for tuple in link_id_tuples ]
108 self.driver.update_relation (subject_type, target_type, subject_id, link_ids)