1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | to test with python |
---|
5 | |
---|
6 | cmd1:env keystore=`pwd` ./id.py |
---|
7 | cmd2:env ABAC_CN=1 keystore=`pwd` ./id.py |
---|
8 | |
---|
9 | """ |
---|
10 | |
---|
11 | import os |
---|
12 | import ABAC |
---|
13 | |
---|
14 | ctxt = ABAC.Context() |
---|
15 | |
---|
16 | print "ABAC version %s" % ctxt.version() |
---|
17 | |
---|
18 | # Keystore is the directory containing the principal credentials. |
---|
19 | # Load existing principals and/or policy credentials |
---|
20 | if (os.environ.has_key("keystore")) : |
---|
21 | keystore=os.environ["keystore"] |
---|
22 | # ctxt.load_directory(keystore) |
---|
23 | else: |
---|
24 | print("keystore is not set...") |
---|
25 | exit(1) |
---|
26 | |
---|
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 | ## case 1 |
---|
34 | ## creating and writing out using libabac ID |
---|
35 | id=ABAC.ID("Mary", 0) |
---|
36 | print "adding -> %s(good)" % id.id_name() |
---|
37 | id.id_write_cert("Mary_ID.pem") |
---|
38 | id.id_write_privkey("Mary_private.pem") |
---|
39 | ## load principal with id/key file pair |
---|
40 | ## note, with this, we do not have handle on the keyid |
---|
41 | ## to Mary but it will be in the db |
---|
42 | ctxt.load_id_files("Mary_ID.pem","Mary_private.pem") |
---|
43 | |
---|
44 | ## case 2 |
---|
45 | ## creating principal using ID |
---|
46 | id2=ABAC.ID("Jack2", 0) |
---|
47 | print "adding -> %s(good)" % id2.id_name() |
---|
48 | ## load principal directly with the ID, no external |
---|
49 | ## credential files were created |
---|
50 | ctxt.load_id(id2) |
---|
51 | |
---|
52 | ## case 3 |
---|
53 | ## creating principal using ID |
---|
54 | id3=ABAC.ID("Mark", 0) |
---|
55 | print "adding -> %s(good)" % id3.id_name() |
---|
56 | ## write cert and key content to a combo file. One is appended |
---|
57 | ## after another |
---|
58 | id3.id_write_privkey("Mark_IDKEY.pem") |
---|
59 | id3.id_write_cert("Mark_IDKEY.pem") |
---|
60 | ## load principal in with the combo file with the tandem format |
---|
61 | ctxt.load_id_file("Mark2_IDKEY.pem") |
---|
62 | |
---|
63 | ## case 4 |
---|
64 | ## creating principal using ID |
---|
65 | id4=ABAC.ID("John", 0) |
---|
66 | print "adding -> %s(good,invisible)" % id4.id_name() |
---|
67 | id4.id_write_cert("John_other.pem") |
---|
68 | ## load id without the key file |
---|
69 | ctxt.load_id_file("John2_other.pem") |
---|
70 | |
---|
71 | ## case 5 |
---|
72 | ## creating principal using ID |
---|
73 | id5=ABAC.ID("Lori", 0) |
---|
74 | print "adding -> %s(good,nokey)" % id5.id_name() |
---|
75 | ## write just cert into the combo file |
---|
76 | id5.id_write_cert("Lori_IDKEY.pem") |
---|
77 | ##load principal from a combo file that only contains cert part |
---|
78 | ctxt.load_id_file("Lori2_IDKEY.pem") |
---|
79 | |
---|
80 | ## case 6 |
---|
81 | ## creating principal using ID |
---|
82 | id6=ABAC.ID("Tom", 0) |
---|
83 | print "adding -> %s(bad,nocert)" % id6.id_name() |
---|
84 | ## write just key into the combo file |
---|
85 | id6.id_write_privkey("Tom_IDKEY.pem") |
---|
86 | ## load principal from combo file that only contains key part |
---|
87 | ctxt.load_id_file("Tom2_IDKEY.pem") |
---|
88 | |
---|
89 | ## case 7 |
---|
90 | ## creating ID using chunk |
---|
91 | ## this already created a Tim with private key and |
---|
92 | ## stored in the master list |
---|
93 | id7=ABAC.ID("Tim", 0) |
---|
94 | chunk=id7.id_cert_chunk() |
---|
95 | id7=ABAC.ID_chunk(chunk) |
---|
96 | ## load principal from new id file |
---|
97 | ctxt.load_id(id7) |
---|
98 | |
---|
99 | ## case 8 |
---|
100 | ## load directly using chunk |
---|
101 | ## this already created a Stanley with private key |
---|
102 | ## and stored in the master list |
---|
103 | id8=ABAC.ID("Stanley", 0) |
---|
104 | chunk=id8.id_cert_chunk() |
---|
105 | ## load principal as chunk |
---|
106 | ctxt.load_id_chunk(chunk) |
---|
107 | |
---|
108 | ## case 9 |
---|
109 | ## failure case, loading a non-existing combo file |
---|
110 | print "adding -> Casper(bad,unknown file)" |
---|
111 | ctxt.load_id_file("Casper_IDKEY.pem") |
---|
112 | |
---|
113 | |
---|
114 | print "...final principal set..." |
---|
115 | out = ctxt.context_principals() |
---|
116 | for x in out[1]: |
---|
117 | print "%s " % x.string() |
---|
118 | print "\n" |
---|
119 | |
---|
120 | #ctxt.context_free_now(); |
---|
121 | #ctxt.dump_yap_db() |
---|