#!/usr/bin/env python """ Run the queries described in README cmd1:env keystore=`pwd` ./query.py cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py """ import os import ABAC ctxt = ABAC.Context() # Keystore is the directory containing the principal credentials. # Load existing principals and/or policy credentials if (os.environ.has_key("keystore")) : keystore=os.environ["keystore"] ctxt.load_directory(keystore) else: print("keystore is not set...") exit(1) # retrieve principals' keyid value from local credential files acmeID=ABAC.ID("Acme_ID.pem"); acmeID.id_load_privkey_file("Acme_private.pem"); acme=acmeID.id_keyid() coyoteID=ABAC.ID("Coyote_ID.pem"); coyoteID.id_load_privkey_file("Coyote_private.pem"); coyote=coyoteID.id_keyid() ########################################################################## # dump the loaded principals/policies # out = ctxt.context_principals() print "\n...final principal set..." for x in out[1]: print "%s " % x.string() out = ctxt.context_credentials() print "\n...final policy attribute set..." for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string()) ########################################################################## # is coyote a preferred_customer of Acme ? # role=[keyid:Acme].role:preferred_customer # p =[keyid:coyote] role = ABAC.Role(acme,"preferred_customer") p = ABAC.Role(coyote) print "\n===good============ Acme.preferred_customer <- Coyote" out = ctxt.query(role, p) for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string()) ########################################################################## # can coyote buy rockets from Acme ? # role=[keyid:Acme].role:buy_rockets # p =[keyid:coyote] role = ABAC.Role(acme,"buy_rockets") p = ABAC.Role(coyote) print "\n===good============ Acme.buy_rockets <- Coyote" out = ctxt.query(role, p) for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string()) ########################################################################## # is Acme a friend of coyote ? # role=[keyid:Coyote].role:friend # p=[keyid:Acme] role = ABAC.Role(coyote,"friend") p = ABAC.Role(acme) print "\n===bad=============== Coyote.friend <- Acme" out = ctxt.query(role, p) for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string()) ########################################################################## # using complex role to ask a question.. expecting to fail # role=[keyid:Acme].role:buy_rockets # p=[keyid:Acme].role:preferred_customer role = ABAC.Role(acme,"buy_rockets") p = ABAC.Role(acme,"preferred_customer") print "\n===bad=============== Acme.buy_rockets <- Acme.preferred_customer" out = ctxt.query(role, p) for c in out[1]: print "%s <- %s" % (c.head_string(), c.tail_string())