2 # Implements SFA Credentials
4 # Credentials are layered on top of certificates, and are essentially a
5 # certificate that stores a tuple of parameters.
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
16 # Credential is a tuple:
17 # (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
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.
23 class CredentialLegacy(Certificate):
31 # Create a Credential object
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
38 def __init__(self, create=False, subject=None, string=None, filename=None):
39 Certificate.__init__(self, create, subject, string, filename)
42 # set the GID of the caller
44 # @param gid GID object of the caller
46 def set_gid_caller(self, gid):
48 # gid origin caller is the caller's gid by default
49 self.gidOriginCaller = gid
52 # get the GID of the object
54 def get_gid_caller(self):
55 if not self.gidCaller:
60 # set the GID of the object
62 # @param gid GID object of the object
64 def set_gid_object(self, gid):
68 # get the GID of the object
70 def get_gid_object(self):
71 if not self.gidObject:
76 # set the lifetime of this credential
78 # @param lifetime lifetime of credential
80 def set_lifetime(self, lifeTime):
81 self.lifeTime = lifeTime
84 # get the lifetime of the credential
86 def get_lifetime(self):
92 # set the delegate bit
94 # @param delegate boolean (True or False)
96 def set_delegate(self, delegate):
97 self.delegate = delegate
100 # get the delegate bit
102 def get_delegate(self):
103 if not self.delegate:
110 # @param privs either a comma-separated list of privileges of a Rights object
112 def set_privileges(self, privs):
113 if isinstance(privs, str):
114 self.privileges = Rights(string = privs)
116 self.privileges = privs
119 # return the privileges as a Rights object
121 def get_privileges(self):
122 if not self.privileges:
124 return self.privileges
127 # determine whether the credential allows a particular operation to be
130 # @param op_name string specifying name of operation ("lookup", "update", etc)
132 def can_perform(self, op_name):
133 rights = self.get_privileges()
136 return rights.can_perform(op_name)
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.
144 dict = {"gidCaller": None,
146 "lifeTime": self.lifeTime,
148 "delegate": self.delegate}
150 dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
152 dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
154 dict["privileges"] = self.privileges.save_to_string()
155 str = xmlrpclib.dumps((dict,), allow_none=True)
156 self.set_data('URI:http://' + str)
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.
164 data = self.get_data().lstrip('URI:http://')
167 dict = xmlrpclib.loads(data)[0][0]
171 self.lifeTime = dict.get("lifeTime", None)
172 self.delegate = dict.get("delegate", None)
174 privStr = dict.get("privileges", None)
176 self.privileges = Rights(string = privStr)
178 self.privileges = None
180 gidCallerStr = dict.get("gidCaller", None)
182 self.gidCaller = GID(string=gidCallerStr)
184 self.gidCaller = None
186 gidObjectStr = dict.get("gidObject", None)
188 self.gidObject = GID(string=gidObjectStr)
190 self.gidObject = None
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.
198 # Each credential must be a subset of the rights of the parent.
200 def verify_chain(self, trusted_certs = None):
201 # do the normal certificate verification stuff
202 Certificate.verify_chain(self, trusted_certs)
205 # make sure the parent delegated rights to the child
206 if not self.parent.get_delegate():
207 raise MissingDelegateBit(self.parent.get_subject())
209 # make sure the rights given to the child are a subset of the
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())
219 # Dump the contents of a credential to stdout in human-readable format
221 # @param dump_parents If true, also dump the parent certificates
223 def dump(self, *args, **kwargs):
224 print self.dump_string(*args,**kwargs)
226 def dump_string(self, dump_parents=False):
228 result += "CREDENTIAL %s\n" % self.get_subject()
230 result += " privs: %s\n" % self.get_privileges().save_to_string()
232 gidCaller = self.get_gid_caller()
234 result += " gidCaller:\n"
235 gidCaller.dump(8, dump_parents)
237 gidObject = self.get_gid_object()
239 result += " gidObject:\n"
240 result += gidObject.dump_string(8, dump_parents)
242 result += " delegate: %s" % self.get_delegate()
244 if self.parent and dump_parents:
246 result += self.parent.dump_string(dump_parents)