source: examples/python_tests/acme_rockets_intersection_rt0/attr.py @ c7df2ad

mei_rt2mei_rt2_fix_1
Last change on this file since c7df2ad 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.5 KB
RevLine 
[7211a95]1#!/usr/bin/env python
2
3"""
[f824a9e]4See README in this directory for the semantics of the example.  This file
5constructs the credentials described and puts copies into this directory
[7211a95]6
7cmd1:env keystore=`pwd` ./attr.py
8"""
9import os
10import ABAC
11
12ctxt = ABAC.Context()
13print "ABAC version %s" % ctxt.version()
14
[f824a9e]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)
20
21# retrieve principals' keyid value from local credential files
[7211a95]22acmeID=ABAC.ID("Acme_ID.pem");
23acmeID.id_load_privkey_file("Acme_private.pem");
24ctxt.load_id(acmeID)
25acme=acmeID.id_keyid()
26
27coyoteID=ABAC.ID("Coyote_ID.pem");
28coyoteID.id_load_privkey_file("Coyote_private.pem");
29ctxt.load_id(coyoteID)
30coyote=coyoteID.id_keyid()
31
32warnerbrosID=ABAC.ID("WarnerBros_ID.pem");
33warnerbrosID.id_load_privkey_file("WarnerBros_private.pem");
34ctxt.load_id(warnerbrosID)
35warnerbros=warnerbrosID.id_keyid()
36
37batmanID=ABAC.ID("Batman_ID.pem");
38batmanID.id_load_privkey_file("Batman_private.pem");
39ctxt.load_id(batmanID)
40batman=batmanID.id_keyid()
41
42
43################################################
[f824a9e]44# Credential 1, establish the intersection rule on who can buy
45# rockets from Acme
[7211a95]46#[keyid:Acme].role:buy_rockets <- [keyid:Acme].role:preferred_customer
47#                                    & [keyid:WarnerBros].role:charater
48head = ABAC.Role(acme,"buy_rockets")
49tail1 = ABAC.Role(acme,"preferred_customer")
50tail2 = ABAC.Role(warnerbros,"character")
51attr=ABAC.Attribute(head, 1800)
[f824a9e]52
53# to add intersection, just add multiple tails, make sure they are all
54# roles or all osets
[7211a95]55attr.attribute_add_tail(tail1)
56attr.attribute_add_tail(tail2)
[f824a9e]57
58# finalize the rule
[7211a95]59attr.attribute_bake()
[f824a9e]60
61# save it out as external credential file
[7211a95]62attr.attribute_write_cert("Acme_buy_rockets__Acme_preferred_customer_and_WarnerBros_character_attr.der")
[f824a9e]63
64# load it into context from the just created credential file
[7211a95]65ctxt.load_attribute_file("Acme_buy_rockets__Acme_preferred_customer_and_WarnerBros_character_attr.der")
66print attr.string() 
67print attr.typed_string()
68print "\n"
69
70#################################################
[f824a9e]71# Credential 2
[7211a95]72#[keyid:Acme].role:preferred_customer <- [keyid:Coyote]
73head = ABAC.Role(acme,"preferred_customer")
74tail = ABAC.Role(coyote)
75attr=ABAC.Attribute(head, 1800)
76attr.attribute_add_tail(tail)
77attr.attribute_bake()
78attr.attribute_write_cert("Acme_preferred_customer__Coyote_attr.der")
79ctxt.load_attribute_file("Acme_preferred_customer__Coyote_attr.der")
80print attr.string() 
81print attr.typed_string()
82print "\n"
83
84#################################################
[f824a9e]85# Credential 3
[7211a95]86#[keyid:Acme].role:preferred_customer <- [keyid:Batman]
87head = ABAC.Role(acme,"preferred_customer")
88tail = ABAC.Role(batman)
89attr=ABAC.Attribute(head,1800)
90attr.attribute_add_tail(tail)
91attr.attribute_bake()
92attr.attribute_write_cert("Acme_preferred_customer__Batman_attr.der")
93ctxt.load_attribute_file("Acme_preferred_customer__Batman_attr.der")
94print attr.string() 
95print attr.typed_string()
96print "\n"
97
98
99################################################
[f824a9e]100# Credential 4
[7211a95]101#[keyid:WarnerBros].role:character <- [keyid:Coyote]
102head=ABAC.Role(warnerbros,"character")
103tail = ABAC.Role(coyote)
104attr=ABAC.Attribute(head, 1800)
105attr.attribute_add_tail(tail)
106attr.attribute_bake()
107attr.attribute_write_cert("WarnerBros_character__Coyote_attr.der")
[f824a9e]108
109# demonstrate how attribute can be load from structure insted of a file
[7211a95]110ctxt.load_attribute(attr)
[f824a9e]111print attr.string() 
112print attr.typed_string()
113print "\n"
[7211a95]114
Note: See TracBrowser for help on using the repository browser.