raise a better exception when username cant be resolved
[sfa.git] / geni / methods / get_credential.py
1 from geni.util.faults import *
2 from geni.util.excep import *
3 from geni.util.method import Method
4 from geni.util.parameter import Parameter, Mixed
5 from geni.util.auth import Auth
6 from geni.util.record import GeniRecord
7 from geni.util.credential import *
8 from geni.util.rights import *
9 from geni.util.debug import log
10
11 class get_credential(Method):
12     """
13     Retrive a credential for an object
14     If cred == Nonee then the behavior reverts to get_self_credential
15
16     @param cred credential object specifying rights of the caller
17     @param type type of object (user | slice | sa | ma | node)
18     @param hrn human readable name of object
19
20     @return the string representation of a credential object  
21     """
22
23     interfaces = ['registry']
24     
25     accepts = [
26         Mixed(Parameter(str, "credential"),
27               Parameter(None, "No credential")),  
28         Parameter(str, "Human readable name (hrn)")
29         ]
30
31     returns = Parameter(str, "String representation of a credential object")
32
33     def call(self, cred, type, hrn):
34         if not cred:
35             return self.get_self_credential(type, hrn)
36
37         self.api.auth.check(cred, 'getcredential')
38         self.api.auth.verify_object_belongs_to_me(hrn)
39         auth_hrn = self.api.auth.get_authority(hrn)
40         if not auth_hrn:
41             auth_hrn = hrn
42         auth_info = self.api.auth.get_auth_info(auth_hrn)
43         table = self.api.auth.get_auth_table(auth_hrn)
44         records = table.resolve('*', hrn)
45         if not records:
46             raise RecordNotFound(hrn)
47         record = records[0]
48         # verify_cancreate_credential requires that the member lists
49         # (researchers, pis, etc) be filled in
50         self.api.fill_record_info(record)
51
52         rights = self.api.auth.determine_user_rights(self.api.auth.client_cred, record)
53         if rights.is_empty():
54             raise PermissionError(self.api.auth.client_cred.get_gid_object().get_hrn() + " has no rights to " + record.get_name())
55
56         # TODO: Check permission that self.client_cred can access the object
57
58         object_gid = record.get_gid_object()
59         new_cred = Credential(subject = object_gid.get_subject())
60         new_cred.set_gid_caller(self.api.auth.client_gid)
61         new_cred.set_gid_object(object_gid)
62         new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
63         new_cred.set_pubkey(object_gid.get_pubkey())
64         new_cred.set_privileges(rights)
65         new_cred.set_delegate(True)
66
67         auth_kind = "authority,ma,sa"
68         new_cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
69
70         new_cred.encode()
71         new_cred.sign()
72
73         return new_cred.save_to_string(save_parents=True)
74
75     def get_self_credential(self, type, hrn):
76         """
77         get_self_credential a degenerate version of get_credential used by a client
78         to get his initial credential when de doesnt have one. This is the same as
79         get_credetial(..., cred = None, ...)
80
81         The registry ensures that the client is the principal that is named by
82         (type, name) by comparing the public key in the record's  GID to the
83         private key used to encrypt the client side of the HTTPS connection. Thus
84         it is impossible for one principal to retrive another principal's
85         credential without having the appropriate private key.
86
87         @param type type of object (user | slice | sa | ma | node)
88         @param hrn human readable name of authority to list
89         @return string representation of a credential object
90         """
91         self.api.auth.verify_object_belongs_to_me(hrn)
92
93         auth_hrn = self.api.auth.get_authority(hrn)
94         if not auth_hrn:
95             auth_hrn = hrn
96         auth_info = self.api.auth.get_auth_info(auth_hrn)
97
98         # find a record that matches
99         record = None
100         table = self.api.auth.get_auth_table(auth_hrn)
101         records = table.resolve('*', hrn)
102         for rec in records:
103             if type in ['*'] or rec.get_type() in [type]:
104                 record = rec
105         if not record:
106             raise RecordNotFound(hrn)
107         gid = record.get_gid_object()
108         peer_cert = self.api.auth.peer_cert
109         if not peer_cert.is_pubkey(gid.get_pubkey()):
110            raise ConnectionKeyGIDMismatch(gid.get_subject())
111
112         rights = self.api.auth.determine_user_rights(None, record)
113         if rights.is_empty():
114             raise PermissionError(self.api.auth.client_cred.get_gid_object().get_hrn() + " has no rights to " + record.get_name())
115
116         # create the credential
117         gid = record.get_gid_object()
118         cred = Credential(subject = gid.get_subject())
119         cred.set_gid_caller(gid)
120         cred.set_gid_object(gid)
121         cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
122         cred.set_pubkey(gid.get_pubkey())
123         cred.set_privileges(rights)
124         cred.set_delegate(True)
125
126         auth_kind = "authority,sa,ma"
127         cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
128
129         cred.encode()
130         cred.sign()
131
132         return cred.save_to_string(save_parents=True)