[373bf68] | 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) |
---|
| 21 | else: |
---|
| 22 | print("keystore is not set...") |
---|
| 23 | exit(1) |
---|
| 24 | |
---|
| 25 | # retrieve principals' keyid value from local credential files |
---|
| 26 | ralphsID=ABAC.ID("Ralphs_ID.pem"); |
---|
| 27 | ralphsID.id_load_privkey_file("Ralphs_private.pem"); |
---|
| 28 | ralphs=ralphsID.id_keyid() |
---|
| 29 | |
---|
| 30 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 31 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 32 | bob=bobID.id_keyid() |
---|
| 33 | |
---|
| 34 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
| 35 | maryID.id_load_privkey_file("Mary_private.pem"); |
---|
| 36 | mary=maryID.id_keyid() |
---|
| 37 | |
---|
| 38 | ################################################ |
---|
| 39 | # Credential 1, what kind of fruit Mary would eat. Anything not costing more |
---|
| 40 | # than 2 dollars |
---|
| 41 | # [keyid:mary].oset:what2eat |
---|
| 42 | # <- [keyid:ralphs].oset:fruitprice([float:?P:[..2.00]]) |
---|
| 43 | head = ABAC.Oset(mary,"what2eat") |
---|
| 44 | |
---|
| 45 | # initialize a float range constraint |
---|
| 46 | cond=ABAC.Constraint("float") |
---|
| 47 | |
---|
| 48 | # add the upper max to the range, and only the max |
---|
| 49 | cond.constraint_add_float_max(2.00) |
---|
| 50 | |
---|
| 51 | # create the data term with the constraint |
---|
| 52 | param=ABAC.DataTerm("float", "P", cond) |
---|
| 53 | tail = ABAC.Oset(ralphs,"fruitprice") |
---|
| 54 | tail.oset_add_data_term(param) |
---|
| 55 | |
---|
| 56 | # compose the attribute policy |
---|
| 57 | attr=ABAC.Attribute(head, 1800) |
---|
| 58 | attr.attribute_add_tail(tail) |
---|
| 59 | |
---|
| 60 | #finalize the policy |
---|
| 61 | attr.attribute_bake() |
---|
| 62 | |
---|
| 63 | #write out the policy to a credential file |
---|
| 64 | attr.attribute_write_cert("mary_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | ################################################ |
---|
| 68 | # Credential 2, |
---|
| 69 | # [keyid:bob].oset:what2eat |
---|
| 70 | # <- [keyid:ralphs].oset:fruitprice([float:?P:[1.00..5.00]]) |
---|
| 71 | head = ABAC.Oset(bob,"what2eat") |
---|
| 72 | |
---|
| 73 | # initialze a float range constraint |
---|
| 74 | cond=ABAC.Constraint("float") |
---|
| 75 | |
---|
| 76 | # add the min and max value to the range |
---|
| 77 | cond.constraint_add_float_min(1.00) |
---|
| 78 | cond.constraint_add_float_max(5.00) |
---|
| 79 | param=ABAC.DataTerm("float", "P", cond) |
---|
| 80 | tail = ABAC.Oset(ralphs,"fruitprice") |
---|
| 81 | tail.oset_add_data_term(param) |
---|
| 82 | |
---|
| 83 | #create attribute policy |
---|
| 84 | attr=ABAC.Attribute(head, 1800) |
---|
| 85 | attr.attribute_add_tail(tail) |
---|
| 86 | |
---|
| 87 | #finalize the policy |
---|
| 88 | attr.attribute_bake() |
---|
| 89 | attr.attribute_write_cert("bob_what2eat__ralphs_fruitprice_qP_attr.der") |
---|
| 90 | |
---|
| 91 | ################################################# |
---|
| 92 | # Credential 3 |
---|
| 93 | # [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'apple'] |
---|
| 94 | param=ABAC.DataTerm("float", "1.50") |
---|
| 95 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 96 | head.oset_add_data_term(param) |
---|
| 97 | param=ABAC.DataTerm("string", "'apple'") |
---|
| 98 | tail = ABAC.Oset(param) |
---|
| 99 | attr=ABAC.Attribute(head, 1800) |
---|
| 100 | attr.attribute_add_tail(tail) |
---|
| 101 | attr.attribute_bake() |
---|
| 102 | attr.attribute_write_cert("Ralphs_fruitprice__apple_attr.der") |
---|
| 103 | |
---|
| 104 | ################################################# |
---|
| 105 | # Credential 4 |
---|
| 106 | # [keyid:ralphs].oset:fruitprice([float:1.50]) <- [string:'kiwi'] |
---|
| 107 | param=ABAC.DataTerm("float", "1.50") |
---|
| 108 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 109 | head.oset_add_data_term(param) |
---|
| 110 | param=ABAC.DataTerm("string", "'kiwi'") |
---|
| 111 | tail = ABAC.Oset(param) |
---|
| 112 | attr=ABAC.Attribute(head, 1800) |
---|
| 113 | attr.attribute_add_tail(tail) |
---|
| 114 | attr.attribute_bake() |
---|
| 115 | attr.attribute_write_cert("Ralphs_fruitprice__kiwi_attr.der") |
---|
| 116 | |
---|
| 117 | ################################################# |
---|
| 118 | # Credential 5 |
---|
| 119 | # [keyid:ralphs].oset:fruitprice([float:2.50]) <- [string:'black berry'] |
---|
| 120 | param=ABAC.DataTerm("float", "2.50") |
---|
| 121 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 122 | head.oset_add_data_term(param) |
---|
| 123 | param=ABAC.DataTerm("string", "'black berry'") |
---|
| 124 | tail = ABAC.Oset(param) |
---|
| 125 | attr=ABAC.Attribute(head, 1800) |
---|
| 126 | attr.attribute_add_tail(tail) |
---|
| 127 | attr.attribute_bake() |
---|
| 128 | attr.attribute_write_cert("Ralphs_fruitprice__blackberry_attr.der") |
---|
| 129 | |
---|
| 130 | ################################################# |
---|
| 131 | # Credential 6 |
---|
| 132 | # [keyid:ralphs].oset:fruitprice([float:0.50]) <- [string:'navel orange'] |
---|
| 133 | param=ABAC.DataTerm("float", "0.50") |
---|
| 134 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 135 | head.oset_add_data_term(param) |
---|
| 136 | param=ABAC.DataTerm("string", "'navel orange'") |
---|
| 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__navelorange_attr.der") |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | ########### NOISE ####################################### |
---|
| 145 | # [keyid:Ralphs].oset:fruitprice([float:X.00]) <- [string:bananaY] |
---|
| 146 | i=1 |
---|
| 147 | while i <= #VAL#: |
---|
| 148 | p="%s.00"%i |
---|
| 149 | n="'banana%s'"%i |
---|
| 150 | nn="banana%s"%i |
---|
| 151 | f="Ralphs_fruitprice__%s_attr.der"%nn |
---|
| 152 | param=ABAC.DataTerm("float", p) |
---|
| 153 | head = ABAC.Oset(ralphs,"fruitprice") |
---|
| 154 | head.oset_add_data_term(param) |
---|
| 155 | param=ABAC.DataTerm("string", n) |
---|
| 156 | tail = ABAC.Oset(param) |
---|
| 157 | attr=ABAC.Attribute(head, 1800) |
---|
| 158 | attr.attribute_add_tail(tail) |
---|
| 159 | attr.attribute_bake() |
---|
| 160 | attr.attribute_write_cert(f) |
---|
| 161 | i=i+1 |
---|
| 162 | |
---|
| 163 | # [keyid:johnX].oset:what2eat |
---|
| 164 | # <- [keyid:ralphs].oset:fruitprice([float:?P:[1.00..5.00]]) |
---|
| 165 | i=1 |
---|
| 166 | while i <= #VAL#: |
---|
| 167 | j="john%s"%i |
---|
| 168 | jid="%s_ID.pem"%j |
---|
| 169 | jp="%s_private.pem"%j |
---|
| 170 | f="%s_what2eat__ralphs_fruitprice_qP_attr.der"%j |
---|
| 171 | |
---|
| 172 | jID=ABAC.ID(jid); |
---|
| 173 | jID.id_load_privkey_file(jp); |
---|
| 174 | john=jID.id_keyid() |
---|
| 175 | |
---|
| 176 | head = ABAC.Oset(john,"what2eat") |
---|
| 177 | cond=ABAC.Constraint("float") |
---|
| 178 | cond.constraint_add_float_min(1.00) |
---|
| 179 | cond.constraint_add_float_max(5.00) |
---|
| 180 | param=ABAC.DataTerm("float", "P", cond) |
---|
| 181 | tail = ABAC.Oset(ralphs,"fruitprice") |
---|
| 182 | tail.oset_add_data_term(param) |
---|
| 183 | |
---|
| 184 | attr=ABAC.Attribute(head, 1800) |
---|
| 185 | attr.attribute_add_tail(tail) |
---|
| 186 | attr.attribute_bake() |
---|
| 187 | attr.attribute_write_cert(f) |
---|
| 188 | i=i+1 |
---|
| 189 | |
---|
| 190 | |
---|