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