[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 | |
---|
| 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 | stateUID=ABAC.ID("StateU_ID.pem") |
---|
| 36 | stateUID.id_load_privkey_file("StateU_private.pem") |
---|
| 37 | stateU=stateUID.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 | markID=ABAC.ID("Mark_ID.pem") |
---|
| 44 | markID.id_load_privkey_file("Mark_private.pem") |
---|
| 45 | mark=markID.id_keyid() |
---|
| 46 | |
---|
| 47 | joeID=ABAC.ID("Joe_ID.pem") |
---|
| 48 | joeID.id_load_privkey_file("Joe_private.pem") |
---|
| 49 | joe=joeID.id_keyid() |
---|
| 50 | |
---|
| 51 | maryannID=ABAC.ID("Maryann_ID.pem") |
---|
| 52 | maryannID.id_load_privkey_file("Maryann_private.pem") |
---|
| 53 | maryann=maryannID.id_keyid() |
---|
| 54 | |
---|
| 55 | janID=ABAC.ID("Jan_ID.pem") |
---|
| 56 | janID.id_load_privkey_file("Jan_private.pem") |
---|
| 57 | jan=janID.id_keyid() |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | ################################################ |
---|
[f824a9e] | 61 | # Credential 1, this policy has two range constraints on different parameters |
---|
[7211a95] | 62 | # [keyid:stateU].role:foundingAlumni |
---|
| 63 | # <- [keyid:stateU].role:diploma([string:?D:['mathmatics','psychology']], |
---|
| 64 | # [integer:?Year:[1960,1961,1963]]) |
---|
| 65 | head = ABAC.Role(stateU,"foundingAlumni") |
---|
[f824a9e] | 66 | |
---|
| 67 | # initialize a string range constraint |
---|
[7211a95] | 68 | cond=ABAC.Constraint("string") |
---|
[f824a9e] | 69 | |
---|
| 70 | # add specific string values to the constraint |
---|
[7211a95] | 71 | cond.constraint_add_string_target("'mathmatics'") |
---|
| 72 | cond.constraint_add_string_target("'psychology'") |
---|
[f824a9e] | 73 | |
---|
| 74 | # create the parameter with the string range constraint |
---|
[7211a95] | 75 | param1=ABAC.DataTerm("string", "D", cond) |
---|
[f824a9e] | 76 | |
---|
| 77 | # initialize another constratnt that is of integer type |
---|
[7211a95] | 78 | cond=ABAC.Constraint("integer") |
---|
[f824a9e] | 79 | |
---|
| 80 | # add specific integer values to the constraint |
---|
[7211a95] | 81 | cond.constraint_add_integer_target(1960) |
---|
| 82 | cond.constraint_add_integer_target(1961) |
---|
| 83 | cond.constraint_add_integer_target(1963) |
---|
[f824a9e] | 84 | |
---|
| 85 | # create the parameter with the integer range constraint |
---|
[7211a95] | 86 | param2=ABAC.DataTerm("integer", "Year", cond) |
---|
| 87 | tail = ABAC.Role(stateU,"diploma") |
---|
[f824a9e] | 88 | |
---|
| 89 | # add the parameter with conditions to a role |
---|
[7211a95] | 90 | tail.role_add_data_term(param1) |
---|
| 91 | tail.role_add_data_term(param2) |
---|
[f824a9e] | 92 | |
---|
| 93 | # build up the policy rule |
---|
[7211a95] | 94 | attr=ABAC.Attribute(head, 1800) |
---|
| 95 | attr.attribute_add_tail(tail) |
---|
[f824a9e] | 96 | |
---|
| 97 | # finalize the policy rule |
---|
[7211a95] | 98 | attr.attribute_bake() |
---|
[f824a9e] | 99 | |
---|
| 100 | # save it to a credential file |
---|
[7211a95] | 101 | attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
| 102 | ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
| 103 | print attr.string() |
---|
| 104 | print attr.typed_string() |
---|
| 105 | print "\n" |
---|
| 106 | |
---|
| 107 | ################################################# |
---|
[f824a9e] | 108 | # Credential 2 |
---|
[7211a95] | 109 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1961]) <- [keyid:bob] |
---|
| 110 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
| 111 | param2=ABAC.DataTerm("integer", "1961") |
---|
| 112 | head = ABAC.Role(stateU,"diploma") |
---|
| 113 | head.role_add_data_term(param1) |
---|
| 114 | head.role_add_data_term(param2) |
---|
| 115 | tail = ABAC.Role(bob) |
---|
| 116 | attr=ABAC.Attribute(head, 1800) |
---|
| 117 | attr.attribute_add_tail(tail) |
---|
| 118 | attr.attribute_bake() |
---|
| 119 | attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der") |
---|
| 120 | ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der") |
---|
| 121 | print attr.string() |
---|
| 122 | print attr.typed_string() |
---|
| 123 | print "\n" |
---|
| 124 | |
---|
| 125 | ################################################# |
---|
[f824a9e] | 126 | # Credential 3 |
---|
[7211a95] | 127 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1965]) <- [keyid:mark] |
---|
| 128 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
| 129 | param2=ABAC.DataTerm("integer", "1965") |
---|
| 130 | head = ABAC.Role(stateU,"diploma") |
---|
| 131 | head.role_add_data_term(param1) |
---|
| 132 | head.role_add_data_term(param2) |
---|
| 133 | tail = ABAC.Role(mark) |
---|
| 134 | attr=ABAC.Attribute(head, 1800) |
---|
| 135 | attr.attribute_add_tail(tail) |
---|
| 136 | attr.attribute_bake() |
---|
| 137 | attr.attribute_write_cert("StateU_diploma_m__Mark_attr.der") |
---|
| 138 | ctxt.load_attribute_file("StateU_diploma_m__Mark_attr.der") |
---|
| 139 | print attr.string() |
---|
| 140 | print attr.typed_string() |
---|
| 141 | print "\n" |
---|
| 142 | |
---|
| 143 | ################################################# |
---|
[f824a9e] | 144 | # Credential 4 |
---|
[7211a95] | 145 | # [keyid:stateU].role:diploma([string:'zoology'],[integer:1961]) <- [keyid:joe] |
---|
| 146 | param1=ABAC.DataTerm("string", "'zoology'") |
---|
| 147 | param2=ABAC.DataTerm("integer", "1961") |
---|
| 148 | head = ABAC.Role(stateU,"diploma") |
---|
| 149 | head.role_add_data_term(param1) |
---|
| 150 | head.role_add_data_term(param2) |
---|
| 151 | tail = ABAC.Role(joe) |
---|
| 152 | attr=ABAC.Attribute(head, 1800) |
---|
| 153 | attr.attribute_add_tail(tail) |
---|
| 154 | attr.attribute_bake() |
---|
| 155 | attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der") |
---|
| 156 | ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der") |
---|
| 157 | print attr.string() |
---|
| 158 | print attr.typed_string() |
---|
| 159 | print "\n" |
---|
| 160 | |
---|
| 161 | ################################################# |
---|
[f824a9e] | 162 | # Credential 5 |
---|
| 163 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) |
---|
| 164 | # <- [keyid:maryann] |
---|
[7211a95] | 165 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
| 166 | param2=ABAC.DataTerm("integer", "1962") |
---|
| 167 | head = ABAC.Role(stateU,"diploma") |
---|
| 168 | head.role_add_data_term(param1) |
---|
| 169 | head.role_add_data_term(param2) |
---|
| 170 | tail = ABAC.Role(maryann) |
---|
| 171 | attr=ABAC.Attribute(head, 1800) |
---|
| 172 | attr.attribute_add_tail(tail) |
---|
| 173 | attr.attribute_bake() |
---|
| 174 | attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der") |
---|
| 175 | ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") |
---|
| 176 | print attr.string() |
---|
| 177 | print attr.typed_string() |
---|
| 178 | print "\n" |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | ################################################# |
---|
[f824a9e] | 182 | # Credential 6 |
---|
| 183 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1960]) |
---|
| 184 | # <- [keyid:jan] |
---|
[7211a95] | 185 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
| 186 | param2=ABAC.DataTerm("integer", "1960") |
---|
| 187 | head = ABAC.Role(stateU,"diploma") |
---|
| 188 | head.role_add_data_term(param1) |
---|
| 189 | head.role_add_data_term(param2) |
---|
| 190 | tail = ABAC.Role(jan) |
---|
| 191 | attr=ABAC.Attribute(head, 1800) |
---|
| 192 | attr.attribute_add_tail(tail) |
---|
| 193 | attr.attribute_bake() |
---|
| 194 | attr.attribute_write_cert("StateU_diploma_p__Jan_attr.der") |
---|
| 195 | ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") |
---|
| 196 | print attr.string() |
---|
| 197 | print attr.typed_string() |
---|
| 198 | print "\n" |
---|
| 199 | |
---|