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