[5110d42] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | to test with python |
---|
| 5 | |
---|
| 6 | cmd1:env keystore=`pwd` ./id.py |
---|
| 7 | cmd2:env ABAC_CN=1 keystore=`pwd` ./id.py |
---|
| 8 | |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | import os |
---|
| 12 | import ABAC |
---|
| 13 | |
---|
| 14 | ctxt = ABAC.Context() |
---|
| 15 | |
---|
| 16 | print "ABAC version %s" % ctxt.version() |
---|
| 17 | |
---|
[f824a9e] | 18 | # Keystore is the directory containing the principal credentials. |
---|
| 19 | # Load existing principals and/or policy credentials |
---|
| 20 | if (os.environ.has_key("keystore")) : |
---|
| 21 | keystore=os.environ["keystore"] |
---|
| 22 | ctxt.load_directory(keystore) |
---|
| 23 | else: |
---|
| 24 | print("keystore is not set...") |
---|
| 25 | exit(1) |
---|
[5110d42] | 26 | |
---|
| 27 | out = ctxt.context_principals() |
---|
| 28 | print "...initial principal set..." |
---|
| 29 | for x in out[1]: |
---|
| 30 | print "%s " % x.string() |
---|
| 31 | print "\n" |
---|
| 32 | |
---|
[09496b3] | 33 | ## case 1 |
---|
[f824a9e] | 34 | ## creating and writing out using libabac ID |
---|
[5110d42] | 35 | id=ABAC.ID("Mary", 0) |
---|
[5d06689] | 36 | print "adding -> %s(good)" % id.id_name() |
---|
[5730a10] | 37 | id.id_write_cert("Mary_ID.pem") |
---|
[5d06689] | 38 | id.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] | 42 | ctxt.load_id_files("Mary_ID.pem","Mary_private.pem") |
---|
[5110d42] | 43 | |
---|
[09496b3] | 44 | ## case 2 |
---|
[f824a9e] | 45 | ## creating principal using ID |
---|
[5110d42] | 46 | nid=ABAC.ID("Jack", 0) |
---|
[5d06689] | 47 | print "adding -> %s(good)" % nid.id_name() |
---|
[f824a9e] | 48 | ## load principal directly with the ID, no external |
---|
| 49 | ## credential files were created |
---|
[5110d42] | 50 | ctxt.load_id(nid) |
---|
| 51 | |
---|
[09496b3] | 52 | ## case 3 |
---|
[f824a9e] | 53 | ## creating principal using ID |
---|
[5110d42] | 54 | id=ABAC.ID("Mark", 0) |
---|
[5d06689] | 55 | print "adding -> %s(good)" % id.id_name() |
---|
[f824a9e] | 56 | ## write cert and key content to a combo file. One is appended |
---|
| 57 | ## after another |
---|
[5d06689] | 58 | id.id_write_privkey("Mark_IDKEY.pem") |
---|
| 59 | id.id_write_cert("Mark_IDKEY.pem") |
---|
[f824a9e] | 60 | ## load principal in with the combo file with the tandem format |
---|
[5110d42] | 61 | ctxt.load_id_file("Mark_IDKEY.pem") |
---|
| 62 | |
---|
[09496b3] | 63 | ## case 4 |
---|
[f824a9e] | 64 | ## creating principal using ID |
---|
[5110d42] | 65 | id=ABAC.ID("John", 0) |
---|
[5d06689] | 66 | print "adding -> %s(good,invisible)" % id.id_name() |
---|
| 67 | id.id_write_privkey("John_other.pem") |
---|
| 68 | id.id_write_cert("John_other.pem") |
---|
[f824a9e] | 69 | ## load id without the key file |
---|
[d9c3886] | 70 | ctxt.load_id_file("John_other.pem") |
---|
[5110d42] | 71 | |
---|
[09496b3] | 72 | ## case 5 |
---|
[f824a9e] | 73 | ## creating principal using ID |
---|
[5110d42] | 74 | id=ABAC.ID("Lori", 0) |
---|
[5d06689] | 75 | print "adding -> %s(good,nokey)" % id.id_name() |
---|
[f824a9e] | 76 | ## write just cert into the combo file |
---|
[5d06689] | 77 | id.id_write_cert("Lori_IDKEY.pem") |
---|
[f824a9e] | 78 | ##load principal from a combo file that only contains cert part |
---|
[5110d42] | 79 | ctxt.load_id_file("Lori_IDKEY.pem") |
---|
| 80 | |
---|
[09496b3] | 81 | ## case 6 |
---|
[f824a9e] | 82 | ## creating principal using ID |
---|
[5110d42] | 83 | id=ABAC.ID("Tom", 0) |
---|
[5d06689] | 84 | print "adding -> %s(bad,nocert)" % id.id_name() |
---|
[f824a9e] | 85 | ## write just key into the combo file |
---|
[5d06689] | 86 | id.id_write_privkey("Tom_IDKEY.pem") |
---|
[f824a9e] | 87 | ## load principal from combo file that only contains key part |
---|
[5110d42] | 88 | ctxt.load_id_file("Tom_IDKEY.pem") |
---|
| 89 | |
---|
[09496b3] | 90 | ## case 7 |
---|
| 91 | ## creating ID using chunk |
---|
| 92 | id=ABAC.ID("Tim", 0) |
---|
[dfe6b61] | 93 | chunk=id.id_cert_chunk() |
---|
| 94 | nid=ABAC.ID_chunk(chunk) |
---|
[09496b3] | 95 | ## load principal from new id file |
---|
| 96 | ctxt.load_id(nid) |
---|
| 97 | |
---|
| 98 | ## case 8 |
---|
[dfe6b61] | 99 | ## load directly using chunk |
---|
| 100 | id=ABAC.ID("Stanley", 0) |
---|
| 101 | chunk=id.id_cert_chunk() |
---|
| 102 | ## load principal as chunk |
---|
| 103 | ctxt.load_id_chunk(chunk) |
---|
| 104 | |
---|
| 105 | ## case 9 |
---|
[f824a9e] | 106 | ## failure case, loading a non-existing combo file |
---|
[5110d42] | 107 | print "adding -> Casper(bad,unknown file)" |
---|
| 108 | ctxt.load_id_file("Casper_IDKEY.pem") |
---|
| 109 | |
---|
[09496b3] | 110 | |
---|
[5110d42] | 111 | print "...final principal set..." |
---|
| 112 | out = ctxt.context_principals() |
---|
| 113 | for x in out[1]: |
---|
| 114 | print "%s " % x.string() |
---|
| 115 | print "\n" |
---|
| 116 | |
---|
[5d06689] | 117 | ctxt.dump_yap_db() |
---|