1 ### $Id: get_credential.py 15321 2009-10-15 05:01:21Z tmack $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/methods/get_credential.py $
4 from sfa.trust.credential import *
5 from sfa.trust.rights import *
6 from sfa.util.faults import *
7 from sfa.util.method import Method
8 from sfa.util.parameter import Parameter, Mixed
9 from sfa.trust.auth import Auth
10 from sfa.trust.gid import GID
11 from sfa.util.record import GeniRecord
12 from sfa.util.genitable import *
13 from sfa.util.debug import log
15 class get_self_credential(Method):
17 Retrive a credential for an object
18 @param cert certificate string
19 @param type type of object (user | slice | sa | ma | node)
20 @param hrn human readable name of object
22 @return the string representation of a credential object
25 interfaces = ['registry']
28 Parameter(str, "certificate"),
29 Parameter(str, "Human readable name (hrn)"),
30 Mixed(Parameter(str, "Request hash"),
31 Parameter(None, "Request hash not specified"))
34 returns = Parameter(str, "String representation of a credential object")
36 def call(self, cert, type, hrn, request_hash=None):
38 get_self_credential a degenerate version of get_credential used by a client
39 to get his initial credential when de doesnt have one. This is the same as
40 get_credetial(..., cred = None, ...)
42 The registry ensures that the client is the principal that is named by
43 (type, name) by comparing the public key in the record's GID to the
44 private key used to encrypt the client side of the HTTPS connection. Thus
45 it is impossible for one principal to retrive another principal's
46 credential without having the appropriate private key.
48 @param type type of object (user | slice | sa | ma | node)
49 @param hrn human readable name of authority to list
50 @return string representation of a credential object
52 self.api.auth.verify_object_belongs_to_me(hrn)
53 auth_hrn = self.api.auth.get_authority(hrn)
55 # if this is a root or sub authority get_authority will return
57 if not auth_hrn or hrn == self.api.config.SFA_INTERFACE_HRN:
60 auth_info = self.api.auth.get_auth_info(auth_hrn)
62 # find a record that matches
65 records = table.findObjects({'type': type, 'hrn': hrn})
67 raise RecordNotFound(hrn)
70 # authenticate the gid
71 gid = record.get_gid_object()
72 gid_str = gid.save_to_string(save_parents=True)
73 self.api.auth.authenticateGid(gid_str, [cert, type, hrn], request_hash)
75 # authenticate the certificate against the gid in the db
76 certificate = Certificate(string=cert)
77 if not certificate.is_pubkey(gid.get_pubkey()):
78 raise ConnectionKeyGIDMismatch(gid.get_subject())
80 # get the right of this record
81 #caller_hrn = certificate.get_subject()
82 # server.cert has subject 'registry'
84 rights = self.api.auth.determine_user_rights(caller_hrn, record)
86 raise PermissionError(caller_hrn + " has no rights to " + record.get_name())
88 # create the credential
89 gid = record.get_gid_object()
90 cred = Credential(subject = gid.get_subject())
91 cred.set_gid_caller(gid)
92 cred.set_gid_object(gid)
93 cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
94 cred.set_pubkey(gid.get_pubkey())
95 cred.set_privileges(rights)
96 cred.set_delegate(True)
98 auth_kind = "authority,sa,ma"
99 cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
103 return cred.save_to_string(save_parents=True)