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
21 from geni.util.cert import *
22 from geni.util.credential import *
23 from geni.util.gid import *
24 from geni.util.misc import *
25 from geni.util.config import *
26 from geni.util.geniticket import *
29 # The AuthInfo class contains the information for an authority. This information
30 # includes the GID, private key, and database connection information.
36 privkey_filename = None
37 dbinfo_filename = None
40 # Initialize and authority object.
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
47 def __init__(self, hrn, gid_filename, privkey_filename, dbinfo_filename):
49 self.set_gid_filename(gid_filename)
50 self.privkey_filename = privkey_filename
51 self.dbinfo_filename = dbinfo_filename
54 # Set the filename of the GID
56 # @param fn filename of file containing GID
58 def set_gid_filename(self, fn):
59 self.gid_filename = fn
60 self.gid_object = None
63 # Get the GID in the form of a GID object
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
71 # Get the private key in the form of a Keypair object
73 def get_pkey_object(self):
74 return Keypair(filename = self.privkey_filename)
77 # Get the dbinfo in the form of a dictionary
80 f = file(self.dbinfo_filename)
86 # Replace the GID with a new one. The file specified by gid_filename is
87 # overwritten with the new GID object
89 # @param gid object containing new GID
91 def update_gid_object(self, gid):
92 gid.save_to_file(self.gid_filename)
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.
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)
106 # Create the hierarchy object.
108 # @param basedir the base directory to store the hierarchy in
110 def __init__(self, basedir = None):
113 basedir = config.config_path + os.sep + "authorities"
114 self.basedir = basedir
116 # Given a hrn, return the filenames of the GID, private key, and dbinfo
119 # @param hrn the human readable name of the authority
121 def get_auth_filenames(self, hrn):
123 parent_hrn = get_authority(hrn)
124 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
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")
130 return (directory, gid_filename, privkey_filename, dbinfo_filename)
133 # Check to see if an authority exists. An authority exists if it's disk
136 # @param the human readable name of the authority to check
138 def auth_exists(self, hrn):
139 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
140 self.get_auth_filenames(hrn)
142 return os.path.exists(gid_filename) and \
143 os.path.exists(privkey_filename) and \
144 os.path.exists(dbinfo_filename)
147 # Create an authority. A private key for the authority and the associated
148 # GID are created and signed by the parent authority.
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
153 def create_auth(self, hrn, create_parents=False):
154 report.trace("Hierarchy: creating authority: " + hrn)
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)
161 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
162 self.get_auth_filenames(hrn)
164 # create the directory to hold the files
166 os.makedirs(directory)
167 # if the path already exists then pass
168 except OSError, (errno, strerr):
172 if os.path.exists(privkey_filename):
173 print "using existing key", privkey_filename, "for authority", hrn
174 pkey = Keypair(filename = privkey_filename)
176 pkey = Keypair(create = True)
177 pkey.save_to_file(privkey_filename)
179 gid = self.create_gid(hrn, create_uuid(), pkey)
180 gid.save_to_file(gid_filename, save_parents=True)
182 # XXX TODO: think up a better way for the dbinfo to work
184 dbinfo = get_default_dbinfo()
185 dbinfo_file = file(dbinfo_filename, "w")
186 dbinfo_file.write(str(dbinfo))
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.
194 # @param hrn the human readable name of the authority to create.
196 def get_auth_info(self, hrn):
197 #report.trace("Hierarchy: getting authority: " + hrn)
199 if not self.auth_exists(hrn):
200 raise MissingAuthority(hrn)
202 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
203 self.get_auth_filenames(hrn)
205 auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
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)
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)
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
224 def create_gid(self, hrn, uuid, pkey):
225 gid = GID(subject=hrn, uuid=uuid, hrn=hrn)
227 parent_hrn = get_authority(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)
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())
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.
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
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
258 # update the gid if we need to
259 if gid_is_expired or hrn or uuid or pubkey:
263 uuid = gid.get_uuid()
265 pubkey = gid.get_pubkey()
267 gid = self.create_gid(hrn, uuid, pubkey)
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.
276 # @param hrn the human readable name of the authority
277 # @param authority type of credential to return (authority | sa | ma)
279 def get_auth_cred(self, hrn, kind="authority"):
280 auth_info = self.get_auth_info(hrn)
281 gid = auth_info.get_gid_object()
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())
290 parent_hrn = get_authority(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)
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))
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
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?
314 # @param hrn the human readable name of the authority
316 def get_auth_ticket(self, hrn):
317 auth_info = self.get_auth_info(hrn)
318 gid = auth_info.get_gid_object()
320 ticket = Ticket(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())
326 parent_hrn = get_authority(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)
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))