[2e9455f] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 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 |
---|
| 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 | |
---|
| 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) |
---|
| 21 | else: |
---|
| 22 | print("keystore is not set...") |
---|
| 23 | exit(1) |
---|
| 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 | |
---|
| 37 | # retrieve principals' keyid value from local credential files |
---|
| 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 | markID=ABAC.ID("Mark_ID.pem") |
---|
| 47 | markID.id_load_privkey_file("Mark_private.pem") |
---|
| 48 | mark=markID.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 | maryannID=ABAC.ID("Maryann_ID.pem") |
---|
| 55 | maryannID.id_load_privkey_file("Maryann_private.pem") |
---|
| 56 | maryann=maryannID.id_keyid() |
---|
| 57 | |
---|
| 58 | janID=ABAC.ID("Jan_ID.pem") |
---|
| 59 | janID.id_load_privkey_file("Jan_private.pem") |
---|
| 60 | jan=janID.id_keyid() |
---|
| 61 | |
---|
| 62 | ################################################ |
---|
| 63 | # Credential 1, this policy has two range constraints on different parameters |
---|
| 64 | # [keyid:stateU].role:foundingAlumni |
---|
| 65 | # <- [keyid:stateU].role:diploma([string:?D:['mathmatics','psychology']], |
---|
| 66 | # [integer:?Year:[1960,1961,1963]]) |
---|
| 67 | head = ABAC.Role(stateU,"foundingAlumni") |
---|
| 68 | |
---|
| 69 | # initialize a string range constraint |
---|
| 70 | cond=ABAC.Constraint("string") |
---|
| 71 | |
---|
| 72 | # add specific string values to the constraint |
---|
| 73 | cond.constraint_add_string_target("'mathmatics'") |
---|
| 74 | cond.constraint_add_string_target("'psychology'") |
---|
| 75 | |
---|
| 76 | # create the parameter with the string range constraint |
---|
| 77 | param1=ABAC.DataTerm("string", "D", cond) |
---|
| 78 | |
---|
| 79 | # initialize another constratnt that is of integer type |
---|
| 80 | cond=ABAC.Constraint("integer") |
---|
| 81 | |
---|
| 82 | # add specific integer values to the constraint |
---|
| 83 | cond.constraint_add_integer_target(1960) |
---|
| 84 | cond.constraint_add_integer_target(1961) |
---|
| 85 | cond.constraint_add_integer_target(1963) |
---|
| 86 | |
---|
| 87 | # create the parameter with the integer range constraint |
---|
| 88 | param2=ABAC.DataTerm("integer", "Year", cond) |
---|
| 89 | tail = ABAC.Role(stateU,"diploma") |
---|
| 90 | |
---|
| 91 | # add the parameter with conditions to a role |
---|
| 92 | tail.role_add_data_term(param1) |
---|
| 93 | tail.role_add_data_term(param2) |
---|
| 94 | |
---|
| 95 | # build up the policy rule |
---|
| 96 | attr=ABAC.Attribute(head, 1800) |
---|
| 97 | attr.attribute_add_tail(tail) |
---|
| 98 | |
---|
| 99 | # finalize the policy rule |
---|
| 100 | attr.attribute_bake() |
---|
| 101 | |
---|
| 102 | # save it to a credential file |
---|
| 103 | attr.attribute_write_cert("StateU_foundingAlumni__stateU_diploma_q_qY_attr.der") |
---|
| 104 | print attr.string() |
---|
| 105 | print attr.typed_string() |
---|
| 106 | print "\n" |
---|
| 107 | |
---|
| 108 | ################################################# |
---|
| 109 | # Credential 2 |
---|
| 110 | # [keyid:stateU].role:diploma([string:'mathmatics'],[integer:1961]) <- [keyid:bob] |
---|
| 111 | param1=ABAC.DataTerm("string", "'mathmatics'") |
---|
| 112 | param2=ABAC.DataTerm("integer", "1961") |
---|
| 113 | head = ABAC.Role(stateU,"diploma") |
---|
| 114 | head.role_add_data_term(param1) |
---|
| 115 | head.role_add_data_term(param2) |
---|
| 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("StateU_diploma_m__Bob_attr.der") |
---|
| 121 | print attr.string() |
---|
| 122 | print attr.typed_string() |
---|
| 123 | print "\n" |
---|
| 124 | |
---|
| 125 | ################################################# |
---|
| 126 | # Credential 3 |
---|
| 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 | print attr.string() |
---|
| 139 | print attr.typed_string() |
---|
| 140 | print "\n" |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | ################################################# |
---|
| 144 | # Credential 4 |
---|
| 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 | print attr.string() |
---|
| 157 | print attr.typed_string() |
---|
| 158 | print "\n" |
---|
| 159 | |
---|
| 160 | ################################################# |
---|
| 161 | # Credential 5 |
---|
| 162 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1962]) |
---|
| 163 | # <- [keyid:maryann] |
---|
| 164 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
| 165 | param2=ABAC.DataTerm("integer", "1962") |
---|
| 166 | head = ABAC.Role(stateU,"diploma") |
---|
| 167 | head.role_add_data_term(param1) |
---|
| 168 | head.role_add_data_term(param2) |
---|
| 169 | tail = ABAC.Role(maryann) |
---|
| 170 | attr=ABAC.Attribute(head, 1800) |
---|
| 171 | attr.attribute_add_tail(tail) |
---|
| 172 | attr.attribute_bake() |
---|
| 173 | attr.attribute_write_cert("StateU_diploma_p__Maryann_attr.der") |
---|
| 174 | print attr.string() |
---|
| 175 | print attr.typed_string() |
---|
| 176 | print "\n" |
---|
| 177 | |
---|
| 178 | ################################################# |
---|
| 179 | # Credential 6 |
---|
| 180 | # [keyid:stateU].role:diploma([string:'psychology'],[integer:1960]) |
---|
| 181 | # <- [keyid:jan] |
---|
| 182 | param1=ABAC.DataTerm("string", "'psychology'") |
---|
| 183 | param2=ABAC.DataTerm("integer", "1960") |
---|
| 184 | head = ABAC.Role(stateU,"diploma") |
---|
| 185 | head.role_add_data_term(param1) |
---|
| 186 | head.role_add_data_term(param2) |
---|
| 187 | tail = ABAC.Role(jan) |
---|
| 188 | attr=ABAC.Attribute(head, 1800) |
---|
| 189 | attr.attribute_add_tail(tail) |
---|
| 190 | attr.attribute_bake() |
---|
| 191 | attr.attribute_write_cert("StateU_diploma_p__Jan_attr.der") |
---|
| 192 | print attr.string() |
---|
| 193 | print attr.typed_string() |
---|
| 194 | print "\n" |
---|
| 195 | |
---|
| 196 | ############################### |
---|