[3f6bc5f] | 1 | #/usr/local/bin/python |
---|
| 2 | |
---|
[5c0d244] | 3 | from tempfile import mkstemp |
---|
| 4 | from subprocess import call |
---|
[3bf0b3c] | 5 | from threading import Lock |
---|
| 6 | |
---|
[dee164e] | 7 | from string import join, hexdigits |
---|
[547aa3b] | 8 | |
---|
[3f6bc5f] | 9 | from fedid import fedid |
---|
[a31b94d] | 10 | from remote_service import service_caller |
---|
| 11 | from service_error import service_error |
---|
[353db8c] | 12 | from util import abac_pem_type, abac_split_cert |
---|
[e83f2f2] | 13 | from proof import proof |
---|
[3f6bc5f] | 14 | |
---|
[3bf0b3c] | 15 | |
---|
[5c0d244] | 16 | import ABAC |
---|
| 17 | import pickle |
---|
| 18 | |
---|
[a31b94d] | 19 | import sys |
---|
[c573278] | 20 | import os, os.path |
---|
[3bf0b3c] | 21 | import re |
---|
[a31b94d] | 22 | |
---|
| 23 | class authorizer_base: |
---|
[3f6bc5f] | 24 | """ |
---|
[a31b94d] | 25 | Classes based on this one keep track of authorization attributes for the |
---|
| 26 | various modules running. This base class holds some utility functions that |
---|
| 27 | they all potentially use. |
---|
[3f6bc5f] | 28 | """ |
---|
[a31b94d] | 29 | |
---|
[3f6bc5f] | 30 | # general error exception for badly formed names. |
---|
| 31 | class bad_name(RuntimeError): pass |
---|
[5c0d244] | 32 | # difficulty creating an attribute |
---|
| 33 | class attribute_error(RuntimeError): pass |
---|
[3f6bc5f] | 34 | |
---|
| 35 | @staticmethod |
---|
| 36 | def auth_name(name): |
---|
| 37 | """ |
---|
| 38 | Helper to convert a non-unicode local name to a unicode string. Mixed |
---|
| 39 | representations can needlessly confuse the authorizer. |
---|
| 40 | """ |
---|
| 41 | if isinstance(name, basestring): |
---|
| 42 | if not isinstance(name, unicode): return unicode(name) |
---|
| 43 | else: return name |
---|
| 44 | else: return name |
---|
| 45 | |
---|
[a31b94d] | 46 | @staticmethod |
---|
| 47 | def valid_name(name): |
---|
[3f6bc5f] | 48 | """ |
---|
| 49 | Ensure that the given name is valid. A valid name can either be a |
---|
| 50 | triple of strings and fedids representing one of our generalized Emulab |
---|
| 51 | names or a single fedid. Compound names can include wildcards (None) |
---|
| 52 | and must anchor to a fedid at their highest level (unless they're all |
---|
| 53 | None) |
---|
| 54 | |
---|
| 55 | This either returns True or throws an exception. More an assertion |
---|
| 56 | than a function. |
---|
| 57 | """ |
---|
| 58 | if isinstance(name, tuple) and len(name) == 3: |
---|
| 59 | for n in name: |
---|
| 60 | if n: |
---|
| 61 | if not (isinstance(n, basestring) or isinstance(n, fedid)): |
---|
[a31b94d] | 62 | raise authorizer_base.bad_name( |
---|
| 63 | "names must be either a triple or a fedid") |
---|
[3f6bc5f] | 64 | for n in name: |
---|
| 65 | if n: |
---|
| 66 | if isinstance(n, fedid): |
---|
| 67 | return True |
---|
| 68 | else: |
---|
[a31b94d] | 69 | raise authorizer_base.bad_name( |
---|
| 70 | "Compound names must be " + \ |
---|
[3f6bc5f] | 71 | "rooted in fedids: %s" % str(name)) |
---|
| 72 | |
---|
| 73 | return True |
---|
| 74 | elif isinstance(name, fedid): |
---|
| 75 | return True |
---|
| 76 | else: |
---|
[a31b94d] | 77 | raise authorizer_base.bad_name( |
---|
| 78 | "Names must be a triple or a fedid (%s)" % name) |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | class authorizer(authorizer_base): |
---|
| 82 | """ |
---|
| 83 | This class keeps track of authorization attributes for the various modules |
---|
| 84 | running. When it gets smarter it will be the basis for a real |
---|
| 85 | attribute-based authentication system. |
---|
| 86 | """ |
---|
| 87 | def __init__(self, def_attr="testbed"): |
---|
| 88 | self.attrs = { } |
---|
| 89 | self.globals=set() |
---|
[3f6bc5f] | 90 | |
---|
| 91 | def set_attribute(self, name, attr): |
---|
| 92 | """ |
---|
| 93 | Attach attr to name. Multiple attrs can be attached. |
---|
| 94 | """ |
---|
| 95 | self.valid_name(name) |
---|
| 96 | if isinstance(name, tuple): |
---|
| 97 | aname = tuple([ self.auth_name(n) for n in name]) |
---|
| 98 | else: |
---|
| 99 | aname = self.auth_name(name) |
---|
| 100 | |
---|
| 101 | if not self.attrs.has_key(aname): |
---|
| 102 | self.attrs[aname] = set() |
---|
| 103 | self.attrs[aname].add(attr) |
---|
| 104 | |
---|
| 105 | def unset_attribute(self, name, attr): |
---|
| 106 | """ |
---|
| 107 | Remove an attribute from name |
---|
| 108 | """ |
---|
| 109 | self.valid_name(name) |
---|
| 110 | if isinstance(name, tuple): |
---|
| 111 | aname = tuple([ self.auth_name(n) for n in name]) |
---|
| 112 | else: |
---|
| 113 | aname = self.auth_name(name) |
---|
| 114 | |
---|
| 115 | attrs = self.attrs.get(aname, None) |
---|
| 116 | if attrs: attrs.discard(attr) |
---|
| 117 | |
---|
[e83f2f2] | 118 | def check_attribute(self, name, attr, with_proof=False): |
---|
[3f6bc5f] | 119 | """ |
---|
[05191a6] | 120 | Return True if name has attr (or if attr is global). Tuple names match |
---|
| 121 | any tuple name that matches all names present and has None entries in |
---|
| 122 | other fileds. For tuple names True implies that there is a matching |
---|
| 123 | tuple name with the attribute. |
---|
[3f6bc5f] | 124 | """ |
---|
| 125 | def tup(tup, i, p): |
---|
| 126 | mask = 1 << i |
---|
| 127 | if p & mask : return authorizer.auth_name(tup[i]) |
---|
| 128 | else: return None |
---|
| 129 | |
---|
| 130 | self.valid_name(name) |
---|
[05191a6] | 131 | if attr in self.globals: |
---|
[e83f2f2] | 132 | if with_proof: return True, proof("me", name, attr) |
---|
| 133 | else: return True |
---|
[3f6bc5f] | 134 | |
---|
| 135 | if isinstance(name, tuple): |
---|
| 136 | for p in range(0,8): |
---|
| 137 | lookup = ( tup(name, 0, p), tup(name,1, p), tup(name,2,p)) |
---|
| 138 | if self.attrs.has_key(lookup): |
---|
| 139 | if attr in self.attrs[lookup]: |
---|
[e83f2f2] | 140 | if with_proof: return True, proof("me", name, attr) |
---|
| 141 | else: return True |
---|
| 142 | # Drop through |
---|
| 143 | if with_proof: return False, proof("me", name, attr) |
---|
| 144 | else: return False |
---|
[3f6bc5f] | 145 | else: |
---|
[e83f2f2] | 146 | if with_proof: |
---|
| 147 | return attr in self.attrs.get(self.auth_name(name), set()), \ |
---|
| 148 | proof("me", name, attr) |
---|
| 149 | else: |
---|
| 150 | return attr in self.attrs.get(self.auth_name(name), set()) |
---|
[3f6bc5f] | 151 | |
---|
[05191a6] | 152 | def set_global_attribute(self, attr): |
---|
| 153 | """ |
---|
| 154 | Set a global attribute. All names, even those otherwise unknown to the |
---|
| 155 | authorizer have this attribute. |
---|
| 156 | """ |
---|
| 157 | self.globals.add(attr) |
---|
| 158 | |
---|
| 159 | def unset_global_attribute(self, attr): |
---|
| 160 | """ |
---|
| 161 | Remove a global attribute |
---|
| 162 | """ |
---|
| 163 | |
---|
| 164 | self.globals.discard(attr) |
---|
[3f6bc5f] | 165 | |
---|
[7206e5a] | 166 | def import_credentials(self, file_list=None, data_list=None): |
---|
| 167 | return False |
---|
| 168 | |
---|
[358e0b8] | 169 | def __str__(self): |
---|
| 170 | rv = "" |
---|
| 171 | rv += "attrs %s\n" % self.attrs |
---|
| 172 | rv += "globals %s" % self.globals |
---|
| 173 | return rv |
---|
| 174 | |
---|
[5c0d244] | 175 | def clone(self): |
---|
| 176 | rv = authorizer() |
---|
| 177 | rv.attrs = self.attrs.copy() |
---|
| 178 | rv.globals = self.globals.copy() |
---|
| 179 | return rv |
---|
| 180 | |
---|
[822d31b] | 181 | def save(self, fn=None): |
---|
| 182 | if fn: |
---|
| 183 | f = open(fn, "w") |
---|
| 184 | pickle.dump(self, f) |
---|
| 185 | f.close() |
---|
| 186 | |
---|
| 187 | def load(self, fn=None): |
---|
| 188 | if fn: |
---|
| 189 | f = open(fn, "r") |
---|
| 190 | a = pickle.load(f) |
---|
| 191 | f.close() |
---|
| 192 | self.attrs = a.attrs |
---|
| 193 | self.globals = a.globals |
---|
[5c0d244] | 194 | |
---|
| 195 | |
---|
[a31b94d] | 196 | class abac_authorizer(authorizer_base): |
---|
| 197 | """ |
---|
| 198 | Use the ABAC authorization system to make attribute decisions. |
---|
| 199 | """ |
---|
| 200 | |
---|
[7206e5a] | 201 | clean_attr_re = re.compile('[^A-Za-z0-9_]+') |
---|
[5097195] | 202 | clean_prefix_attr_re = re.compile('^_+') |
---|
[27d964d] | 203 | cred_file_re = re.compile('.*\.der$') |
---|
[09b1e9d] | 204 | bad_name = authorizer_base.bad_name |
---|
| 205 | attribute_error = authorizer_base.attribute_error |
---|
[6e63513] | 206 | class no_file_error(RuntimeError): pass |
---|
| 207 | class bad_cert_error(RuntimeError): pass |
---|
[27d964d] | 208 | |
---|
[353db8c] | 209 | def __init__(self, certs=None, me=None, key=None, load=None, save=None): |
---|
[5c0d244] | 210 | self.creddy = '/usr/local/bin/creddy' |
---|
[2628e5d] | 211 | self.globals = set() |
---|
| 212 | self.lock = Lock() |
---|
[a31b94d] | 213 | self.me = me |
---|
[353db8c] | 214 | self.save_dir = load or save |
---|
[36f642f] | 215 | self.local_files = False |
---|
[c573278] | 216 | if self.save_dir: |
---|
| 217 | self.save_dir = os.path.abspath(self.save_dir) |
---|
[353db8c] | 218 | # If the me parameter is a combination certificate, split it into the |
---|
| 219 | # abac_authorizer save directory (if any) for use with creddy. |
---|
[6e63513] | 220 | if self.me is not None and abac_pem_type(self.me) == 'both': |
---|
[353db8c] | 221 | if self.save_dir: |
---|
[e65150a] | 222 | keyfile="%s/key.pem" % self.save_dir |
---|
| 223 | certfile = "%s/cert.pem" % self.save_dir |
---|
| 224 | |
---|
| 225 | # Clear a spot for the new key and cert files. |
---|
| 226 | for fn in (keyfile, certfile): |
---|
| 227 | if os.access(fn, os.F_OK): |
---|
| 228 | os.unlink(fn) |
---|
| 229 | |
---|
| 230 | self.key, self.me = abac_split_cert(self.me, keyfile, certfile) |
---|
[36f642f] | 231 | self.local_files = True |
---|
[353db8c] | 232 | else: |
---|
[6e63513] | 233 | raise abac_authorizer.bad_cert_error("Combination " + \ |
---|
| 234 | "certificate and nowhere to split it"); |
---|
[353db8c] | 235 | else: |
---|
| 236 | self.key = key |
---|
[5c0d244] | 237 | self.context = ABAC.Context() |
---|
[3bf0b3c] | 238 | if me: |
---|
| 239 | self.fedid = fedid(file=self.me) |
---|
[547aa3b] | 240 | rv = self.context.load_id_file(self.me) |
---|
| 241 | if rv != 0: |
---|
| 242 | raise abac_authorizer.bad_name( |
---|
[8cf2c507] | 243 | 'Cannot load identity from %s' % me) |
---|
[09b1e9d] | 244 | else: |
---|
| 245 | self.fedid = None |
---|
[3bf0b3c] | 246 | |
---|
[2628e5d] | 247 | if isinstance(certs, basestring): |
---|
| 248 | certs = [ certs ] |
---|
| 249 | |
---|
[5c0d244] | 250 | for dir in certs or []: |
---|
| 251 | self.context.load_directory(dir) |
---|
| 252 | |
---|
[7206e5a] | 253 | if load: |
---|
| 254 | self.load(load) |
---|
[2628e5d] | 255 | |
---|
[8cf2c507] | 256 | # Modify the pickling operations so that the context and lock are not |
---|
| 257 | # pickled |
---|
| 258 | |
---|
| 259 | def __getstate__(self): |
---|
| 260 | d = self.__dict__.copy() |
---|
| 261 | del d['lock'] |
---|
| 262 | del d['context'] |
---|
| 263 | return d |
---|
| 264 | |
---|
| 265 | def __setstate__(self, d): |
---|
| 266 | # Import everything from the pickle dict (except what we excluded in |
---|
| 267 | # __getstate__) |
---|
| 268 | self.__dict__.update(d) |
---|
| 269 | # Initialize the unpicklables |
---|
| 270 | self.context = ABAC.Context() |
---|
| 271 | self.lock = Lock() |
---|
| 272 | |
---|
[27d964d] | 273 | @staticmethod |
---|
| 274 | def clean_attr(attr): |
---|
[5097195] | 275 | a = abac_authorizer.clean_attr_re.sub('_', attr) |
---|
| 276 | return abac_authorizer.clean_prefix_attr_re.sub('', a) |
---|
[27d964d] | 277 | |
---|
[dee164e] | 278 | |
---|
[7206e5a] | 279 | def import_credentials(self, file_list=None, data_list=None): |
---|
| 280 | if data_list: |
---|
| 281 | return any([self.import_credential(data=d) for d in data_list]) |
---|
| 282 | elif file_list: |
---|
| 283 | return any([self.import_credential(file=f) for f in file_list]) |
---|
| 284 | else: |
---|
| 285 | return False |
---|
| 286 | |
---|
| 287 | def import_credential(self, file=None, data=None): |
---|
| 288 | if data: |
---|
[725c55d] | 289 | if self.context.load_id_chunk(data) != ABAC.ABAC_CERT_SUCCESS: |
---|
| 290 | return self.context.load_attribute_chunk(data) == \ |
---|
| 291 | ABAC.ABAC_CERT_SUCCESS |
---|
| 292 | else: |
---|
| 293 | return True |
---|
[7206e5a] | 294 | elif file: |
---|
| 295 | if self.context.load_id_file(file) != ABAC.ABAC_CERT_SUCCESS: |
---|
| 296 | return self.context.load_attribute_file(file) == \ |
---|
| 297 | ABAC.ABAC_CERT_SUCCESS |
---|
| 298 | else: |
---|
| 299 | return True |
---|
| 300 | else: |
---|
| 301 | return False |
---|
| 302 | |
---|
[5c0d244] | 303 | def set_attribute(self, name=None, attr=None, cert=None): |
---|
| 304 | if name and attr: |
---|
| 305 | if isinstance(name, tuple): |
---|
[09b1e9d] | 306 | raise abac_authorizer.bad_name( |
---|
| 307 | "ABAC doesn't understand three-names") |
---|
[7206e5a] | 308 | # Convert non-string attributes to strings |
---|
| 309 | if not isinstance(attr, basestring): |
---|
| 310 | attr = "%s" % attr |
---|
[dee164e] | 311 | |
---|
[5c0d244] | 312 | if self.me and self.key: |
---|
| 313 | # Create a credential and insert it into context |
---|
| 314 | # This will simplify when we have libcreddy |
---|
| 315 | try: |
---|
| 316 | # create temp file |
---|
| 317 | f, fn = mkstemp() |
---|
[3bf0b3c] | 318 | os.close(f) |
---|
[5c0d244] | 319 | except EnvironmentError, e: |
---|
[09b1e9d] | 320 | raise abac_authorizer.attribute_error( |
---|
[5c0d244] | 321 | "Cannot create temp file: %s" %e) |
---|
| 322 | |
---|
| 323 | # Create the attribute certificate with creddy |
---|
[3bf0b3c] | 324 | cmd = [self.creddy, '--attribute', '--issuer=%s' % self.me, |
---|
[27d964d] | 325 | '--key=%s' % self.key, '--role=%s' % self.clean_attr(attr), |
---|
[3bf0b3c] | 326 | '--subject-id=%s' % name, '--out=%s' % fn] |
---|
| 327 | rv = call(cmd) |
---|
[5c0d244] | 328 | if rv == 0: |
---|
[3bf0b3c] | 329 | self.lock.acquire() |
---|
[5c0d244] | 330 | # load it to context and remove the file |
---|
[3bf0b3c] | 331 | rv = self.context.load_attribute_file(fn) |
---|
| 332 | self.lock.release() |
---|
[5c0d244] | 333 | os.unlink(fn) |
---|
| 334 | else: |
---|
| 335 | os.unlink(fn) |
---|
[09b1e9d] | 336 | raise abac_authorizer.attribute_error( |
---|
| 337 | "creddy returned %s" % rv) |
---|
[a31b94d] | 338 | else: |
---|
[09b1e9d] | 339 | raise abac_authorizer.attribute_error( |
---|
[5c0d244] | 340 | "Identity and key not specified on creation") |
---|
| 341 | elif cert: |
---|
| 342 | # Insert this credential into the context |
---|
[3bf0b3c] | 343 | self.lock.acquire() |
---|
[5c0d244] | 344 | self.context.load_attribute_chunk(cert) |
---|
[3bf0b3c] | 345 | self.lock.release() |
---|
[a31b94d] | 346 | else: |
---|
[09b1e9d] | 347 | raise abac_authorizer.attribute_error( |
---|
| 348 | "Neither name/attr nor cert is set") |
---|
[a31b94d] | 349 | |
---|
[1fc09db] | 350 | def unset_attribute(self, name, attr): |
---|
| 351 | if isinstance(name, tuple): |
---|
[09b1e9d] | 352 | raise abac_authorizer.bad_name( |
---|
| 353 | "ABAC doesn't understand three-names") |
---|
[7206e5a] | 354 | # Convert non-string attributes to strings |
---|
| 355 | if not isinstance(attr, basestring): |
---|
| 356 | attr = "%s" % attr |
---|
[27d964d] | 357 | cattr = self.clean_attr(attr) |
---|
[1fc09db] | 358 | self.lock.acquire() |
---|
| 359 | ctxt = ABAC.Context() |
---|
| 360 | ids = set() |
---|
| 361 | for c in self.context.credentials(): |
---|
| 362 | h = c.head() |
---|
| 363 | t = c.tail() |
---|
| 364 | if h.is_role() and t.is_principal(): |
---|
| 365 | if t.principal() == '%s' % name and \ |
---|
| 366 | h.principal() == '%s' % self.fedid and \ |
---|
[27d964d] | 367 | h.role_name() == cattr: |
---|
[1fc09db] | 368 | continue |
---|
| 369 | |
---|
| 370 | id = c.issuer_cert() |
---|
| 371 | if id not in ids: |
---|
| 372 | ctxt.load_id_chunk(id) |
---|
| 373 | ids.add(id) |
---|
| 374 | ctxt.load_attribute_chunk(c.attribute_cert()) |
---|
| 375 | self.context = ctxt |
---|
| 376 | self.lock.release() |
---|
| 377 | |
---|
[dee164e] | 378 | @staticmethod |
---|
| 379 | def starts_with_fedid(attr): |
---|
| 380 | """ |
---|
| 381 | Return true if the first 40 characters of the string are hex digits |
---|
| 382 | followed by a dot. False otherwise. Used in check_attribute. |
---|
| 383 | """ |
---|
| 384 | if attr.find('.') == 40: |
---|
| 385 | return all([ x in hexdigits for x in attr[0:40]]) |
---|
| 386 | else: |
---|
| 387 | return False |
---|
| 388 | |
---|
[1fc09db] | 389 | |
---|
[e83f2f2] | 390 | def check_attribute(self, name, attr, with_proof=False): |
---|
[a31b94d] | 391 | if isinstance(name, tuple): |
---|
[09b1e9d] | 392 | raise abac_authorizer.bad_name( |
---|
| 393 | "ABAC doesn't understand three-names") |
---|
[5c0d244] | 394 | else: |
---|
[7206e5a] | 395 | # Convert non-string attributes to strings |
---|
| 396 | if not isinstance(attr, basestring): |
---|
| 397 | attr = "%s" % attr |
---|
[dee164e] | 398 | # Attributes that start with a fedid only have the part of the |
---|
| 399 | # attribute after the dot cleaned. Others are completely cleaned |
---|
| 400 | # and have the owner fedid attached. |
---|
| 401 | if self.starts_with_fedid(attr): |
---|
[27d964d] | 402 | r, a = attr.split('.',1) |
---|
[25f66c3] | 403 | a = "%s.%s" % ( r, self.clean_attr(a)) |
---|
[dee164e] | 404 | else: |
---|
| 405 | a = "%s.%s" % (self.fedid, self.clean_attr(attr)) |
---|
| 406 | |
---|
| 407 | a = str(a) |
---|
| 408 | n = str("%s" % name) |
---|
[3bf0b3c] | 409 | |
---|
| 410 | self.lock.acquire() |
---|
[dee164e] | 411 | # Sigh. Unicode vs swig and swig seems to lose. Make sure |
---|
| 412 | # everything we pass into ABAC is a str not a unicode. |
---|
[e83f2f2] | 413 | rv, p = self.context.query(a, n) |
---|
[5c0d244] | 414 | # XXX delete soon |
---|
[e83f2f2] | 415 | if not rv and attr in self.globals: |
---|
| 416 | rv = True |
---|
| 417 | p = None |
---|
[3bf0b3c] | 418 | self.lock.release() |
---|
[e83f2f2] | 419 | if with_proof: return rv, proof(self.fedid, name, a, p) |
---|
| 420 | else: return rv |
---|
[a31b94d] | 421 | |
---|
| 422 | def set_global_attribute(self, attr): |
---|
| 423 | """ |
---|
| 424 | Set a global attribute. All names, even those otherwise unknown to the |
---|
| 425 | authorizer have this attribute. |
---|
| 426 | """ |
---|
[3bf0b3c] | 427 | self.lock.acquire() |
---|
[27d964d] | 428 | self.globals.add(self.clean_attr(attr)) |
---|
[3bf0b3c] | 429 | self.lock.release() |
---|
[a31b94d] | 430 | |
---|
| 431 | def unset_global_attribute(self, attr): |
---|
| 432 | """ |
---|
| 433 | Remove a global attribute |
---|
| 434 | """ |
---|
| 435 | |
---|
[3bf0b3c] | 436 | self.lock.acquire() |
---|
[27d964d] | 437 | self.globals.discard(self.clean_attr(attr)) |
---|
[3bf0b3c] | 438 | self.lock.release() |
---|
[a31b94d] | 439 | |
---|
[5c0d244] | 440 | def clone(self): |
---|
[3bf0b3c] | 441 | self.lock.acquire() |
---|
[5c0d244] | 442 | rv = abac_authorizer(me=self.me, key=self.key) |
---|
| 443 | rv.globals = self.globals.copy() |
---|
| 444 | rv.context = ABAC.Context(self.context) |
---|
[3bf0b3c] | 445 | self.lock.release() |
---|
[5c0d244] | 446 | return rv |
---|
| 447 | |
---|
[36f642f] | 448 | def copy_file(self, src, dest, mode=0600): |
---|
| 449 | ''' |
---|
| 450 | Copy src to dest with file mode mode. May raise exceptions on file ops |
---|
| 451 | ''' |
---|
| 452 | d = open(dest, 'w') |
---|
| 453 | s = open(src, 'r') |
---|
| 454 | d.write(s.read()) |
---|
| 455 | s.close() |
---|
| 456 | d.close() |
---|
| 457 | os.chmod(dest, mode) |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | |
---|
[7206e5a] | 461 | def save(self, dir=None): |
---|
[3bf0b3c] | 462 | self.lock.acquire() |
---|
[7206e5a] | 463 | if dir: |
---|
[c573278] | 464 | self.save_dir = os.path.abspath(dir) |
---|
[7206e5a] | 465 | else: |
---|
| 466 | dir = self.save_dir |
---|
| 467 | if dir is None: |
---|
| 468 | self.lock.release() |
---|
| 469 | raise abac_authorizer.no_file_error("No load directory specified") |
---|
[3bf0b3c] | 470 | try: |
---|
| 471 | if not os.access(dir, os.F_OK): |
---|
| 472 | os.mkdir(dir) |
---|
[09b1e9d] | 473 | |
---|
[36f642f] | 474 | # if self.key and self.me were split, copy the split files into the |
---|
| 475 | # new directory. |
---|
| 476 | if self.local_files: |
---|
| 477 | self.copy_file(self.key, '%s/key.pem' % dir) |
---|
| 478 | self.copy_file(self.me, '%s/cert.pem' % dir) |
---|
| 479 | # Point the key and me members to the new locations for |
---|
| 480 | # pickling |
---|
| 481 | hold_key = self.key |
---|
| 482 | hold_me = self.me |
---|
| 483 | self.key = '%s/key.pem' % dir |
---|
| 484 | self.me = '%s/cert.pem' % dir |
---|
| 485 | |
---|
[09b1e9d] | 486 | f = open("%s/state" % dir, "w") |
---|
| 487 | pickle.dump(self, f) |
---|
[5c0d244] | 488 | f.close() |
---|
[3bf0b3c] | 489 | |
---|
[36f642f] | 490 | if self.local_files: |
---|
| 491 | self.key = hold_key |
---|
| 492 | self.me = hold_me |
---|
| 493 | |
---|
[3bf0b3c] | 494 | if not os.access("%s/certs" %dir, os.F_OK): |
---|
| 495 | os.mkdir("%s/certs" % dir) |
---|
[09b1e9d] | 496 | |
---|
[8cf2c507] | 497 | # Clear the certs subdir |
---|
[3bf0b3c] | 498 | for fn in [ f for f in os.listdir("%s/certs" % dir) \ |
---|
[27d964d] | 499 | if abac_authorizer.cred_file_re.match(f)]: |
---|
[3bf0b3c] | 500 | os.unlink('%s/certs/%s' % (dir, fn)) |
---|
[8cf2c507] | 501 | |
---|
| 502 | # Save the context |
---|
[3bf0b3c] | 503 | ii = 0 |
---|
| 504 | ai = 0 |
---|
[8cf2c507] | 505 | seenid = set() |
---|
| 506 | seenattr = set() |
---|
[3bf0b3c] | 507 | for c in self.context.credentials(): |
---|
| 508 | id = c.issuer_cert() |
---|
| 509 | attr = c.attribute_cert() |
---|
| 510 | # NB: file naming conventions matter here. The trailing_ID and |
---|
| 511 | # _attr are required by ABAC.COntext.load_directory() |
---|
[7206e5a] | 512 | if id and id not in seenid: |
---|
[3bf0b3c] | 513 | f = open("%s/certs/ID_%03d_ID.der" % (dir, ii), "w") |
---|
[7206e5a] | 514 | f.write(id) |
---|
[3bf0b3c] | 515 | f.close() |
---|
| 516 | ii += 1 |
---|
[7206e5a] | 517 | seenid.add(id) |
---|
| 518 | if attr and attr not in seenattr: |
---|
[3bf0b3c] | 519 | f = open("%s/certs/attr_%03d_attr.der" % (dir, ai), "w") |
---|
[7206e5a] | 520 | f.write(attr) |
---|
[3bf0b3c] | 521 | f.close() |
---|
| 522 | ai += 1 |
---|
[7206e5a] | 523 | seenattr.add(attr) |
---|
[3bf0b3c] | 524 | except EnvironmentError, e: |
---|
[8cf2c507] | 525 | self.lock.release() |
---|
[3bf0b3c] | 526 | raise e |
---|
| 527 | except pickle.PickleError, e: |
---|
[8cf2c507] | 528 | self.lock.release() |
---|
[3bf0b3c] | 529 | raise e |
---|
| 530 | self.lock.release() |
---|
[5c0d244] | 531 | |
---|
[7206e5a] | 532 | def load(self, dir=None): |
---|
[3bf0b3c] | 533 | self.lock.acquire() |
---|
[7206e5a] | 534 | if dir: |
---|
| 535 | self.save_dir = dir |
---|
| 536 | else: |
---|
| 537 | dir = self.save_dir |
---|
| 538 | if dir is None: |
---|
| 539 | self.lock.release() |
---|
| 540 | raise abac_authorizer.no_file_error("No load directory specified") |
---|
[3bf0b3c] | 541 | try: |
---|
[09b1e9d] | 542 | if os.access("%s/state" % dir, os.R_OK): |
---|
| 543 | f = open("%s/state" % dir, "r") |
---|
| 544 | st = pickle.load(f) |
---|
[3bf0b3c] | 545 | f.close() |
---|
[353db8c] | 546 | # Copy the useful attributes from the pickled state |
---|
[09b1e9d] | 547 | for a in ('globals', 'key', 'me', 'cert', 'fedid'): |
---|
| 548 | setattr(self, a, getattr(st, a, None)) |
---|
| 549 | |
---|
| 550 | # Initialize the new context with the new identity |
---|
[3bf0b3c] | 551 | self.context = ABAC.Context() |
---|
[09b1e9d] | 552 | if self.me: |
---|
| 553 | self.context.load_id_file(self.me) |
---|
[3bf0b3c] | 554 | self.context.load_directory("%s/certs" % dir) |
---|
[7206e5a] | 555 | self.save_dir = dir |
---|
[3bf0b3c] | 556 | except EnvironmentError, e: |
---|
| 557 | self.lock.release() |
---|
| 558 | raise e |
---|
| 559 | except pickle.PickleError, e: |
---|
| 560 | self.lock.release() |
---|
| 561 | raise e |
---|
| 562 | self.lock.release() |
---|
| 563 | |
---|
[547aa3b] | 564 | @staticmethod |
---|
| 565 | def encode_credential(c): |
---|
| 566 | return '%s <- %s' % (c.head().string(), c.tail().string()) |
---|
| 567 | |
---|
| 568 | def get_creds_for_principal(self, fid): |
---|
| 569 | look_for = set(["%s" % fid]) |
---|
| 570 | found_attrs = set() |
---|
| 571 | next_look = set() |
---|
| 572 | found = set([]) |
---|
| 573 | |
---|
| 574 | self.lock.acquire() |
---|
| 575 | while look_for: |
---|
| 576 | for c in self.context.credentials(): |
---|
| 577 | tail = c.tail() |
---|
| 578 | # XXX: This needs to be more aggressive for linked stuff |
---|
| 579 | if tail.string() in look_for and c not in found: |
---|
| 580 | found.add(c) |
---|
| 581 | next_look.add(c.head().string()) |
---|
| 582 | |
---|
| 583 | look_for = next_look |
---|
| 584 | next_look = set() |
---|
[c573278] | 585 | self.lock.release() |
---|
[547aa3b] | 586 | |
---|
| 587 | return found |
---|
| 588 | |
---|
[3bf0b3c] | 589 | def __str__(self): |
---|
| 590 | |
---|
| 591 | self.lock.acquire() |
---|
| 592 | rv = "%s" % self.fedid |
---|
[85f5d11] | 593 | add = join([abac_authorizer.encode_credential(c) |
---|
[547aa3b] | 594 | for c in self.context.credentials()], '\n'); |
---|
[85f5d11] | 595 | if add: rv += "\n%s" % add |
---|
[3bf0b3c] | 596 | self.lock.release() |
---|
| 597 | return rv |
---|
[5c0d244] | 598 | |
---|