[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 |
---|
| 6 | |
---|
[7211a95] | 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 | leagueID=ABAC.ID("League_ID.pem"); |
---|
| 39 | leagueID.id_load_privkey_file("League_private.pem"); |
---|
| 40 | league=leagueID.id_keyid() |
---|
| 41 | |
---|
| 42 | johnID=ABAC.ID("John_ID.pem"); |
---|
| 43 | johnID.id_load_privkey_file("John_private.pem"); |
---|
| 44 | john=johnID.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 | ################################################ |
---|
[f824a9e] | 51 | # Credential 1, oset constraint on a time parameter |
---|
[7211a95] | 52 | #[keyid:league].role:stadium([string:'access'],[boolean:true], |
---|
| 53 | # [time:?F:[keyid:league].oset.gametime([string:?T])]) |
---|
| 54 | # <-[keyid:league].role:players([string:?T]) |
---|
| 55 | param1=ABAC.DataTerm("string","'access'") |
---|
| 56 | param2=ABAC.DataTerm("boolean","true") |
---|
| 57 | |
---|
[f824a9e] | 58 | # setup the uninstantiated variable as the oset constraint's parameter |
---|
[7211a95] | 59 | param=ABAC.DataTerm("string","T") |
---|
[f824a9e] | 60 | |
---|
| 61 | # make the oset condition |
---|
[7211a95] | 62 | condoset=ABAC.Oset(league,"gametime") |
---|
[f824a9e] | 63 | |
---|
| 64 | # add the parameter for the constraint |
---|
[7211a95] | 65 | condoset.oset_add_data_term(param) |
---|
[f824a9e] | 66 | |
---|
| 67 | # create the constraint with the oset condition |
---|
[7211a95] | 68 | cond=ABAC.Constraint(condoset) |
---|
[f824a9e] | 69 | |
---|
| 70 | # make the data term that is being constrained |
---|
[7211a95] | 71 | param3=ABAC.DataTerm("time", "F", cond) |
---|
| 72 | head = ABAC.Role(league,"stadium") |
---|
| 73 | head.role_add_data_term(param1) |
---|
| 74 | head.role_add_data_term(param2) |
---|
| 75 | head.role_add_data_term(param3) |
---|
| 76 | param=ABAC.DataTerm("string", "T") |
---|
| 77 | tail = ABAC.Role(league,"players") |
---|
| 78 | tail.role_add_data_term(param) |
---|
[f824a9e] | 79 | |
---|
| 80 | # build up the attribute policy |
---|
[7211a95] | 81 | attr=ABAC.Attribute(head, 1800) |
---|
| 82 | attr.attribute_add_tail(tail) |
---|
[f824a9e] | 83 | |
---|
| 84 | # finalize the policy |
---|
[7211a95] | 85 | attr.attribute_bake() |
---|
[f824a9e] | 86 | |
---|
| 87 | # create the credential file for this policy |
---|
[7211a95] | 88 | attr.attribute_write_cert("League_access_qFqT__League_players_qT_attr.der") |
---|
| 89 | ctxt.load_attribute_file("League_access_qFqT__League_players_qT_attr.der") |
---|
| 90 | print attr.string() |
---|
| 91 | print attr.typed_string() |
---|
| 92 | print "\n" |
---|
| 93 | |
---|
| 94 | ################################################ |
---|
[f824a9e] | 95 | # Credential 2, 2 different range constraints, one on a boolean and |
---|
| 96 | # and the other is on a time |
---|
[7211a95] | 97 | #[keyid:league].role:stadium([string:'access'],[boolean:?B:[true], |
---|
| 98 | # [time:?F:[20120228T080000..20120228T090000]]) |
---|
| 99 | # <- [keyid:league].role:players(string:?T) |
---|
| 100 | param1=ABAC.DataTerm("string","'access'") |
---|
[f824a9e] | 101 | |
---|
| 102 | # generate a range constraint with a single target value |
---|
[7211a95] | 103 | cond=ABAC.Constraint("boolean") |
---|
| 104 | cond.constraint_add_boolean_target("true") |
---|
| 105 | param2=ABAC.DataTerm("boolean", "B", cond) |
---|
[f824a9e] | 106 | |
---|
| 107 | # generate a range constraint with a min and a max |
---|
[7211a95] | 108 | cond=ABAC.Constraint("time") |
---|
| 109 | cond.constraint_add_time_min("20120228T080000") |
---|
| 110 | cond.constraint_add_time_max("20120228T090000") |
---|
| 111 | param3=ABAC.DataTerm("time", "F", cond) |
---|
| 112 | head = ABAC.Role(league,"stadium") |
---|
| 113 | head.role_add_data_term(param1) |
---|
| 114 | head.role_add_data_term(param2) |
---|
| 115 | head.role_add_data_term(param3) |
---|
| 116 | |
---|
| 117 | param=ABAC.DataTerm("string", "T") |
---|
| 118 | tail = ABAC.Role(league,"players") |
---|
| 119 | tail.role_add_data_term(param) |
---|
| 120 | |
---|
| 121 | attr=ABAC.Attribute(head, 1800) |
---|
| 122 | attr.attribute_add_tail(tail) |
---|
| 123 | attr.attribute_bake() |
---|
| 124 | attr.attribute_write_cert("League_access_qR__League_players_qT_attr.der") |
---|
| 125 | ctxt.load_attribute_file("League_access_qR__League_players_qT_attr.der") |
---|
| 126 | print attr.string() |
---|
| 127 | print attr.typed_string() |
---|
| 128 | print "\n" |
---|
| 129 | |
---|
| 130 | ################################################# |
---|
[f824a9e] | 131 | # Credential 3 |
---|
[7211a95] | 132 | # [keyid:league].oset:gametime([string:'north']) |
---|
| 133 | # <- [time:20120228T130000] |
---|
| 134 | param=ABAC.DataTerm("string","'north'") |
---|
| 135 | head = ABAC.Oset(league,"gametime") |
---|
| 136 | head.oset_add_data_term(param) |
---|
| 137 | term=ABAC.DataTerm("time", "20120228T130000") |
---|
| 138 | tail = ABAC.Oset(term) |
---|
| 139 | attr=ABAC.Attribute(head, 1800) |
---|
| 140 | attr.attribute_add_tail(tail) |
---|
| 141 | attr.attribute_bake() |
---|
| 142 | attr.attribute_write_cert("League_gametime_north__timeT_attr.der") |
---|
| 143 | ctxt.load_attribute_file("League_gametime_north__timeT_attr.der") |
---|
| 144 | print attr.string() |
---|
| 145 | print attr.typed_string() |
---|
| 146 | print "\n" |
---|
| 147 | |
---|
| 148 | ################################################# |
---|
[f824a9e] | 149 | # Credential 4 |
---|
[7211a95] | 150 | # [keyid:league].oset:gametime([string:'south']) |
---|
| 151 | # <-[time:20120228T140000] |
---|
| 152 | param=ABAC.DataTerm("string","'south'") |
---|
| 153 | head = ABAC.Oset(league,"gametime") |
---|
| 154 | head.oset_add_data_term(param) |
---|
| 155 | term=ABAC.DataTerm("time", "20120228T140000") |
---|
| 156 | tail = ABAC.Oset(term) |
---|
| 157 | attr=ABAC.Attribute(head, 1800) |
---|
| 158 | attr.attribute_add_tail(tail) |
---|
| 159 | attr.attribute_bake() |
---|
| 160 | attr.attribute_write_cert("League_gametime_south__time2T_attr.der") |
---|
| 161 | ctxt.load_attribute_file("League_gametime_south__time2T_attr.der") |
---|
| 162 | print attr.string() |
---|
| 163 | print attr.typed_string() |
---|
| 164 | print "\n" |
---|
| 165 | |
---|
| 166 | ################################################# |
---|
[f824a9e] | 167 | # Credential 5 |
---|
[7211a95] | 168 | # [keyid:league].role:players([string:'north'])<-[keyid:John] |
---|
| 169 | param=ABAC.DataTerm("string", "'north'") |
---|
| 170 | head = ABAC.Role(league,"players") |
---|
| 171 | head.role_add_data_term(param) |
---|
| 172 | tail = ABAC.Role(john) |
---|
| 173 | attr=ABAC.Attribute(head, 1800) |
---|
| 174 | attr.attribute_add_tail(tail) |
---|
| 175 | attr.attribute_bake() |
---|
| 176 | attr.attribute_write_cert("League_players_north__John_attr.der") |
---|
| 177 | ctxt.load_attribute_file("League_players_north__John_attr.der") |
---|
| 178 | print attr.string() |
---|
| 179 | print attr.typed_string() |
---|
| 180 | print "\n" |
---|
| 181 | |
---|
| 182 | ################################################# |
---|
[f824a9e] | 183 | # Credential 6 |
---|
[7211a95] | 184 | # [keyid:league].role:players([string:'south'])<-[keyid:Mark] |
---|
| 185 | param=ABAC.DataTerm("string", "'south'") |
---|
| 186 | head = ABAC.Role(league,"players") |
---|
| 187 | head.role_add_data_term(param) |
---|
| 188 | tail = ABAC.Role(mark) |
---|
| 189 | attr=ABAC.Attribute(head, 1800) |
---|
| 190 | attr.attribute_add_tail(tail) |
---|
| 191 | attr.attribute_bake() |
---|
| 192 | attr.attribute_write_cert("League_players_south__Mark_attr.der") |
---|
| 193 | ctxt.load_attribute_file("League_players_north__John_attr.der") |
---|
| 194 | print attr.string() |
---|
| 195 | print attr.typed_string() |
---|
| 196 | print "\n" |
---|
| 197 | |
---|
| 198 | |
---|