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 *
18 # Credential is a tuple:
19 # (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
21 # These fields are encoded using xmlrpc into the subjectAltName field of the
22 # x509 certificate. Note: Call encode() once the fields have been filled in
23 # to perform this encoding.
25 class Credential(Certificate):
33 # Create a Credential object
35 # @param create If true, create a blank x509 certificate
36 # @param subject If subject!=None, create an x509 cert with the subject name
37 # @param string If string!=None, load the credential from the string
38 # @param filename If filename!=None, load the credential from the file
40 def __init__(self, create=False, subject=None, string=None, filename=None):
41 Certificate.__init__(self, create, subject, string, filename)
44 # set the GID of the caller
46 # @param gid GID object of the caller
48 def set_gid_caller(self, 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 RightList object
112 def set_privileges(self, privs):
113 if isinstance(privs, str):
114 self.privileges = RightList(string = privs)
116 self.privileges = privs
119 # return the privileges as a RightList 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)
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()
166 dict = xmlrpclib.loads(self.get_data())[0][0]
170 self.lifeTime = dict.get("lifeTime", None)
171 self.delegate = dict.get("delegate", None)
173 privStr = dict.get("privileges", None)
175 self.privileges = RightList(string = privStr)
177 self.privileges = None
179 gidCallerStr = dict.get("gidCaller", None)
181 self.gidCaller = GID(string=gidCallerStr)
183 self.gidCaller = None
185 gidObjectStr = dict.get("gidObject", None)
187 self.gidObject = GID(string=gidObjectStr)
189 self.gidObject = None
192 # Verify that a chain of credentials is valid (see cert.py:verify). In
193 # addition to the checks for ordinary certificates, verification also
194 # ensures that the delegate bit was set by each parent in the chain. If
195 # a delegate bit was not set, then an exception is thrown.
197 # Each credential must be a subset of the rights of the parent.
199 def verify_chain(self, trusted_certs = None):
200 # do the normal certificate verification stuff
201 Certificate.verify_chain(self, trusted_certs)
204 # make sure the parent delegated rights to the child
205 if not self.parent.get_delegate():
206 raise MissingDelegateBit(self.parent.get_subject())
208 # make sure the rights given to the child are a subset of the
210 if not self.parent.get_privileges().is_superset(self.get_privileges()):
211 raise ChildRightsNotSubsetOfParent(self.get_subject()
212 + " " + self.parent.get_privileges().save_to_string()
213 + " " + self.get_privileges().save_to_string())
218 # Dump the contents of a credential to stdout in human-readable format
220 # @param dump_parents If true, also dump the parent certificates
222 def dump(self, dump_parents=False):
223 print "CREDENTIAL", self.get_subject()
225 print " privs:", self.get_privileges().save_to_string()
228 gidCaller = self.get_gid_caller()
230 gidCaller.dump(8, dump_parents)
233 gidObject = self.get_gid_object()
235 gidObject.dump(8, dump_parents)
237 print " delegate:", self.get_delegate()
239 if self.parent and dump_parents:
241 self.parent.dump(dump_parents)