source: examples/python_tests/basic_id/id.py @ 08c8a53

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

1) add more doc to python_tests

  • Property mode set to 100755
File size: 2.6 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## creating and writing out using libabac ID
34id=ABAC.ID("Mary", 0)
35print "adding -> %s(good)" % id.id_name()
36id.id_write_cert("Mary_ID.pem")
37id.id_write_privkey("Mary_private.pem")
38## load principal with id/key file pair
39## note, with this, we do not have handle on the keyid
40## to Mary but it will be in the db
41ctxt.load_id_files("Mary_ID.pem","Mary_private.pem")
42
43## creating principal using ID
44nid=ABAC.ID("Jack", 0)
45print "adding -> %s(good)" % nid.id_name()
46## load principal directly with the ID, no external
47## credential files were created
48ctxt.load_id(nid)
49
50## creating principal using ID
51id=ABAC.ID("Mark", 0)
52print "adding -> %s(good)" % id.id_name()
53## write cert and key content to a combo file. One is appended
54## after another
55id.id_write_privkey("Mark_IDKEY.pem")
56id.id_write_cert("Mark_IDKEY.pem")
57## load principal in with the combo file with the tandem format
58ctxt.load_id_file("Mark_IDKEY.pem")
59
60## creating principal using ID
61id=ABAC.ID("John", 0)
62print "adding -> %s(good,invisible)" % id.id_name()
63id.id_write_privkey("John_other.pem")
64id.id_write_cert("John_other.pem")
65## load id without the key file
66ctxt.load_id_file("John_other.pem")
67
68## creating principal using ID
69id=ABAC.ID("Lori", 0)
70print "adding -> %s(good,nokey)" % id.id_name()
71## write just cert into the combo file
72id.id_write_cert("Lori_IDKEY.pem")
73##load principal from a combo file that only contains cert part
74ctxt.load_id_file("Lori_IDKEY.pem")
75
76## creating principal using ID
77id=ABAC.ID("Tom", 0)
78print "adding -> %s(bad,nocert)" % id.id_name()
79## write just key into the combo file
80id.id_write_privkey("Tom_IDKEY.pem")
81## load principal from combo file that only contains key part
82ctxt.load_id_file("Tom_IDKEY.pem")
83
84## failure case, loading a non-existing combo file
85print "adding -> Casper(bad,unknown file)"
86ctxt.load_id_file("Casper_IDKEY.pem")
87
88print "...final principal set..."
89out = ctxt.context_principals()
90for x in out[1]:
91    print "%s " % x.string()
92print "\n"
93
94ctxt.dump_yap_db()
Note: See TracBrowser for help on using the repository browser.