[373bf68] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Run the queries described in README |
---|
| 5 | |
---|
| 6 | cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py |
---|
| 7 | |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | import os |
---|
| 11 | import ABAC |
---|
| 12 | |
---|
| 13 | ctxt = ABAC.Context() |
---|
| 14 | |
---|
| 15 | # Keystore is the directory containing the principal credentials. |
---|
| 16 | # Load existing principals and/or policy credentials |
---|
| 17 | if (os.environ.has_key("keystore")) : |
---|
| 18 | keystore=os.environ["keystore"] |
---|
| 19 | ctxt.load_directory(keystore) |
---|
| 20 | else: |
---|
| 21 | print("keystore is not set...") |
---|
| 22 | exit(1) |
---|
| 23 | |
---|
| 24 | ########################################################################## |
---|
| 25 | # dump the loaded principals/policies |
---|
| 26 | # |
---|
| 27 | |
---|
| 28 | fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT) |
---|
| 29 | out = ctxt.context_principals() |
---|
| 30 | for x in out[1]: |
---|
| 31 | os.write(fd, x.string()) |
---|
| 32 | os.write(fd,"\n") |
---|
| 33 | out = ctxt.context_credentials() |
---|
| 34 | for c in out[1]: |
---|
| 35 | string="%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 36 | os.write(fd,string) |
---|
| 37 | os.write(fd,"\n") |
---|
| 38 | os.close(fd) |
---|
| 39 | |
---|
| 40 | ########################################################################## |
---|
| 41 | # Does JohnX likes John0 ? |
---|
| 42 | # role = [keyid:JohnX].role:after |
---|
| 43 | # p [Keyid:john0] |
---|
| 44 | aid="John%s_ID.pem"% #VAL# |
---|
| 45 | aID=ABAC.ID(aid) |
---|
| 46 | a=aID.id_keyid() |
---|
| 47 | |
---|
| 48 | bID=ABAC.ID("John0_ID.pem") |
---|
| 49 | b=bID.id_keyid() |
---|
| 50 | |
---|
| 51 | role = ABAC.Role(a,"likes") |
---|
| 52 | p = ABAC.Role(b) |
---|
| 53 | |
---|
| 54 | print "\n===good============ johnN.likes <- john0 " |
---|
| 55 | out = ctxt.query(role, p) |
---|
| 56 | for c in out[1]: |
---|
| 57 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 58 | |
---|