[be6cb41] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | See README in this directory for the semantics of the example. This file |
---|
| 5 | constructs the credentials described and puts copies into this directory |
---|
| 6 | |
---|
| 7 | cmd: ./attr.py |
---|
| 8 | """ |
---|
| 9 | import os |
---|
| 10 | import ABAC |
---|
| 11 | |
---|
| 12 | ctxt = ABAC.Context() |
---|
| 13 | |
---|
| 14 | # retrieve principals' keyid value from local credential files |
---|
| 15 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 16 | acmeID.load_privkey("Acme_private.pem"); |
---|
| 17 | ctxt.load_id_chunk(acmeID.cert_chunk()) |
---|
| 18 | acme=acmeID.keyid() |
---|
| 19 | |
---|
| 20 | coyoteID=ABAC.ID("Coyote_ID.pem"); |
---|
| 21 | coyoteID.load_privkey("Coyote_private.pem"); |
---|
| 22 | ctxt.load_id_chunk(coyoteID.cert_chunk()) |
---|
| 23 | coyote=coyoteID.keyid() |
---|
| 24 | |
---|
| 25 | bigbirdID=ABAC.ID("Bigbird_ID.pem"); |
---|
| 26 | bigbirdID.load_privkey("Bigbird_private.pem"); |
---|
| 27 | ctxt.load_id_chunk(bigbirdID.cert_chunk()) |
---|
| 28 | bigbird=bigbirdID.keyid() |
---|
| 29 | |
---|
| 30 | ################################################ |
---|
| 31 | # Credential 1, only preferred_customer of Acme can buy_rockets |
---|
| 32 | #[keyid:Acme].role:buy_rockets <- [keyid:Acme].role:preferred_customer |
---|
| 33 | |
---|
| 34 | # compose the attribute of a basic rt0 role rule |
---|
| 35 | attr = ABAC.Attribute(acmeID, "buy_rockets", 0) |
---|
| 36 | attr.role(acme,"preferred_customer") |
---|
| 37 | |
---|
| 38 | # finalize the policy |
---|
| 39 | attr.bake() |
---|
| 40 | |
---|
| 41 | # create a policy file at the file system |
---|
| 42 | attr.write_file("Acme_buy_rockets__Acme_preferred_customer_attr.xml") |
---|
| 43 | |
---|
| 44 | # load the policy into current context by with the newly created policy file |
---|
| 45 | ctxt.load_attribute_file("Acme_buy_rockets__Acme_preferred_customer_attr.xml") |
---|
| 46 | |
---|
| 47 | ################################################# |
---|
| 48 | # Credential 2 |
---|
| 49 | #[keyid:Acme].role:preferred_customer <- [keyid:Coyote] |
---|
| 50 | attr = ABAC.Attribute(acmeID, "preferred_customer", 0) |
---|
| 51 | attr.principal(coyote) |
---|
| 52 | attr.bake() |
---|
| 53 | |
---|
| 54 | attr.write_file("Acme_preferred_customer__Coyote_attr.xml") |
---|
| 55 | ctxt.load_attribute_file("Acme_preferred_customer__Coyote_attr.xml") |
---|
| 56 | |
---|
| 57 | ################################################# |
---|
| 58 | # Credential 3 |
---|
| 59 | #[keyid:Coyote].role:friend <- [keyid:Bigbird] |
---|
| 60 | attr = ABAC.Attribute(coyoteID, "friend", 0) |
---|
| 61 | attr.principal(bigbird) |
---|
| 62 | attr.bake() |
---|
| 63 | |
---|
| 64 | attr.write_file("Coyote_friend__Bigbird_attr.xml") |
---|
| 65 | ctxt.load_attribute_chunk(attr.cert_chunk()) |
---|
| 66 | |
---|
| 67 | ################################################# |
---|
| 68 | credentials = ctxt.credentials() |
---|
| 69 | for credential in credentials: |
---|
| 70 | print "context: %s <- %s" % (credential.head().string(), credential.tail().string()) |
---|