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