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 | |
---|
23 | # Print the principals and credentials in the keystore |
---|
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 | |
---|
36 | # Construct a "Joe" principal and load it into the ABAC context |
---|
37 | joeID=ABAC.ID("Joe", 0) |
---|
38 | ctxt.load_id(joeID) |
---|
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. |
---|
41 | joeID.id_write_privkey("Joe_private.pem") |
---|
42 | joeID.id_write_cert("Joe_ID.pem") |
---|
43 | # Keep a copy of Joe's key identifier as a string. |
---|
44 | joe=joeID.id_keyid() |
---|
45 | |
---|
46 | # Load alpha and bob from local files (created by ./setup.py) |
---|
47 | alphaID=ABAC.ID("Alpha_ID.pem") |
---|
48 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
49 | alpha=alphaID.id_keyid() |
---|
50 | |
---|
51 | bobID=ABAC.ID("Bob_ID.pem") |
---|
52 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
53 | bob=bobID.id_keyid() |
---|
54 | |
---|
55 | ################################################ |
---|
56 | # Create the credential |
---|
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'") |
---|
60 | head = ABAC.Role(alpha,"access") |
---|
61 | |
---|
62 | # Attach the parameters to the access role |
---|
63 | head.role_add_data_term(param1) |
---|
64 | head.role_add_data_term(param2) |
---|
65 | tail = ABAC.Role(bob) |
---|
66 | |
---|
67 | # Hook the head to the tail |
---|
68 | attr=ABAC.Attribute(head, 1800) |
---|
69 | attr.attribute_add_tail(tail) |
---|
70 | |
---|
71 | # create the credential |
---|
72 | attr.attribute_bake() |
---|
73 | |
---|
74 | # Save a copy and add the credential to the context |
---|
75 | attr.attribute_write_cert("Alpha_access_fileB__Bob_attr.der") |
---|
76 | ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der") |
---|
77 | print attr.string() |
---|
78 | print attr.typed_string() |
---|
79 | print "\n" |
---|
80 | |
---|
81 | # Constructing the others are similar |
---|
82 | |
---|
83 | ################################################# |
---|
84 | ## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob] |
---|
85 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
86 | head = ABAC.Role(alpha,"team") |
---|
87 | head.role_add_data_term(param1) |
---|
88 | tail = ABAC.Role(bob) |
---|
89 | attr=ABAC.Attribute(head, 1800) |
---|
90 | attr.attribute_add_tail(tail) |
---|
91 | attr.attribute_bake() |
---|
92 | attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der") |
---|
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'") |
---|
101 | head = ABAC.Role(alpha,"team") |
---|
102 | head.role_add_data_term(param1) |
---|
103 | tail = ABAC.Role(joe) |
---|
104 | attr=ABAC.Attribute(head, 1800) |
---|
105 | attr.attribute_add_tail(tail) |
---|
106 | attr.attribute_bake() |
---|
107 | attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der") |
---|
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") |
---|
119 | oset.oset_add_data_term(param) |
---|
120 | cond=ABAC.Constraint(oset) |
---|
121 | param2=ABAC.DataTerm("urn", "F", cond) |
---|
122 | param1=ABAC.DataTerm("string", "'Read'") |
---|
123 | head = ABAC.Role(alpha,"access") |
---|
124 | head.role_add_data_term(param1) |
---|
125 | head.role_add_data_term(param2) |
---|
126 | param3=ABAC.DataTerm("string", "P") |
---|
127 | tail = ABAC.Role(alpha,"team") |
---|
128 | tail.role_add_data_term(param3) |
---|
129 | attr=ABAC.Attribute(head, 1800) |
---|
130 | attr.attribute_add_tail(tail) |
---|
131 | attr.attribute_bake() |
---|
132 | attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
133 | ctxt.load_attribute(attr) |
---|
134 | print attr.string() |
---|
135 | print attr.typed_string() |
---|
136 | print "\n" |
---|
137 | |
---|
138 | |
---|
139 | ################################################# |
---|
140 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA'] |
---|
141 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
142 | head = ABAC.Oset(alpha,"documents") |
---|
143 | head.oset_add_data_term(param1) |
---|
144 | obj = ABAC.DataTerm("urn", "'file//fileA'") |
---|
145 | tail= ABAC.Oset(obj) |
---|
146 | attr=ABAC.Attribute(head, 1800) |
---|
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) |
---|
151 | print attr.string() |
---|
152 | print attr.typed_string() |
---|
153 | print "\n" |
---|
154 | |
---|