f3a96127c09dbd160286bb522c06a1569f4b36e8
[sfa.git] / sfa / methods / GetSelfCredential.py
1 from sfa.util.faults import RecordNotFound, ConnectionKeyGIDMismatch
2 from sfa.util.xrn import urn_to_hrn
3 from sfa.util.method import Method
4
5 from sfa.trust.certificate import Certificate
6
7 from sfa.storage.parameter import Parameter, Mixed
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         # import here so we can load this module at build-time for sfa2wsdl
58         #from sfa.storage.alchemy import dbsession
59         from sfa.storage.model import RegRecord
60
61         # xxx-local - the current code runs Resolve, which would forward to 
62         # another registry if needed
63         # I wonder if this is truly the intention, or shouldn't we instead 
64         # only look in the local db ?
65         records = self.api.manager.Resolve(self.api, xrn, type, details=False)
66         if not records:
67             raise RecordNotFound(hrn)
68
69         record_obj = RegRecord (dict=records[0])
70         # xxx-local the local-only version would read 
71         #record_obj = dbsession.query(RegRecord).filter_by(hrn=hrn).first()
72         #if not record_obj: raise RecordNotFound(hrn)
73         gid = record_obj.get_gid_object()
74         gid_str = gid.save_to_string(save_parents=True)
75         self.api.auth.authenticateGid(gid_str, [cert, type, hrn])
76         # authenticate the certificate against the gid in the db
77         certificate = Certificate(string=cert)
78         if not certificate.is_pubkey(gid.get_pubkey()):
79             for (obj,name) in [ (certificate,"CERT"), (gid,"GID"), ]:
80                 self.api.logger.debug("ConnectionKeyGIDMismatch, %s pubkey: %s"%(name,obj.get_pubkey().get_pubkey_string()))
81                 self.api.logger.debug("ConnectionKeyGIDMismatch, %s dump: %s"%(name,obj.dump_string()))
82                 if hasattr (obj,'filename'): 
83                     self.api.logger.debug("ConnectionKeyGIDMismatch, %s filename: %s"%(name,obj.filename))
84             raise ConnectionKeyGIDMismatch(gid.get_subject())
85         
86         return self.api.manager.GetCredential(self.api, xrn, type)