source: examples/python_tests/basic_id/id.py @ 7f04233

mei_rt2
Last change on this file since 7f04233 was 2e9455f, checked in by Mei <mei@…>, 11 years ago

1) added namespace
2) tweak ?This,
3) allowing linking role/oset as constraining conditions
4) adding access_tests regression testing that uses GENI's access policy
5) added couple multi contexts regression tests
6) add compression/uncompression calls to abac_encode_string/abac_decode_string
(libstrongwan only allows 512 char for attribute rule storage)
7) add attribute_now option to creddy that takes a whole char string for attribute
rule

  • Property mode set to 100755
File size: 3.1 KB
RevLine 
[5110d42]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
[f824a9e]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"]
[2e9455f]22#    ctxt.load_directory(keystore)
[f824a9e]23else:
24    print("keystore is not set...")
25    exit(1) 
[5110d42]26
27out = ctxt.context_principals()
28print "...initial principal set..."
29for x in out[1]:
30    print "%s " % x.string()
31print "\n" 
32
[09496b3]33## case 1
[f824a9e]34## creating and writing out using libabac ID
[5110d42]35id=ABAC.ID("Mary", 0)
[5d06689]36print "adding -> %s(good)" % id.id_name()
[5730a10]37id.id_write_cert("Mary_ID.pem")
[5d06689]38id.id_write_privkey("Mary_private.pem")
[f824a9e]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
[5d06689]42ctxt.load_id_files("Mary_ID.pem","Mary_private.pem")
[5110d42]43
[09496b3]44## case 2
[f824a9e]45## creating principal using ID
[2e9455f]46id2=ABAC.ID("Jack2", 0)
47print "adding -> %s(good)" % id2.id_name()
[f824a9e]48## load principal directly with the ID, no external
49## credential files were created
[2e9455f]50ctxt.load_id(id2)
[5110d42]51
[09496b3]52## case 3
[f824a9e]53## creating principal using ID
[2e9455f]54id3=ABAC.ID("Mark", 0)
55print "adding -> %s(good)" % id3.id_name()
[f824a9e]56## write cert and key content to a combo file. One is appended
57## after another
[2e9455f]58id3.id_write_privkey("Mark_IDKEY.pem")
59id3.id_write_cert("Mark_IDKEY.pem")
[f824a9e]60## load principal in with the combo file with the tandem format
[2e9455f]61ctxt.load_id_file("Mark2_IDKEY.pem")
[5110d42]62
[09496b3]63## case 4
[f824a9e]64## creating principal using ID
[2e9455f]65id4=ABAC.ID("John", 0)
66print "adding -> %s(good,invisible)" % id4.id_name()
67id4.id_write_cert("John_other.pem")
[f824a9e]68## load id without the key file
[2e9455f]69ctxt.load_id_file("John2_other.pem")
[5110d42]70
[09496b3]71## case 5
[f824a9e]72## creating principal using ID
[2e9455f]73id5=ABAC.ID("Lori", 0)
74print "adding -> %s(good,nokey)" % id5.id_name()
[f824a9e]75## write just cert into the combo file
[2e9455f]76id5.id_write_cert("Lori_IDKEY.pem")
[f824a9e]77##load principal from a combo file that only contains cert part
[2e9455f]78ctxt.load_id_file("Lori2_IDKEY.pem")
[5110d42]79
[09496b3]80## case 6
[f824a9e]81## creating principal using ID
[2e9455f]82id6=ABAC.ID("Tom", 0)
83print "adding -> %s(bad,nocert)" % id6.id_name()
[f824a9e]84## write just key into the combo file
[2e9455f]85id6.id_write_privkey("Tom_IDKEY.pem")
[f824a9e]86## load principal from combo file that only contains key part
[2e9455f]87ctxt.load_id_file("Tom2_IDKEY.pem")
[5110d42]88
[09496b3]89## case 7
90## creating ID using chunk
[2e9455f]91## this already created a Tim with private key and
92## stored in the master list
93id7=ABAC.ID("Tim", 0)
94chunk=id7.id_cert_chunk() 
95id7=ABAC.ID_chunk(chunk)
[09496b3]96## load principal from new id file
[2e9455f]97ctxt.load_id(id7)
[09496b3]98
99## case 8
[dfe6b61]100## load directly using chunk
[2e9455f]101## this already created a Stanley with private key
102## and stored in the master list
103id8=ABAC.ID("Stanley", 0)
104chunk=id8.id_cert_chunk() 
[dfe6b61]105## load principal as chunk
106ctxt.load_id_chunk(chunk)
107
108## case 9
[f824a9e]109## failure case, loading a non-existing combo file
[5110d42]110print "adding -> Casper(bad,unknown file)"
111ctxt.load_id_file("Casper_IDKEY.pem")
112
[09496b3]113
[5110d42]114print "...final principal set..."
115out = ctxt.context_principals()
116for x in out[1]:
117    print "%s " % x.string()
118print "\n"
119
[2e9455f]120#ctxt.context_free_now();
121#ctxt.dump_yap_db()
Note: See TracBrowser for help on using the repository browser.