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.util.faults import *
14 from sfa.trust.certificate import Certificate
15 from sfa.trust.rights import Right,Rights
16 from sfa.trust.gid import GID
19 # Credential is a tuple:
20 # (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
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.
26 class CredentialLegacy(Certificate):
34 # Create a Credential object
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
41 def __init__(self, create=False, subject=None, string=None, filename=None):
42 Certificate.__init__(self, create, subject, string, filename)
45 # set the GID of the caller
47 # @param gid GID object of the caller
49 def set_gid_caller(self, gid):
51 # gid origin caller is the caller's gid by default
52 self.gidOriginCaller = gid
55 # get the GID of the object
57 def get_gid_caller(self):
58 if not self.gidCaller:
63 # set the GID of the object
65 # @param gid GID object of the object
67 def set_gid_object(self, gid):
71 # get the GID of the object
73 def get_gid_object(self):
74 if not self.gidObject:
79 # set the lifetime of this credential
81 # @param lifetime lifetime of credential
83 def set_lifetime(self, lifeTime):
84 self.lifeTime = lifeTime
87 # get the lifetime of the credential
89 def get_lifetime(self):
95 # set the delegate bit
97 # @param delegate boolean (True or False)
99 def set_delegate(self, delegate):
100 self.delegate = delegate
103 # get the delegate bit
105 def get_delegate(self):
106 if not self.delegate:
113 # @param privs either a comma-separated list of privileges of a Rights object
115 def set_privileges(self, privs):
116 if isinstance(privs, str):
117 self.privileges = Rights(string = privs)
119 self.privileges = privs
122 # return the privileges as a Rights object
124 def get_privileges(self):
125 if not self.privileges:
127 return self.privileges
130 # determine whether the credential allows a particular operation to be
133 # @param op_name string specifying name of operation ("lookup", "update", etc)
135 def can_perform(self, op_name):
136 rights = self.get_privileges()
139 return rights.can_perform(op_name)
142 # Encode the attributes of the credential into a string and store that
143 # string in the alt-subject-name field of the X509 object. This should be
144 # done immediately before signing the credential.
147 dict = {"gidCaller": None,
149 "lifeTime": self.lifeTime,
151 "delegate": self.delegate}
153 dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
155 dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
157 dict["privileges"] = self.privileges.save_to_string()
158 str = xmlrpclib.dumps((dict,), allow_none=True)
159 self.set_data('URI:http://' + str)
162 # Retrieve the attributes of the credential from the alt-subject-name field
163 # of the X509 certificate. This is automatically done by the various
164 # get_* methods of this class and should not need to be called explicitly.
167 data = self.get_data().lstrip('URI:http://')
170 dict = xmlrpclib.loads(data)[0][0]
174 self.lifeTime = dict.get("lifeTime", None)
175 self.delegate = dict.get("delegate", None)
177 privStr = dict.get("privileges", None)
179 self.privileges = Rights(string = privStr)
181 self.privileges = None
183 gidCallerStr = dict.get("gidCaller", None)
185 self.gidCaller = GID(string=gidCallerStr)
187 self.gidCaller = None
189 gidObjectStr = dict.get("gidObject", None)
191 self.gidObject = GID(string=gidObjectStr)
193 self.gidObject = None
196 # Verify that a chain of credentials is valid (see cert.py:verify). In
197 # addition to the checks for ordinary certificates, verification also
198 # ensures that the delegate bit was set by each parent in the chain. If
199 # a delegate bit was not set, then an exception is thrown.
201 # Each credential must be a subset of the rights of the parent.
203 def verify_chain(self, trusted_certs = None):
204 # do the normal certificate verification stuff
205 Certificate.verify_chain(self, trusted_certs)
208 # make sure the parent delegated rights to the child
209 if not self.parent.get_delegate():
210 raise MissingDelegateBit(self.parent.get_subject())
212 # make sure the rights given to the child are a subset of the
214 if not self.parent.get_privileges().is_superset(self.get_privileges()):
215 raise ChildRightsNotSubsetOfParent(self.get_subject()
216 + " " + self.parent.get_privileges().save_to_string()
217 + " " + self.get_privileges().save_to_string())
222 # Dump the contents of a credential to stdout in human-readable format
224 # @param dump_parents If true, also dump the parent certificates
226 def dump(self, dump_parents=False):
227 print "CREDENTIAL", self.get_subject()
229 print " privs:", self.get_privileges().save_to_string()
232 gidCaller = self.get_gid_caller()
234 gidCaller.dump(8, dump_parents)
237 gidObject = self.get_gid_object()
239 gidObject.dump(8, dump_parents)
241 print " delegate:", self.get_delegate()
243 if self.parent and dump_parents:
245 self.parent.dump(dump_parents)