[be6cb41] | 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_a(ctxt, msg, dotdot): |
---|
| 16 | print "%s rule set..." % msg |
---|
| 17 | credentials = ctxt.credentials() |
---|
| 18 | for credential in credentials: |
---|
| 19 | print "%s:%s<-%s" % (dotdot,credential.head().string(), credential.tail().string()) |
---|
| 20 | |
---|
| 21 | # Keystore is the directory containing the principal credentials. |
---|
| 22 | # Load existing principals and/or policy credentials |
---|
| 23 | if (os.environ.has_key("keystore")) : |
---|
| 24 | keystore=os.environ["keystore"] |
---|
| 25 | else: |
---|
| 26 | print("keystore is not set...") |
---|
| 27 | exit(1) |
---|
| 28 | |
---|
| 29 | superKID=ABAC.ID("SuperK_ID.pem"); |
---|
| 30 | superKID.load_privkey("SuperK_private.pem"); |
---|
| 31 | ctxt.load_id_chunk(superKID.cert_chunk()) |
---|
| 32 | superK=superKID.keyid() |
---|
| 33 | |
---|
| 34 | jackID=ABAC.ID("Jack_ID.pem"); |
---|
| 35 | jackID.load_privkey("Jack_private.pem"); |
---|
| 36 | ctxt.load_id_chunk(jackID.cert_chunk()) |
---|
| 37 | jack=jackID.keyid() |
---|
| 38 | |
---|
| 39 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 40 | bobID.load_privkey("Bob_private.pem"); |
---|
| 41 | ctxt.load_id_chunk(bobID.cert_chunk()) |
---|
| 42 | bob=bobID.keyid() |
---|
| 43 | |
---|
| 44 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 45 | maryID.load_privkey("Mary_private.pem"); |
---|
| 46 | ctxt.load_id_chunk(maryID.cert_chunk()) |
---|
| 47 | mary=maryID.keyid() |
---|
| 48 | |
---|
| 49 | #case 1: |
---|
| 50 | #Only employee of SuperK can park |
---|
| 51 | #[keyid:SuperK].role:park <- [keyid:SuperK].role:employee |
---|
| 52 | attr = ABAC.Attribute(superKID, "park", 0) |
---|
| 53 | attr.role(superK,"employee") |
---|
| 54 | attr.bake() |
---|
| 55 | attr.write_file("SuperK_park__SuperK_employee_attr.xml") |
---|
| 56 | ctxt.load_attribute_file("SuperK_park__SuperK_employee_attr.xml") |
---|
| 57 | print_a(ctxt,"case1","..") |
---|
| 58 | |
---|
| 59 | #case 2: |
---|
| 60 | #Jack is an employee of SuperK |
---|
| 61 | #[keyid:SuperK].role:employee <- [keyid:Jack] |
---|
| 62 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
| 63 | attr.principal(jack) |
---|
| 64 | attr.bake() |
---|
| 65 | # create a policy file at the file system |
---|
| 66 | attr.write_file("SuperK_employee__Jack_attr.xml") |
---|
| 67 | ctxt.load_attribute_chunk(attr.cert_chunk()); |
---|
| 68 | print_a(ctxt,"case2","....") |
---|
| 69 | |
---|
| 70 | #case 3: |
---|
| 71 | #Bob is an employee of SuperK |
---|
| 72 | #[keyid:SuperK].role:employee <- [keyid:Jack] |
---|
| 73 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
| 74 | attr.principal(bob) |
---|
| 75 | attr.bake() |
---|
| 76 | #chunk=attr.cert_chunk() |
---|
| 77 | #nattr=ABAC.Attribute_chunk(chunk) |
---|
| 78 | #ctxt.load_attribute(nattr); |
---|
| 79 | #print_a(ctxt,"case3", "....") |
---|
| 80 | |
---|
| 81 | #case 4: |
---|
| 82 | #Mary is an employee of SuperK |
---|
| 83 | #[keyid:SuperK].role:employee <- [keyid:Mary] |
---|
| 84 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
| 85 | attr.principal(mary) |
---|
| 86 | attr.bake() |
---|
| 87 | chunk=attr.cert_chunk() |
---|
| 88 | ctxt.load_attribute_chunk(chunk); |
---|
| 89 | print_a(ctxt,"case4","......") |
---|