server.cert has subject set to registry, and in this case certificate.get_subject...
[sfa.git] / sfa / methods / get_self_credential.py
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 $
3
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
14
15 class get_self_credential(Method):
16     """
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
21
22     @return the string representation of a credential object  
23     """
24
25     interfaces = ['registry']
26     
27     accepts = [
28         Parameter(str, "certificate"),
29         Parameter(str, "Human readable name (hrn)"),
30         Mixed(Parameter(str, "Request hash"),
31               Parameter(None, "Request hash not specified"))
32         ]
33
34     returns = Parameter(str, "String representation of a credential object")
35
36     def call(self, cert, type, hrn, request_hash=None):
37         """
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, ...)
41
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.
47
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
51         """
52         self.api.auth.verify_object_belongs_to_me(hrn)
53         auth_hrn = self.api.auth.get_authority(hrn)
54          
55         # if this is a root or sub authority get_authority will return
56         # an empty string
57         if not auth_hrn or hrn == self.api.config.SFA_INTERFACE_HRN:
58             auth_hrn = hrn
59
60         auth_info = self.api.auth.get_auth_info(auth_hrn)
61
62         # find a record that matches
63         record = None
64         table = GeniTable()
65         records = table.findObjects({'type': type, 'hrn':  hrn})
66         if not records:
67             raise RecordNotFound(hrn)
68         record = records[0]
69         
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)
74         
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())
79
80         # get the right of this record
81         #caller_hrn = certificate.get_subject()    
82         # server.cert has subject 'registry'
83         caller_hrn=hrn
84         rights = self.api.auth.determine_user_rights(caller_hrn, record)
85         if rights.is_empty():
86             raise PermissionError(caller_hrn + " has no rights to " + record.get_name())
87
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)
97
98         auth_kind = "authority,sa,ma"
99         cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
100
101         cred.encode()
102         cred.sign()
103         return cred.save_to_string(save_parents=True)