c33ed6f31a55a7976ed463ed314d6d53b353271b
[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 ### $Id: credential.py 17477 2010-03-25 16:49:34Z jkarlin $
9 ### $URL: svn+ssh://svn.planet-lab.org/svn/sfa/branches/geni-api/sfa/trust/credential.py $
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 from sfa.util.sfalogging import *
18
19 ##
20 # Credential is a tuple:
21 #     (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
22 #
23 # These fields are encoded using xmlrpc into the subjectAltName field of the
24 # x509 certificate. Note: Call encode() once the fields have been filled in
25 # to perform this encoding.
26
27 class CredentialLegacy(Certificate):
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     ##
46     # set the GID of the caller
47     #
48     # @param gid GID object of the caller
49
50     def set_gid_caller(self, gid):
51         self.gidCaller = gid
52         # gid origin caller is the caller's gid by default
53         self.gidOriginCaller = gid
54
55     ##
56     # get the GID of the object
57
58     def get_gid_caller(self):
59         if not self.gidCaller:
60             self.decode()
61         return self.gidCaller
62
63     ##
64     # set the GID of the object
65     #
66     # @param gid GID object of the object
67
68     def set_gid_object(self, gid):
69         self.gidObject = gid
70
71     ##
72     # get the GID of the object
73
74     def get_gid_object(self):
75         if not self.gidObject:
76             self.decode()
77         return self.gidObject
78
79     ##
80     # set the lifetime of this credential
81     #
82     # @param lifetime lifetime of credential
83
84     def set_lifetime(self, lifeTime):
85         self.lifeTime = lifeTime
86
87     ##
88     # get the lifetime of the credential
89
90     def get_lifetime(self):
91         if not self.lifeTime:
92             self.decode()
93         return self.lifeTime
94
95     ##
96     # set the delegate bit
97     #
98     # @param delegate boolean (True or False)
99
100     def set_delegate(self, delegate):
101         self.delegate = delegate
102
103     ##
104     # get the delegate bit
105
106     def get_delegate(self):
107         if not self.delegate:
108             self.decode()
109         return self.delegate
110
111     ##
112     # set the privileges
113     #
114     # @param privs either a comma-separated list of privileges of a RightList object
115
116     def set_privileges(self, privs):
117         if isinstance(privs, str):
118             self.privileges = RightList(string = privs)
119         else:
120             self.privileges = privs
121
122     ##
123     # return the privileges as a RightList object
124
125     def get_privileges(self):
126         if not self.privileges:
127             self.decode()
128         return self.privileges
129
130     ##
131     # determine whether the credential allows a particular operation to be
132     # performed
133     #
134     # @param op_name string specifying name of operation ("lookup", "update", etc)
135
136     def can_perform(self, op_name):
137         rights = self.get_privileges()
138         if not rights:
139             return False
140         return rights.can_perform(op_name)
141
142     ##
143     # Encode the attributes of the credential into a string and store that
144     # string in the alt-subject-name field of the X509 object. This should be
145     # done immediately before signing the credential.
146
147     def encode(self):
148         dict = {"gidCaller": None,
149                 "gidObject": None,
150                 "lifeTime": self.lifeTime,
151                 "privileges": None,
152                 "delegate": self.delegate}
153         if self.gidCaller:
154             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
155         if self.gidObject:
156             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
157         if self.privileges:
158             dict["privileges"] = self.privileges.save_to_string()
159         str = xmlrpclib.dumps((dict,), allow_none=True)
160         self.set_data('URI:http://' + str)
161
162     ##
163     # Retrieve the attributes of the credential from the alt-subject-name field
164     # of the X509 certificate. This is automatically done by the various
165     # get_* methods of this class and should not need to be called explicitly.
166
167     def decode(self):
168         data = self.get_data().lstrip('URI:http://')
169         
170         if data:
171             dict = xmlrpclib.loads(data)[0][0]
172         else:
173             dict = {}
174
175         self.lifeTime = dict.get("lifeTime", None)
176         self.delegate = dict.get("delegate", None)
177
178         privStr = dict.get("privileges", None)
179         if privStr:
180             self.privileges = RightList(string = privStr)
181         else:
182             self.privileges = None
183
184         gidCallerStr = dict.get("gidCaller", None)
185         if gidCallerStr:
186             self.gidCaller = GID(string=gidCallerStr)
187         else:
188             self.gidCaller = None
189
190         gidObjectStr = dict.get("gidObject", None)
191         if gidObjectStr:
192             self.gidObject = GID(string=gidObjectStr)
193         else:
194             self.gidObject = None
195
196     ##
197     # Verify that a chain of credentials is valid (see cert.py:verify). In
198     # addition to the checks for ordinary certificates, verification also
199     # ensures that the delegate bit was set by each parent in the chain. If
200     # a delegate bit was not set, then an exception is thrown.
201     #
202     # Each credential must be a subset of the rights of the parent.
203
204     def verify_chain(self, trusted_certs = None):
205         # do the normal certificate verification stuff
206         Certificate.verify_chain(self, trusted_certs)
207
208         if self.parent:
209             # make sure the parent delegated rights to the child
210             if not self.parent.get_delegate():
211                 raise MissingDelegateBit(self.parent.get_subject())
212
213             # make sure the rights given to the child are a subset of the
214             # parents rights
215             if not self.parent.get_privileges().is_superset(self.get_privileges()):
216                 raise ChildRightsNotSubsetOfParent(self.get_subject() 
217                                                    + " " + self.parent.get_privileges().save_to_string()
218                                                    + " " + self.get_privileges().save_to_string())
219
220         return
221
222     ##
223     # Dump the contents of a credential to stdout in human-readable format
224     #
225     # @param dump_parents If true, also dump the parent certificates
226
227     def dump(self, dump_parents=False):
228         print "CREDENTIAL", self.get_subject()
229
230         print "      privs:", self.get_privileges().save_to_string()
231
232         print "  gidCaller:"
233         gidCaller = self.get_gid_caller()
234         if gidCaller:
235             gidCaller.dump(8, dump_parents)
236
237         print "  gidObject:"
238         gidObject = self.get_gid_object()
239         if gidObject:
240             gidObject.dump(8, dump_parents)
241
242         print "   delegate:", self.get_delegate()
243
244         if self.parent and dump_parents:
245            print "PARENT",
246            self.parent.dump(dump_parents)
247