fault module is required here, but was missing
[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     gidCaller = None
28     gidObject = None
29     lifeTime = None
30     privileges = None
31     delegate = False
32
33     ##
34     # Create a Credential object
35     #
36     # @param create If true, create a blank x509 certificate
37     # @param subject If subject!=None, create an x509 cert with the subject name
38     # @param string If string!=None, load the credential from the string
39     # @param filename If filename!=None, load the credential from the file
40
41     def __init__(self, create=False, subject=None, string=None, filename=None):
42         Certificate.__init__(self, create, subject, string, filename)
43
44     ##
45     # set the GID of the caller
46     #
47     # @param gid GID object of the caller
48
49     def set_gid_caller(self, gid):
50         self.gidCaller = gid
51
52     ##
53     # get the GID of the object
54
55     def get_gid_caller(self):
56         if not self.gidCaller:
57             self.decode()
58         return self.gidCaller
59
60     ##
61     # set the GID of the object
62     #
63     # @param gid GID object of the object
64
65     def set_gid_object(self, gid):
66         self.gidObject = gid
67
68     ##
69     # get the GID of the object
70
71     def get_gid_object(self):
72         if not self.gidObject:
73             self.decode()
74         return self.gidObject
75
76     ##
77     # set the lifetime of this credential
78     #
79     # @param lifetime lifetime of credential
80
81     def set_lifetime(self, lifeTime):
82         self.lifeTime = lifeTime
83
84     ##
85     # get the lifetime of the credential
86
87     def get_lifetime(self):
88         if not self.lifeTime:
89             self.decode()
90         return self.lifeTime
91
92     ##
93     # set the delegate bit
94     #
95     # @param delegate boolean (True or False)
96
97     def set_delegate(self, delegate):
98         self.delegate = delegate
99
100     ##
101     # get the delegate bit
102
103     def get_delegate(self):
104         if not self.delegate:
105             self.decode()
106         return self.delegate
107
108     ##
109     # set the privileges
110     #
111     # @param privs either a comma-separated list of privileges of a RightList object
112
113     def set_privileges(self, privs):
114         if isinstance(privs, str):
115             self.privileges = RightList(string = privs)
116         else:
117             self.privileges = privs
118
119     ##
120     # return the privileges as a RightList object
121
122     def get_privileges(self):
123         if not self.privileges:
124             self.decode()
125         return self.privileges
126
127     ##
128     # determine whether the credential allows a particular operation to be
129     # performed
130     #
131     # @param op_name string specifying name of operation ("lookup", "update", etc)
132
133     def can_perform(self, op_name):
134         rights = self.get_privileges()
135         if not rights:
136             return False
137         return rights.can_perform(op_name)
138
139     ##
140     # Encode the attributes of the credential into a string and store that
141     # string in the alt-subject-name field of the X509 object. This should be
142     # done immediately before signing the credential.
143
144     def encode(self):
145         dict = {"gidCaller": None,
146                 "gidObject": None,
147                 "lifeTime": self.lifeTime,
148                 "privileges": None,
149                 "delegate": self.delegate}
150         if self.gidCaller:
151             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
152         if self.gidObject:
153             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
154         if self.privileges:
155             dict["privileges"] = self.privileges.save_to_string()
156         str = xmlrpclib.dumps((dict,), allow_none=True)
157         self.set_data(str)
158
159     ##
160     # Retrieve the attributes of the credential from the alt-subject-name field
161     # of the X509 certificate. This is automatically done by the various
162     # get_* methods of this class and should not need to be called explicitly.
163
164     def decode(self):
165         data = self.get_data()
166         if data:
167             dict = xmlrpclib.loads(self.get_data())[0][0]
168         else:
169             dict = {}
170
171         self.lifeTime = dict.get("lifeTime", None)
172         self.delegate = dict.get("delegate", None)
173
174         privStr = dict.get("privileges", None)
175         if privStr:
176             self.privileges = RightList(string = privStr)
177         else:
178             self.privileges = None
179
180         gidCallerStr = dict.get("gidCaller", None)
181         if gidCallerStr:
182             self.gidCaller = GID(string=gidCallerStr)
183         else:
184             self.gidCaller = None
185
186         gidObjectStr = dict.get("gidObject", None)
187         if gidObjectStr:
188             self.gidObject = GID(string=gidObjectStr)
189         else:
190             self.gidObject = None
191
192     ##
193     # Verify that a chain of credentials is valid (see cert.py:verify). In
194     # addition to the checks for ordinary certificates, verification also
195     # ensures that the delegate bit was set by each parent in the chain. If
196     # a delegate bit was not set, then an exception is thrown.
197     #
198     # Each credential must be a subset of the rights of the parent.
199
200     def verify_chain(self, trusted_certs = None):
201         # do the normal certificate verification stuff
202         Certificate.verify_chain(self, trusted_certs)
203
204         if self.parent:
205             # make sure the parent delegated rights to the child
206             if not self.parent.get_delegate():
207                 raise MissingDelegateBit(self.parent.get_subject())
208
209             # make sure the rights given to the child are a subset of the
210             # parents rights
211             if not self.parent.get_privileges().is_superset(self.get_privileges()):
212                 raise ChildRightsNotSubsetOfParent(self.get_subject() 
213                                                    + " " + self.parent.get_privileges().save_to_string()
214                                                    + " " + self.get_privileges().save_to_string())
215
216         return
217
218     ##
219     # Dump the contents of a credential to stdout in human-readable format
220     #
221     # @param dump_parents If true, also dump the parent certificates
222
223     def dump(self, dump_parents=False):
224         print "CREDENTIAL", self.get_subject()
225
226         print "      privs:", self.get_privileges().save_to_string()
227
228         print "  gidCaller:"
229         gidCaller = self.get_gid_caller()
230         if gidCaller:
231             gidCaller.dump(8, dump_parents)
232
233         print "  gidObject:"
234         gidObject = self.get_gid_object()
235         if gidObject:
236             gidObject.dump(8, dump_parents)
237
238         print "   delegate:", self.get_delegate()
239
240         if self.parent and dump_parents:
241            print "PARENT",
242            self.parent.dump(dump_parents)
243