[b8a6fd2a] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | to test with python |
---|
| 5 | |
---|
| 6 | cmd1:env keystore=`pwd` ./attr.py |
---|
| 7 | |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | import os |
---|
| 11 | import ABAC |
---|
| 12 | |
---|
| 13 | ctxt = ABAC.Context() |
---|
| 14 | |
---|
| 15 | def print_p(ctxt, tt, msg): |
---|
| 16 | os.environ["ABAC_CN"]="1" |
---|
| 17 | out = ctxt.context_principals() |
---|
| 18 | print "%s principal set..." % tt |
---|
| 19 | for x in out[1]: |
---|
| 20 | print "%s%s " % (msg,x.string()) |
---|
[09531ca] | 21 | del os.environ["ABAC_CN"] |
---|
[b8a6fd2a] | 22 | |
---|
| 23 | def print_a(ctxt, tt, msg): |
---|
| 24 | os.environ["ABAC_CN"]="1" |
---|
| 25 | print "%s rule set..." % tt |
---|
| 26 | out = ctxt.context_credentials() |
---|
| 27 | for x in out[1]: |
---|
| 28 | print "%s%s " % (msg,x.string()) |
---|
[09531ca] | 29 | del os.environ["ABAC_CN"] |
---|
[b8a6fd2a] | 30 | |
---|
| 31 | ##wrint db with ABAC_CN enabled |
---|
| 32 | def print_db(ctxt): |
---|
| 33 | ctxt.dump_yap_db() |
---|
| 34 | |
---|
| 35 | # Keystore is the directory containing the principal credentials. |
---|
| 36 | # Load existing principals and/or policy credentials |
---|
| 37 | if (os.environ.has_key("keystore")) : |
---|
| 38 | keystore=os.environ["keystore"] |
---|
| 39 | else: |
---|
| 40 | print("keystore is not set...") |
---|
| 41 | exit(1) |
---|
| 42 | |
---|
| 43 | superKID=ABAC.ID("SuperK_ID.pem"); |
---|
| 44 | superKID.id_load_privkey_file("SuperK_private.pem"); |
---|
| 45 | ctxt.load_id(superKID) |
---|
| 46 | superK=superKID.id_keyid() |
---|
| 47 | |
---|
| 48 | jackID=ABAC.ID("Jack_ID.pem"); |
---|
| 49 | jackID.id_load_privkey_file("Jack_private.pem"); |
---|
| 50 | ctxt.load_id(jackID) |
---|
| 51 | jack=jackID.id_keyid() |
---|
| 52 | |
---|
| 53 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 54 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 55 | ctxt.load_id(bobID) |
---|
| 56 | bob=bobID.id_keyid() |
---|
| 57 | |
---|
| 58 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 59 | maryID.id_load_privkey_file("Mary_private.pem"); |
---|
| 60 | ctxt.load_id(maryID) |
---|
| 61 | mary=maryID.id_keyid() |
---|
| 62 | |
---|
| 63 | #case 1: |
---|
| 64 | #Only employee of SuperK can park |
---|
| 65 | #[keyid:SuperK].role:park <- [keyid:SuperK].role:employee |
---|
| 66 | head = ABAC.Role(superK,"park") |
---|
| 67 | tail = ABAC.Role(superK,"employee") |
---|
| 68 | attr=ABAC.Attribute(head, 1800) |
---|
| 69 | attr.attribute_add_tail(tail) |
---|
| 70 | attr.attribute_bake() |
---|
| 71 | attr.attribute_write_cert("SuperK_park__SuperK_employee_attr.der") |
---|
| 72 | ctxt.load_attribute_file("SuperK_park__SuperK_employee_attr.der") |
---|
| 73 | print_a(ctxt,"case1", "..") |
---|
| 74 | |
---|
| 75 | #case 2: |
---|
| 76 | #Jack is an employee of SuperK |
---|
| 77 | #[keyid:SuperK].role:employee <- [keyid:Jack] |
---|
| 78 | head = ABAC.Role(superK,"employee") |
---|
| 79 | tail = ABAC.Role(jack) |
---|
| 80 | attr=ABAC.Attribute(head, 1800) |
---|
| 81 | attr.attribute_add_tail(tail) |
---|
| 82 | attr.attribute_bake() |
---|
| 83 | # create a policy file at the file system |
---|
| 84 | attr.attribute_write_cert("SuperK_employee__Jack_attr.der") |
---|
| 85 | ctxt.load_attribute(attr); |
---|
| 86 | print_a(ctxt,"case2", "....") |
---|
| 87 | |
---|
| 88 | #case 3: |
---|
| 89 | #Bob is an employee of SuperK |
---|
| 90 | #[keyid:SuperK].role:employee <- [keyid:Jack] |
---|
| 91 | head = ABAC.Role(superK,"employee") |
---|
| 92 | tail = ABAC.Role(bob) |
---|
| 93 | attr=ABAC.Attribute(head, 1800) |
---|
| 94 | attr.attribute_add_tail(tail) |
---|
| 95 | attr.attribute_bake() |
---|
| 96 | chunk=attr.cert_chunk() |
---|
| 97 | nattr=ABAC.Attribute_chunk(chunk) |
---|
| 98 | ctxt.load_attribute(nattr); |
---|
| 99 | print_a(ctxt,"case3", "....") |
---|
| 100 | |
---|
| 101 | #case 4: |
---|
| 102 | #Mary is an employee of SuperK |
---|
| 103 | #[keyid:SuperK].role:employee <- [keyid:Mary] |
---|
| 104 | head = ABAC.Role(superK,"employee") |
---|
| 105 | tail = ABAC.Role(mary) |
---|
| 106 | attr=ABAC.Attribute(head, 1800) |
---|
| 107 | attr.attribute_add_tail(tail) |
---|
| 108 | attr.attribute_bake() |
---|
| 109 | chunk=attr.cert_chunk() |
---|
| 110 | ctxt.load_attribute_chunk(chunk); |
---|
| 111 | print_a(ctxt,"case4", "......") |
---|