1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | abac_attr.py |
---|
5 | |
---|
6 | To demonstrate how to use ABAC's api in python |
---|
7 | |
---|
8 | call: attr_abac IceCream_ID.pem IceCream_private.pem IceCream_attr.der Chocolate_ID.pem |
---|
9 | |
---|
10 | pre-conditions: generate IceCream_ID.pem and IceCream_private.pem with |
---|
11 | creddy --generate --cn IceCream |
---|
12 | generate Chocolate_ID.pem and Chocolate_private.pem with |
---|
13 | creddy --generate --cn Chocolate |
---|
14 | |
---|
15 | This program will generate an attribute rule, write it out to an external |
---|
16 | file and also load it into the context (prolog db) |
---|
17 | [keyid:IceCream].delicious <- [Keyid:Chocolate] |
---|
18 | |
---|
19 | Then, a query is made against the context to see if it is populated correctly. |
---|
20 | |
---|
21 | Note: Chocolate's principal is loaded without it private key. It does not |
---|
22 | need to because it is not being used to generate attribute credential |
---|
23 | |
---|
24 | """ |
---|
25 | |
---|
26 | from sys import argv, exit |
---|
27 | from ABAC import Context |
---|
28 | from ABAC import ID, Attribute, Role |
---|
29 | |
---|
30 | debug=0 |
---|
31 | |
---|
32 | ## initial context |
---|
33 | ctxt = Context() |
---|
34 | |
---|
35 | print len(argv) |
---|
36 | |
---|
37 | if len(argv) != 5: |
---|
38 | print "Usage: abac_attr.py <cert.pem> <key.pem> <attr.der> <pcert.pem>" |
---|
39 | exit(1) |
---|
40 | |
---|
41 | # load the ID and its key |
---|
42 | id = None |
---|
43 | cid = None |
---|
44 | |
---|
45 | try: |
---|
46 | id = ID(argv[1]) |
---|
47 | id.id_load_privkey_file(argv[2]) |
---|
48 | cid = ID(argv[4]) |
---|
49 | except Exception, e: |
---|
50 | print "Problem loading ID cert: %s" % e |
---|
51 | exit(1) |
---|
52 | |
---|
53 | if debug : |
---|
54 | print "before the load" |
---|
55 | id.print_key_chunk() |
---|
56 | |
---|
57 | # load the id into the context |
---|
58 | ctxt.load_id_chunks(id.id_cert_chunk(), id.id_privkey_chunk()) |
---|
59 | # another way to load the id into the context |
---|
60 | ctxt.load_id(cid) |
---|
61 | #ctxt.load_id_chunks(cid.id_cert_chunk(), cid.id_privkey_chunk()) |
---|
62 | |
---|
63 | if debug : |
---|
64 | print "after the load" |
---|
65 | print "old," |
---|
66 | id.print_key_chunk() |
---|
67 | |
---|
68 | nid=ctxt.lookup_principal(id.id_keyid()) |
---|
69 | if debug : |
---|
70 | print "new," |
---|
71 | nid.print_key_chunk() |
---|
72 | |
---|
73 | #out = ctxt.context_principals() |
---|
74 | #print "\n...final principal set..." |
---|
75 | #for x in out[1]: |
---|
76 | # print "%s " % x.string() |
---|
77 | |
---|
78 | |
---|
79 | # create an attribute cert |
---|
80 | # iceCream.delicous <- chocolate |
---|
81 | head= Role(id.id_keyid(),"delicious") |
---|
82 | tail= Role(cid.id_keyid()) |
---|
83 | |
---|
84 | attr = Attribute(head, 1800) |
---|
85 | attr.attribute_add_tail(tail) |
---|
86 | attr.attribute_bake() |
---|
87 | |
---|
88 | # load attribute cert into the context |
---|
89 | ctxt.load_attribute_chunk(attr.cert_chunk()) |
---|
90 | |
---|
91 | # another way to load the attribute cert into the context, |
---|
92 | #ctxt.load_attribute(attr) |
---|
93 | |
---|
94 | # yet another way to load the attribute cert into the context, |
---|
95 | attr.attribute_write_cert(argv[3]) |
---|
96 | # ctxt.load_attribute_file(argv[3]) |
---|
97 | |
---|
98 | # what is in prolog db |
---|
99 | # ctxt.dump_yap_db() |
---|
100 | |
---|
101 | # run a proof |
---|
102 | role = Role(id.id_keyid(),"delicious") |
---|
103 | p=Role(cid.id_keyid()) |
---|
104 | |
---|
105 | out = ctxt.query(role, p) |
---|
106 | for c in out[1]: |
---|
107 | print "%s <- %s" % (c.head_string(), c.tail_string()) |
---|
108 | |
---|