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