added gidOriginCaller to keep track of the original caller (useful for logging)
[sfa.git] / sfa / trust / credential.py
1 ##
2 # Implements Geni Credentials
3 #
4 # Credentials are layered on top of certificates, and are essentially a
5 # certificate that stores a tuple of parameters.
6 ##
7
8 ### $Id$
9 ### $URL$
10
11 import xmlrpclib
12
13 from sfa.trust.certificate import Certificate
14 from sfa.trust.rights import *
15 from sfa.trust.gid import *
16 from sfa.util.faults import *
17
18 ##
19 # Credential is a tuple:
20 #     (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
21 #
22 # These fields are encoded using xmlrpc into the subjectAltName field of the
23 # x509 certificate. Note: Call encode() once the fields have been filled in
24 # to perform this encoding.
25
26 class Credential(Certificate):
27     gidOriginCaller = None
28     gidCaller = None
29     gidObject = None
30     lifeTime = None
31     privileges = None
32     delegate = False
33
34     ##
35     # Create a Credential object
36     #
37     # @param create If true, create a blank x509 certificate
38     # @param subject If subject!=None, create an x509 cert with the subject name
39     # @param string If string!=None, load the credential from the string
40     # @param filename If filename!=None, load the credential from the file
41
42     def __init__(self, create=False, subject=None, string=None, filename=None):
43         Certificate.__init__(self, create, subject, string, filename)
44
45     ## set the GID of the original caller
46     #
47     # @param gid GID object of the original caller
48     def set_gid_origin_caller(self, gid):
49         self.gidOriginCaller = gid  
50
51     ##
52     # get the GID of the object
53
54     def get_gid_origin_caller(self):
55         if not self.gidOriginCaller:
56             self.decode()
57         return self.gidOriginCaller
58
59     ##
60     # set the GID of the caller
61     #
62     # @param gid GID object of the caller
63
64     def set_gid_caller(self, gid):
65         self.gidCaller = gid
66         # gid origin caller is the caller's gid by default
67         self.gidOriginCaller = gid
68
69     ##
70     # get the GID of the object
71
72     def get_gid_caller(self):
73         if not self.gidCaller:
74             self.decode()
75         return self.gidCaller
76
77     ##
78     # set the GID of the object
79     #
80     # @param gid GID object of the object
81
82     def set_gid_object(self, gid):
83         self.gidObject = gid
84
85     ##
86     # get the GID of the object
87
88     def get_gid_object(self):
89         if not self.gidObject:
90             self.decode()
91         return self.gidObject
92
93     ##
94     # set the lifetime of this credential
95     #
96     # @param lifetime lifetime of credential
97
98     def set_lifetime(self, lifeTime):
99         self.lifeTime = lifeTime
100
101     ##
102     # get the lifetime of the credential
103
104     def get_lifetime(self):
105         if not self.lifeTime:
106             self.decode()
107         return self.lifeTime
108
109     ##
110     # set the delegate bit
111     #
112     # @param delegate boolean (True or False)
113
114     def set_delegate(self, delegate):
115         self.delegate = delegate
116
117     ##
118     # get the delegate bit
119
120     def get_delegate(self):
121         if not self.delegate:
122             self.decode()
123         return self.delegate
124
125     ##
126     # set the privileges
127     #
128     # @param privs either a comma-separated list of privileges of a RightList object
129
130     def set_privileges(self, privs):
131         if isinstance(privs, str):
132             self.privileges = RightList(string = privs)
133         else:
134             self.privileges = privs
135
136     ##
137     # return the privileges as a RightList object
138
139     def get_privileges(self):
140         if not self.privileges:
141             self.decode()
142         return self.privileges
143
144     ##
145     # determine whether the credential allows a particular operation to be
146     # performed
147     #
148     # @param op_name string specifying name of operation ("lookup", "update", etc)
149
150     def can_perform(self, op_name):
151         rights = self.get_privileges()
152         if not rights:
153             return False
154         return rights.can_perform(op_name)
155
156     ##
157     # Encode the attributes of the credential into a string and store that
158     # string in the alt-subject-name field of the X509 object. This should be
159     # done immediately before signing the credential.
160
161     def encode(self):
162         dict = {"gidOriginCaller": None,
163                 "gidCaller": None,
164                 "gidObject": None,
165                 "lifeTime": self.lifeTime,
166                 "privileges": None,
167                 "delegate": self.delegate}
168         if self.gidOriginCaller:
169             dict["gidOriginCaller"] = self.gidOriginCaller.save_to_string(save_parents=True)
170         if self.gidCaller:
171             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
172         if self.gidObject:
173             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
174         if self.privileges:
175             dict["privileges"] = self.privileges.save_to_string()
176         str = xmlrpclib.dumps((dict,), allow_none=True)
177         self.set_data(str)
178
179     ##
180     # Retrieve the attributes of the credential from the alt-subject-name field
181     # of the X509 certificate. This is automatically done by the various
182     # get_* methods of this class and should not need to be called explicitly.
183
184     def decode(self):
185         data = self.get_data()
186         if data:
187             dict = xmlrpclib.loads(self.get_data())[0][0]
188         else:
189             dict = {}
190
191         self.lifeTime = dict.get("lifeTime", None)
192         self.delegate = dict.get("delegate", None)
193
194         privStr = dict.get("privileges", None)
195         if privStr:
196             self.privileges = RightList(string = privStr)
197         else:
198             self.privileges = None
199
200         gidOriginCallerStr = dict.get("gidOriginCaller", None)
201         if gidOriginCallerStr:
202             self.gidOriginCaller = GID(string=gidOriginCallerStr)
203         else:
204             self.gidOriginCaller = None
205
206         gidCallerStr = dict.get("gidCaller", None)
207         if gidCallerStr:
208             self.gidCaller = GID(string=gidCallerStr)
209         else:
210             self.gidCaller = None
211
212         gidObjectStr = dict.get("gidObject", None)
213         if gidObjectStr:
214             self.gidObject = GID(string=gidObjectStr)
215         else:
216             self.gidObject = None
217
218     ##
219     # Verify that a chain of credentials is valid (see cert.py:verify). In
220     # addition to the checks for ordinary certificates, verification also
221     # ensures that the delegate bit was set by each parent in the chain. If
222     # a delegate bit was not set, then an exception is thrown.
223     #
224     # Each credential must be a subset of the rights of the parent.
225
226     def verify_chain(self, trusted_certs = None):
227         # do the normal certificate verification stuff
228         Certificate.verify_chain(self, trusted_certs)
229
230         if self.parent:
231             # make sure the parent delegated rights to the child
232             if not self.parent.get_delegate():
233                 raise MissingDelegateBit(self.parent.get_subject())
234
235             # make sure the rights given to the child are a subset of the
236             # parents rights
237             if not self.parent.get_privileges().is_superset(self.get_privileges()):
238                 raise ChildRightsNotSubsetOfParent(self.get_subject() 
239                                                    + " " + self.parent.get_privileges().save_to_string()
240                                                    + " " + self.get_privileges().save_to_string())
241
242         return
243
244     ##
245     # Dump the contents of a credential to stdout in human-readable format
246     #
247     # @param dump_parents If true, also dump the parent certificates
248
249     def dump(self, dump_parents=False):
250         print "CREDENTIAL", self.get_subject()
251
252         print "      privs:", self.get_privileges().save_to_string()
253
254         print "  gidOriginCaller:"
255         gidOriginCaller = self.get_gid_origin_caller()
256         if gidOriginCaller:
257             gidOriginCaller.dump(8, dump_parents)
258
259         print "  gidCaller:"
260         gidCaller = self.get_gid_caller()
261         if gidCaller:
262             gidCaller.dump(8, dump_parents)
263
264         print "  gidObject:"
265         gidObject = self.get_gid_object()
266         if gidObject:
267             gidObject.dump(8, dump_parents)
268
269         print "   delegate:", self.get_delegate()
270
271         if self.parent and dump_parents:
272            print "PARENT",
273            self.parent.dump(dump_parents)
274