1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | to test with python |
---|
5 | |
---|
6 | cmd1:env keystore=`pwd` ./query.py |
---|
7 | |
---|
8 | """ |
---|
9 | |
---|
10 | import os |
---|
11 | import ABAC |
---|
12 | |
---|
13 | from test_util import runTest |
---|
14 | |
---|
15 | ctxt = ABAC.Context() |
---|
16 | |
---|
17 | def print_a(ctxt, msg, dotdot): |
---|
18 | print "%s rule set..." % msg |
---|
19 | credentials = ctxt.credentials() |
---|
20 | for credential in credentials: |
---|
21 | print "%s:%s<-%s" % (dotdot,credential.head().string(), credential.tail().string()) |
---|
22 | |
---|
23 | # Keystore is the directory containing the principal credentials. |
---|
24 | # Load existing principals and/or policy credentials |
---|
25 | if (os.environ.has_key("keystore")) : |
---|
26 | keystore=os.environ["keystore"] |
---|
27 | else: |
---|
28 | print("keystore is not set, using current directory...") |
---|
29 | ctxt.load_directory(".") |
---|
30 | |
---|
31 | superKID=ABAC.ID("SuperK_ID.pem"); |
---|
32 | superKID.load_privkey("SuperK_private.pem"); |
---|
33 | ctxt.load_id_chunk(superKID.cert_chunk()) |
---|
34 | superK=superKID.keyid() |
---|
35 | |
---|
36 | jackID=ABAC.ID("Jack_ID.pem"); |
---|
37 | jackID.load_privkey("Jack_private.pem"); |
---|
38 | ctxt.load_id_chunk(jackID.cert_chunk()) |
---|
39 | jack=jackID.keyid() |
---|
40 | |
---|
41 | bobID=ABAC.ID("Bob_ID.pem"); |
---|
42 | bobID.load_privkey("Bob_private.pem"); |
---|
43 | ctxt.load_id_chunk(bobID.cert_chunk()) |
---|
44 | bob=bobID.keyid() |
---|
45 | |
---|
46 | maryID=ABAC.ID("Mary_ID.pem"); |
---|
47 | maryID.load_privkey("Mary_private.pem"); |
---|
48 | ctxt.load_id_chunk(maryID.cert_chunk()) |
---|
49 | mary=maryID.keyid() |
---|
50 | |
---|
51 | #case 1: |
---|
52 | #Only employee of SuperK can park |
---|
53 | #[keyid:SuperK].role:park <- [keyid:SuperK].role:employee |
---|
54 | attr = ABAC.Attribute(superKID, "park", 0) |
---|
55 | attr.role(superK,"employee") |
---|
56 | attr.bake() |
---|
57 | attr.write_file("SuperK_park__SuperK_employee_attr.xml") |
---|
58 | ctxt.load_attribute_file("SuperK_park__SuperK_employee_attr.xml") |
---|
59 | print_a(ctxt,"case1","..") |
---|
60 | |
---|
61 | #case 2: |
---|
62 | #Jack is an employee of SuperK |
---|
63 | #[keyid:SuperK].role:employee <- [keyid:Jack] |
---|
64 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
65 | attr.principal(jack) |
---|
66 | attr.bake() |
---|
67 | # create a policy file at the file system |
---|
68 | attr.write_file("SuperK_employee__Jack_attr.xml") |
---|
69 | ctxt.load_attribute_chunk(attr.cert_chunk()); |
---|
70 | print_a(ctxt,"case2","....") |
---|
71 | ########################################################################## |
---|
72 | #Jack of SuperK can park? |
---|
73 | print "===good============ SuperK.park <-?- Jack" |
---|
74 | runTest("python_tests/basic_attribute","test1",ctxt,"%s.park" % superK, jack, 1, "check loading of attribute as cert chunk") |
---|
75 | |
---|
76 | #case 3: |
---|
77 | #Bob is an employee of SuperK |
---|
78 | #[keyid:SuperK].role:employee <- [keyid:Bob] |
---|
79 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
80 | attr.principal(bob) |
---|
81 | attr.bake() |
---|
82 | #chunk=attr.cert_chunk() |
---|
83 | #nattr=ABAC.Attribute_chunk(chunk) |
---|
84 | #ctxt.load_attribute(nattr); |
---|
85 | #print_a(ctxt,"case3", "....") |
---|
86 | |
---|
87 | #case 4: |
---|
88 | #Mary is an employee of SuperK |
---|
89 | #[keyid:SuperK].role:employee <- [keyid:Mary] |
---|
90 | attr = ABAC.Attribute(superKID, "employee", 0) |
---|
91 | attr.principal(mary) |
---|
92 | attr.bake() |
---|
93 | chunk=attr.cert_chunk() |
---|
94 | ctxt.load_attribute_chunk(chunk); |
---|
95 | print_a(ctxt,"case4","......") |
---|
96 | ########################################################################## |
---|
97 | #is Mary an employee of superK? |
---|
98 | print "===good============ SuperK.employee <-?- Mary" |
---|
99 | runTest("python_tests/basic_attribute","test2",ctxt,"%s.employee" % superK, mary, 1, "check loading of attribute as chunk") |
---|
100 | |
---|