1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 #----------------------------------------------------------------------
24 # Implements SFA Credentials
26 # Credentials are layered on top of certificates, and are essentially a
27 # certificate that stores a tuple of parameters.
33 from sfa.util.faults import MissingDelegateBit, ChildRightsNotSubsetOfParent
34 from sfa.trust.certificate import Certificate
35 from sfa.trust.gid import GID
38 # Credential is a tuple:
39 # (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
41 # These fields are encoded using xmlrpc into the subjectAltName field of the
42 # x509 certificate. Note: Call encode() once the fields have been filled in
43 # to perform this encoding.
45 class CredentialLegacy(Certificate):
53 # Create a Credential object
55 # @param create If true, create a blank x509 certificate
56 # @param subject If subject!=None, create an x509 cert with the subject name
57 # @param string If string!=None, load the credential from the string
58 # @param filename If filename!=None, load the credential from the file
60 def __init__(self, create=False, subject=None, string=None, filename=None):
61 Certificate.__init__(self, create, subject, string, filename)
64 # set the GID of the caller
66 # @param gid GID object of the caller
68 def set_gid_caller(self, gid):
70 # gid origin caller is the caller's gid by default
71 self.gidOriginCaller = gid
74 # get the GID of the object
76 def get_gid_caller(self):
77 if not self.gidCaller:
82 # set the GID of the object
84 # @param gid GID object of the object
86 def set_gid_object(self, gid):
90 # get the GID of the object
92 def get_gid_object(self):
93 if not self.gidObject:
98 # set the lifetime of this credential
100 # @param lifetime lifetime of credential
102 def set_lifetime(self, lifeTime):
103 self.lifeTime = lifeTime
106 # get the lifetime of the credential
108 def get_lifetime(self):
109 if not self.lifeTime:
114 # set the delegate bit
116 # @param delegate boolean (True or False)
118 def set_delegate(self, delegate):
119 self.delegate = delegate
122 # get the delegate bit
124 def get_delegate(self):
125 if not self.delegate:
132 # @param privs either a comma-separated list of privileges of a Rights object
134 def set_privileges(self, privs):
135 if isinstance(privs, str):
136 self.privileges = Rights(string = privs)
138 self.privileges = privs
141 # return the privileges as a Rights object
143 def get_privileges(self):
144 if not self.privileges:
146 return self.privileges
149 # determine whether the credential allows a particular operation to be
152 # @param op_name string specifying name of operation ("lookup", "update", etc)
154 def can_perform(self, op_name):
155 rights = self.get_privileges()
158 return rights.can_perform(op_name)
161 # Encode the attributes of the credential into a string and store that
162 # string in the alt-subject-name field of the X509 object. This should be
163 # done immediately before signing the credential.
166 dict = {"gidCaller": None,
168 "lifeTime": self.lifeTime,
170 "delegate": self.delegate}
172 dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
174 dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
176 dict["privileges"] = self.privileges.save_to_string()
177 str = xmlrpclib.dumps((dict,), allow_none=True)
178 self.set_data('URI:http://' + str)
181 # Retrieve the attributes of the credential from the alt-subject-name field
182 # of the X509 certificate. This is automatically done by the various
183 # get_* methods of this class and should not need to be called explicitly.
186 data = self.get_data().lstrip('URI:http://')
189 dict = xmlrpclib.loads(data)[0][0]
193 self.lifeTime = dict.get("lifeTime", None)
194 self.delegate = dict.get("delegate", None)
196 privStr = dict.get("privileges", None)
198 self.privileges = Rights(string = privStr)
200 self.privileges = None
202 gidCallerStr = dict.get("gidCaller", None)
204 self.gidCaller = GID(string=gidCallerStr)
206 self.gidCaller = None
208 gidObjectStr = dict.get("gidObject", None)
210 self.gidObject = GID(string=gidObjectStr)
212 self.gidObject = None
215 # Verify that a chain of credentials is valid (see cert.py:verify). In
216 # addition to the checks for ordinary certificates, verification also
217 # ensures that the delegate bit was set by each parent in the chain. If
218 # a delegate bit was not set, then an exception is thrown.
220 # Each credential must be a subset of the rights of the parent.
222 def verify_chain(self, trusted_certs = None):
223 # do the normal certificate verification stuff
224 Certificate.verify_chain(self, trusted_certs)
227 # make sure the parent delegated rights to the child
228 if not self.parent.get_delegate():
229 raise MissingDelegateBit(self.parent.get_subject())
231 # make sure the rights given to the child are a subset of the
233 if not self.parent.get_privileges().is_superset(self.get_privileges()):
234 raise ChildRightsNotSubsetOfParent(self.get_subject()
235 + " " + self.parent.get_privileges().save_to_string()
236 + " " + self.get_privileges().save_to_string())
241 # Dump the contents of a credential to stdout in human-readable format
243 # @param dump_parents If true, also dump the parent certificates
245 def dump(self, *args, **kwargs):
246 print self.dump_string(*args,**kwargs)
248 def dump_string(self, dump_parents=False):
250 result += "CREDENTIAL %s\n" % self.get_subject()
252 result += " privs: %s\n" % self.get_privileges().save_to_string()
254 gidCaller = self.get_gid_caller()
256 result += " gidCaller:\n"
257 gidCaller.dump(8, dump_parents)
259 gidObject = self.get_gid_object()
261 result += " gidObject:\n"
262 result += gidObject.dump_string(8, dump_parents)
264 result += " delegate: %s" % self.get_delegate()
266 if self.parent and dump_parents:
268 result += self.parent.dump_string(dump_parents)