[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 |
---|
| 6 | |
---|
[7211a95] | 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 | |
---|
[f824a9e] | 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 | |
---|
[7211a95] | 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 | |
---|
[f824a9e] | 38 | # retrieve principals' keyid value from local credential files |
---|
[7211a95] | 39 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
| 40 | ralphsID.id_load_privkey_file("Ralphs_private.pem"); |
---|
| 41 | ralphs=ralphsID.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 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 48 | maryID.id_load_privkey_file("Mary_private.pem"); |
---|
| 49 | mary=maryID.id_keyid() |
---|
| 50 | |
---|
| 51 | ################################################ |
---|
[f824a9e] | 52 | # Credential 1, what kind of fruit Mary would eat. Anything not costing more |
---|
| 53 | # than 2 dollars |
---|
[7211a95] | 54 | # [keyid:mary].oset:what2eat |
---|
| 55 | # <- [keyid:ralphs].oset:fruitprice([float:?P:[..2.00]]) |
---|
| 56 | head = ABAC.Oset(mary,"what2eat") |
---|
[f824a9e] | 57 | |
---|
| 58 | # initialize a float range constraint |
---|
[7211a95] | 59 | cond=ABAC.Constraint("float") |
---|
[f824a9e] | 60 | |
---|
| 61 | # add the upper max to the range, and only the max |
---|
[7211a95] | 62 | cond.constraint_add_float_max(2.00) |
---|
[f824a9e] | 63 | |
---|
| 64 | # create the data term with the constraint |
---|
[7211a95] | 65 | param=ABAC.DataTerm("float", "P", cond) |
---|
| 66 | tail = ABAC.Oset(ralphs,"fruitprice") |
---|
| 67 | tail.oset_add_data_term(param) |
---|
[f824a9e] | 68 | |
---|
| 69 | # compose the attribute policy |
---|
[7211a95] | 70 | attr=ABAC.Attribute(head, 1800) |
---|
| 71 | attr.attribute_add_tail(tail) |
---|
[f824a9e] | 72 | |
---|
| 73 | #finalize the policy |
---|
[7211a95] | 74 | attr.attribute_bake() |
---|
[f824a9e] | 75 | |
---|
| 76 | #write out the policy to a credential file |
---|
[7211a95] | 77 | attr.attribute_write_cert("mary_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
[f824a9e] | 78 | |
---|
| 79 | # load the rule back into context via credential file |
---|
[7211a95] | 80 | ctxt.load_attribute_file("mary_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
| 81 | print attr.string() |
---|
| 82 | print attr.typed_string() |
---|
| 83 | print "\n" |
---|
| 84 | |
---|
| 85 | ################################################ |
---|
[f824a9e] | 86 | # Credential 2, |
---|
[7211a95] | 87 | # [keyid:bob].oset:what2eat |
---|
| 88 | # <- [keyid:ralphs].oset:fruitprice([float:?P:[1.00..5.00]]) |
---|
| 89 | head = ABAC.Oset(bob,"what2eat") |
---|
[f824a9e] | 90 | |
---|
| 91 | # initialze a float range constraint |
---|
[7211a95] | 92 | cond=ABAC.Constraint("float") |
---|
[f824a9e] | 93 | |
---|
| 94 | # add the min and max value to the range |
---|
[7211a95] | 95 | cond.constraint_add_float_min(1.00) |
---|
| 96 | cond.constraint_add_float_max(5.00) |
---|
| 97 | param=ABAC.DataTerm("float", "P", cond) |
---|
| 98 | tail = ABAC.Oset(ralphs,"fruitprice") |
---|
| 99 | tail.oset_add_data_term(param) |
---|
[f824a9e] | 100 | |
---|
| 101 | #create attribute policy |
---|
[7211a95] | 102 | attr=ABAC.Attribute(head, 1800) |
---|
| 103 | attr.attribute_add_tail(tail) |
---|
[f824a9e] | 104 | |
---|
| 105 | #finalize the policy |
---|
[7211a95] | 106 | attr.attribute_bake() |
---|
| 107 | attr.attribute_write_cert("bob_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
| 108 | ctxt.load_attribute_file("bob_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
| 109 | print attr.string() |
---|
| 110 | print attr.typed_string() |
---|
| 111 | print "\n" |
---|
| 112 | |
---|
| 113 | ################################################# |
---|
[f824a9e] | 114 | # Credential 3 |
---|
[7211a95] | 115 | # [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'apple'] |
---|
| 116 | param=ABAC.DataTerm("float", "1.50") |
---|
| 117 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 118 | head.oset_add_data_term(param) |
---|
| 119 | param=ABAC.DataTerm("string", "'apple'") |
---|
| 120 | tail = ABAC.Oset(param) |
---|
| 121 | attr=ABAC.Attribute(head, 1800) |
---|
| 122 | attr.attribute_add_tail(tail) |
---|
| 123 | attr.attribute_bake() |
---|
| 124 | attr.attribute_write_cert("Ralphs_fruitprice__apple_attr.der") |
---|
| 125 | ctxt.load_attribute_file("Ralphs_fruitprice__apple_attr.der") |
---|
| 126 | print attr.string() |
---|
| 127 | print attr.typed_string() |
---|
| 128 | print "\n" |
---|
| 129 | |
---|
| 130 | ################################################# |
---|
[f824a9e] | 131 | # Credential 4 |
---|
[7211a95] | 132 | # [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'kiwi'] |
---|
| 133 | param=ABAC.DataTerm("float", "1.50") |
---|
| 134 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 135 | head.oset_add_data_term(param) |
---|
| 136 | param=ABAC.DataTerm("string", "'kiwi'") |
---|
| 137 | tail = ABAC.Oset(param) |
---|
| 138 | attr=ABAC.Attribute(head, 1800) |
---|
| 139 | attr.attribute_add_tail(tail) |
---|
| 140 | attr.attribute_bake() |
---|
| 141 | attr.attribute_write_cert("Ralphs_fruitprice__kiwi_attr.der") |
---|
| 142 | ctxt.load_attribute_file("Ralphs_fruitprice__kiwi_attr.der") |
---|
| 143 | print attr.string() |
---|
| 144 | print attr.typed_string() |
---|
| 145 | print "\n" |
---|
| 146 | |
---|
| 147 | ################################################# |
---|
[f824a9e] | 148 | # Credential 5 |
---|
[7211a95] | 149 | # [keyid:ralphs].oset:fruitprice([float:2.50]) <- [string:'black berry'] |
---|
| 150 | param=ABAC.DataTerm("float", "2.50") |
---|
| 151 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 152 | head.oset_add_data_term(param) |
---|
| 153 | param=ABAC.DataTerm("string", "'black berry'") |
---|
| 154 | tail = ABAC.Oset(param) |
---|
| 155 | attr=ABAC.Attribute(head, 1800) |
---|
| 156 | attr.attribute_add_tail(tail) |
---|
| 157 | attr.attribute_bake() |
---|
| 158 | attr.attribute_write_cert("Ralphs_fruitprice__blackberry_attr.der") |
---|
| 159 | ctxt.load_attribute_file("Ralphs_fruitprice__blackberry_attr.der") |
---|
| 160 | print attr.string() |
---|
| 161 | print attr.typed_string() |
---|
| 162 | print "\n" |
---|
| 163 | |
---|
| 164 | ################################################# |
---|
[f824a9e] | 165 | # Credential 6 |
---|
[7211a95] | 166 | # [keyid:ralphs].oset:fruitprice([float:0.50]) <- [string:'navel orange'] |
---|
| 167 | param=ABAC.DataTerm("float", "0.50") |
---|
| 168 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 169 | head.oset_add_data_term(param) |
---|
| 170 | param=ABAC.DataTerm("string", "'navel orange'") |
---|
| 171 | tail = ABAC.Oset(param) |
---|
| 172 | attr=ABAC.Attribute(head, 1800) |
---|
| 173 | attr.attribute_add_tail(tail) |
---|
| 174 | attr.attribute_bake() |
---|
| 175 | attr.attribute_write_cert("Ralphs_fruitprice__navelorange_attr.der") |
---|
| 176 | ctxt.load_attribute_file("Ralphs_fruitprice__navelorange_attr.der") |
---|
| 177 | print attr.string() |
---|
| 178 | print attr.typed_string() |
---|
| 179 | print "\n" |
---|
| 180 | |
---|