[5f551d3] | 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 |
---|
[5f551d3] | 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) |
---|
[5f551d3] | 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 |
---|
[5f551d3] | 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 | joeID=ABAC.ID("Joe_ID.pem") |
---|
| 44 | joeID.id_load_privkey_file("Joe_private.pem") |
---|
| 45 | joe=joeID.id_keyid() |
---|
| 46 | |
---|
| 47 | maryannID=ABAC.ID("Maryann_ID.pem") |
---|
| 48 | maryannID.id_load_privkey_file("Maryann_private.pem") |
---|
| 49 | maryann=maryannID.id_keyid() |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | ################################################ |
---|
[f824a9e] | 53 | # Credential 1, Any alumni of stateU is a member of foundingAlumni if |
---|
| 54 | # they have diploma from those specific years |
---|
[5f551d3] | 55 | # [keyid:stateU].role:foundingAlumni |
---|
[f824a9e] | 56 | # <- [keyid:stateU].role:diploma([?], [integer:?Year:[1960,1961,1963]]) |
---|
[5f551d3] | 57 | head = ABAC.Role(stateU,"foundingAlumni") |
---|
[f824a9e] | 58 | |
---|
| 59 | # constructing an anonymous parameter |
---|
[5f551d3] | 60 | param1=ABAC.DataTerm("anonymous", "_") |
---|
[f824a9e] | 61 | |
---|
| 62 | # initialize a integer range constraint |
---|
[5f551d3] | 63 | cond=ABAC.Constraint("integer") |
---|
[f824a9e] | 64 | |
---|
| 65 | # add specific target value for the integer range constraint |
---|
[5f551d3] | 66 | cond.constraint_add_integer_target(1963) |
---|
[2efdff5] | 67 | cond.constraint_add_integer_target(1961) |
---|
| 68 | cond.constraint_add_integer_target(1960) |
---|
[f824a9e] | 69 | |
---|
| 70 | # create a paramter term that has a range constraint on it, |
---|
| 71 | # the variable name supplied has to start with a capitalized alphabet character |
---|
[5f551d3] | 72 | param2=ABAC.DataTerm("integer", "Year", cond) |
---|
| 73 | tail = ABAC.Role(stateU,"diploma") |
---|
| 74 | tail.role_add_data_term(param1) |
---|
| 75 | tail.role_add_data_term(param2) |
---|
| 76 | attr=ABAC.Attribute(head, 1800) |
---|
[f824a9e] | 77 | |
---|
| 78 | # construct the attribute |
---|
[5f551d3] | 79 | attr.attribute_add_tail(tail) |
---|
[f824a9e] | 80 | |
---|
| 81 | # finalize the attribute |
---|
[5f551d3] | 82 | attr.attribute_bake() |
---|
| 83 | attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
| 84 | ctxt.load_attribute_file("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
| 85 | print attr.string() |
---|
| 86 | print attr.typed_string() |
---|
| 87 | print "\n" |
---|
| 88 | |
---|
| 89 | ################################################# |
---|
[f824a9e] | 90 | # Credential 2 |
---|
[5f551d3] | 91 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1961]) <- [keyid:bob] |
---|
| 92 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
| 93 | param2=ABAC.DataTerm("integer", "1961") |
---|
| 94 | head = ABAC.Role(stateU,"diploma") |
---|
| 95 | head.role_add_data_term(param1) |
---|
| 96 | head.role_add_data_term(param2) |
---|
| 97 | tail = ABAC.Role(bob) |
---|
| 98 | attr=ABAC.Attribute(head, 1800) |
---|
| 99 | attr.attribute_add_tail(tail) |
---|
| 100 | attr.attribute_bake() |
---|
| 101 | attr.attribute_write_cert("StateU_diploma_m__Bob_attr.der") |
---|
| 102 | ctxt.load_attribute_file("StateU_diploma_m__Bob_attr.der") |
---|
| 103 | print attr.string() |
---|
| 104 | print attr.typed_string() |
---|
| 105 | print "\n" |
---|
| 106 | |
---|
| 107 | ################################################# |
---|
[f824a9e] | 108 | # Credential 3 |
---|
[5f551d3] | 109 | # [keyid:stateU].role:diploma([string:'zoology'],[integer:1955]) <- [keyid:joe] |
---|
| 110 | param1=ABAC.DataTerm("string", "'zoology'") |
---|
| 111 | param2=ABAC.DataTerm("integer", "1955") |
---|
| 112 | head = ABAC.Role(stateU,"diploma") |
---|
| 113 | head.role_add_data_term(param1) |
---|
| 114 | head.role_add_data_term(param2) |
---|
| 115 | tail = ABAC.Role(joe) |
---|
| 116 | attr=ABAC.Attribute(head, 1800) |
---|
| 117 | attr.attribute_add_tail(tail) |
---|
| 118 | attr.attribute_bake() |
---|
| 119 | attr.attribute_write_cert("StateU_diploma_z__Joe_attr.der") |
---|
| 120 | ctxt.load_attribute_file("StateU_diploma_z__Joe_attr.der") |
---|
| 121 | print attr.string() |
---|
| 122 | print attr.typed_string() |
---|
| 123 | print "\n" |
---|
| 124 | |
---|
| 125 | ################################################# |
---|
[f824a9e] | 126 | # Credential 4 |
---|
[5f551d3] | 127 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) <- [keyid:maryann] |
---|
| 128 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
| 129 | param2=ABAC.DataTerm("integer", "1962") |
---|
| 130 | head = ABAC.Role(stateU,"diploma") |
---|
| 131 | head.role_add_data_term(param1) |
---|
| 132 | head.role_add_data_term(param2) |
---|
| 133 | tail = ABAC.Role(maryann) |
---|
| 134 | attr=ABAC.Attribute(head, 1800) |
---|
| 135 | attr.attribute_add_tail(tail) |
---|
| 136 | attr.attribute_bake() |
---|
| 137 | attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der") |
---|
| 138 | ctxt.load_attribute_file("StateU_diploma_p__Maryann_attr.der") |
---|
| 139 | print attr.string() |
---|
| 140 | print attr.typed_string() |
---|
| 141 | print "\n" |
---|
| 142 | |
---|