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