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.
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
17 from sfa.util.faults import MissingAuthority
18 from sfa.util.sfalogging import logger
19 from sfa.util.xrn import get_leaf, get_authority, hrn_to_urn, urn_to_hrn
20 from sfa.trust.certificate import Keypair
21 from sfa.trust.credential import Credential
22 from sfa.trust.gid import GID, create_uuid
23 from sfa.util.config import Config
24 from sfa.trust.sfaticket import SfaTicket
27 # The AuthInfo class contains the information for an authority. This information
28 # includes the GID, private key, and database connection information.
34 privkey_filename = None
35 dbinfo_filename = None
37 # Initialize and authority object.
39 # @param xrn the human readable name of the authority (urn will be converted to hrn)
40 # @param gid_filename the filename containing the GID
41 # @param privkey_filename the filename containing the private key
42 # @param dbinfo_filename the filename containing the database info
44 def __init__(self, xrn, gid_filename, privkey_filename, dbinfo_filename):
45 hrn, type = urn_to_hrn(xrn)
47 self.set_gid_filename(gid_filename)
48 self.privkey_filename = privkey_filename
49 self.dbinfo_filename = dbinfo_filename
52 # Set the filename of the GID
54 # @param fn filename of file containing GID
56 def set_gid_filename(self, fn):
57 self.gid_filename = fn
58 self.gid_object = None
60 def get_privkey_filename(self):
61 return self.privkey_filename
63 def get_gid_filename(self):
64 return self.gid_filename
67 # Get the GID in the form of a GID object
69 def get_gid_object(self):
70 if not self.gid_object:
71 self.gid_object = GID(filename = self.gid_filename)
72 return self.gid_object
75 # Get the private key in the form of a Keypair object
77 def get_pkey_object(self):
78 return Keypair(filename = self.privkey_filename)
81 # Get the dbinfo in the form of a dictionary
84 f = file(self.dbinfo_filename)
90 # Replace the GID with a new one. The file specified by gid_filename is
91 # overwritten with the new GID object
93 # @param gid object containing new GID
95 def update_gid_object(self, gid):
96 gid.save_to_file(self.gid_filename)
100 # The Hierarchy class is responsible for managing the tree of authorities.
101 # Each authority is a node in the tree and exists as an AuthInfo object.
103 # The tree is stored on disk in a hierarchical manner than reflects the
104 # structure of the tree. Each authority is a subdirectory, and each subdirectory
105 # contains the GID, pkey, and dbinfo files for that authority (as well as
106 # subdirectories for each sub-authority)
110 # Create the hierarchy object.
112 # @param basedir the base directory to store the hierarchy in
114 def __init__(self, basedir = None):
115 self.config = Config()
117 basedir = os.path.join(self.config.SFA_DATA_DIR, "authorities")
118 self.basedir = basedir
120 # Given a hrn, return the filenames of the GID, private key, and dbinfo
123 # @param xrn the human readable name of the authority (urn will be convertd to hrn)
125 def get_auth_filenames(self, xrn):
126 hrn, type = urn_to_hrn(xrn)
128 parent_hrn = get_authority(hrn)
129 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
131 gid_filename = os.path.join(directory, leaf+".gid")
132 privkey_filename = os.path.join(directory, leaf+".pkey")
133 dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
135 return (directory, gid_filename, privkey_filename, dbinfo_filename)
138 # Check to see if an authority exists. An authority exists if it's disk
141 # @param the human readable name of the authority to check
143 def auth_exists(self, xrn):
144 hrn, type = urn_to_hrn(xrn)
145 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
146 self.get_auth_filenames(hrn)
148 return os.path.exists(gid_filename) and \
149 os.path.exists(privkey_filename) and \
150 os.path.exists(dbinfo_filename)
153 # Create an authority. A private key for the authority and the associated
154 # GID are created and signed by the parent authority.
156 # @param xrn the human readable name of the authority to create (urn will be converted to hrn)
157 # @param create_parents if true, also create the parents if they do not exist
159 def create_auth(self, xrn, create_parents=False):
160 hrn, type = urn_to_hrn(xrn)
161 logger.debug("Hierarchy: creating authority: %s"% hrn)
163 # create the parent authority if necessary
164 parent_hrn = get_authority(hrn)
165 parent_urn = hrn_to_urn(parent_hrn, 'authority')
166 if (parent_hrn) and (not self.auth_exists(parent_urn)) and (create_parents):
167 self.create_auth(parent_urn, create_parents)
168 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
169 self.get_auth_filenames(hrn)
171 # create the directory to hold the files
173 os.makedirs(directory)
174 # if the path already exists then pass
175 except OSError, (errno, strerr):
179 if os.path.exists(privkey_filename):
180 logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn))
181 pkey = Keypair(filename = privkey_filename)
183 pkey = Keypair(create = True)
184 pkey.save_to_file(privkey_filename)
186 gid = self.create_gid(xrn, create_uuid(), pkey)
187 gid.save_to_file(gid_filename, save_parents=True)
189 # XXX TODO: think up a better way for the dbinfo to work
191 dbinfo = Config().get_plc_dbinfo()
192 dbinfo_file = file(dbinfo_filename, "w")
193 dbinfo_file.write(str(dbinfo))
196 def create_top_level_auth(self, hrn=None):
198 Create top level records (includes root and sub authorities (local/remote)
201 hrn = self.config.SFA_INTERFACE_HRN
202 # make sure parent exists
203 parent_hrn = get_authority(hrn)
206 if not parent_hrn == hrn:
207 self.create_top_level_auth(parent_hrn)
209 # create the authority if it doesnt alrady exist
210 if not self.auth_exists(hrn):
211 self.create_auth(hrn)
214 def get_interface_auth_info(self, create=True):
215 hrn = self.config.SFA_INTERFACE_HRN
216 if not self.auth_exists(hrn):
218 self.create_top_level_auth(hrn)
220 raise MissingAuthority(hrn)
221 return self.get_auth_info(hrn)
223 # Return the AuthInfo object for the specified authority. If the authority
224 # does not exist, then an exception is thrown. As a side effect, disk files
225 # and a subdirectory may be created to store the authority.
227 # @param xrn the human readable name of the authority to create (urn will be converted to hrn).
229 def get_auth_info(self, xrn):
230 hrn, type = urn_to_hrn(xrn)
231 if not self.auth_exists(hrn):
232 logger.warning("Hierarchy: missing authority - xrn=%s, hrn=%s"%(xrn,hrn))
233 raise MissingAuthority(hrn)
235 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
236 self.get_auth_filenames(hrn)
238 auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
240 # check the GID and see if it needs to be refreshed
241 gid = auth_info.get_gid_object()
242 gid_refreshed = self.refresh_gid(gid)
243 if gid != gid_refreshed:
244 auth_info.update_gid_object(gid_refreshed)
249 # Create a new GID. The GID will be signed by the authority that is it's
250 # immediate parent in the hierarchy (and recursively, the parents' GID
251 # will be signed by its parent)
253 # @param hrn the human readable name to store in the GID
254 # @param uuid the unique identifier to store in the GID
255 # @param pkey the public key to store in the GID
257 def create_gid(self, xrn, uuid, pkey, CA=False):
258 hrn, type = urn_to_hrn(xrn)
261 parent_hrn = get_authority(hrn)
262 # Using hrn_to_urn() here to make sure the urn is in the right format
263 # If xrn was a hrn instead of a urn, then the gid's urn will be
265 urn = hrn_to_urn(hrn, type)
266 gid = GID(subject=hrn, uuid=uuid, hrn=hrn, urn=urn)
268 if hrn == self.config.SFA_INTERFACE_HRN or not parent_hrn:
269 # root or sub authority
270 gid.set_intermediate_ca(True)
271 elif type and 'authority' in type:
273 gid.set_intermediate_ca(True)
275 gid.set_intermediate_ca(True)
277 gid.set_intermediate_ca(False)
280 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
281 # if there is no parent hrn, then it must be self-signed. this
282 # is where we terminate the recursion
283 gid.set_issuer(pkey, hrn)
285 # we need the parent's private key in order to sign this GID
286 parent_auth_info = self.get_auth_info(parent_hrn)
287 gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
288 gid.set_parent(parent_auth_info.get_gid_object())
297 # Refresh a GID. The primary use of this function is to refresh the
298 # the expiration time of the GID. It may also be used to change the HRN,
299 # UUID, or Public key of the GID.
301 # @param gid the GID to refresh
302 # @param hrn if !=None, change the hrn
303 # @param uuid if !=None, change the uuid
304 # @param pubkey if !=None, change the public key
306 def refresh_gid(self, gid, xrn=None, uuid=None, pubkey=None):
307 # TODO: compute expiration time of GID, refresh it if necessary
308 gid_is_expired = False
310 # update the gid if we need to
311 if gid_is_expired or xrn or uuid or pubkey:
316 uuid = gid.get_uuid()
318 pubkey = gid.get_pubkey()
320 gid = self.create_gid(xrn, uuid, pubkey)
325 # Retrieve an authority credential for an authority. The authority
326 # credential will contain the authority privilege and will be signed by
327 # the authority's parent.
329 # @param hrn the human readable name of the authority (urn is converted to hrn)
330 # @param authority type of credential to return (authority | sa | ma)
332 def get_auth_cred(self, xrn, kind="authority"):
333 hrn, type = urn_to_hrn(xrn)
334 auth_info = self.get_auth_info(hrn)
335 gid = auth_info.get_gid_object()
337 cred = Credential(subject=hrn)
338 cred.set_gid_caller(gid)
339 cred.set_gid_object(gid)
340 cred.set_privileges(kind)
341 cred.get_privileges().delegate_all_privileges(True)
342 #cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
344 parent_hrn = get_authority(hrn)
345 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
346 # if there is no parent hrn, then it must be self-signed. this
347 # is where we terminate the recursion
348 cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
350 # we need the parent's private key in order to sign this GID
351 parent_auth_info = self.get_auth_info(parent_hrn)
352 cred.set_issuer_keys(parent_auth_info.get_privkey_filename(), parent_auth_info.get_gid_filename())
355 cred.set_parent(self.get_auth_cred(parent_hrn, kind))
362 # Retrieve an authority ticket. An authority ticket is not actually a
363 # redeemable ticket, but only serves the purpose of being included as the
364 # parent of another ticket, in order to provide a chain of authentication
367 # This looks almost the same as get_auth_cred, but works for tickets
368 # XXX does similarity imply there should be more code re-use?
370 # @param xrn the human readable name of the authority (urn is converted to hrn)
372 def get_auth_ticket(self, xrn):
373 hrn, type = urn_to_hrn(xrn)
374 auth_info = self.get_auth_info(hrn)
375 gid = auth_info.get_gid_object()
377 ticket = SfaTicket(subject=hrn)
378 ticket.set_gid_caller(gid)
379 ticket.set_gid_object(gid)
380 ticket.set_delegate(True)
381 ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
383 parent_hrn = get_authority(hrn)
385 # if there is no parent hrn, then it must be self-signed. this
386 # is where we terminate the recursion
387 ticket.set_issuer(auth_info.get_pkey_object(), hrn)
389 # we need the parent's private key in order to sign this GID
390 parent_auth_info = self.get_auth_info(parent_hrn)
391 ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
392 ticket.set_parent(self.get_auth_cred(parent_hrn))