[abf8d5d] | 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 | |
---|
| 10 | import os |
---|
| 11 | import ABAC |
---|
| 12 | |
---|
| 13 | ctxt = ABAC.Context() |
---|
| 14 | print "ABAC version %s" % ctxt.version() |
---|
| 15 | |
---|
| 16 | # Keystore is the directory containing the principal credentials. |
---|
| 17 | # Load existing principals and/or policy credentials |
---|
| 18 | if (os.environ.has_key("keystore")) : |
---|
| 19 | keystore=os.environ["keystore"] |
---|
| 20 | ctxt.load_directory(keystore) |
---|
[646e57e] | 21 | else: |
---|
| 22 | print("keystore is not set...") |
---|
| 23 | exit(1) |
---|
| 24 | |
---|
[abf8d5d] | 25 | |
---|
| 26 | out = ctxt.context_principals() |
---|
| 27 | print "...initial principal set..." |
---|
| 28 | for x in out[1]: |
---|
| 29 | print "%s " % x.string() |
---|
| 30 | print "\n" |
---|
| 31 | |
---|
| 32 | out = ctxt.context_credentials() |
---|
| 33 | print "...initial policy attribute set..." |
---|
| 34 | for c in out[1]: |
---|
| 35 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 36 | print "\n" |
---|
| 37 | |
---|
| 38 | # retrieve principals' keyid value from local credential files |
---|
| 39 | acmeID=ABAC.ID("Acme_ID.pem"); |
---|
| 40 | acmeID.id_load_privkey_file("Acme_private.pem"); |
---|
| 41 | acme=acmeID.id_keyid() |
---|
| 42 | |
---|
| 43 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 44 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 45 | bob=bobID.id_keyid() |
---|
| 46 | |
---|
| 47 | aliceID=ABAC.ID("Alice_ID.pem"); |
---|
| 48 | aliceID.id_load_privkey_file("Alice_private.pem"); |
---|
| 49 | alice=aliceID.id_keyid() |
---|
| 50 | |
---|
| 51 | daveID=ABAC.ID("Dave_ID.pem"); |
---|
| 52 | daveID.id_load_privkey_file("Dave_private.pem"); |
---|
| 53 | dave=daveID.id_keyid() |
---|
| 54 | |
---|
| 55 | globotronID=ABAC.ID("Globotron_ID.pem"); |
---|
| 56 | globotronID.id_load_privkey_file("Globotron_private.pem"); |
---|
| 57 | globotron=globotronID.id_keyid() |
---|
| 58 | |
---|
| 59 | ################################################ |
---|
| 60 | # Credential 1, Anyone who is allowed to create experiment by Acme's |
---|
| 61 | # partners can create experiment at Acme |
---|
| 62 | # [keyid:Acme].role:experiment_create |
---|
| 63 | # <- [keyid:Acme].role:partner.role:experiment_create |
---|
| 64 | head=ABAC.Role(acme,"experiment_create") |
---|
| 65 | |
---|
| 66 | # creating a linking role |
---|
| 67 | tail = ABAC.Role(acme,"partner","experiment_create") |
---|
| 68 | |
---|
| 69 | # compose the policy attribute |
---|
| 70 | attr=ABAC.Attribute(head, 1800) |
---|
| 71 | attr.attribute_add_tail(tail) |
---|
| 72 | |
---|
| 73 | # finalize the policy |
---|
| 74 | attr.attribute_bake() |
---|
| 75 | |
---|
| 76 | # write out the policy to an external file |
---|
| 77 | attr.attribute_write_cert("Acme_experiment_create__Acme_partner_experiment_create_attr.der") |
---|
| 78 | |
---|
| 79 | # load the policy into the context by accessing that external file |
---|
| 80 | ctxt.load_attribute_file("Acme_experiment_create__Acme_partner_experiment_create_attr.der") |
---|
| 81 | print attr.string() |
---|
| 82 | print attr.typed_string() |
---|
| 83 | print "\n" |
---|
| 84 | |
---|
| 85 | ################################################# |
---|
| 86 | # Credential 2 |
---|
| 87 | # [keyid:Acme].role:partner <- [keyid:Globotron] |
---|
| 88 | # |
---|
| 89 | head=ABAC.Role(acme,"partner") |
---|
| 90 | tail = ABAC.Role(globotron) |
---|
| 91 | attr=ABAC.Attribute(head, 1800) |
---|
| 92 | attr.attribute_add_tail(tail) |
---|
| 93 | attr.attribute_bake() |
---|
| 94 | attr.attribute_write_cert("Acme_partner__Globotron_attr.der") |
---|
| 95 | ctxt.load_attribute_file("Acme_partner__Globotron_attr.der") |
---|
| 96 | print attr.string() |
---|
| 97 | print attr.typed_string() |
---|
| 98 | print "\n" |
---|
| 99 | |
---|
| 100 | ################################################# |
---|
| 101 | # Credential 3 |
---|
| 102 | # [keyid:Globotron].role:expriment_create |
---|
| 103 | # <- [keyid:Globotron].role:admin.role:power_user |
---|
| 104 | head = ABAC.Role(acme,"experiment_create") |
---|
| 105 | |
---|
| 106 | # a linking role |
---|
| 107 | tail = ABAC.Role(globotron,"admin","power_user") |
---|
| 108 | attr=ABAC.Attribute(head, 1800) |
---|
| 109 | attr.attribute_add_tail(tail) |
---|
| 110 | attr.attribute_bake() |
---|
| 111 | attr.attribute_write_cert("Globotron_experiment_create__Globotron_admin_power_user_attr.der") |
---|
| 112 | ctxt.load_attribute_file("Globotron_experiment_create__Globotron_admin_power_user_attr.der") |
---|
| 113 | print attr.string() |
---|
| 114 | print attr.typed_string() |
---|
| 115 | print "\n" |
---|
| 116 | |
---|
| 117 | ################################################# |
---|
| 118 | # Credential 4, named term at the right |
---|
| 119 | # [keyid:Globotron].role:admin <- [keyid:Alice] |
---|
| 120 | head = ABAC.Role(globotron,"admin") |
---|
| 121 | |
---|
| 122 | # the named role is using keyid of alice |
---|
| 123 | tail = ABAC.Role(alice) |
---|
| 124 | attr=ABAC.Attribute(head, 1800) |
---|
| 125 | attr.attribute_add_tail(tail) |
---|
| 126 | attr.attribute_bake() |
---|
| 127 | attr.attribute_write_cert("Globotron_admin__Alice_attr.der") |
---|
| 128 | ctxt.load_attribute_file("Globotron_admin__Alice_attr.der") |
---|
| 129 | print attr.string() |
---|
| 130 | print attr.typed_string() |
---|
| 131 | print "\n" |
---|
| 132 | |
---|
| 133 | ################################################# |
---|
| 134 | # Credential 5, |
---|
| 135 | # [keyid:Alice].role:power_user <- [keyid:Bob] |
---|
| 136 | head = ABAC.Role(alice,"power_user") |
---|
| 137 | tail = ABAC.Role(bob) |
---|
| 138 | attr=ABAC.Attribute(head, 1800) |
---|
| 139 | attr.attribute_add_tail(tail) |
---|
| 140 | attr.attribute_bake() |
---|
| 141 | attr.attribute_write_cert("Alice_power_user__Bob_attr.der") |
---|
| 142 | ctxt.load_attribute_file("Alice_power_user__Bob_attr.der") |
---|
| 143 | print attr.string() |
---|
| 144 | print attr.typed_string() |
---|
| 145 | print "\n" |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | ################################################# |
---|
| 149 | # Credential 6, named term at the right |
---|
| 150 | # [keyid:Globotron].role:admin <- [keyid:Dave] |
---|
| 151 | head = ABAC.Role(globotron,"admin") |
---|
| 152 | |
---|
| 153 | # the named role is using keyid of alice |
---|
| 154 | tail = ABAC.Role(dave) |
---|
| 155 | attr=ABAC.Attribute(head, 1800) |
---|
| 156 | attr.attribute_add_tail(tail) |
---|
| 157 | attr.attribute_bake() |
---|
| 158 | attr.attribute_write_cert("Globotron_admin__Dave_attr.der") |
---|
| 159 | ctxt.load_attribute_file("Globotron_admin__Dave_attr.der") |
---|
| 160 | print attr.string() |
---|
| 161 | print attr.typed_string() |
---|
| 162 | print "\n" |
---|
| 163 | |
---|
| 164 | ################################################# |
---|
| 165 | # Credential 7, |
---|
| 166 | # [keyid:Dave].role:power_user <- [keyid:Bob] |
---|
| 167 | head = ABAC.Role(dave,"power_user") |
---|
| 168 | tail = ABAC.Role(bob) |
---|
| 169 | attr=ABAC.Attribute(head, 1800) |
---|
| 170 | attr.attribute_add_tail(tail) |
---|
| 171 | attr.attribute_bake() |
---|
| 172 | attr.attribute_write_cert("Dave_power_user__Bob_attr.der") |
---|
| 173 | ctxt.load_attribute_file("Dave_power_user__Bob_attr.der") |
---|
| 174 | print attr.string() |
---|
| 175 | print attr.typed_string() |
---|
| 176 | print "\n" |
---|
| 177 | |
---|