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
16 from sfa.util.faults import MissingAuthority
17 from sfa.util.sfalogging import logger
18 from sfa.util.xrn import get_leaf, get_authority, hrn_to_urn, urn_to_hrn
19 from sfa.trust.certificate import Keypair
20 from sfa.trust.credential import Credential
21 from sfa.trust.gid import GID, create_uuid
22 from sfa.util.config import Config
23 from sfa.trust.sfaticket import SfaTicket
26 # The AuthInfo class contains the information for an authority. This information
27 # includes the GID, private key, and database connection information.
33 privkey_filename = None
35 # Initialize and authority object.
37 # @param xrn the human readable name of the authority (urn will be converted to hrn)
38 # @param gid_filename the filename containing the GID
39 # @param privkey_filename the filename containing the private key
41 def __init__(self, xrn, gid_filename, privkey_filename):
42 hrn, type = urn_to_hrn(xrn)
44 self.set_gid_filename(gid_filename)
45 self.privkey_filename = privkey_filename
48 # Set the filename of the GID
50 # @param fn filename of file containing GID
52 def set_gid_filename(self, fn):
53 self.gid_filename = fn
54 self.gid_object = None
56 def get_privkey_filename(self):
57 return self.privkey_filename
59 def get_gid_filename(self):
60 return self.gid_filename
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 # Replace the GID with a new one. The file specified by gid_filename is
78 # overwritten with the new GID object
80 # @param gid object containing new GID
82 def update_gid_object(self, gid):
83 gid.save_to_file(self.gid_filename)
87 # The Hierarchy class is responsible for managing the tree of authorities.
88 # Each authority is a node in the tree and exists as an AuthInfo object.
90 # The tree is stored on disk in a hierarchical manner than reflects the
91 # structure of the tree. Each authority is a subdirectory, and each subdirectory
92 # contains the GID and pkey files for that authority (as well as
93 # subdirectories for each sub-authority)
97 # Create the hierarchy object.
99 # @param basedir the base directory to store the hierarchy in
101 def __init__(self, basedir = None):
102 self.config = Config()
104 basedir = os.path.join(self.config.SFA_DATA_DIR, "authorities")
105 self.basedir = basedir
107 # Given a hrn, return the filenames of the GID, private key
110 # @param xrn the human readable name of the authority (urn will be convertd to hrn)
112 def get_auth_filenames(self, xrn):
113 hrn, type = urn_to_hrn(xrn)
115 hrn = hrn.replace('\\', '')
119 parent_hrn = get_authority(hrn)
120 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
122 gid_filename = os.path.join(directory, leaf+".gid")
123 privkey_filename = os.path.join(directory, leaf+".pkey")
125 return (directory, gid_filename, privkey_filename)
128 # Check to see if an authority exists. An authority exists if it's disk
131 # @param the human readable name of the authority to check
133 def auth_exists(self, xrn):
134 hrn, type = urn_to_hrn(xrn)
135 (directory, gid_filename, privkey_filename) = \
136 self.get_auth_filenames(hrn)
138 return os.path.exists(gid_filename) and os.path.exists(privkey_filename)
141 # Create an authority. A private key for the authority and the associated
142 # GID are created and signed by the parent authority.
144 # @param xrn the human readable name of the authority to create (urn will be converted to hrn)
145 # @param create_parents if true, also create the parents if they do not exist
147 def create_auth(self, xrn, create_parents=False):
148 hrn, type = urn_to_hrn(str(xrn))
149 logger.debug("Hierarchy: creating authority: %s"% hrn)
151 # create the parent authority if necessary
152 parent_hrn = get_authority(hrn)
153 parent_urn = hrn_to_urn(parent_hrn, 'authority')
154 if (parent_hrn) and (not self.auth_exists(parent_urn)) and (create_parents):
155 self.create_auth(parent_urn, create_parents)
156 (directory, gid_filename, privkey_filename,) = \
157 self.get_auth_filenames(hrn)
159 # create the directory to hold the files
161 os.makedirs(directory)
162 # if the path already exists then pass
163 except OSError, (errno, strerr):
167 if os.path.exists(privkey_filename):
168 logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn))
169 pkey = Keypair(filename = privkey_filename)
171 pkey = Keypair(create = True)
172 pkey.save_to_file(privkey_filename)
174 gid = self.create_gid(xrn, create_uuid(), pkey)
175 gid.save_to_file(gid_filename, save_parents=True)
177 def create_top_level_auth(self, hrn=None):
179 Create top level records (includes root and sub authorities (local/remote)
181 # create the authority if it doesnt alrady exist
182 if not self.auth_exists(hrn):
183 self.create_auth(hrn, create_parents=True)
186 def get_interface_auth_info(self, create=True):
187 hrn = self.config.SFA_INTERFACE_HRN
188 if not self.auth_exists(hrn):
190 self.create_top_level_auth(hrn)
192 raise MissingAuthority(hrn)
193 return self.get_auth_info(hrn)
195 # Return the AuthInfo object for the specified authority. If the authority
196 # does not exist, then an exception is thrown. As a side effect, disk files
197 # and a subdirectory may be created to store the authority.
199 # @param xrn the human readable name of the authority to create (urn will be converted to hrn).
201 def get_auth_info(self, xrn):
202 hrn, type = urn_to_hrn(xrn)
203 if not self.auth_exists(hrn):
204 logger.warning("Hierarchy: missing authority - xrn=%s, hrn=%s"%(xrn,hrn))
205 raise MissingAuthority(hrn)
207 (directory, gid_filename, privkey_filename, ) = \
208 self.get_auth_filenames(hrn)
210 auth_info = AuthInfo(hrn, gid_filename, privkey_filename)
212 # check the GID and see if it needs to be refreshed
213 gid = auth_info.get_gid_object()
214 gid_refreshed = self.refresh_gid(gid)
215 if gid != gid_refreshed:
216 auth_info.update_gid_object(gid_refreshed)
221 # Create a new GID. The GID will be signed by the authority that is it's
222 # immediate parent in the hierarchy (and recursively, the parents' GID
223 # will be signed by its parent)
225 # @param hrn the human readable name to store in the GID
226 # @param uuid the unique identifier to store in the GID
227 # @param pkey the public key to store in the GID
229 def create_gid(self, xrn, uuid, pkey, CA=False, email=None):
230 hrn, type = urn_to_hrn(xrn)
233 parent_hrn = get_authority(hrn)
234 # Using hrn_to_urn() here to make sure the urn is in the right format
235 # If xrn was a hrn instead of a urn, then the gid's urn will be
237 urn = hrn_to_urn(hrn, type)
238 gid = GID(subject=hrn, uuid=uuid, hrn=hrn, urn=urn, email=email)
240 if hrn == self.config.SFA_INTERFACE_HRN or not parent_hrn:
241 # root or sub authority
242 gid.set_intermediate_ca(True)
243 elif type and 'authority' in type:
245 gid.set_intermediate_ca(True)
247 gid.set_intermediate_ca(True)
249 gid.set_intermediate_ca(False)
252 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
253 # if there is no parent hrn, then it must be self-signed. this
254 # is where we terminate the recursion
255 gid.set_issuer(pkey, hrn)
257 # we need the parent's private key in order to sign this GID
258 parent_auth_info = self.get_auth_info(parent_hrn)
259 gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
260 gid.set_parent(parent_auth_info.get_gid_object())
269 # Refresh a GID. The primary use of this function is to refresh the
270 # the expiration time of the GID. It may also be used to change the HRN,
271 # UUID, or Public key of the GID.
273 # @param gid the GID to refresh
274 # @param hrn if !=None, change the hrn
275 # @param uuid if !=None, change the uuid
276 # @param pubkey if !=None, change the public key
278 def refresh_gid(self, gid, xrn=None, uuid=None, pubkey=None):
279 # TODO: compute expiration time of GID, refresh it if necessary
280 gid_is_expired = False
282 # update the gid if we need to
283 if gid_is_expired or xrn or uuid or pubkey:
288 uuid = gid.get_uuid()
290 pubkey = gid.get_pubkey()
292 gid = self.create_gid(xrn, uuid, pubkey)
297 # Retrieve an authority credential for an authority. The authority
298 # credential will contain the authority privilege and will be signed by
299 # the authority's parent.
301 # @param hrn the human readable name of the authority (urn is converted to hrn)
302 # @param authority type of credential to return (authority | sa | ma)
304 def get_auth_cred(self, xrn, kind="authority"):
305 hrn, type = urn_to_hrn(xrn)
306 auth_info = self.get_auth_info(hrn)
307 gid = auth_info.get_gid_object()
309 cred = Credential(subject=hrn)
310 cred.set_gid_caller(gid)
311 cred.set_gid_object(gid)
312 cred.set_privileges(kind)
313 cred.get_privileges().delegate_all_privileges(True)
314 #cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
316 parent_hrn = get_authority(hrn)
317 if not parent_hrn or hrn == self.config.SFA_INTERFACE_HRN:
318 # if there is no parent hrn, then it must be self-signed. this
319 # is where we terminate the recursion
320 cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
322 # we need the parent's private key in order to sign this GID
323 parent_auth_info = self.get_auth_info(parent_hrn)
324 cred.set_issuer_keys(parent_auth_info.get_privkey_filename(), parent_auth_info.get_gid_filename())
327 cred.set_parent(self.get_auth_cred(parent_hrn, kind))
334 # Retrieve an authority ticket. An authority ticket is not actually a
335 # redeemable ticket, but only serves the purpose of being included as the
336 # parent of another ticket, in order to provide a chain of authentication
339 # This looks almost the same as get_auth_cred, but works for tickets
340 # XXX does similarity imply there should be more code re-use?
342 # @param xrn the human readable name of the authority (urn is converted to hrn)
344 def get_auth_ticket(self, xrn):
345 hrn, type = urn_to_hrn(xrn)
346 auth_info = self.get_auth_info(hrn)
347 gid = auth_info.get_gid_object()
349 ticket = SfaTicket(subject=hrn)
350 ticket.set_gid_caller(gid)
351 ticket.set_gid_object(gid)
352 ticket.set_delegate(True)
353 ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
355 parent_hrn = get_authority(hrn)
357 # if there is no parent hrn, then it must be self-signed. this
358 # is where we terminate the recursion
359 ticket.set_issuer(auth_info.get_pkey_object(), hrn)
361 # we need the parent's private key in order to sign this GID
362 parent_auth_info = self.get_auth_info(parent_hrn)
363 ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
364 ticket.set_parent(self.get_auth_cred(parent_hrn))