[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) |
---|
[646e57e] | 21 | else: |
---|
| 22 | print("keystore is not set...") |
---|
| 23 | exit(1) |
---|
[7211a95] | 24 | |
---|
| 25 | out = ctxt.context_principals() |
---|
| 26 | print "...initial principal set..." |
---|
| 27 | for x in out[1]: |
---|
| 28 | print "%s " % x.string() |
---|
| 29 | print "\n" |
---|
| 30 | |
---|
| 31 | out = ctxt.context_credentials() |
---|
| 32 | print "...initial policy attribute set..." |
---|
| 33 | for c in out[1]: |
---|
| 34 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 35 | print "\n" |
---|
| 36 | |
---|
[f824a9e] | 37 | # retrieve principals' keyid value from local credential files |
---|
[7211a95] | 38 | alphaID=ABAC.ID("Alpha_ID.pem"); |
---|
| 39 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
| 40 | alpha=alphaID.id_keyid() |
---|
| 41 | |
---|
| 42 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
| 43 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 44 | bob=bobID.id_keyid() |
---|
| 45 | |
---|
| 46 | maryannID=ABAC.ID("Maryann_ID.pem"); |
---|
| 47 | maryannID.id_load_privkey_file("Maryann_private.pem"); |
---|
| 48 | maryann=maryannID.id_keyid() |
---|
| 49 | |
---|
| 50 | joeID=ABAC.ID("Joe_ID.pem"); |
---|
| 51 | joeID.id_load_privkey_file("Joe_private.pem"); |
---|
| 52 | joe=joeID.id_keyid() |
---|
| 53 | |
---|
| 54 | ################################################ |
---|
[f824a9e] | 55 | # Credential 1, intersecting linking roles with This principal param for the |
---|
| 56 | # linked role |
---|
[7211a95] | 57 | # [keyid:alpha].role:payRaise <- |
---|
| 58 | # [keyid:alpha].role:evaluatorOf([principal:?this]).role:goodPerformance & |
---|
| 59 | # [keyid:alpha].role:managerOf([principal:?this]).role:niceCoworker |
---|
| 60 | head=ABAC.Role(alpha,"payRaise") |
---|
[f824a9e] | 61 | |
---|
| 62 | # build a data term with ?This |
---|
[7211a95] | 63 | param=ABAC.DataTerm("principal", "This") |
---|
| 64 | tail1 = ABAC.Role(alpha,"evaluatorOf","goodPerformance") |
---|
[f824a9e] | 65 | |
---|
| 66 | # add the param to the linked role |
---|
[7211a95] | 67 | tail1.role_add_linked_data_term(param) |
---|
[f824a9e] | 68 | |
---|
| 69 | # build another data term with ?This |
---|
[7211a95] | 70 | param=ABAC.DataTerm("principal", "This") |
---|
| 71 | tail2 = ABAC.Role(alpha,"managerOf","niceCoworker") |
---|
[f824a9e] | 72 | |
---|
| 73 | # add the param to the linked role |
---|
[7211a95] | 74 | tail2.role_add_linked_data_term(param) |
---|
[f824a9e] | 75 | |
---|
| 76 | # compose the intersecting role attribute policy |
---|
[7211a95] | 77 | attr=ABAC.Attribute(head, 1800) |
---|
| 78 | attr.attribute_add_tail(tail1) |
---|
| 79 | attr.attribute_add_tail(tail2) |
---|
[f824a9e] | 80 | |
---|
| 81 | # finalize the attribute policy |
---|
[7211a95] | 82 | attr.attribute_bake() |
---|
[f824a9e] | 83 | |
---|
| 84 | # write out the attribute credential file |
---|
[7211a95] | 85 | attr.attribute_write_cert("Alpha_payraise__Alpha_performance_qT_niceguy_qT_attr.der") |
---|
| 86 | ctxt.load_attribute_file("Alpha_payraise__Alpha_performance_qT_niceguy_qT_attr.der") |
---|
| 87 | print attr.string() |
---|
| 88 | print attr.typed_string() |
---|
| 89 | print "\n" |
---|
| 90 | |
---|
| 91 | ################################################# |
---|
[f824a9e] | 92 | # Credential 2, |
---|
[7211a95] | 93 | # [keyid:alpha].role:managerOf([principal:?Z])<- |
---|
| 94 | # [keyid:alpha].role:evaluatorOf([principal:?Z]) |
---|
| 95 | param=ABAC.DataTerm("principal", "Z") |
---|
| 96 | head=ABAC.Role(alpha,"managerOf") |
---|
| 97 | head.role_add_data_term(param) |
---|
| 98 | param=ABAC.DataTerm("principal", "Z") |
---|
| 99 | tail = ABAC.Role(alpha,"evaluatorOf") |
---|
| 100 | tail.role_add_data_term(param) |
---|
| 101 | attr=ABAC.Attribute(head, 1800) |
---|
| 102 | attr.attribute_add_tail(tail) |
---|
| 103 | attr.attribute_bake() |
---|
| 104 | attr.attribute_write_cert("Alpha_manager_qZ__Alpha_evaluator_qZ_attr.der") |
---|
| 105 | ctxt.load_attribute_file("Alpha_manager_qZ__Alpha_evaluator_qZ_attr.der") |
---|
| 106 | print attr.string() |
---|
| 107 | print attr.typed_string() |
---|
| 108 | print "\n" |
---|
| 109 | |
---|
| 110 | ################################################# |
---|
[f824a9e] | 111 | # Credential 3 |
---|
[7211a95] | 112 | # [keyid:alpha].role:evaluatorOf([keyid:Maryann]) <-[keyid:Bob] |
---|
| 113 | param=ABAC.DataTerm(maryannID) |
---|
| 114 | head = ABAC.Role(alpha,"evaluatorOf") |
---|
| 115 | head.role_add_data_term(param) |
---|
| 116 | tail = ABAC.Role(bob) |
---|
| 117 | attr=ABAC.Attribute(head, 1800) |
---|
| 118 | attr.attribute_add_tail(tail) |
---|
| 119 | attr.attribute_bake() |
---|
| 120 | attr.attribute_write_cert("Alpha_evaluator_m__Bob_attr.der") |
---|
| 121 | ctxt.load_attribute_file("Alpha_evaluator_m__Bob_attr.der") |
---|
| 122 | print attr.string() |
---|
| 123 | print attr.typed_string() |
---|
| 124 | print "\n" |
---|
| 125 | |
---|
| 126 | ################################################# |
---|
[f824a9e] | 127 | # Credential 4 |
---|
[7211a95] | 128 | # [keyid:Bob].role:goodPerformance <- [keyid:Maryann]) |
---|
| 129 | head = ABAC.Role(bob,"goodPerformance") |
---|
| 130 | tail = ABAC.Role(maryann) |
---|
| 131 | attr=ABAC.Attribute(head, 1800) |
---|
| 132 | attr.attribute_add_tail(tail) |
---|
| 133 | attr.attribute_bake() |
---|
| 134 | attr.attribute_write_cert("Bob_goodperformance__Maryann_attr.der") |
---|
| 135 | ctxt.load_attribute_file("Bob_goodperformance__Maryann_attr.der") |
---|
| 136 | print attr.string() |
---|
| 137 | print attr.typed_string() |
---|
| 138 | print "\n" |
---|
| 139 | |
---|
| 140 | ################################################# |
---|
[f824a9e] | 141 | # Credential 5 |
---|
[7211a95] | 142 | # [keyid:Bob].role:niceCoworker <- [keyid:Maryann]) |
---|
| 143 | head = ABAC.Role(bob,"niceCoworker") |
---|
| 144 | tail = ABAC.Role(maryann) |
---|
| 145 | attr=ABAC.Attribute(head, 1800) |
---|
| 146 | attr.attribute_add_tail(tail) |
---|
| 147 | attr.attribute_bake() |
---|
| 148 | attr.attribute_write_cert("Bob_nicecoworker__Maryann_attr.der") |
---|
| 149 | ctxt.load_attribute_file("Bob_nicecoworker__Maryann_attr.der") |
---|
| 150 | print attr.string() |
---|
| 151 | print attr.typed_string() |
---|
| 152 | print "\n" |
---|
| 153 | |
---|
| 154 | ################################################# |
---|
[f824a9e] | 155 | # Credential 6 |
---|
[7211a95] | 156 | # [keyid:alpha].role:evaluatorOf([keyid:Joe]) <-[keyid:Bob] |
---|
| 157 | param=ABAC.DataTerm(joeID) |
---|
| 158 | head = ABAC.Role(alpha,"evaluatorOf") |
---|
| 159 | head.role_add_data_term(param) |
---|
| 160 | tail = ABAC.Role(bob) |
---|
| 161 | attr=ABAC.Attribute(head, 1800) |
---|
| 162 | attr.attribute_add_tail(tail) |
---|
| 163 | attr.attribute_bake() |
---|
| 164 | attr.attribute_write_cert("Alpha_evaluator_j__Bob_attr.der") |
---|
| 165 | ctxt.load_attribute_file("Alpha_evaluator_j__Bob_attr.der") |
---|
| 166 | print attr.string() |
---|
| 167 | print attr.typed_string() |
---|
| 168 | print "\n" |
---|
| 169 | |
---|
| 170 | ################################################# |
---|
[f824a9e] | 171 | # Credential 7 |
---|
[7211a95] | 172 | # [keyid:Bob].role:goodPerformance <- [keyid:Joe]) |
---|
| 173 | head = ABAC.Role(bob,"goodPerformance") |
---|
| 174 | tail = ABAC.Role(joe) |
---|
| 175 | attr=ABAC.Attribute(head, 1800) |
---|
| 176 | attr.attribute_add_tail(tail) |
---|
| 177 | attr.attribute_bake() |
---|
| 178 | attr.attribute_write_cert("Bob_goodperformance__Joe_attr.der") |
---|
| 179 | ctxt.load_attribute_file("Bob_goodperformance__Joe_attr.der") |
---|
| 180 | print attr.string() |
---|
| 181 | print attr.typed_string() |
---|
| 182 | print "\n" |
---|
| 183 | |
---|