[ba6027a] | 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 | cmd1:env keystore=`pwd` ./attr.py |
---|
| 8 | """ |
---|
| 9 | import os |
---|
| 10 | import ABAC |
---|
| 11 | |
---|
| 12 | ctxt = ABAC.Context() |
---|
| 13 | print "ABAC version %s" % ctxt.version() |
---|
| 14 | |
---|
| 15 | # retrieve principals' keyid value from local credential files |
---|
| 16 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 17 | acmeID.id_load_encrypted_privkey_file("Acme_private.pem","pfile"); |
---|
| 18 | ctxt.load_id(acmeID) |
---|
| 19 | acme=acmeID.id_keyid() |
---|
| 20 | |
---|
| 21 | coyoteID=ABAC.ID("Coyote_ID.pem"); |
---|
| 22 | coyoteID.id_load_privkey_file("Coyote_private.pem"); |
---|
| 23 | ctxt.load_id(coyoteID) |
---|
| 24 | coyote=coyoteID.id_keyid() |
---|
| 25 | |
---|
| 26 | ################################################ |
---|
| 27 | # Credential 1, only preferred_customer of Acme can buy_rockets |
---|
| 28 | #[keyid:Acme].role:buy_rockets <- [keyid:Acme].role:preferred_customer |
---|
| 29 | head = ABAC.Role(acme,"buy_rockets") |
---|
| 30 | tail = ABAC.Role(acme,"preferred_customer") |
---|
| 31 | |
---|
| 32 | # compose the attribute of a basic rt0 role rule |
---|
| 33 | attr=ABAC.Attribute(head, 1800) |
---|
| 34 | attr.attribute_add_tail(tail) |
---|
| 35 | |
---|
| 36 | # finalize the policy |
---|
| 37 | attr.attribute_bake() |
---|
| 38 | |
---|
| 39 | # create a policy file at the file system |
---|
| 40 | attr.attribute_write_cert("Acme_buy_rockets__Acme_preferred_customer_attr.der") |
---|
| 41 | |
---|
| 42 | # load the policy into current context by with the newly created policy file |
---|
| 43 | ctxt.load_attribute_file("Acme_buy_rockets__Acme_preferred_customer_attr.der") |
---|
| 44 | print attr.string() |
---|
| 45 | print attr.typed_string() |
---|
| 46 | print "\n" |
---|
| 47 | |
---|
| 48 | ################################################# |
---|
| 49 | # Credential 2 |
---|
| 50 | #[keyid:Acme].role:preferred_customer <- [keyid:Coyote] |
---|
| 51 | head = ABAC.Role(acme,"preferred_customer") |
---|
| 52 | tail = ABAC.Role(coyote) |
---|
| 53 | attr=ABAC.Attribute(head, 1800) |
---|
| 54 | attr.attribute_add_tail(tail) |
---|
| 55 | attr.attribute_bake() |
---|
| 56 | attr.attribute_write_cert("Acme_preferred_customer__Coyote_attr.der") |
---|
| 57 | ctxt.load_attribute_file("Acme_preferred_customer__Coyote_attr.der") |
---|
| 58 | print attr.string() |
---|
| 59 | print attr.typed_string() |
---|
| 60 | print "\n" |
---|
| 61 | |
---|