[7211a95] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
[f824a9e] | 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 |
---|
[7211a95] | 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 | |
---|
[f824a9e] | 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 | |
---|
| 21 | # retrieve principals' keyid value from local credential files |
---|
[7211a95] | 22 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 23 | acmeID.id_load_privkey_file("Acme_private.pem"); |
---|
| 24 | ctxt.load_id(acmeID) |
---|
| 25 | acme=acmeID.id_keyid() |
---|
| 26 | |
---|
| 27 | coyoteID=ABAC.ID("Coyote_ID.pem"); |
---|
| 28 | coyoteID.id_load_privkey_file("Coyote_private.pem"); |
---|
| 29 | ctxt.load_id(coyoteID) |
---|
| 30 | coyote=coyoteID.id_keyid() |
---|
| 31 | |
---|
| 32 | warnerbrosID=ABAC.ID("WarnerBros_ID.pem"); |
---|
| 33 | warnerbrosID.id_load_privkey_file("WarnerBros_private.pem"); |
---|
| 34 | ctxt.load_id(warnerbrosID) |
---|
| 35 | warnerbros=warnerbrosID.id_keyid() |
---|
| 36 | |
---|
| 37 | batmanID=ABAC.ID("Batman_ID.pem"); |
---|
| 38 | batmanID.id_load_privkey_file("Batman_private.pem"); |
---|
| 39 | ctxt.load_id(batmanID) |
---|
| 40 | batman=batmanID.id_keyid() |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | ################################################ |
---|
[f824a9e] | 44 | # Credential 1, establish the intersection rule on who can buy |
---|
| 45 | # rockets from Acme |
---|
[7211a95] | 46 | #[keyid:Acme].role:buy_rockets <- [keyid:Acme].role:preferred_customer |
---|
| 47 | # & [keyid:WarnerBros].role:charater |
---|
| 48 | head = ABAC.Role(acme,"buy_rockets") |
---|
| 49 | tail1 = ABAC.Role(acme,"preferred_customer") |
---|
| 50 | tail2 = ABAC.Role(warnerbros,"character") |
---|
| 51 | attr=ABAC.Attribute(head, 1800) |
---|
[f824a9e] | 52 | |
---|
| 53 | # to add intersection, just add multiple tails, make sure they are all |
---|
| 54 | # roles or all osets |
---|
[7211a95] | 55 | attr.attribute_add_tail(tail1) |
---|
| 56 | attr.attribute_add_tail(tail2) |
---|
[f824a9e] | 57 | |
---|
| 58 | # finalize the rule |
---|
[7211a95] | 59 | attr.attribute_bake() |
---|
[f824a9e] | 60 | |
---|
| 61 | # save it out as external credential file |
---|
[7211a95] | 62 | attr.attribute_write_cert("Acme_buy_rockets__Acme_preferred_customer_and_WarnerBros_character_attr.der") |
---|
[f824a9e] | 63 | |
---|
| 64 | # load it into context from the just created credential file |
---|
[7211a95] | 65 | ctxt.load_attribute_file("Acme_buy_rockets__Acme_preferred_customer_and_WarnerBros_character_attr.der") |
---|
| 66 | print attr.string() |
---|
| 67 | print attr.typed_string() |
---|
| 68 | print "\n" |
---|
| 69 | |
---|
| 70 | ################################################# |
---|
[f824a9e] | 71 | # Credential 2 |
---|
[7211a95] | 72 | #[keyid:Acme].role:preferred_customer <- [keyid:Coyote] |
---|
| 73 | head = ABAC.Role(acme,"preferred_customer") |
---|
| 74 | tail = ABAC.Role(coyote) |
---|
| 75 | attr=ABAC.Attribute(head, 1800) |
---|
| 76 | attr.attribute_add_tail(tail) |
---|
| 77 | attr.attribute_bake() |
---|
| 78 | attr.attribute_write_cert("Acme_preferred_customer__Coyote_attr.der") |
---|
| 79 | ctxt.load_attribute_file("Acme_preferred_customer__Coyote_attr.der") |
---|
| 80 | print attr.string() |
---|
| 81 | print attr.typed_string() |
---|
| 82 | print "\n" |
---|
| 83 | |
---|
| 84 | ################################################# |
---|
[f824a9e] | 85 | # Credential 3 |
---|
[7211a95] | 86 | #[keyid:Acme].role:preferred_customer <- [keyid:Batman] |
---|
| 87 | head = ABAC.Role(acme,"preferred_customer") |
---|
| 88 | tail = ABAC.Role(batman) |
---|
| 89 | attr=ABAC.Attribute(head,1800) |
---|
| 90 | attr.attribute_add_tail(tail) |
---|
| 91 | attr.attribute_bake() |
---|
| 92 | attr.attribute_write_cert("Acme_preferred_customer__Batman_attr.der") |
---|
| 93 | ctxt.load_attribute_file("Acme_preferred_customer__Batman_attr.der") |
---|
| 94 | print attr.string() |
---|
| 95 | print attr.typed_string() |
---|
| 96 | print "\n" |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | ################################################ |
---|
[f824a9e] | 100 | # Credential 4 |
---|
[7211a95] | 101 | #[keyid:WarnerBros].role:character <- [keyid:Coyote] |
---|
| 102 | head=ABAC.Role(warnerbros,"character") |
---|
| 103 | tail = ABAC.Role(coyote) |
---|
| 104 | attr=ABAC.Attribute(head, 1800) |
---|
| 105 | attr.attribute_add_tail(tail) |
---|
| 106 | attr.attribute_bake() |
---|
| 107 | attr.attribute_write_cert("WarnerBros_character__Coyote_attr.der") |
---|
[f824a9e] | 108 | |
---|
| 109 | # demonstrate how attribute can be load from structure insted of a file |
---|
[7211a95] | 110 | ctxt.load_attribute(attr) |
---|
[f824a9e] | 111 | print attr.string() |
---|
| 112 | print attr.typed_string() |
---|
| 113 | print "\n" |
---|
[7211a95] | 114 | |
---|