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