added some more methods
[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(name)
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         record = None
44         table = self.api.auth.get_auth_table(auth_hrn)
45         records = table.resolve('*', hrn)
46
47         # verify_cancreate_credential requires that the member lists
48         # (researchers, pis, etc) be filled in
49         self.api.fill_record_info(record)
50
51         self.api.auth.verify_cancreate_credential(self.client_cred, record)
52
53         # TODO: Check permission that self.client_cred can access the object
54
55         object_gid = record.get_gid_object()
56         new_cred = Credential(subject = object_gid.get_subject())
57         new_cred.set_gid_caller(self.client_gid)
58         new_cred.set_gid_object(object_gid)
59         new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
60         new_cred.set_pubkey(object_gid.get_pubkey())
61
62         rl = determine_rights(type, name)
63         new_cred.set_privileges(rl)
64
65         # determine the type of credential that we want to use as a parent for
66         # this credential.
67
68         if (type == "ma") or (type == "node"):
69             auth_kind = "authority,ma"
70         else: # user, slice, sa
71             auth_kind = "authority,sa"
72
73         new_cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
74
75         new_cred.encode()
76         new_cred.sign()
77
78         return new_cred.save_to_string(save_parents=True)
79
80     def get_self_credential(self, type, hrn):
81         """
82         get_self_credential a degenerate version of get_credential used by a client
83         to get his initial credential when de doesnt have one. This is the same as 
84         get_credetial(..., cred = None, ...)
85     
86         The registry ensures that the client is the principal that is named by 
87         (type, name) by comparing the public key in the record's  GID to the 
88         private key used to encrypt the client side of the HTTPS connection. Thus
89         it is impossible for one principal to retrive another principal's 
90         credential without having the appropriate private key.    
91
92         @param type type of object (user | slice | sa | ma | node)
93         @param hrn human readable name of authority to list
94         @return string representation of a credential object 
95         """
96         self.api.auth.verify_object_belongs_to_me(hrn)
97
98         auth_hrn = self.api.auth.get_authority(hrn)
99         if not auth_hrn:
100             auth_hrn = hrn
101         auth_info = self.api.auth.get_auth_info(auth_hrn)
102
103         # find a record that matches
104         record = None
105         table = self.api.auth.get_auth_table(auth_hrn)
106         records = table.resolve('*', hrn)
107         for rec in records:
108             if type in ['*'] or rec.get_type() in [type]:
109                 record = rec
110         gid = record.get_gid_object()
111         peer_cert = self.api.auth.peer_cert
112         if not peer_cert.is_pubkey(gid.get_pubkey()):
113            raise ConnectionKeyGIDMismatch(gid.get_subject())
114
115         # create the credential
116         gid = record.get_gid_object()
117         cred = Credential(subject = gid.get_subject())
118         cred.set_gid_caller(gid)
119         cred.set_gid_object(gid)
120         cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
121         cred.set_pubkey(gid.get_pubkey())
122         
123         rl = determine_rights(type, hrn)
124         cred.set_privileges(rl)
125
126         # determine the type of credential that we want to use as a parent for
127         # this credential.
128
129         if (type == "ma") or (type == "node"):
130             auth_kind = "authority,ma"
131         else: # user, slice, sa
132             auth_kind = "authority,sa"
133
134         cred.set_parent(self.api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
135
136         cred.encode()
137         cred.sign()
138
139         return cred.save_to_string(save_parents=True)