1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | to test with python |
---|
5 | |
---|
6 | cmd1:env keystore=`pwd` ./attr.py |
---|
7 | """ |
---|
8 | |
---|
9 | import os |
---|
10 | import ABAC |
---|
11 | |
---|
12 | keystore=os.environ["keystore"] |
---|
13 | |
---|
14 | ctxt = ABAC.Context() |
---|
15 | print "ABAC version %s" % ctxt.version() |
---|
16 | |
---|
17 | ctxt.load_directory(keystore) |
---|
18 | |
---|
19 | out = ctxt.context_principals() |
---|
20 | print "...initial principal set..." |
---|
21 | for x in out[1]: |
---|
22 | print "%s " % x.string() |
---|
23 | print "\n" |
---|
24 | |
---|
25 | out = ctxt.context_credentials() |
---|
26 | print "...initial policy attribute set..." |
---|
27 | for c in out[1]: |
---|
28 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
29 | print "\n" |
---|
30 | |
---|
31 | joeID=ABAC.ID("Joe", 0) |
---|
32 | ctxt.load_id(joeID) |
---|
33 | #joeID.id_write_privkey("Joe_IDKEY.pem") |
---|
34 | #joeID.id_write_cert("Joe_IDKEY.pem") |
---|
35 | joeID.id_write_privkey("Joe_private.pem") |
---|
36 | joeID.id_write_cert("Joe_ID.pem") |
---|
37 | joe=joeID.id_keyid() |
---|
38 | |
---|
39 | #ctxt.load_id_file("Alpha_ID.pem","Alpha_private.pem") |
---|
40 | alphaID=ABAC.ID("Alpha_ID.pem") |
---|
41 | alphaID.id_load_privkey_file("Alpha_private.pem"); |
---|
42 | alpha=alphaID.id_keyid() |
---|
43 | |
---|
44 | #ctxt.load_id_file("Bob_ID.pem","Bob_private.pem") |
---|
45 | bobID=ABAC.ID("Bob_ID.pem") |
---|
46 | bobID.id_load_privkey_file("Bob_private.pem"); |
---|
47 | bob=bobID.id_keyid() |
---|
48 | |
---|
49 | ################################################ |
---|
50 | # [keyid:alpha].role:access([string:'Read'],[urn:'file//fileB']) <- [keyid:bob] |
---|
51 | param1=ABAC.DataTerm("string", "'Read'") |
---|
52 | param2=ABAC.DataTerm("urn","'file//fileB'") |
---|
53 | role = ABAC.Role(alpha,"access") |
---|
54 | role.role_add_data_term(param1) |
---|
55 | role.role_add_data_term(param2) |
---|
56 | p = ABAC.Role(bob) |
---|
57 | attr=ABAC.Attribute(role, 1800) |
---|
58 | attr.attribute_add_tail(p) |
---|
59 | attr.attribute_bake() |
---|
60 | attr.attribute_write_cert("Alpha_access_fileB__Bob_attr.der") |
---|
61 | ctxt.load_attribute_file("Alpha_access_fileB__Bob_attr.der") |
---|
62 | print attr.string() |
---|
63 | print attr.typed_string() |
---|
64 | print "\n" |
---|
65 | |
---|
66 | ################################################# |
---|
67 | ## [keyid:alpha].role:team([string:'proj1'])<-[keyid:bob] |
---|
68 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
69 | role = ABAC.Role(alpha,"team") |
---|
70 | role.role_add_data_term(param1) |
---|
71 | tail = ABAC.Role(bob) |
---|
72 | attr=ABAC.Attribute(role, 1800) |
---|
73 | attr.attribute_add_tail(tail) |
---|
74 | attr.attribute_bake() |
---|
75 | attr.attribute_write_cert("Alpha_team_proj1__Bob_attr.der") |
---|
76 | ctxt.load_attribute_file("Alpha_team_proj1__Bob_attr.der") |
---|
77 | print attr.string() |
---|
78 | print attr.typed_string() |
---|
79 | print "\n" |
---|
80 | |
---|
81 | ################################################# |
---|
82 | ## [keyid:alpha].role:team([string:'proj2'])<-[keyid:Joe] |
---|
83 | param1=ABAC.DataTerm("string", "'proj2'") |
---|
84 | role = ABAC.Role(alpha,"team") |
---|
85 | role.role_add_data_term(param1) |
---|
86 | tail = ABAC.Role(joe) |
---|
87 | attr=ABAC.Attribute(role, 1800) |
---|
88 | attr.attribute_add_tail(tail) |
---|
89 | attr.attribute_bake() |
---|
90 | attr.attribute_write_cert("Alpha_team_proj2__Joe_attr.der") |
---|
91 | ctxt.load_attribute_file("Alpha_team_proj2__Joe_attr.der") |
---|
92 | print attr.string() |
---|
93 | print attr.typed_string() |
---|
94 | print "\n" |
---|
95 | |
---|
96 | ## bad beause of that constraint.. |
---|
97 | ################################################ |
---|
98 | # [keyid:alpha].role:access([string:'Read', |
---|
99 | # [urn:?F[keyid:alpha].oset:documents([string:?P])]) |
---|
100 | # <- [keyid:alpha].role:team([string:?P]) |
---|
101 | param=ABAC.DataTerm("string", "P") |
---|
102 | oset=ABAC.Oset(alpha,"documents") |
---|
103 | oset.oset_add_data_term(param) |
---|
104 | cond=ABAC.Constraint(oset) |
---|
105 | param2=ABAC.DataTerm("urn", "F", cond) |
---|
106 | param1=ABAC.DataTerm("string", "'Read'") |
---|
107 | head = ABAC.Role(alpha,"access") |
---|
108 | head.role_add_data_term(param1) |
---|
109 | head.role_add_data_term(param2) |
---|
110 | param3=ABAC.DataTerm("string", "P") |
---|
111 | tail = ABAC.Role(alpha,"team") |
---|
112 | tail.role_add_data_term(param3) |
---|
113 | |
---|
114 | attr=ABAC.Attribute(head, 1800) |
---|
115 | attr.attribute_add_tail(tail) |
---|
116 | attr.attribute_bake() |
---|
117 | attr.attribute_write_cert("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
118 | #ctxt.load_attribute_file("Alpha_access_qFqP__alpha_team_qP_attr.der") |
---|
119 | ctxt.load_attribute(attr) |
---|
120 | #print attr.string() |
---|
121 | #print attr.typed_string() |
---|
122 | #print "\n" |
---|
123 | |
---|
124 | |
---|
125 | ################################################# |
---|
126 | ## [keyid:alpha].oset:documents([string:'proj1'])<-[urn:'file//fileA'] |
---|
127 | param1=ABAC.DataTerm("string", "'proj1'") |
---|
128 | oset = ABAC.Oset(alpha,"documents") |
---|
129 | oset.oset_add_data_term(param1) |
---|
130 | obj = ABAC.DataTerm("urn", "'file//fileA'") |
---|
131 | tail= ABAC.Oset(obj) |
---|
132 | attr=ABAC.Attribute(oset, 1800) |
---|
133 | attr.attribute_add_tail(tail) |
---|
134 | attr.attribute_bake() |
---|
135 | attr.attribute_write_cert("Alpha_team_proj1__fileA_attr.der") |
---|
136 | #ctxt.load_attribute_file("Alpha_team_proj1__fileA_attr.der") |
---|
137 | ctxt.load_attribute(attr) |
---|
138 | print attr.string() |
---|
139 | print attr.typed_string() |
---|
140 | print "\n" |
---|
141 | |
---|
142 | ctxt.dump_yap_db() |
---|
143 | ## |
---|