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