source: examples/python_tests/acme_friend_rt1/query.py @ 4b46680

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

1) add more doc to python_tests

  • Property mode set to 100755
File size: 3.7 KB
Line 
1#!/usr/bin/env python
2
3"""
4Run the queries described in README
5
6cmd1:env keystore=`pwd` ./query.py
7cmd2: env ABAC_CN=1 keystore=`pwd` ./query.py
8"""
9
10import os
11import ABAC
12
13ctxt = ABAC.Context()
14
15# Keystore is the directory containing the principal credentials.
16# Load existing principals and/or policy credentials
17if (os.environ.has_key("keystore")) :
18    keystore=os.environ["keystore"]
19    ctxt.load_directory(keystore)
20else:
21    print("keystore is not set...")
22    exit(1)
23
24# extract principal's keyid from local principal cert files
25acmeID=ABAC.ID("Acme_ID.pem")
26acmeID.id_load_privkey_file("Acme_private.pem");
27acme=acmeID.id_keyid()
28
29coyoteID=ABAC.ID("Coyote_ID.pem")
30coyoteID.id_load_privkey_file("Coyote_private.pem");
31coyote=coyoteID.id_keyid()
32
33roadrunnerID=ABAC.ID("Roadrunner_ID.pem")
34roadrunnerID.id_load_privkey_file("Roadrunner_private.pem");
35roadrunner=roadrunnerID.id_keyid()
36
37jackrabbitID=ABAC.ID("Jackrabbit_ID.pem")
38jackrabbitID.id_load_privkey_file("Jackrabbit_private.pem");
39jackrabbit=jackrabbitID.id_keyid()
40
41##########################################################################
42# dump the loaded principals/policies
43#
44out = ctxt.context_principals()
45print "\n...final principal set..."
46for x in out[1]:
47    print "%s " % x.string()
48out = ctxt.context_credentials()
49print "\n...final policy attribute set..."
50for c in out[1]:
51    print "%s <- %s" % (c.head_string(), c.tail_string())
52
53##########################################################################
54# is coyote a friend of roadrunner?
55# role =[keyid:Acme].role:friendof([keyid:Roadrunner])
56# p= [keyid:Coyote]
57param=ABAC.DataTerm(roadrunnerID)
58role = ABAC.Role(acme,"friendof")
59role.role_add_data_term(param)
60p = ABAC.Role(coyote)
61print "\n===bad============ Acme.friendOf(Roadrunner) <- Coyote"
62out = ctxt.query(role, p)
63
64for c in out[1]:
65    print "%s <- %s" % (c.head_string(), c.tail_string())
66
67##########################################################################
68# is jackrabbit a friend of roadrunner ?
69# role =[keyid:Acme].role:friendof([keyid:Roadrunner])
70# p= [keyid:Jackrabbit]
71param=ABAC.DataTerm(roadrunnerID)
72role = ABAC.Role(acme,"friendof")
73role.role_add_data_term(param)
74p = ABAC.Role(jackrabbit)
75print "\n===good============ Acme.friendOf(Roadrunner) <- Jackrabbit"
76out = ctxt.query(role, p)
77
78for c in out[1]:
79    print "%s <- %s" % (c.head_string(), c.tail_string())
80
81
82##########################################################################
83# is jackrabbit a preferred_customer of Acme ?
84# role =[keyid:Acme].role:preferred_customer
85# p =[keyid:Jackrabbit]
86role = ABAC.Role(acme,"preferred_customer")
87p = ABAC.Role(jackrabbit)
88print "\n===good============ Acme.preferred_customer <- Jackrabbit"
89out = ctxt.query(role, p)
90
91for c in out[1]:
92    print "%s <- %s" % (c.head_string(), c.tail_string())
93
94##########################################################################
95# is coyote a preferred_customer of Acme ?
96# role =[keyid:Acme].role:preferred_customer
97# p =[keyid:Coyote]
98role = ABAC.Role(acme,"preferred_customer")
99p = ABAC.Role(coyote)
100print "\n===good============ Acme.preferred_customer <- Coyote"
101out = ctxt.query(role, p)
102
103for c in out[1]:
104    print "%s <- %s" % (c.head_string(), c.tail_string())
105
106
107##########################################################################
108# is blah_blah a preferred_customer of Acme ?
109# role =[keyid:Acme].role:preferred_customer
110# p =[keyid:badCoyote]
111role = ABAC.Role(acme,"preferred_customer")
112# some random SHA keyid value
113badcoyote="1fb66793d453226f0b93a48c7f6ae0d51632e628"
114p = ABAC.Role(badcoyote)
115print "\n===bad============ Acme.preferred_customer <- badcoyote"
116out = ctxt.query(role, p)
117
118for c in out[1]:
119    print "%s <- %s" % (c.head_string(), c.tail_string())
120
Note: See TracBrowser for help on using the repository browser.