3 # hierarchy of GENI authorities
5 # This correspond's almost identically to the functionality of Soner's
6 # "tree" module. Each component of an HRN is stored in a different subdirectory.
7 # Inside this subdirectory are:
9 # *.PKEY - private key file
10 # *.DBINFO - database info
15 from credential import *
19 from geniticket import *
25 privkey_filename = None
26 dbinfo_filename = None
28 def __init__(self, hrn, gid_filename, privkey_filename, dbinfo_filename):
30 self.set_gid_filename(gid_filename)
31 self.privkey_filename = privkey_filename
32 self.dbinfo_filename = dbinfo_filename
34 def set_gid_filename(self, fn):
35 self.gid_filename = fn
36 self.gid_object = None
38 def get_gid_object(self):
39 if not self.gid_object:
40 self.gid_object = GID(filename = self.gid_filename)
41 return self.gid_object
43 def get_pkey_object(self):
44 return Keypair(filename = self.privkey_filename)
47 f = file(self.dbinfo_filename)
48 dict = eval(f.read())
\r
52 def update_gid_object(self, gid):
\r
53 gid.save_to_file(self.gid_filename)
\r
54 self.gid_object = gid
\r
57 def __init__(self, basedir="."):
58 self.basedir = os.path.join(basedir, "authorities")
60 def get_auth_filenames(self, hrn):
62 parent_hrn = get_authority(hrn)
63 directory = os.path.join(self.basedir, hrn.replace(".", "/"))
65 gid_filename = os.path.join(directory, leaf+".gid")
66 privkey_filename = os.path.join(directory, leaf+".pkey")
67 dbinfo_filename = os.path.join(directory, leaf+".dbinfo")
69 return (directory, gid_filename, privkey_filename, dbinfo_filename)
71 def auth_exists(self, hrn):
72 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
73 self.get_auth_filenames(hrn)
75 return os.path.exists(gid_filename) and \
76 os.path.exists(privkey_filename) and \
77 os.path.exists(dbinfo_filename)
79 def create_auth(self, hrn, create_parents=False):
80 report.trace("Hierarchy: creating authority: " + hrn)
82 # create the parent authority if necessary
83 parent_hrn = get_authority(hrn)
84 if (parent_hrn) and (not self.auth_exists(parent_hrn)) and (create_parents):
85 self.create_auth(parent_hrn, create_parents)
87 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
88 self.get_auth_filenames(hrn)
90 # create the directory to hold the files
92 os.makedirs(directory)
\r
93 # if the path already exists then pass
\r
94 except OSError, (errno, strerr):
\r
98 pkey = Keypair(create = True)
99 pkey.save_to_file(privkey_filename)
101 gid = self.create_gid(hrn, create_uuid(), pkey)
102 gid.save_to_file(gid_filename, save_parents=True)
104 # XXX TODO: think up a better way for the dbinfo to work
106 dbinfo = get_default_dbinfo()
107 dbinfo_file = file(dbinfo_filename, "w")
108 dbinfo_file.write(str(dbinfo))
\r
111 def get_auth_info(self, hrn):
112 #report.trace("Hierarchy: getting authority: " + hrn)
114 if not self.auth_exists(hrn):
115 raise MissingAuthority(hrn)
117 (directory, gid_filename, privkey_filename, dbinfo_filename) = \
118 self.get_auth_filenames(hrn)
120 auth_info = AuthInfo(hrn, gid_filename, privkey_filename, dbinfo_filename)
122 # check the GID and see if it needs to be refreshed
123 gid = auth_info.get_gid_object()
124 gid_refreshed = self.refresh_gid(gid)
125 if gid != gid_refreshed:
126 auth_info.update_gid_object(gid_refreshed)
130 def create_gid(self, hrn, uuid, pkey):
131 gid = GID(subject=hrn, uuid=uuid, hrn=hrn)
133 parent_hrn = get_authority(hrn)
135 # if there is no parent hrn, then it must be self-signed. this
136 # is where we terminate the recursion
137 gid.set_issuer(pkey, hrn)
139 # we need the parent's private key in order to sign this GID
140 parent_auth_info = self.get_auth_info(parent_hrn)
141 gid.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
142 gid.set_parent(parent_auth_info.get_gid_object())
150 def refresh_gid(self, gid, hrn=None, uuid=None, pubkey=None):
151 # TODO: compute expiration time of GID, refresh it if necessary
152 gid_is_expired = False
154 # update the gid if we need to
155 if gid_is_expired or hrn or uuid or pubkey:
159 uuid = gid.get_uuid()
161 pubkey = gid.get_pubkey()
163 gid = self.create_gid(hrn, uuid, pubkey)
167 def get_auth_cred(self, hrn):
168 auth_info = self.get_auth_info(hrn)
169 gid = auth_info.get_gid_object()
171 cred = Credential(subject=hrn)
172 cred.set_gid_caller(gid)
173 cred.set_gid_object(gid)
174 cred.set_privileges("authority")
175 cred.set_delegate(True)
176 cred.set_pubkey(auth_info.get_gid_object().get_pubkey())
178 parent_hrn = get_authority(hrn)
180 # if there is no parent hrn, then it must be self-signed. this
181 # is where we terminate the recursion
182 cred.set_issuer(auth_info.get_pkey_object(), hrn)
184 # we need the parent's private key in order to sign this GID
185 parent_auth_info = self.get_auth_info(parent_hrn)
186 cred.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
187 cred.set_parent(self.get_auth_cred(parent_hrn))
194 # this looks almost the same as get_auth_cred, but works for tickets
195 # XXX does similarity imply there should be more code re-use?
196 def get_auth_ticket(self, hrn):
197 auth_info = self.get_auth_info(hrn)
198 gid = auth_info.get_gid_object()
200 ticket = Ticket(subject=hrn)
201 ticket.set_gid_caller(gid)
202 ticket.set_gid_object(gid)
203 ticket.set_delegate(True)
204 ticket.set_pubkey(auth_info.get_gid_object().get_pubkey())
206 parent_hrn = get_authority(hrn)
208 # if there is no parent hrn, then it must be self-signed. this
209 # is where we terminate the recursion
210 ticket.set_issuer(auth_info.get_pkey_object(), hrn)
212 # we need the parent's private key in order to sign this GID
213 parent_auth_info = self.get_auth_info(parent_hrn)
214 ticket.set_issuer(parent_auth_info.get_pkey_object(), parent_auth_info.hrn)
215 ticket.set_parent(self.get_auth_cred(parent_hrn))