Replaced get_credential with GetCredential and get_self_credential with GetSelfCredential
[sfa.git] / sfa / methods / GetSelfCredential.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.namespace import *
8 from sfa.util.method import Method
9 from sfa.util.parameter import Parameter, Mixed
10 from sfa.util.record import SfaRecord
11 from sfa.util.debug import log
12 from sfa.trust.certificate import Certificate
13
14 class GetSelfCredential(Method):
15     """
16     Retrive a credential for an object
17     @param cert certificate string 
18     @param type type of object (user | slice | sa | ma | node)
19     @param hrn human readable name of object (hrn or urn)
20
21     @return the string representation of a credential object  
22     """
23
24     interfaces = ['registry']
25     
26     accepts = [
27         Parameter(str, "certificate"),
28         Parameter(str, "Human readable name (hrn or urn)"),
29         Parameter(str, "Object type"),
30         ]
31
32     returns = Parameter(str, "String representation of a credential object")
33
34     def call(self, cert, xrn, type):
35         """
36         get_self_credential a degenerate version of get_credential used by a client
37         to get his initial credential when de doesnt have one. This is the same as
38         get_credetial(..., cred = None, ...)
39
40         The registry ensures that the client is the principal that is named by
41         (type, name) by comparing the public key in the record's  GID to the
42         private key used to encrypt the client side of the HTTPS connection. Thus
43         it is impossible for one principal to retrive another principal's
44         credential without having the appropriate private key.
45
46         @param type type of object (user | slice | sa | ma | node)
47         @param hrn human readable name of authority to list
48         @return string representation of a credential object
49         """
50         if type:
51             hrn = urn_to_hrn(xrn)[0]
52         else:
53             hrn, type = urn_to_hrn(xrn) 
54         self.api.auth.verify_object_belongs_to_me(hrn)
55
56         origin_hrn = Certificate(string=cert).get_subject()
57         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
58         
59         manager = self.api.get_interface_manager()
60  
61         # authenticate the gid
62         records = manager.resolve(self.api, xrn, type)
63         if not records:
64             raise RecordNotFound(hrn)
65         record = SfaRecord(dict=records[0])
66         gid = record.get_gid_object()
67         gid_str = gid.save_to_string(save_parents=True)
68         self.api.auth.authenticateGid(gid_str, [cert, type, hrn])
69         # authenticate the certificate against the gid in the db
70         certificate = Certificate(string=cert)
71         if not certificate.is_pubkey(gid.get_pubkey()):
72             raise ConnectionKeyGIDMismatch(gid.get_subject())
73         
74         return manager.get_credential(self.api, xrn, type, is_self=True)