removed another bunch of references to geni
[sfa.git] / sfa / trust / credential.py
1 ##
2 # Implements SFA Credentials
3 #
4 # Credentials are layered on top of certificates, and are essentially a
5 # certificate that stores a tuple of parameters.
6 ##
7
8 ### $Id$
9 ### $URL$
10
11 import xmlrpclib
12
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 *
17
18 ##
19 # Credential is a tuple:
20 #     (GIDCaller, GIDObject, LifeTime, Privileges, Delegate)
21 #
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.
25
26 class Credential(Certificate):
27     gidCaller = None
28     gidObject = None
29     lifeTime = None
30     privileges = None
31     delegate = False
32
33     ##
34     # Create a Credential object
35     #
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
40
41     def __init__(self, create=False, subject=None, string=None, filename=None):
42         Certificate.__init__(self, create, subject, string, filename)
43
44     ##
45     # set the GID of the caller
46     #
47     # @param gid GID object of the caller
48
49     def set_gid_caller(self, gid):
50         self.gidCaller = gid
51         # gid origin caller is the caller's gid by default
52         self.gidOriginCaller = gid
53
54     ##
55     # get the GID of the object
56
57     def get_gid_caller(self):
58         if not self.gidCaller:
59             self.decode()
60         return self.gidCaller
61
62     ##
63     # set the GID of the object
64     #
65     # @param gid GID object of the object
66
67     def set_gid_object(self, gid):
68         self.gidObject = gid
69
70     ##
71     # get the GID of the object
72
73     def get_gid_object(self):
74         if not self.gidObject:
75             self.decode()
76         return self.gidObject
77
78     ##
79     # set the lifetime of this credential
80     #
81     # @param lifetime lifetime of credential
82
83     def set_lifetime(self, lifeTime):
84         self.lifeTime = lifeTime
85
86     ##
87     # get the lifetime of the credential
88
89     def get_lifetime(self):
90         if not self.lifeTime:
91             self.decode()
92         return self.lifeTime
93
94     ##
95     # set the delegate bit
96     #
97     # @param delegate boolean (True or False)
98
99     def set_delegate(self, delegate):
100         self.delegate = delegate
101
102     ##
103     # get the delegate bit
104
105     def get_delegate(self):
106         if not self.delegate:
107             self.decode()
108         return self.delegate
109
110     ##
111     # set the privileges
112     #
113     # @param privs either a comma-separated list of privileges of a RightList object
114
115     def set_privileges(self, privs):
116         if isinstance(privs, str):
117             self.privileges = RightList(string = privs)
118         else:
119             self.privileges = privs
120
121     ##
122     # return the privileges as a RightList object
123
124     def get_privileges(self):
125         if not self.privileges:
126             self.decode()
127         return self.privileges
128
129     ##
130     # determine whether the credential allows a particular operation to be
131     # performed
132     #
133     # @param op_name string specifying name of operation ("lookup", "update", etc)
134
135     def can_perform(self, op_name):
136         rights = self.get_privileges()
137         if not rights:
138             return False
139         return rights.can_perform(op_name)
140
141     ##
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.
145
146     def encode(self):
147         dict = {"gidCaller": None,
148                 "gidObject": None,
149                 "lifeTime": self.lifeTime,
150                 "privileges": None,
151                 "delegate": self.delegate}
152         if self.gidCaller:
153             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
154         if self.gidObject:
155             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
156         if self.privileges:
157             dict["privileges"] = self.privileges.save_to_string()
158         str = xmlrpclib.dumps((dict,), allow_none=True)
159         self.set_data(str)
160
161     ##
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.
165
166     def decode(self):
167         data = self.get_data()
168         if data:
169             dict = xmlrpclib.loads(self.get_data())[0][0]
170         else:
171             dict = {}
172
173         self.lifeTime = dict.get("lifeTime", None)
174         self.delegate = dict.get("delegate", None)
175
176         privStr = dict.get("privileges", None)
177         if privStr:
178             self.privileges = RightList(string = privStr)
179         else:
180             self.privileges = None
181
182         gidCallerStr = dict.get("gidCaller", None)
183         if gidCallerStr:
184             self.gidCaller = GID(string=gidCallerStr)
185         else:
186             self.gidCaller = None
187
188         gidObjectStr = dict.get("gidObject", None)
189         if gidObjectStr:
190             self.gidObject = GID(string=gidObjectStr)
191         else:
192             self.gidObject = None
193
194     ##
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.
199     #
200     # Each credential must be a subset of the rights of the parent.
201
202     def verify_chain(self, trusted_certs = None):
203         # do the normal certificate verification stuff
204         Certificate.verify_chain(self, trusted_certs)
205
206         if self.parent:
207             # make sure the parent delegated rights to the child
208             if not self.parent.get_delegate():
209                 raise MissingDelegateBit(self.parent.get_subject())
210
211             # make sure the rights given to the child are a subset of the
212             # parents rights
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())
217
218         return
219
220     ##
221     # Dump the contents of a credential to stdout in human-readable format
222     #
223     # @param dump_parents If true, also dump the parent certificates
224
225     def dump(self, dump_parents=False):
226         print "CREDENTIAL", self.get_subject()
227
228         print "      privs:", self.get_privileges().save_to_string()
229
230         print "  gidCaller:"
231         gidCaller = self.get_gid_caller()
232         if gidCaller:
233             gidCaller.dump(8, dump_parents)
234
235         print "  gidObject:"
236         gidObject = self.get_gid_object()
237         if gidObject:
238             gidObject.dump(8, dump_parents)
239
240         print "   delegate:", self.get_delegate()
241
242         if self.parent and dump_parents:
243            print "PARENT",
244            self.parent.dump(dump_parents)
245