dda7096f8de0f0ec79c274a2bcc730b2d3f2b4e5
[sfa.git] / sfa / trust / credential_legacy.py
1 ##
2 # Implements SFA 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 import xmlrpclib
9
10 from sfa.util.faults import *
11 from sfa.trust.certificate import Certificate
12 from sfa.trust.rights import Right,Rights
13 from sfa.trust.gid import GID
14
15 ##
16 # Credential is a tuple:
17 #     (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
18 #
19 # These fields are encoded using xmlrpc into the subjectAltName field of the
20 # x509 certificate. Note: Call encode() once the fields have been filled in
21 # to perform this encoding.
22
23 class CredentialLegacy(Certificate):
24     gidCaller = None
25     gidObject = None
26     lifeTime = None
27     privileges = None
28     delegate = False
29
30     ##
31     # Create a Credential object
32     #
33     # @param create If true, create a blank x509 certificate
34     # @param subject If subject!=None, create an x509 cert with the subject name
35     # @param string If string!=None, load the credential from the string
36     # @param filename If filename!=None, load the credential from the file
37
38     def __init__(self, create=False, subject=None, string=None, filename=None):
39         Certificate.__init__(self, create, subject, string, filename)
40
41     ##
42     # set the GID of the caller
43     #
44     # @param gid GID object of the caller
45
46     def set_gid_caller(self, gid):
47         self.gidCaller = gid
48         # gid origin caller is the caller's gid by default
49         self.gidOriginCaller = gid
50
51     ##
52     # get the GID of the object
53
54     def get_gid_caller(self):
55         if not self.gidCaller:
56             self.decode()
57         return self.gidCaller
58
59     ##
60     # set the GID of the object
61     #
62     # @param gid GID object of the object
63
64     def set_gid_object(self, gid):
65         self.gidObject = gid
66
67     ##
68     # get the GID of the object
69
70     def get_gid_object(self):
71         if not self.gidObject:
72             self.decode()
73         return self.gidObject
74
75     ##
76     # set the lifetime of this credential
77     #
78     # @param lifetime lifetime of credential
79
80     def set_lifetime(self, lifeTime):
81         self.lifeTime = lifeTime
82
83     ##
84     # get the lifetime of the credential
85
86     def get_lifetime(self):
87         if not self.lifeTime:
88             self.decode()
89         return self.lifeTime
90
91     ##
92     # set the delegate bit
93     #
94     # @param delegate boolean (True or False)
95
96     def set_delegate(self, delegate):
97         self.delegate = delegate
98
99     ##
100     # get the delegate bit
101
102     def get_delegate(self):
103         if not self.delegate:
104             self.decode()
105         return self.delegate
106
107     ##
108     # set the privileges
109     #
110     # @param privs either a comma-separated list of privileges of a Rights object
111
112     def set_privileges(self, privs):
113         if isinstance(privs, str):
114             self.privileges = Rights(string = privs)
115         else:
116             self.privileges = privs
117
118     ##
119     # return the privileges as a Rights object
120
121     def get_privileges(self):
122         if not self.privileges:
123             self.decode()
124         return self.privileges
125
126     ##
127     # determine whether the credential allows a particular operation to be
128     # performed
129     #
130     # @param op_name string specifying name of operation ("lookup", "update", etc)
131
132     def can_perform(self, op_name):
133         rights = self.get_privileges()
134         if not rights:
135             return False
136         return rights.can_perform(op_name)
137
138     ##
139     # Encode the attributes of the credential into a string and store that
140     # string in the alt-subject-name field of the X509 object. This should be
141     # done immediately before signing the credential.
142
143     def encode(self):
144         dict = {"gidCaller": None,
145                 "gidObject": None,
146                 "lifeTime": self.lifeTime,
147                 "privileges": None,
148                 "delegate": self.delegate}
149         if self.gidCaller:
150             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
151         if self.gidObject:
152             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
153         if self.privileges:
154             dict["privileges"] = self.privileges.save_to_string()
155         str = xmlrpclib.dumps((dict,), allow_none=True)
156         self.set_data('URI:http://' + str)
157
158     ##
159     # Retrieve the attributes of the credential from the alt-subject-name field
160     # of the X509 certificate. This is automatically done by the various
161     # get_* methods of this class and should not need to be called explicitly.
162
163     def decode(self):
164         data = self.get_data().lstrip('URI:http://')
165         
166         if data:
167             dict = xmlrpclib.loads(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 = Rights(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, *args, **kwargs):
224         print self.dump_string(*args,**kwargs)
225
226     def dump_string(self, dump_parents=False):
227         result=""
228         result += "CREDENTIAL %s\n" % self.get_subject()
229
230         result += "      privs: %s\n" % self.get_privileges().save_to_string()
231
232         gidCaller = self.get_gid_caller()
233         if gidCaller:
234             result += "  gidCaller:\n"
235             gidCaller.dump(8, dump_parents)
236
237         gidObject = self.get_gid_object()
238         if gidObject:
239             result += "  gidObject:\n"
240             result += gidObject.dump_string(8, dump_parents)
241
242         result += "   delegate: %s" % self.get_delegate()
243
244         if self.parent and dump_parents:
245             result += "PARENT\n"
246             result += self.parent.dump_string(dump_parents)
247
248         return result