2 # Implements Geni 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):
53 # get the GID of the object
55 def get_gid_caller(self):
56 if not self.gidCaller:
61 # set the GID of the object
63 # @param gid GID object of the object
65 def set_gid_object(self, gid):
69 # get the GID of the object
71 def get_gid_object(self):
72 if not self.gidObject:
77 # set the lifetime of this credential
79 # @param lifetime lifetime of credential
81 def set_lifetime(self, lifeTime):
82 self.lifeTime = lifeTime
85 # get the lifetime of the credential
87 def get_lifetime(self):
93 # set the delegate bit
95 # @param delegate boolean (True or False)
97 def set_delegate(self, delegate):
98 self.delegate = delegate
101 # get the delegate bit
103 def get_delegate(self):
104 if not self.delegate:
111 # @param privs either a comma-separated list of privileges of a RightList object
113 def set_privileges(self, privs):
114 if isinstance(privs, str):
115 self.privileges = RightList(string = privs)
117 self.privileges = privs
120 # return the privileges as a RightList object
122 def get_privileges(self):
123 if not self.privileges:
125 return self.privileges
128 # determine whether the credential allows a particular operation to be
131 # @param op_name string specifying name of operation ("lookup", "update", etc)
133 def can_perform(self, op_name):
134 rights = self.get_privileges()
137 return rights.can_perform(op_name)
140 # Encode the attributes of the credential into a string and store that
141 # string in the alt-subject-name field of the X509 object. This should be
142 # done immediately before signing the credential.
145 dict = {"gidCaller": None,
147 "lifeTime": self.lifeTime,
149 "delegate": self.delegate}
151 dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
153 dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
155 dict["privileges"] = self.privileges.save_to_string()
156 str = xmlrpclib.dumps((dict,), allow_none=True)
160 # Retrieve the attributes of the credential from the alt-subject-name field
161 # of the X509 certificate. This is automatically done by the various
162 # get_* methods of this class and should not need to be called explicitly.
165 data = self.get_data()
167 dict = xmlrpclib.loads(self.get_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 = RightList(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, dump_parents=False):
224 print "CREDENTIAL", self.get_subject()
226 print " privs:", self.get_privileges().save_to_string()
229 gidCaller = self.get_gid_caller()
231 gidCaller.dump(8, dump_parents)
234 gidObject = self.get_gid_object()
236 gidObject.dump(8, dump_parents)
238 print " delegate:", self.get_delegate()
240 if self.parent and dump_parents:
242 self.parent.dump(dump_parents)