2 # Implements SFA Credentials
4 # Credentials are layered on top of certificates, and are essentially a
5 # certificate that stores a tuple of parameters.
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 *
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 Credential(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 RightList object
115 def set_privileges(self, privs):
116 if isinstance(privs, str):
117 self.privileges = RightList(string = privs)
119 self.privileges = privs
122 # return the privileges as a RightList 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)
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()
169 dict = xmlrpclib.loads(self.get_data())[0][0]
173 self.lifeTime = dict.get("lifeTime", None)
174 self.delegate = dict.get("delegate", None)
176 privStr = dict.get("privileges", None)
178 self.privileges = RightList(string = privStr)
180 self.privileges = None
182 gidCallerStr = dict.get("gidCaller", None)
184 self.gidCaller = GID(string=gidCallerStr)
186 self.gidCaller = None
188 gidObjectStr = dict.get("gidObject", None)
190 self.gidObject = GID(string=gidObjectStr)
192 self.gidObject = None
195 # Verify that a chain of credentials is valid (see cert.py:verify). In
196 # addition to the checks for ordinary certificates, verification also
197 # ensures that the delegate bit was set by each parent in the chain. If
198 # a delegate bit was not set, then an exception is thrown.
200 # Each credential must be a subset of the rights of the parent.
202 def verify_chain(self, trusted_certs = None):
203 # do the normal certificate verification stuff
204 Certificate.verify_chain(self, trusted_certs)
207 # make sure the parent delegated rights to the child
208 if not self.parent.get_delegate():
209 raise MissingDelegateBit(self.parent.get_subject())
211 # make sure the rights given to the child are a subset of the
213 if not self.parent.get_privileges().is_superset(self.get_privileges()):
214 raise ChildRightsNotSubsetOfParent(self.get_subject()
215 + " " + self.parent.get_privileges().save_to_string()
216 + " " + self.get_privileges().save_to_string())
221 # Dump the contents of a credential to stdout in human-readable format
223 # @param dump_parents If true, also dump the parent certificates
225 def dump(self, dump_parents=False):
226 print "CREDENTIAL", self.get_subject()
228 print " privs:", self.get_privileges().save_to_string()
231 gidCaller = self.get_gid_caller()
233 gidCaller.dump(8, dump_parents)
236 gidObject = self.get_gid_object()
238 gidObject.dump(8, dump_parents)
240 print " delegate:", self.get_delegate()
242 if self.parent and dump_parents:
244 self.parent.dump(dump_parents)