source: examples/python_tests/basic_id/id.py @ a59bc06

mei_rt2mei_rt2_fix_1
Last change on this file since a59bc06 was dfe6b61, checked in by Mei <mei@…>, 12 years ago

1) added ID_chunk() and Attribute_chunk() to abac.hh

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