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