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 | # Create the credential |
---|
60 | # [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob] |
---|
61 | param1=ABAC.DataTerm("string", "'Read'") |
---|
62 | param2=ABAC.DataTerm("urn","'file//fileB'") |
---|
63 | head = ABAC.Role(alpha,"access") |
---|
64 | |
---|
65 | # Attach the parameters to the access role |
---|
66 | head.role_add_data_term(param1) |
---|
67 | head.role_add_data_term(param2) |
---|
68 | tail = ABAC.Role(bob) |
---|
69 | |
---|
70 | # Hook the head to the tail |
---|
71 | attr=ABAC.Attribute(head, 1800) |
---|
72 | attr.attribute_add_tail(tail) |
---|
73 | |
---|
74 | # create the credential |
---|
75 | attr.attribute_bake() |
---|
76 | |
---|
77 | # Save a copy and add the credential to the context |
---|
78 | attr.attribute_write_cert("Alpha_access_fileB__Bob_attr.der") |
---|
79 | ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der") |
---|
80 | print attr.string() |
---|
81 | print attr.typed_string() |
---|
82 | print "\n" |
---|
83 | |
---|
84 | # Constructing the others are similar |
---|
85 | |
---|
86 | ################################################# |
---|
87 | ## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob] |
---|
88 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
89 | head = ABAC.Role(alpha,"team") |
---|
90 | head.role_add_data_term(param1) |
---|
91 | tail = ABAC.Role(bob) |
---|
92 | attr=ABAC.Attribute(head, 1800) |
---|
93 | attr.attribute_add_tail(tail) |
---|
94 | attr.attribute_bake() |
---|
95 | attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der") |
---|
96 | ctxt.load_attribute_file("Alpha_team_proj1__Bob_attr.der") |
---|
97 | print attr.string() |
---|
98 | print attr.typed_string() |
---|
99 | print "\n" |
---|
100 | |
---|
101 | ################################################# |
---|
102 | ## [keyid:alpha].role:team([string:'proj2'])<-[keyid:Joe] |
---|
103 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
104 | head = ABAC.Role(alpha,"team") |
---|
105 | head.role_add_data_term(param1) |
---|
106 | tail = ABAC.Role(joe) |
---|
107 | attr=ABAC.Attribute(head, 1800) |
---|
108 | attr.attribute_add_tail(tail) |
---|
109 | attr.attribute_bake() |
---|
110 | attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der") |
---|
111 | ctxt.load_attribute_file("Alpha_team_proj2__Joe_attr.der") |
---|
112 | print attr.string() |
---|
113 | print attr.typed_string() |
---|
114 | print "\n" |
---|
115 | |
---|
116 | ################################################ |
---|
117 | # [keyid:alpha].role:access([string:'Read', |
---|
118 | # [urn:?F[keyid:alpha].oset:documents([string:?P])]) |
---|
119 | # <- [keyid:alpha].role:team([string:?P]) |
---|
120 | param=ABAC.DataTerm("string", "P") |
---|
121 | oset=ABAC.Oset(alpha,"documents") |
---|
122 | oset.oset_add_data_term(param) |
---|
123 | cond=ABAC.Constraint(oset) |
---|
124 | param2=ABAC.DataTerm("urn", "F", cond) |
---|
125 | param1=ABAC.DataTerm("string", "'Read'") |
---|
126 | head = ABAC.Role(alpha,"access") |
---|
127 | head.role_add_data_term(param1) |
---|
128 | head.role_add_data_term(param2) |
---|
129 | param3=ABAC.DataTerm("string", "P") |
---|
130 | tail = ABAC.Role(alpha,"team") |
---|
131 | tail.role_add_data_term(param3) |
---|
132 | attr=ABAC.Attribute(head, 1800) |
---|
133 | attr.attribute_add_tail(tail) |
---|
134 | attr.attribute_bake() |
---|
135 | attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
136 | ctxt.load_attribute(attr) |
---|
137 | print attr.string() |
---|
138 | print attr.typed_string() |
---|
139 | print "\n" |
---|
140 | |
---|
141 | |
---|
142 | ################################################# |
---|
143 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA'] |
---|
144 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
145 | head = ABAC.Oset(alpha,"documents") |
---|
146 | head.oset_add_data_term(param1) |
---|
147 | obj = ABAC.DataTerm("urn", "'file//fileA'") |
---|
148 | tail= ABAC.Oset(obj) |
---|
149 | attr=ABAC.Attribute(head, 1800) |
---|
150 | attr.attribute_add_tail(tail) |
---|
151 | attr.attribute_bake() |
---|
152 | attr.attribute_write_cert("Alpha_team_proj1__fileA_attr.der") |
---|
153 | ctxt.load_attribute(attr) |
---|
154 | print attr.string() |
---|
155 | print attr.typed_string() |
---|
156 | print "\n" |
---|
157 | |
---|