[669b481] | 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 |
---|
[669b481] | 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) |
---|
| 24 | |
---|
[669b481] | 25 | |
---|
[f824a9e] | 26 | # retrieve principals' keyid value from local credential files |
---|
[669b481] | 27 | out = ctxt.context_principals() |
---|
| 28 | print "...initial principal set..." |
---|
| 29 | for x in out[1]: |
---|
| 30 | print "%s " % x.string() |
---|
| 31 | print "\n" |
---|
| 32 | |
---|
| 33 | out = ctxt.context_credentials() |
---|
| 34 | print "...initial policy attribute set..." |
---|
| 35 | for c in out[1]: |
---|
| 36 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 37 | print "\n" |
---|
| 38 | |
---|
| 39 | aliceID=ABAC.ID("Alice_ID.pem") |
---|
| 40 | aliceID.id_load_privkey_file("Alice_private.pem") |
---|
| 41 | alice=aliceID.id_keyid() |
---|
| 42 | |
---|
| 43 | partyID=ABAC.ID("Party_ID.pem") |
---|
| 44 | partyID.id_load_privkey_file("Party_private.pem") |
---|
| 45 | party=partyID.id_keyid() |
---|
| 46 | |
---|
| 47 | teaID=ABAC.ID("Tea_ID.pem") |
---|
| 48 | teaID.id_load_privkey_file("Tea_private.pem") |
---|
| 49 | tea=teaID.id_keyid() |
---|
| 50 | |
---|
| 51 | hatterID=ABAC.ID("Hatter_ID.pem") |
---|
| 52 | hatterID.id_load_privkey_file("Hatter_private.pem") |
---|
| 53 | hatter=hatterID.id_keyid() |
---|
| 54 | |
---|
| 55 | marchhareID=ABAC.ID("Marchhare_ID.pem") |
---|
| 56 | marchhareID.id_load_privkey_file("Marchhare_private.pem") |
---|
| 57 | marchhare=marchhareID.id_keyid() |
---|
| 58 | |
---|
| 59 | dormouseID=ABAC.ID("Dormouse_ID.pem") |
---|
| 60 | dormouseID.id_load_privkey_file("Dormouse_private.pem") |
---|
| 61 | dormouse=dormouseID.id_keyid() |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | # can not have attribute string more than 382 characters or so |
---|
| 65 | # that would cause encoded attribute string more than 511 in length |
---|
| 66 | # and could not push/pull out of ietf_attribute_t properly |
---|
| 67 | ############################################### |
---|
[f824a9e] | 68 | # NOTE: space and '~' in string value |
---|
| 69 | # time value's optional ending |
---|
[669b481] | 70 | # [keyid:Party].role: about([keyid:tea],[time:20101010T],[boolean:true], |
---|
| 71 | # [integer:4],[float:-200.0],[float:8],[time:20120205T182930], |
---|
[f824a9e] | 72 | # [string:'a list'],[urn:'file://usr/party/~teaparty']) |
---|
[669b481] | 73 | # <- [keyid:Party].role:guestOf([keyid:MarchHare]) |
---|
| 74 | head=ABAC.Role(party,"about") |
---|
| 75 | param=ABAC.DataTerm(teaID) |
---|
| 76 | head.role_add_data_term(param) |
---|
| 77 | param=ABAC.DataTerm("time","20101010T") |
---|
| 78 | head.role_add_data_term(param) |
---|
| 79 | param=ABAC.DataTerm("boolean","true") |
---|
| 80 | head.role_add_data_term(param) |
---|
| 81 | param=ABAC.DataTerm("integer","4") |
---|
| 82 | head.role_add_data_term(param) |
---|
| 83 | param=ABAC.DataTerm("float","-200.0") |
---|
| 84 | head.role_add_data_term(param) |
---|
| 85 | param=ABAC.DataTerm("float","8") |
---|
| 86 | head.role_add_data_term(param) |
---|
| 87 | param=ABAC.DataTerm("time","20120205T182930") |
---|
| 88 | head.role_add_data_term(param) |
---|
[f824a9e] | 89 | param=ABAC.DataTerm("string","'a list'") |
---|
[669b481] | 90 | head.role_add_data_term(param) |
---|
| 91 | param=ABAC.DataTerm("urn","'file://usr/party/~teaparty'") |
---|
| 92 | head.role_add_data_term(param) |
---|
| 93 | tail=ABAC.Role(party,"guestOf") |
---|
| 94 | param=ABAC.DataTerm(marchhareID) |
---|
| 95 | tail.role_add_data_term(param) |
---|
| 96 | attr=ABAC.Attribute(head, 1800) |
---|
| 97 | attr.attribute_add_tail(tail) |
---|
| 98 | attr.attribute_bake() |
---|
| 99 | attr.attribute_write_cert("party_about_party__guestof_marchhare_attr.der") |
---|
| 100 | ctxt.load_attribute_file("party_about_party__guestof_marchhare_attr.der") |
---|
| 101 | print attr.string() |
---|
| 102 | print attr.typed_string() |
---|
| 103 | print "\n" |
---|
| 104 | |
---|
| 105 | ############################################### |
---|
[f824a9e] | 106 | # NOTE: anonymous parameter |
---|
| 107 | # Variable value for different parameter types |
---|
[669b481] | 108 | # [keyid:Party].role:about_other_party([keyid:tea],[?], |
---|
| 109 | # [integer:99],[time:?AAA],[integer:?BBB], |
---|
| 110 | # [boolean:true],[integer:?]) |
---|
| 111 | # <- [keyid:MarchHare] |
---|
| 112 | head=ABAC.Role(party,"about_other_party") |
---|
| 113 | param=ABAC.DataTerm(teaID) |
---|
| 114 | head.role_add_data_term(param) |
---|
| 115 | param=ABAC.DataTerm("anonymous","_") |
---|
| 116 | head.role_add_data_term(param) |
---|
| 117 | param=ABAC.DataTerm("integer","99") |
---|
| 118 | head.role_add_data_term(param) |
---|
| 119 | param=ABAC.DataTerm("time","AAA") |
---|
| 120 | head.role_add_data_term(param) |
---|
| 121 | param=ABAC.DataTerm("integer","BBB") |
---|
| 122 | head.role_add_data_term(param) |
---|
| 123 | param=ABAC.DataTerm("boolean","true") |
---|
| 124 | head.role_add_data_term(param) |
---|
| 125 | param=ABAC.DataTerm("integer","?") |
---|
| 126 | head.role_add_data_term(param) |
---|
| 127 | tail=ABAC.Role(marchhare) |
---|
| 128 | attr=ABAC.Attribute(head, 1800) |
---|
| 129 | attr.attribute_add_tail(tail) |
---|
| 130 | attr.attribute_bake() |
---|
| 131 | attr.attribute_write_cert("party_about_other_party__marchhare_attr.der") |
---|
| 132 | ctxt.load_attribute_file("party_about_other_party__marchhare_attr.der") |
---|
| 133 | print attr.string() |
---|
| 134 | print attr.typed_string() |
---|
| 135 | print "\n" |
---|
| 136 | |
---|
| 137 | ############################################### |
---|
| 138 | # [keyid:Party].role:about_string([string:'a confused party']) |
---|
| 139 | # <- [keyid:MarchHare] |
---|
| 140 | head=ABAC.Role(party,"about_string") |
---|
| 141 | param=ABAC.DataTerm("string","'a confused party'") |
---|
| 142 | head.role_add_data_term(param) |
---|
| 143 | tail=ABAC.Role(marchhare) |
---|
| 144 | attr=ABAC.Attribute(head, 1800) |
---|
| 145 | attr.attribute_add_tail(tail) |
---|
| 146 | attr.attribute_bake() |
---|
| 147 | attr.attribute_write_cert("party_about_string__marchhare_attr.der") |
---|
| 148 | ctxt.load_attribute_file("party_about_string__marchhare_attr.der") |
---|
| 149 | print attr.string() |
---|
| 150 | print attr.typed_string() |
---|
| 151 | print "\n" |
---|
| 152 | |
---|
| 153 | ############################################### |
---|
[f824a9e] | 154 | # NOTE: how to escape within a string value |
---|
[669b481] | 155 | # [keyid:Party].role:about_string2([string:'a mangled \\'string\\'']) |
---|
| 156 | # <- [keyid:MarchHare] |
---|
| 157 | head=ABAC.Role(party,"about_string2") |
---|
| 158 | param=ABAC.DataTerm("string","'a mangled \\'string\\''") |
---|
| 159 | head.role_add_data_term(param) |
---|
| 160 | tail=ABAC.Role(marchhare) |
---|
| 161 | attr=ABAC.Attribute(head, 1800) |
---|
| 162 | attr.attribute_add_tail(tail) |
---|
| 163 | attr.attribute_bake() |
---|
| 164 | attr.attribute_write_cert("party_about_string2__marchhare_attr.der") |
---|
| 165 | ctxt.load_attribute_file("party_about_string2__marchhare_attr.der") |
---|
| 166 | print attr.string() |
---|
| 167 | print attr.typed_string() |
---|
| 168 | print "\n" |
---|
| 169 | |
---|
| 170 | ############################################### |
---|
| 171 | # [keyid:Party].role:about_urn([urn:'file://user/local/party/~sillyparty']) |
---|
| 172 | # <- [keyid:MarchHare] |
---|
| 173 | head=ABAC.Role(party,"about_urn") |
---|
| 174 | param=ABAC.DataTerm("urn","'file://user/local/party/~sillyparty'") |
---|
| 175 | head.role_add_data_term(param) |
---|
| 176 | tail=ABAC.Role(marchhare) |
---|
| 177 | attr=ABAC.Attribute(head, 1800) |
---|
| 178 | attr.attribute_add_tail(tail) |
---|
| 179 | attr.attribute_bake() |
---|
| 180 | attr.attribute_write_cert("party_about_urn__marchhare_attr.der") |
---|
| 181 | ctxt.load_attribute_file("party_about_urn__marchhare_attr.der") |
---|
| 182 | print attr.string() |
---|
| 183 | print attr.typed_string() |
---|
| 184 | print "\n" |
---|
| 185 | |
---|
| 186 | ############################################### |
---|
| 187 | # [keyid:Party].role:about_another_float([float:0.22]) |
---|
| 188 | # <- [keyid:MarchHare] |
---|
| 189 | head=ABAC.Role(party,"about_another_float") |
---|
| 190 | param=ABAC.DataTerm("float","0.22") |
---|
| 191 | head.role_add_data_term(param) |
---|
| 192 | tail=ABAC.Role(marchhare) |
---|
| 193 | attr=ABAC.Attribute(head, 1800) |
---|
| 194 | attr.attribute_add_tail(tail) |
---|
| 195 | attr.attribute_bake() |
---|
| 196 | attr.attribute_write_cert("party_about_third_float__marchhare_attr.der") |
---|
| 197 | ctxt.load_attribute_file("party_about_third_float__marchhare_attr.der") |
---|
| 198 | print attr.string() |
---|
| 199 | print attr.typed_string() |
---|
| 200 | print "\n" |
---|
| 201 | |
---|
| 202 | ############################################### |
---|
| 203 | # [keyid:Party].role:about_another_float([float:8]) |
---|
| 204 | # <- [keyid:MarchHare] |
---|
| 205 | head=ABAC.Role(party,"about_another_float") |
---|
| 206 | param=ABAC.DataTerm("float","8") |
---|
| 207 | head.role_add_data_term(param) |
---|
| 208 | tail=ABAC.Role(marchhare) |
---|
| 209 | attr=ABAC.Attribute(head, 1800) |
---|
| 210 | attr.attribute_add_tail(tail) |
---|
| 211 | attr.attribute_bake() |
---|
| 212 | attr.attribute_write_cert("party_about_another_float__marchhare_attr.der") |
---|
| 213 | ctxt.load_attribute_file("party_about_another_float__marchhare_attr.der") |
---|
| 214 | print attr.string() |
---|
| 215 | print attr.typed_string() |
---|
| 216 | print "\n" |
---|
| 217 | |
---|
| 218 | ############################################### |
---|
| 219 | # [keyid:Party].role:about_float([float:-200.0]) |
---|
| 220 | # <- [keyid:MarchHare] |
---|
| 221 | head=ABAC.Role(party,"about_float") |
---|
| 222 | param=ABAC.DataTerm("float","-200.0") |
---|
| 223 | head.role_add_data_term(param) |
---|
| 224 | tail=ABAC.Role(marchhare) |
---|
| 225 | attr=ABAC.Attribute(head, 1800) |
---|
| 226 | attr.attribute_add_tail(tail) |
---|
| 227 | attr.attribute_bake() |
---|
| 228 | attr.attribute_write_cert("party_about_float__marchhare_attr.der") |
---|
| 229 | ctxt.load_attribute_file("party_about_float__marchhare_attr.der") |
---|
| 230 | print attr.string() |
---|
| 231 | print attr.typed_string() |
---|
| 232 | print "\n" |
---|
| 233 | |
---|
| 234 | ############################################### |
---|
| 235 | # [keyid:Party].role:about_another_integer([integer:-7]) |
---|
| 236 | # <- [keyid:MarchHare] |
---|
| 237 | head=ABAC.Role(party,"about_another_integer") |
---|
| 238 | param=ABAC.DataTerm("integer","-7") |
---|
| 239 | head.role_add_data_term(param) |
---|
| 240 | tail=ABAC.Role(marchhare) |
---|
| 241 | attr=ABAC.Attribute(head, 1800) |
---|
| 242 | attr.attribute_add_tail(tail) |
---|
| 243 | attr.attribute_bake() |
---|
| 244 | attr.attribute_write_cert("party_about_another_integer__marchhare_attr.der") |
---|
| 245 | ctxt.load_attribute_file("party_about_another_integer__marchhare_attr.der") |
---|
| 246 | print attr.string() |
---|
| 247 | print attr.typed_string() |
---|
| 248 | print "\n" |
---|
| 249 | |
---|
| 250 | ############################################### |
---|
| 251 | # [keyid:Party].role:about_integer([integer:4]) |
---|
| 252 | # <- [keyid:MarchHare] |
---|
| 253 | head=ABAC.Role(party,"about_integer") |
---|
| 254 | param=ABAC.DataTerm("integer","4") |
---|
| 255 | head.role_add_data_term(param) |
---|
| 256 | tail=ABAC.Role(marchhare) |
---|
| 257 | attr=ABAC.Attribute(head, 1800) |
---|
| 258 | attr.attribute_add_tail(tail) |
---|
| 259 | attr.attribute_bake() |
---|
| 260 | attr.attribute_write_cert("party_about_integer__marchhare_attr.der") |
---|
| 261 | ctxt.load_attribute_file("party_about_integer__marchhare_attr.der") |
---|
| 262 | print attr.string() |
---|
| 263 | print attr.typed_string() |
---|
| 264 | print "\n" |
---|
| 265 | |
---|
| 266 | ############################################### |
---|
| 267 | # [keyid:Party].role:about_boolean([boolean:true]) |
---|
| 268 | # <- [keyid:MarchHare] |
---|
| 269 | head=ABAC.Role(party,"about_boolean") |
---|
| 270 | param=ABAC.DataTerm("boolean","true") |
---|
| 271 | head.role_add_data_term(param) |
---|
| 272 | tail=ABAC.Role(marchhare) |
---|
| 273 | attr=ABAC.Attribute(head, 1800) |
---|
| 274 | attr.attribute_add_tail(tail) |
---|
| 275 | attr.attribute_bake() |
---|
| 276 | attr.attribute_write_cert("party_about_boolean__marchhare_attr.der") |
---|
| 277 | ctxt.load_attribute_file("party_about_boolean__marchhare_attr.der") |
---|
| 278 | print attr.string() |
---|
| 279 | print attr.typed_string() |
---|
| 280 | print "\n" |
---|
| 281 | |
---|
| 282 | ############################################### |
---|
| 283 | # [keyid:Party].role:about_key([keyid:tea]) |
---|
| 284 | # <- [keyid:MarchHare] |
---|
| 285 | head=ABAC.Role(party,"about_key") |
---|
| 286 | param=ABAC.DataTerm(teaID) |
---|
| 287 | head.role_add_data_term(param) |
---|
| 288 | tail=ABAC.Role(marchhare) |
---|
| 289 | attr=ABAC.Attribute(head, 1800) |
---|
| 290 | attr.attribute_add_tail(tail) |
---|
| 291 | attr.attribute_bake() |
---|
| 292 | attr.attribute_write_cert("party_about_key__marchhare_attr.der") |
---|
| 293 | ctxt.load_attribute_file("party_about_key__marchhare_attr.der") |
---|
| 294 | print attr.string() |
---|
| 295 | print attr.typed_string() |
---|
| 296 | print "\n" |
---|
| 297 | |
---|
| 298 | ############################################### |
---|
| 299 | # [keyid:Party].role:about_another_time([time:20201101T182930]) |
---|
| 300 | # <- [keyid:MarchHare] |
---|
| 301 | head=ABAC.Role(party,"about_another_time") |
---|
| 302 | param=ABAC.DataTerm("time","20201101T182930") |
---|
| 303 | head.role_add_data_term(param) |
---|
| 304 | tail=ABAC.Role(marchhare) |
---|
| 305 | attr=ABAC.Attribute(head, 1800) |
---|
| 306 | attr.attribute_add_tail(tail) |
---|
| 307 | attr.attribute_bake() |
---|
| 308 | attr.attribute_write_cert("party_about_another_time__marchhare_attr.der") |
---|
| 309 | ctxt.load_attribute_file("party_about_another_time__marchhare_attr.der") |
---|
| 310 | print attr.string() |
---|
| 311 | print attr.typed_string() |
---|
| 312 | print "\n" |
---|
| 313 | |
---|
| 314 | ############################################### |
---|
| 315 | # [keyid:Party].role:about_time([time:20201101T]) |
---|
| 316 | # <- [keyid:MarchHare] |
---|
| 317 | head=ABAC.Role(party,"about_time") |
---|
| 318 | param=ABAC.DataTerm("time","20201101T") |
---|
| 319 | head.role_add_data_term(param) |
---|
| 320 | tail=ABAC.Role(marchhare) |
---|
| 321 | attr=ABAC.Attribute(head, 1800) |
---|
| 322 | attr.attribute_add_tail(tail) |
---|
| 323 | attr.attribute_bake() |
---|
| 324 | attr.attribute_write_cert("party_about_time__marchhare_attr.der") |
---|
| 325 | ctxt.load_attribute_file("party_about_time__marchhare_attr.der") |
---|
| 326 | print attr.string() |
---|
| 327 | print attr.typed_string() |
---|
| 328 | print "\n" |
---|
| 329 | |
---|
| 330 | ############################################### |
---|
[f824a9e] | 331 | # NOTE: principal parameter type |
---|
[669b481] | 332 | # [keyid:Party].role:guestof([principal:?V]) |
---|
| 333 | # <- [keyid:Party].role:friendOf([principal:?V]) |
---|
| 334 | # |
---|
| 335 | head=ABAC.Role(party,"guestOf") |
---|
| 336 | param=ABAC.DataTerm("principal","V") |
---|
| 337 | head.role_add_data_term(param) |
---|
| 338 | tail=ABAC.Role(party,"friendOf") |
---|
| 339 | param=ABAC.DataTerm("principal","V") |
---|
| 340 | tail.role_add_data_term(param) |
---|
| 341 | attr=ABAC.Attribute(head, 1800) |
---|
| 342 | attr.attribute_add_tail(tail) |
---|
| 343 | attr.attribute_bake() |
---|
| 344 | attr.attribute_write_cert("party_guestof_qV__party_friendof_qV_attr.der") |
---|
| 345 | ctxt.load_attribute_file("party_guestof_qV__party_friendof_qV_attr.der") |
---|
| 346 | print attr.string() |
---|
| 347 | print attr.typed_string() |
---|
| 348 | print "\n" |
---|
| 349 | |
---|
| 350 | ############################################### |
---|
| 351 | # [keyid:Party].role:guests |
---|
| 352 | # <- [keyid:Party].role:friendOf([keyid:MarchHare]) |
---|
| 353 | # |
---|
| 354 | head=ABAC.Role(party,"guests") |
---|
| 355 | tail=ABAC.Role(party,"friendOf") |
---|
| 356 | param=ABAC.DataTerm(marchhareID) |
---|
| 357 | tail.role_add_data_term(param) |
---|
| 358 | attr=ABAC.Attribute(head, 1800) |
---|
| 359 | attr.attribute_add_tail(tail) |
---|
| 360 | attr.attribute_bake() |
---|
| 361 | attr.attribute_write_cert("party_guests__party_friendof_marchhare_attr.der") |
---|
| 362 | ctxt.load_attribute_file("party_guests__party_friendof_marchhare_attr.der") |
---|
| 363 | print attr.string() |
---|
| 364 | print attr.typed_string() |
---|
| 365 | print "\n" |
---|
| 366 | |
---|
| 367 | ############################################### |
---|
| 368 | # [keyid:Party].role:friendOf([keyid:MarchHare]) |
---|
| 369 | # <- [keyid:Dormouse] |
---|
| 370 | # |
---|
| 371 | head=ABAC.Role(party,"friendOf") |
---|
| 372 | param=ABAC.DataTerm(marchhareID) |
---|
| 373 | head.role_add_data_term(param) |
---|
| 374 | tail=ABAC.Role(dormouse) |
---|
| 375 | attr=ABAC.Attribute(head, 1800) |
---|
| 376 | attr.attribute_add_tail(tail) |
---|
| 377 | attr.attribute_bake() |
---|
| 378 | attr.attribute_write_cert("party_friendof_marchhare__dormouse_attr.der") |
---|
| 379 | ctxt.load_attribute_file("party_friendof_marchhare__dormouse_attr.der") |
---|
| 380 | print attr.string() |
---|
| 381 | print attr.typed_string() |
---|
| 382 | print "\n" |
---|
| 383 | |
---|
| 384 | ############################################### |
---|
[f824a9e] | 385 | # NOTE: named principal tail |
---|
[669b481] | 386 | # [keyid:Party].role:friendOf([keyid:Alice]) |
---|
| 387 | # <- [keyid:Hatter] |
---|
| 388 | # |
---|
| 389 | head=ABAC.Role(party,"friendOf") |
---|
| 390 | param=ABAC.DataTerm(aliceID) |
---|
| 391 | head.role_add_data_term(param) |
---|
| 392 | tail=ABAC.Role(hatter) |
---|
| 393 | attr=ABAC.Attribute(head, 1800) |
---|
| 394 | attr.attribute_add_tail(tail) |
---|
| 395 | attr.attribute_bake() |
---|
| 396 | attr.attribute_write_cert("party_friendof_alice__hatter_attr.der") |
---|
| 397 | ctxt.load_attribute_file("party_friendof_alice__hatter_attr.der") |
---|
| 398 | print attr.string() |
---|
| 399 | print attr.typed_string() |
---|
| 400 | print "\n" |
---|
| 401 | |
---|