removed another bunch of references to geni
[sfa.git] / sfa / trust / hierarchy.py
1 ##
2 # This module implements a hierarchy of authorities and performs a similar
3 # function as the "tree" module of the original SFA prototype. An HRN
4 # is assumed to be a string of authorities separated by dots. For example,
5 # "planetlab.us.arizona.bakers". Each component of the HRN is a different
6 # authority, with the last component being a leaf in the tree.
7 #
8 # Each authority is stored in a subdirectory on the registry. Inside this
9 # subdirectory are several files:
10 #      *.GID - GID file
11 #      *.PKEY - private key file
12 #      *.DBINFO - database info
13 ##
14
15 ### $Id$
16 ### $URL$
17
18 import os
19
20 from sfa.util.report import *
21 from sfa.trust.certificate import Keypair
22 from sfa.trust.credential import *
23 from sfa.trust.gid import GID, create_uuid
24 from sfa.util.namespace import *
25 from sfa.util.config import Config
26 from sfa.util.sfaticket import SfaTicket
27
28 ##
29 # The AuthInfo class contains the information for an authority. This information
30 # includes the GID, private key, and database connection information.
31
32 class AuthInfo:
33     hrn = None
34     gid_object = None
35     gid_filename = None
36     privkey_filename = None
37     dbinfo_filename = None
38
39     ##
40     # Initialize and authority object.
41     #
42     # @param hrn the human readable name of the authority
43     # @param gid_filename the filename containing the GID
44     # @param privkey_filename the filename containing the private key
45     # @param dbinfo_filename the filename containing the database info
46
47     def __init__(self, hrn, gid_filename, privkey_filename, dbinfo_filename):
48         self.hrn = hrn
49         self.set_gid_filename(gid_filename)
50         self.privkey_filename = privkey_filename
51         self.dbinfo_filename = dbinfo_filename
52
53     ##
54     # Set the filename of the GID
55     #
56     # @param fn filename of file containing GID
57
58     def set_gid_filename(self, fn):
59         self.gid_filename = fn
60         self.gid_object = None
61
62     ##
63     # Get the GID in the form of a GID object
64
65     def get_gid_object(self):
66         if not self.gid_object:
67             self.gid_object = GID(filename = self.gid_filename)
68         return self.gid_object
69
70     ##
71     # Get the private key in the form of a Keypair object
72
73     def get_pkey_object(self):
74         return Keypair(filename = self.privkey_filename)
75
76     ##
77     # Get the dbinfo in the form of a dictionary
78
79     def get_dbinfo(self):
80         f = file(self.dbinfo_filename)
81         dict = eval(f.read())
82         f.close()
83         return dict
84
85     ##
86     # Replace the GID with a new one. The file specified by gid_filename is
87     # overwritten with the new GID object
88     #
89     # @param gid object containing new GID
90
91     def update_gid_object(self, gid):
92         gid.save_to_file(self.gid_filename)
93         self.gid_object = gid
94
95 ##
96 # The Hierarchy class is responsible for managing the tree of authorities.
97 # Each authority is a node in the tree and exists as an AuthInfo object.
98 #
99 # The tree is stored on disk in a hierarchical manner than reflects the
100 # structure of the tree. Each authority is a subdirectory, and each subdirectory
101 # contains the GID, pkey, and dbinfo files for that authority (as well as
102 # subdirectories for each sub-authority)
103
104 class Hierarchy:
105     ##
106     # Create the hierarchy object.
107     #
108     # @param basedir the base directory to store the hierarchy in
109
110     def __init__(self, basedir = None):
111         if not basedir:
112             self.config = Config()
113             basedir = os.path.join(self.config.SFA_DATA_DIR, "authorities")
114         self.basedir = basedir
115     ##
116     # Given a hrn, return the filenames of the GID, private key, and dbinfo
117     # files.
118     #
119     # @param hrn the human readable name of the authority
120
121     def get_auth_filenames(self, hrn):
122         leaf = get_leaf(hrn)
123         parent_hrn = get_authority(hrn)
124         directory = os.path.join(self.basedir, hrn.replace(".", "/"))
125
126         gid_filename = os.path.join(directory, leaf+".gid")
127         privkey_filename = os.path.join(directory, leaf+".pkey")
128         dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
129
130         return (directory, gid_filename, privkey_filename, dbinfo_filename)
131
132     ##
133     # Check to see if an authority exists. An authority exists if it's disk
134     # files exist.
135     #
136     # @param the human readable name of the authority to check
137
138     def auth_exists(self, hrn):
139         (directory, gid_filename, privkey_filename, dbinfo_filename) = \
140             self.get_auth_filenames(hrn)
141         
142         return os.path.exists(gid_filename) and \
143                os.path.exists(privkey_filename) and \
144                os.path.exists(dbinfo_filename)
145
146     ##
147     # Create an authority. A private key for the authority and the associated
148     # GID are created and signed by the parent authority.
149     #
150     # @param hrn the human readable name of the authority to create
151     # @param create_parents if true, also create the parents if they do not exist
152
153     def create_auth(self, hrn, create_parents=False):
154         trace("Hierarchy: creating authority: " + hrn)
155
156         # create the parent authority if necessary
157         parent_hrn = get_authority(hrn)
158         if (parent_hrn) and (not self.auth_exists(parent_hrn)) and (create_parents):
159             self.create_auth(parent_hrn, create_parents)
160
161         (directory, gid_filename, privkey_filename, dbinfo_filename) = \
162             self.get_auth_filenames(hrn)
163
164         # create the directory to hold the files
165         try:
166             os.makedirs(directory)
167         # if the path already exists then pass
168         except OSError, (errno, strerr):
169             if errno == 17:
170                 pass
171
172         if os.path.exists(privkey_filename):
173             print "using existing key", privkey_filename, "for authority", hrn
174             pkey = Keypair(filename = privkey_filename)
175         else:
176             pkey = Keypair(create = True)
177             pkey.save_to_file(privkey_filename)
178
179         gid = self.create_gid(hrn, create_uuid(), pkey)
180         gid.save_to_file(gid_filename, save_parents=True)
181
182         # XXX TODO: think up a better way for the dbinfo to work
183
184         dbinfo = Config().get_plc_dbinfo()
185         dbinfo_file = file(dbinfo_filename, "w")
186         dbinfo_file.write(str(dbinfo))
187         dbinfo_file.close()
188
189     ##
190     # Return the AuthInfo object for the specified authority. If the authority
191     # does not exist, then an exception is thrown. As a side effect, disk files
192     # and a subdirectory may be created to store the authority.
193     #
194     # @param hrn the human readable name of the authority to create.
195
196     def get_auth_info(self, hrn):
197         #trace("Hierarchy: getting authority: " + hrn)
198    
199         if not self.auth_exists(hrn):
200             raise MissingAuthority(hrn)
201
202         (directory, gid_filename, privkey_filename, dbinfo_filename) = \
203             self.get_auth_filenames(hrn)
204
205         auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
206
207         # check the GID and see if it needs to be refreshed
208         gid = auth_info.get_gid_object()
209         gid_refreshed = self.refresh_gid(gid)
210         if gid != gid_refreshed:
211             auth_info.update_gid_object(gid_refreshed)
212
213         return auth_info
214
215     ##
216     # Create a new GID. The GID will be signed by the authority that is it's
217     # immediate parent in the hierarchy (and recursively, the parents' GID
218     # will be signed by its parent)
219     #
220     # @param hrn the human readable name to store in the GID
221     # @param uuid the unique identifier to store in the GID
222     # @param pkey the public key to store in the GID
223
224     def create_gid(self, hrn, uuid, pkey):
225         gid = GID(subject=hrn, uuid=uuid, hrn=hrn)
226
227         parent_hrn = get_authority(hrn)
228         if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
229             # if there is no parent hrn, then it must be self-signed. this
230             # is where we terminate the recursion
231             gid.set_issuer(pkey, hrn)
232         else:
233             # we need the parent's private key in order to sign this GID
234             parent_auth_info = self.get_auth_info(parent_hrn)
235             gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
236             gid.set_parent(parent_auth_info.get_gid_object())
237
238         gid.set_pubkey(pkey)
239         gid.encode()
240         gid.sign()
241
242         return gid
243
244     ##
245     # Refresh a GID. The primary use of this function is to refresh the
246     # the expiration time of the GID. It may also be used to change the HRN,
247     # UUID, or Public key of the GID.
248     #
249     # @param gid the GID to refresh
250     # @param hrn if !=None, change the hrn
251     # @param uuid if !=None, change the uuid
252     # @param pubkey if !=None, change the public key
253
254     def refresh_gid(self, gid, hrn=None, uuid=None, pubkey=None):
255         # TODO: compute expiration time of GID, refresh it if necessary
256         gid_is_expired = False
257
258         # update the gid if we need to
259         if gid_is_expired or hrn or uuid or pubkey:
260             if not hrn:
261                 hrn = gid.get_hrn()
262             if not uuid:
263                 uuid = gid.get_uuid()
264             if not pubkey:
265                 pubkey = gid.get_pubkey()
266
267             gid = self.create_gid(hrn, uuid, pubkey)
268
269         return gid
270
271     ##
272     # Retrieve an authority credential for an authority. The authority
273     # credential will contain the authority privilege and will be signed by
274     # the authority's parent.
275     #
276     # @param hrn the human readable name of the authority
277     # @param authority type of credential to return (authority | sa | ma)
278
279     def get_auth_cred(self, hrn, kind="authority"):
280         auth_info = self.get_auth_info(hrn)
281         gid = auth_info.get_gid_object()
282
283         cred = Credential(subject=hrn)
284         cred.set_gid_caller(gid)
285         cred.set_gid_object(gid)
286         cred.set_privileges(kind)
287         cred.set_delegate(True)
288         cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
289
290         parent_hrn = get_authority(hrn)
291         if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
292             # if there is no parent hrn, then it must be self-signed. this
293             # is where we terminate the recursion
294             cred.set_issuer(auth_info.get_pkey_object(), hrn)
295         else:
296             # we need the parent's private key in order to sign this GID
297             parent_auth_info = self.get_auth_info(parent_hrn)
298             cred.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
299             cred.set_parent(self.get_auth_cred(parent_hrn, kind))
300
301         cred.encode()
302         cred.sign()
303
304         return cred
305     ##
306     # Retrieve an authority ticket. An authority ticket is not actually a
307     # redeemable ticket, but only serves the purpose of being included as the
308     # parent of another ticket, in order to provide a chain of authentication
309     # for a ticket.
310     #
311     # This looks almost the same as get_auth_cred, but works for tickets
312     # XXX does similarity imply there should be more code re-use?
313     #
314     # @param hrn the human readable name of the authority
315
316     def get_auth_ticket(self, hrn):
317         auth_info = self.get_auth_info(hrn)
318         gid = auth_info.get_gid_object()
319
320         ticket = SfaTicket(subject=hrn)
321         ticket.set_gid_caller(gid)
322         ticket.set_gid_object(gid)
323         ticket.set_delegate(True)
324         ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
325
326         parent_hrn = get_authority(hrn)
327         if not parent_hrn:
328             # if there is no parent hrn, then it must be self-signed. this
329             # is where we terminate the recursion
330             ticket.set_issuer(auth_info.get_pkey_object(), hrn)
331         else:
332             # we need the parent's private key in order to sign this GID
333             parent_auth_info = self.get_auth_info(parent_hrn)
334             ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
335             ticket.set_parent(self.get_auth_cred(parent_hrn))
336
337         ticket.encode()
338         ticket.sign()
339
340         return ticket
341