2 # Implements SFA Credentials
4 # Credentials are layered on top of certificates, and are essentially a
5 # certificate that stores a tuple of parameters.
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 $
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 *
20 # Credential is a tuple:
21 # (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
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.
27 class CredentialLegacy(Certificate):
35 # Create a Credential object
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
42 def __init__(self, create=False, subject=None, string=None, filename=None):
43 Certificate.__init__(self, create, subject, string, filename)
46 # set the GID of the caller
48 # @param gid GID object of the caller
50 def set_gid_caller(self, gid):
52 # gid origin caller is the caller's gid by default
53 self.gidOriginCaller = gid
56 # get the GID of the object
58 def get_gid_caller(self):
59 if not self.gidCaller:
64 # set the GID of the object
66 # @param gid GID object of the object
68 def set_gid_object(self, gid):
72 # get the GID of the object
74 def get_gid_object(self):
75 if not self.gidObject:
80 # set the lifetime of this credential
82 # @param lifetime lifetime of credential
84 def set_lifetime(self, lifeTime):
85 self.lifeTime = lifeTime
88 # get the lifetime of the credential
90 def get_lifetime(self):
96 # set the delegate bit
98 # @param delegate boolean (True or False)
100 def set_delegate(self, delegate):
101 self.delegate = delegate
104 # get the delegate bit
106 def get_delegate(self):
107 if not self.delegate:
114 # @param privs either a comma-separated list of privileges of a RightList object
116 def set_privileges(self, privs):
117 if isinstance(privs, str):
118 self.privileges = RightList(string = privs)
120 self.privileges = privs
123 # return the privileges as a RightList object
125 def get_privileges(self):
126 if not self.privileges:
128 return self.privileges
131 # determine whether the credential allows a particular operation to be
134 # @param op_name string specifying name of operation ("lookup", "update", etc)
136 def can_perform(self, op_name):
137 rights = self.get_privileges()
140 return rights.can_perform(op_name)
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.
148 dict = {"gidCaller": None,
150 "lifeTime": self.lifeTime,
152 "delegate": self.delegate}
154 dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
156 dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
158 dict["privileges"] = self.privileges.save_to_string()
159 str = xmlrpclib.dumps((dict,), allow_none=True)
160 self.set_data('URI:http://' + str)
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.
168 data = self.get_data().lstrip('URI:http://')
171 dict = xmlrpclib.loads(data)[0][0]
175 self.lifeTime = dict.get("lifeTime", None)
176 self.delegate = dict.get("delegate", None)
178 privStr = dict.get("privileges", None)
180 self.privileges = RightList(string = privStr)
182 self.privileges = None
184 gidCallerStr = dict.get("gidCaller", None)
186 self.gidCaller = GID(string=gidCallerStr)
188 self.gidCaller = None
190 gidObjectStr = dict.get("gidObject", None)
192 self.gidObject = GID(string=gidObjectStr)
194 self.gidObject = None
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.
202 # Each credential must be a subset of the rights of the parent.
204 def verify_chain(self, trusted_certs = None):
205 # do the normal certificate verification stuff
206 Certificate.verify_chain(self, trusted_certs)
209 # make sure the parent delegated rights to the child
210 if not self.parent.get_delegate():
211 raise MissingDelegateBit(self.parent.get_subject())
213 # make sure the rights given to the child are a subset of the
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())
223 # Dump the contents of a credential to stdout in human-readable format
225 # @param dump_parents If true, also dump the parent certificates
227 def dump(self, dump_parents=False):
228 print "CREDENTIAL", self.get_subject()
230 print " privs:", self.get_privileges().save_to_string()
233 gidCaller = self.get_gid_caller()
235 gidCaller.dump(8, dump_parents)
238 gidObject = self.get_gid_object()
240 gidObject.dump(8, dump_parents)
242 print " delegate:", self.get_delegate()
244 if self.parent and dump_parents:
246 self.parent.dump(dump_parents)