[5110d42] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
[f824a9e] | 4 | See README in this directory for the semantics of the example. This file |
---|
| 5 | creates a principal(Joe) and constructs the credentials described and puts |
---|
| 6 | copies into this directory |
---|
[5110d42] | 7 | |
---|
| 8 | cmd1:env keystore=`pwd` ./attr.py |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | import os |
---|
| 12 | import ABAC |
---|
| 13 | |
---|
| 14 | ctxt = ABAC.Context() |
---|
| 15 | print "ABAC version %s" % ctxt.version() |
---|
| 16 | |
---|
[f824a9e] | 17 | # Keystore is the directory containing the principal credentials. |
---|
| 18 | # Load existing principals and/or policy credentials |
---|
| 19 | if (os.environ.has_key("keystore")) : |
---|
| 20 | keystore=os.environ["keystore"] |
---|
| 21 | ctxt.load_directory(keystore) |
---|
[5110d42] | 22 | |
---|
[47d5cf9] | 23 | # Print the principals and credentials in the keystore |
---|
[5110d42] | 24 | out = ctxt.context_principals() |
---|
| 25 | print "...initial principal set..." |
---|
| 26 | for x in out[1]: |
---|
| 27 | print "%s " % x.string() |
---|
| 28 | print "\n" |
---|
| 29 | |
---|
| 30 | out = ctxt.context_credentials() |
---|
| 31 | print "...initial policy attribute set..." |
---|
| 32 | for c in out[1]: |
---|
| 33 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
| 34 | print "\n" |
---|
| 35 | |
---|
[47d5cf9] | 36 | # Construct a "Joe" principal and load it into the ABAC context |
---|
[5110d42] | 37 | joeID=ABAC.ID("Joe", 0) |
---|
| 38 | ctxt.load_id(joeID) |
---|
[47d5cf9] | 39 | # Write out the Joe Principal to 2 files - one for the key and one for the |
---|
| 40 | # identity. The identity can be shared, but the key must be kept secret. |
---|
[5d06689] | 41 | joeID.id_write_privkey("Joe_private.pem") |
---|
| 42 | joeID.id_write_cert("Joe_ID.pem") |
---|
[f824a9e] | 43 | # Keep a copy of Joe's key identifier as a string. |
---|
[5d06689] | 44 | joe=joeID.id_keyid() |
---|
[5110d42] | 45 | |
---|
[f824a9e] | 46 | # Load alpha and bob from local files (created by ./setup.py) |
---|
[5110d42] | 47 | alphaID=ABAC.ID("Alpha_ID.pem") |
---|
[5d06689] | 48 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
| 49 | alpha=alphaID.id_keyid() |
---|
[5110d42] | 50 | |
---|
| 51 | bobID=ABAC.ID("Bob_ID.pem") |
---|
[5d06689] | 52 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
| 53 | bob=bobID.id_keyid() |
---|
[5110d42] | 54 | |
---|
| 55 | ################################################ |
---|
[47d5cf9] | 56 | # Create the credential |
---|
[5110d42] | 57 | # [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob] |
---|
| 58 | param1=ABAC.DataTerm("string", "'Read'") |
---|
| 59 | param2=ABAC.DataTerm("urn","'file//fileB'") |
---|
[669b481] | 60 | head = ABAC.Role(alpha,"access") |
---|
[47d5cf9] | 61 | |
---|
| 62 | # Attach the parameters to the access role |
---|
[669b481] | 63 | head.role_add_data_term(param1) |
---|
| 64 | head.role_add_data_term(param2) |
---|
| 65 | tail = ABAC.Role(bob) |
---|
[47d5cf9] | 66 | |
---|
| 67 | # Hook the head to the tail |
---|
[669b481] | 68 | attr=ABAC.Attribute(head, 1800) |
---|
| 69 | attr.attribute_add_tail(tail) |
---|
[47d5cf9] | 70 | |
---|
| 71 | # create the credential |
---|
[5d06689] | 72 | attr.attribute_bake() |
---|
[47d5cf9] | 73 | |
---|
| 74 | # Save a copy and add the credential to the context |
---|
[5d06689] | 75 | attr.attribute_write_cert("Alpha_access_fileB__Bob_attr.der") |
---|
[5110d42] | 76 | ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der") |
---|
| 77 | print attr.string() |
---|
| 78 | print attr.typed_string() |
---|
| 79 | print "\n" |
---|
| 80 | |
---|
[47d5cf9] | 81 | # Constructing the others are similar |
---|
| 82 | |
---|
[5110d42] | 83 | ################################################# |
---|
| 84 | ## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob] |
---|
| 85 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
[669b481] | 86 | head = ABAC.Role(alpha,"team") |
---|
| 87 | head.role_add_data_term(param1) |
---|
[5110d42] | 88 | tail = ABAC.Role(bob) |
---|
[669b481] | 89 | attr=ABAC.Attribute(head, 1800) |
---|
[5d06689] | 90 | attr.attribute_add_tail(tail) |
---|
| 91 | attr.attribute_bake() |
---|
| 92 | attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der") |
---|
[5110d42] | 93 | ctxt.load_attribute_file("Alpha_team_proj1__Bob_attr.der") |
---|
| 94 | print attr.string() |
---|
| 95 | print attr.typed_string() |
---|
| 96 | print "\n" |
---|
| 97 | |
---|
| 98 | ################################################# |
---|
| 99 | ## [keyid:alpha].role:team([string:'proj2'])<-[keyid:Joe] |
---|
| 100 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
[669b481] | 101 | head = ABAC.Role(alpha,"team") |
---|
| 102 | head.role_add_data_term(param1) |
---|
[5110d42] | 103 | tail = ABAC.Role(joe) |
---|
[669b481] | 104 | attr=ABAC.Attribute(head, 1800) |
---|
[5d06689] | 105 | attr.attribute_add_tail(tail) |
---|
| 106 | attr.attribute_bake() |
---|
| 107 | attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der") |
---|
[5110d42] | 108 | ctxt.load_attribute_file("Alpha_team_proj2__Joe_attr.der") |
---|
| 109 | print attr.string() |
---|
| 110 | print attr.typed_string() |
---|
| 111 | print "\n" |
---|
| 112 | |
---|
| 113 | ################################################ |
---|
| 114 | # [keyid:alpha].role:access([string:'Read', |
---|
| 115 | # [urn:?F[keyid:alpha].oset:documents([string:?P])]) |
---|
| 116 | # <- [keyid:alpha].role:team([string:?P]) |
---|
| 117 | param=ABAC.DataTerm("string", "P") |
---|
| 118 | oset=ABAC.Oset(alpha,"documents") |
---|
[5d06689] | 119 | oset.oset_add_data_term(param) |
---|
[5110d42] | 120 | cond=ABAC.Constraint(oset) |
---|
| 121 | param2=ABAC.DataTerm("urn", "F", cond) |
---|
| 122 | param1=ABAC.DataTerm("string", "'Read'") |
---|
| 123 | head = ABAC.Role(alpha,"access") |
---|
[5d06689] | 124 | head.role_add_data_term(param1) |
---|
| 125 | head.role_add_data_term(param2) |
---|
[5110d42] | 126 | param3=ABAC.DataTerm("string", "P") |
---|
| 127 | tail = ABAC.Role(alpha,"team") |
---|
[5d06689] | 128 | tail.role_add_data_term(param3) |
---|
[5110d42] | 129 | attr=ABAC.Attribute(head, 1800) |
---|
[5d06689] | 130 | attr.attribute_add_tail(tail) |
---|
| 131 | attr.attribute_bake() |
---|
| 132 | attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
[5110d42] | 133 | ctxt.load_attribute(attr) |
---|
[f824a9e] | 134 | print attr.string() |
---|
| 135 | print attr.typed_string() |
---|
| 136 | print "\n" |
---|
[5110d42] | 137 | |
---|
| 138 | |
---|
| 139 | ################################################# |
---|
| 140 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA'] |
---|
| 141 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
[669b481] | 142 | head = ABAC.Oset(alpha,"documents") |
---|
| 143 | head.oset_add_data_term(param1) |
---|
[5110d42] | 144 | obj = ABAC.DataTerm("urn", "'file//fileA'") |
---|
| 145 | tail= ABAC.Oset(obj) |
---|
[669b481] | 146 | attr=ABAC.Attribute(head, 1800) |
---|
[5d06689] | 147 | attr.attribute_add_tail(tail) |
---|
| 148 | attr.attribute_bake() |
---|
| 149 | attr.attribute_write_cert("Alpha_team_proj1__fileA_attr.der") |
---|
| 150 | ctxt.load_attribute(attr) |
---|
[5110d42] | 151 | print attr.string() |
---|
| 152 | print attr.typed_string() |
---|
| 153 | print "\n" |
---|
| 154 | |
---|