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