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 | |
---|
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 | |
---|
34 | # retrieve principals' keyid value from local credential files |
---|
35 | geniID=ABAC.ID("Geni_ID.pem"); |
---|
36 | geniID.id_load_privkey_file("Geni_private.pem"); |
---|
37 | geni=geniID.id_keyid() |
---|
38 | |
---|
39 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
40 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
41 | bob=bobID.id_keyid() |
---|
42 | |
---|
43 | jackID=ABAC.ID("Jack_ID.pem"); |
---|
44 | jackID.id_load_privkey_file("Jack_private.pem"); |
---|
45 | jack=jackID.id_keyid() |
---|
46 | |
---|
47 | joeID=ABAC.ID("Joe_ID.pem"); |
---|
48 | joeID.id_load_privkey_file("Joe_private.pem"); |
---|
49 | joe=joeID.id_keyid() |
---|
50 | |
---|
51 | ################################################ |
---|
52 | # Credential 1, |
---|
53 | # [keyid:geni].role:leader |
---|
54 | # <- [keyid:geni].role:equivalent([principal:?P[keyid:geni].role:leader]) |
---|
55 | head=ABAC.Role(geni,"leader") |
---|
56 | |
---|
57 | # initialize the role constraint on a principlal |
---|
58 | condrole=ABAC.Role(geni,"leader") |
---|
59 | cond=ABAC.Constraint(condrole) |
---|
60 | |
---|
61 | # make the data term with the role constraint |
---|
62 | param=ABAC.DataTerm("principal","P", cond) |
---|
63 | tail = ABAC.Role(geni,"equivalent") |
---|
64 | tail.role_add_data_term(param) |
---|
65 | |
---|
66 | # build he attribute policy |
---|
67 | attr=ABAC.Attribute(head, 1800) |
---|
68 | attr.attribute_add_tail(tail) |
---|
69 | |
---|
70 | # finalize the policy |
---|
71 | attr.attribute_bake() |
---|
72 | |
---|
73 | # write the policy out to a credential file |
---|
74 | attr.attribute_write_cert("geni_leader__geni_leader_qP_attr.der") |
---|
75 | |
---|
76 | # load the policy into the context using the credential file |
---|
77 | ctxt.load_attribute_file("geni_leader__geni_leader_qP_attr.der") |
---|
78 | print attr.string() |
---|
79 | print attr.typed_string() |
---|
80 | print "\n" |
---|
81 | |
---|
82 | ################################################# |
---|
83 | # Credential 2 |
---|
84 | # [keyid:geni].role:leader <- [keyid:bob] |
---|
85 | head=ABAC.Role(geni,"leader") |
---|
86 | tail = ABAC.Role(bob) |
---|
87 | attr=ABAC.Attribute(head, 1800) |
---|
88 | attr.attribute_add_tail(tail) |
---|
89 | attr.attribute_bake() |
---|
90 | attr.attribute_write_cert("geni_leader__Bob_attr.der") |
---|
91 | ctxt.load_attribute_file("geni_leader__Bob_attr.der") |
---|
92 | print attr.string() |
---|
93 | print attr.typed_string() |
---|
94 | print "\n" |
---|
95 | |
---|
96 | ################################################# |
---|
97 | # Credential 3 |
---|
98 | # [keyid:geni].role:equivalent([keyid:bob]) <- [keyid:Joe] |
---|
99 | param=ABAC.DataTerm(bobID) |
---|
100 | head = ABAC.Role(geni,"equivalent") |
---|
101 | head.role_add_data_term(param) |
---|
102 | tail = ABAC.Role(joe) |
---|
103 | attr=ABAC.Attribute(head, 1800) |
---|
104 | attr.attribute_add_tail(tail) |
---|
105 | attr.attribute_bake() |
---|
106 | attr.attribute_write_cert("geni_equivalent_Bob__Joe_attr.der") |
---|
107 | ctxt.load_attribute_file("geni_equivalent_Bob__Joe_attr.der") |
---|
108 | print attr.string() |
---|
109 | print attr.typed_string() |
---|
110 | print "\n" |
---|
111 | |
---|
112 | ################################################# |
---|
113 | # Credential 4 |
---|
114 | # [keyid:geni].role:equivalent([keyid:Joe]) <- [keyid:Bob] |
---|
115 | param=ABAC.DataTerm(joeID) |
---|
116 | head = ABAC.Role(geni,"equivalent") |
---|
117 | head.role_add_data_term(param) |
---|
118 | tail = ABAC.Role(bob) |
---|
119 | attr=ABAC.Attribute(head, 1800) |
---|
120 | attr.attribute_add_tail(tail) |
---|
121 | attr.attribute_bake() |
---|
122 | attr.attribute_write_cert("geni_equivalent_Joe__Bob_attr.der") |
---|
123 | ctxt.load_attribute_file("geni_equivalent_Joe__Bob_attr.der") |
---|
124 | print attr.string() |
---|
125 | print attr.typed_string() |
---|
126 | print "\n" |
---|