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
20 from sfa.util.sfalogging import sfa_logger
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
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 xrn the human readable name of the authority (urn will be converted to hrn)
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, xrn, gid_filename, privkey_filename, dbinfo_filename):
48 hrn, type = urn_to_hrn(xrn)
50 self.set_gid_filename(gid_filename)
51 self.privkey_filename = privkey_filename
52 self.dbinfo_filename = dbinfo_filename
55 # Set the filename of the GID
57 # @param fn filename of file containing GID
59 def set_gid_filename(self, fn):
60 self.gid_filename = fn
61 self.gid_object = None
63 def get_privkey_filename(self):
64 return self.privkey_filename
66 def get_gid_filename(self):
67 return self.gid_filename
70 # Get the GID in the form of a GID object
72 def get_gid_object(self):
73 if not self.gid_object:
74 self.gid_object = GID(filename = self.gid_filename)
75 return self.gid_object
78 # Get the private key in the form of a Keypair object
80 def get_pkey_object(self):
81 return Keypair(filename = self.privkey_filename)
84 # Get the dbinfo in the form of a dictionary
87 f = file(self.dbinfo_filename)
93 # Replace the GID with a new one. The file specified by gid_filename is
94 # overwritten with the new GID object
96 # @param gid object containing new GID
98 def update_gid_object(self, gid):
99 gid.save_to_file(self.gid_filename)
100 self.gid_object = gid
103 # The Hierarchy class is responsible for managing the tree of authorities.
104 # Each authority is a node in the tree and exists as an AuthInfo object.
106 # The tree is stored on disk in a hierarchical manner than reflects the
107 # structure of the tree. Each authority is a subdirectory, and each subdirectory
108 # contains the GID, pkey, and dbinfo files for that authority (as well as
109 # subdirectories for each sub-authority)
113 # Create the hierarchy object.
115 # @param basedir the base directory to store the hierarchy in
117 def __init__(self, basedir = None):
119 self.config = Config()
120 basedir = os.path.join(self.config.SFA_DATA_DIR, "authorities")
121 self.basedir = basedir
123 # Given a hrn, return the filenames of the GID, private key, and dbinfo
126 # @param xrn the human readable name of the authority (urn will be convertd to hrn)
128 def get_auth_filenames(self, xrn):
129 hrn, type = urn_to_hrn(xrn)
131 parent_hrn = get_authority(hrn)
132 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
134 gid_filename = os.path.join(directory, leaf+".gid")
135 privkey_filename = os.path.join(directory, leaf+".pkey")
136 dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
138 return (directory, gid_filename, privkey_filename, dbinfo_filename)
141 # Check to see if an authority exists. An authority exists if it's disk
144 # @param the human readable name of the authority to check
146 def auth_exists(self, xrn):
147 hrn, type = urn_to_hrn(xrn)
148 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
149 self.get_auth_filenames(hrn)
151 return os.path.exists(gid_filename) and \
152 os.path.exists(privkey_filename) and \
153 os.path.exists(dbinfo_filename)
156 # Create an authority. A private key for the authority and the associated
157 # GID are created and signed by the parent authority.
159 # @param xrn the human readable name of the authority to create (urn will be converted to hrn)
160 # @param create_parents if true, also create the parents if they do not exist
162 def create_auth(self, xrn, create_parents=False):
163 hrn, type = urn_to_hrn(xrn)
164 sfa_logger.debug("Hierarchy: creating authority: " + hrn)
166 # create the parent authority if necessary
167 parent_hrn = get_authority(hrn)
168 parent_urn = hrn_to_urn(parent_hrn, 'authority')
169 if (parent_hrn) and (not self.auth_exists(parent_urn)) and (create_parents):
170 self.create_auth(parent_urn, create_parents)
172 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
173 self.get_auth_filenames(hrn)
175 # create the directory to hold the files
177 os.makedirs(directory)
178 # if the path already exists then pass
179 except OSError, (errno, strerr):
183 if os.path.exists(privkey_filename):
184 sfa_logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn))
185 pkey = Keypair(filename = privkey_filename)
187 pkey = Keypair(create = True)
188 pkey.save_to_file(privkey_filename)
190 gid = self.create_gid(xrn, create_uuid(), pkey)
191 gid.save_to_file(gid_filename, save_parents=True)
193 # XXX TODO: think up a better way for the dbinfo to work
195 dbinfo = Config().get_plc_dbinfo()
196 dbinfo_file = file(dbinfo_filename, "w")
197 dbinfo_file.write(str(dbinfo))
201 # Return the AuthInfo object for the specified authority. If the authority
202 # does not exist, then an exception is thrown. As a side effect, disk files
203 # and a subdirectory may be created to store the authority.
205 # @param xrn the human readable name of the authority to create (urn will be converted to hrn).
207 def get_auth_info(self, xrn):
208 hrn, type = urn_to_hrn(xrn)
209 sfa_logger.debug("Hierarchy: getting authority: " + hrn)
210 if not self.auth_exists(hrn):
211 raise MissingAuthority(hrn)
213 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
214 self.get_auth_filenames(hrn)
216 auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
218 # check the GID and see if it needs to be refreshed
219 gid = auth_info.get_gid_object()
220 gid_refreshed = self.refresh_gid(gid)
221 if gid != gid_refreshed:
222 auth_info.update_gid_object(gid_refreshed)
227 # Create a new GID. The GID will be signed by the authority that is it's
228 # immediate parent in the hierarchy (and recursively, the parents' GID
229 # will be signed by its parent)
231 # @param hrn the human readable name to store in the GID
232 # @param uuid the unique identifier to store in the GID
233 # @param pkey the public key to store in the GID
235 def create_gid(self, xrn, uuid, pkey):
236 hrn, type = urn_to_hrn(xrn)
237 # Using hrn_to_urn() here to make sure the urn is in the right format
238 # If xrn was a hrn instead of a urn, then the gid's urn will be
240 urn = hrn_to_urn(hrn, type)
241 gid = GID(subject=hrn, uuid=uuid, hrn=hrn, urn=urn)
243 parent_hrn = get_authority(hrn)
244 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
245 # if there is no parent hrn, then it must be self-signed. this
246 # is where we terminate the recursion
247 gid.set_issuer(pkey, hrn)
249 # we need the parent's private key in order to sign this GID
250 parent_auth_info = self.get_auth_info(parent_hrn)
251 gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
252 gid.set_parent(parent_auth_info.get_gid_object())
253 gid.set_intermediate_ca(True)
262 # Refresh a GID. The primary use of this function is to refresh the
263 # the expiration time of the GID. It may also be used to change the HRN,
264 # UUID, or Public key of the GID.
266 # @param gid the GID to refresh
267 # @param hrn if !=None, change the hrn
268 # @param uuid if !=None, change the uuid
269 # @param pubkey if !=None, change the public key
271 def refresh_gid(self, gid, xrn=None, uuid=None, pubkey=None):
272 # TODO: compute expiration time of GID, refresh it if necessary
273 gid_is_expired = False
275 # update the gid if we need to
276 if gid_is_expired or xrn or uuid or pubkey:
281 uuid = gid.get_uuid()
283 pubkey = gid.get_pubkey()
285 gid = self.create_gid(xrn, uuid, pubkey)
290 # Retrieve an authority credential for an authority. The authority
291 # credential will contain the authority privilege and will be signed by
292 # the authority's parent.
294 # @param hrn the human readable name of the authority (urn is converted to hrn)
295 # @param authority type of credential to return (authority | sa | ma)
297 def get_auth_cred(self, xrn, kind="authority"):
298 hrn, type = urn_to_hrn(xrn)
299 auth_info = self.get_auth_info(hrn)
300 gid = auth_info.get_gid_object()
302 cred = Credential(subject=hrn)
303 cred.set_gid_caller(gid)
304 cred.set_gid_object(gid)
305 cred.set_privileges(kind)
306 cred.get_privileges().delegate_all_privileges(True)
307 #cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
309 parent_hrn = get_authority(hrn)
310 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
311 # if there is no parent hrn, then it must be self-signed. this
312 # is where we terminate the recursion
313 cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
315 # we need the parent's private key in order to sign this GID
316 parent_auth_info = self.get_auth_info(parent_hrn)
317 cred.set_issuer_keys(parent_auth_info.get_privkey_filename(), parent_auth_info.get_gid_filename())
320 cred.set_parent(self.get_auth_cred(parent_hrn, kind))
327 # Retrieve an authority ticket. An authority ticket is not actually a
328 # redeemable ticket, but only serves the purpose of being included as the
329 # parent of another ticket, in order to provide a chain of authentication
332 # This looks almost the same as get_auth_cred, but works for tickets
333 # XXX does similarity imply there should be more code re-use?
335 # @param xrn the human readable name of the authority (urn is converted to hrn)
337 def get_auth_ticket(self, xrn):
338 hrn, type = urn_to_hrn(xrn)
339 auth_info = self.get_auth_info(hrn)
340 gid = auth_info.get_gid_object()
342 ticket = SfaTicket(subject=hrn)
343 ticket.set_gid_caller(gid)
344 ticket.set_gid_object(gid)
345 ticket.set_delegate(True)
346 ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
348 parent_hrn = get_authority(hrn)
350 # if there is no parent hrn, then it must be self-signed. this
351 # is where we terminate the recursion
352 ticket.set_issuer(auth_info.get_pkey_object(), hrn)
354 # we need the parent's private key in order to sign this GID
355 parent_auth_info = self.get_auth_info(parent_hrn)
356 ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
357 ticket.set_parent(self.get_auth_cred(parent_hrn))