2 # This module implements a hierarchy of authorities and performs a similar
3 # function as the "tree" module of the original geniwrapper 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.
8 # Each authority is stored in a subdirectory on the registry. Inside this
9 # subdirectory are several files:
11 # *.PKEY - private key file
12 # *.DBINFO - database info
18 from credential import *
22 from geniticket import *
25 # The AuthInfo class contains the information for an authority. This information
26 # includes the GID, private key, and database connection information.
32 privkey_filename = None
33 dbinfo_filename = None
36 # Initialize and authority object.
38 # @param hrn the human readable name of the authority
39 # @param gid_filename the filename containing the GID
40 # @param privkey_filename the filename containing the private key
41 # @param dbinfo_filename the filename containing the database info
43 def __init__(self, hrn, gid_filename, privkey_filename, dbinfo_filename):
45 self.set_gid_filename(gid_filename)
46 self.privkey_filename = privkey_filename
47 self.dbinfo_filename = dbinfo_filename
50 # Set the filename of the GID
52 # @param fn filename of file containing GID
54 def set_gid_filename(self, fn):
55 self.gid_filename = fn
56 self.gid_object = None
59 # Get the GID in the form of a GID object
61 def get_gid_object(self):
62 if not self.gid_object:
63 self.gid_object = GID(filename = self.gid_filename)
64 return self.gid_object
67 # Get the private key in the form of a Keypair object
69 def get_pkey_object(self):
70 return Keypair(filename = self.privkey_filename)
73 # Get the dbinfo in the form of a dictionary
76 f = file(self.dbinfo_filename)
77 dict = eval(f.read())
\r
82 # Replace the GID with a new one. The file specified by gid_filename is
\r
83 # overwritten with the new GID object
\r
85 # @param gid object containing new GID
\r
87 def update_gid_object(self, gid):
\r
88 gid.save_to_file(self.gid_filename)
\r
89 self.gid_object = gid
\r
92 # The Hierarchy class is responsible for managing the tree of authorities.
\r
93 # Each authority is a node in the tree and exists as an AuthInfo object.
\r
95 # The tree is stored on disk in a hierarchical manner than reflects the
\r
96 # structure of the tree. Each authority is a subdirectory, and each subdirectory
\r
97 # contains the GID, pkey, and dbinfo files for that authority (as well as
\r
98 # subdirectories for each sub-authority)
\r
102 # Create the hierarchy object.
104 # @param basedir the base directory to store the hierarchy in
106 def __init__(self, basedir = None):
109 basedir = config.GENI_BASE_DIR + os.sep + 'geni' + os.sep
110 self.basedir = os.path.join(basedir, "authorities")
113 # Given a hrn, return the filenames of the GID, private key, and dbinfo
116 # @param hrn the human readable name of the authority
118 def get_auth_filenames(self, hrn):
120 parent_hrn = get_authority(hrn)
121 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
123 gid_filename = os.path.join(directory, leaf+".gid")
124 privkey_filename = os.path.join(directory, leaf+".pkey")
125 dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
127 return (directory, gid_filename, privkey_filename, dbinfo_filename)
130 # Check to see if an authority exists. An authority exists if it's disk
133 # @param the human readable name of the authority to check
135 def auth_exists(self, hrn):
136 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
137 self.get_auth_filenames(hrn)
139 return os.path.exists(gid_filename) and \
140 os.path.exists(privkey_filename) and \
141 os.path.exists(dbinfo_filename)
144 # Create an authority. A private key for the authority and the associated
145 # GID are created and signed by the parent authority.
147 # @param hrn the human readable name of the authority to create
148 # @param create_parents if true, also create the parents if they do not exist
150 def create_auth(self, hrn, create_parents=False):
151 report.trace("Hierarchy: creating authority: " + hrn)
153 # create the parent authority if necessary
154 parent_hrn = get_authority(hrn)
155 if (parent_hrn) and (not self.auth_exists(parent_hrn)) and (create_parents):
156 self.create_auth(parent_hrn, create_parents)
158 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
159 self.get_auth_filenames(hrn)
161 # create the directory to hold the files
163 os.makedirs(directory)
\r
164 # if the path already exists then pass
\r
165 except OSError, (errno, strerr):
\r
169 if os.path.exists(privkey_filename):
170 print "using existing key", privkey_filename, "for authority", hrn
171 pkey = Keypair(filename = privkey_filename)
173 pkey = Keypair(create = True)
174 pkey.save_to_file(privkey_filename)
176 gid = self.create_gid(hrn, create_uuid(), pkey)
177 gid.save_to_file(gid_filename, save_parents=True)
179 # XXX TODO: think up a better way for the dbinfo to work
181 dbinfo = get_default_dbinfo()
182 dbinfo_file = file(dbinfo_filename, "w")
183 dbinfo_file.write(str(dbinfo))
\r
187 # Return the AuthInfo object for the specified authority. If the authority
188 # does not exist, then an exception is thrown. As a side effect, disk files
189 # and a subdirectory may be created to store the authority.
191 # @param hrn the human readable name of the authority to create.
193 def get_auth_info(self, hrn):
194 #report.trace("Hierarchy: getting authority: " + hrn)
196 if not self.auth_exists(hrn):
197 raise MissingAuthority(hrn)
199 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
200 self.get_auth_filenames(hrn)
202 auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
204 # check the GID and see if it needs to be refreshed
205 gid = auth_info.get_gid_object()
206 gid_refreshed = self.refresh_gid(gid)
207 if gid != gid_refreshed:
208 auth_info.update_gid_object(gid_refreshed)
213 # Create a new GID. The GID will be signed by the authority that is it's
214 # immediate parent in the hierarchy (and recursively, the parents' GID
215 # will be signed by its parent)
217 # @param hrn the human readable name to store in the GID
218 # @param uuid the unique identifier to store in the GID
219 # @param pkey the public key to store in the GID
221 def create_gid(self, hrn, uuid, pkey):
222 gid = GID(subject=hrn, uuid=uuid, hrn=hrn)
224 parent_hrn = get_authority(hrn)
226 # if there is no parent hrn, then it must be self-signed. this
227 # is where we terminate the recursion
228 gid.set_issuer(pkey, hrn)
230 # we need the parent's private key in order to sign this GID
231 parent_auth_info = self.get_auth_info(parent_hrn)
232 gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
233 gid.set_parent(parent_auth_info.get_gid_object())
242 # Refresh a GID. The primary use of this function is to refresh the
243 # the expiration time of the GID. It may also be used to change the HRN,
244 # UUID, or Public key of the GID.
246 # @param gid the GID to refresh
247 # @param hrn if !=None, change the hrn
248 # @param uuid if !=None, change the uuid
249 # @param pubkey if !=None, change the public key
251 def refresh_gid(self, gid, hrn=None, uuid=None, pubkey=None):
252 # TODO: compute expiration time of GID, refresh it if necessary
253 gid_is_expired = False
255 # update the gid if we need to
256 if gid_is_expired or hrn or uuid or pubkey:
260 uuid = gid.get_uuid()
262 pubkey = gid.get_pubkey()
264 gid = self.create_gid(hrn, uuid, pubkey)
269 # Retrieve an authority credential for an authority. The authority
270 # credential will contain the authority privilege and will be signed by
271 # the authority's parent.
273 # @param hrn the human readable name of the authority
274 # @param authority type of credential to return (authority | sa | ma)
276 def get_auth_cred(self, hrn, kind="authority"):
277 auth_info = self.get_auth_info(hrn)
278 gid = auth_info.get_gid_object()
280 cred = Credential(subject=hrn)
281 cred.set_gid_caller(gid)
282 cred.set_gid_object(gid)
283 cred.set_privileges(kind)
284 cred.set_delegate(True)
285 cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
287 parent_hrn = get_authority(hrn)
289 # if there is no parent hrn, then it must be self-signed. this
290 # is where we terminate the recursion
291 cred.set_issuer(auth_info.get_pkey_object(), hrn)
293 # we need the parent's private key in order to sign this GID
294 parent_auth_info = self.get_auth_info(parent_hrn)
295 cred.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
296 cred.set_parent(self.get_auth_cred(parent_hrn, kind))
303 # Retrieve an authority ticket. An authority ticket is not actually a
304 # redeemable ticket, but only serves the purpose of being included as the
305 # parent of another ticket, in order to provide a chain of authentication
308 # This looks almost the same as get_auth_cred, but works for tickets
309 # XXX does similarity imply there should be more code re-use?
311 # @param hrn the human readable name of the authority
313 def get_auth_ticket(self, hrn):
314 auth_info = self.get_auth_info(hrn)
315 gid = auth_info.get_gid_object()
317 ticket = Ticket(subject=hrn)
318 ticket.set_gid_caller(gid)
319 ticket.set_gid_object(gid)
320 ticket.set_delegate(True)
321 ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
323 parent_hrn = get_authority(hrn)
325 # if there is no parent hrn, then it must be self-signed. this
326 # is where we terminate the recursion
327 ticket.set_issuer(auth_info.get_pkey_object(), hrn)
329 # we need the parent's private key in order to sign this GID
330 parent_auth_info = self.get_auth_info(parent_hrn)
331 ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
332 ticket.set_parent(self.get_auth_cred(parent_hrn))