1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
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 |
---|
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 | |
---|
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) |
---|
22 | else: |
---|
23 | print("keystore is not set...") |
---|
24 | exit(1) |
---|
25 | |
---|
26 | # Print the principals and credentials in the keystore |
---|
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 | # Construct a "Joe" principal and load it into the ABAC context |
---|
40 | joeID=ABAC.ID("Joe", 0) |
---|
41 | ctxt.load_id(joeID) |
---|
42 | # Write out the Joe Principal to 2 files - one for the key and one for the |
---|
43 | # identity. The identity can be shared, but the key must be kept secret. |
---|
44 | joeID.id_write_privkey("Joe_private.pem") |
---|
45 | joeID.id_write_cert("Joe_ID.pem") |
---|
46 | # Keep a copy of Joe's key identifier as a string. |
---|
47 | joe=joeID.id_keyid() |
---|
48 | |
---|
49 | # Load alpha and bob from local files (created by ./setup.py) |
---|
50 | alphaID=ABAC.ID("Alpha_ID.pem") |
---|
51 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
52 | alpha=alphaID.id_keyid() |
---|
53 | |
---|
54 | bobID=ABAC.ID("Bob_ID.pem") |
---|
55 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
56 | bob=bobID.id_keyid() |
---|
57 | |
---|
58 | ################################################ |
---|
59 | # [keyid:alpha].role:access([string:'Read', |
---|
60 | # [urn:?F[keyid:alpha].oset:documents([string:?P])]) |
---|
61 | # <- [keyid:alpha].role:team([string:?P]) |
---|
62 | # Credential 1 |
---|
63 | param=ABAC.DataTerm("string", "P") |
---|
64 | oset=ABAC.Oset(alpha,"documents") |
---|
65 | oset.oset_add_data_term(param) |
---|
66 | cond=ABAC.Constraint(oset) |
---|
67 | param2=ABAC.DataTerm("urn", "F", cond) |
---|
68 | param1=ABAC.DataTerm("string", "'Read'") |
---|
69 | head = ABAC.Role(alpha,"access") |
---|
70 | head.role_add_data_term(param1) |
---|
71 | head.role_add_data_term(param2) |
---|
72 | param3=ABAC.DataTerm("string", "P") |
---|
73 | tail = ABAC.Role(alpha,"team") |
---|
74 | tail.role_add_data_term(param3) |
---|
75 | attr=ABAC.Attribute(head, 1800) |
---|
76 | attr.attribute_add_tail(tail) |
---|
77 | attr.attribute_bake() |
---|
78 | attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
79 | |
---|
80 | ################################################# |
---|
81 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA'] |
---|
82 | ## Credential 2 |
---|
83 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
84 | head = ABAC.Oset(alpha,"documents") |
---|
85 | head.oset_add_data_term(param1) |
---|
86 | obj = ABAC.DataTerm("urn", "'file//fileA'") |
---|
87 | tail= ABAC.Oset(obj) |
---|
88 | attr=ABAC.Attribute(head, 1800) |
---|
89 | attr.attribute_add_tail(tail) |
---|
90 | attr.attribute_bake() |
---|
91 | attr.attribute_write_cert("Alpha_team_proj1__fileA_attr.der") |
---|
92 | |
---|
93 | ################################################# |
---|
94 | ## [keyid:alpha].oset:documents([string:'proj2'])<-[urn:'file//fileB'] |
---|
95 | ## Credential 3 |
---|
96 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
97 | head = ABAC.Oset(alpha,"documents") |
---|
98 | head.oset_add_data_term(param1) |
---|
99 | obj = ABAC.DataTerm("urn", "'file//fileB'") |
---|
100 | tail= ABAC.Oset(obj) |
---|
101 | attr=ABAC.Attribute(head, 1800) |
---|
102 | attr.attribute_add_tail(tail) |
---|
103 | attr.attribute_bake() |
---|
104 | attr.attribute_write_cert("Alpha_team_proj2__fileB_attr.der") |
---|
105 | |
---|
106 | ################################################# |
---|
107 | ## [keyid:alpha].oset:documents([string:'proj2'])<-[urn:'file//fileC'] |
---|
108 | ## Credential 4 |
---|
109 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
110 | head = ABAC.Oset(alpha,"documents") |
---|
111 | head.oset_add_data_term(param1) |
---|
112 | obj = ABAC.DataTerm("urn", "'file//fileC'") |
---|
113 | tail= ABAC.Oset(obj) |
---|
114 | attr=ABAC.Attribute(head, 1800) |
---|
115 | attr.attribute_add_tail(tail) |
---|
116 | attr.attribute_bake() |
---|
117 | attr.attribute_write_cert("Alpha_team_proj2__fileC_attr.der") |
---|
118 | |
---|
119 | ################################################# |
---|
120 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileC'] |
---|
121 | ## Credential 5 |
---|
122 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
123 | head = ABAC.Oset(alpha,"documents") |
---|
124 | head.oset_add_data_term(param1) |
---|
125 | obj = ABAC.DataTerm("urn", "'file//fileC'") |
---|
126 | tail= ABAC.Oset(obj) |
---|
127 | attr=ABAC.Attribute(head, 1800) |
---|
128 | attr.attribute_add_tail(tail) |
---|
129 | attr.attribute_bake() |
---|
130 | attr.attribute_write_cert("Alpha_team_proj1__fileC_attr.der") |
---|
131 | |
---|
132 | ################################################# |
---|
133 | ## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob] |
---|
134 | ## Credential 6 |
---|
135 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
136 | head = ABAC.Role(alpha,"team") |
---|
137 | head.role_add_data_term(param1) |
---|
138 | tail = ABAC.Role(bob) |
---|
139 | attr=ABAC.Attribute(head, 1800) |
---|
140 | attr.attribute_add_tail(tail) |
---|
141 | attr.attribute_bake() |
---|
142 | attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der") |
---|
143 | |
---|
144 | ################################################# |
---|
145 | ## [keyid:alpha].role:team([string:'proj2'])<-[keyid:joe] |
---|
146 | ## Credential 7 |
---|
147 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
148 | head = ABAC.Role(alpha,"team") |
---|
149 | head.role_add_data_term(param1) |
---|
150 | tail = ABAC.Role(joe) |
---|
151 | attr=ABAC.Attribute(head, 1800) |
---|
152 | attr.attribute_add_tail(tail) |
---|
153 | attr.attribute_bake() |
---|
154 | attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der") |
---|
155 | |
---|
156 | |
---|