[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 | |
---|
[f824a9e] | 33 | ## creating and writing out using libabac ID |
---|
[5110d42] | 34 | id=ABAC.ID("Mary", 0) |
---|
[5d06689] | 35 | print "adding -> %s(good)" % id.id_name() |
---|
| 36 | id.id_write_cert("Mary_ID.pem") |
---|
| 37 | id.id_write_privkey("Mary_private.pem") |
---|
[f824a9e] | 38 | ## load principal with id/key file pair |
---|
| 39 | ## note, with this, we do not have handle on the keyid |
---|
| 40 | ## to Mary but it will be in the db |
---|
[5d06689] | 41 | ctxt.load_id_files("Mary_ID.pem","Mary_private.pem") |
---|
[5110d42] | 42 | |
---|
[f824a9e] | 43 | ## creating principal using ID |
---|
[5110d42] | 44 | nid=ABAC.ID("Jack", 0) |
---|
[5d06689] | 45 | print "adding -> %s(good)" % nid.id_name() |
---|
[f824a9e] | 46 | ## load principal directly with the ID, no external |
---|
| 47 | ## credential files were created |
---|
[5110d42] | 48 | ctxt.load_id(nid) |
---|
| 49 | |
---|
[f824a9e] | 50 | ## creating principal using ID |
---|
[5110d42] | 51 | id=ABAC.ID("Mark", 0) |
---|
[5d06689] | 52 | print "adding -> %s(good)" % id.id_name() |
---|
[f824a9e] | 53 | ## write cert and key content to a combo file. One is appended |
---|
| 54 | ## after another |
---|
[5d06689] | 55 | id.id_write_privkey("Mark_IDKEY.pem") |
---|
| 56 | id.id_write_cert("Mark_IDKEY.pem") |
---|
[f824a9e] | 57 | ## load principal in with the combo file with the tandem format |
---|
[5110d42] | 58 | ctxt.load_id_file("Mark_IDKEY.pem") |
---|
| 59 | |
---|
[f824a9e] | 60 | ## creating principal using ID |
---|
[5110d42] | 61 | id=ABAC.ID("John", 0) |
---|
[5d06689] | 62 | print "adding -> %s(good,invisible)" % id.id_name() |
---|
| 63 | id.id_write_privkey("John_other.pem") |
---|
| 64 | id.id_write_cert("John_other.pem") |
---|
[f824a9e] | 65 | ## load id without the key file |
---|
[d9c3886] | 66 | ctxt.load_id_file("John_other.pem") |
---|
[5110d42] | 67 | |
---|
[f824a9e] | 68 | ## creating principal using ID |
---|
[5110d42] | 69 | id=ABAC.ID("Lori", 0) |
---|
[5d06689] | 70 | print "adding -> %s(good,nokey)" % id.id_name() |
---|
[f824a9e] | 71 | ## write just cert into the combo file |
---|
[5d06689] | 72 | id.id_write_cert("Lori_IDKEY.pem") |
---|
[f824a9e] | 73 | ##load principal from a combo file that only contains cert part |
---|
[5110d42] | 74 | ctxt.load_id_file("Lori_IDKEY.pem") |
---|
| 75 | |
---|
[f824a9e] | 76 | ## creating principal using ID |
---|
[5110d42] | 77 | id=ABAC.ID("Tom", 0) |
---|
[5d06689] | 78 | print "adding -> %s(bad,nocert)" % id.id_name() |
---|
[f824a9e] | 79 | ## write just key into the combo file |
---|
[5d06689] | 80 | id.id_write_privkey("Tom_IDKEY.pem") |
---|
[f824a9e] | 81 | ## load principal from combo file that only contains key part |
---|
[5110d42] | 82 | ctxt.load_id_file("Tom_IDKEY.pem") |
---|
| 83 | |
---|
[f824a9e] | 84 | ## failure case, loading a non-existing combo file |
---|
[5110d42] | 85 | print "adding -> Casper(bad,unknown file)" |
---|
| 86 | ctxt.load_id_file("Casper_IDKEY.pem") |
---|
| 87 | |
---|
| 88 | print "...final principal set..." |
---|
| 89 | out = ctxt.context_principals() |
---|
| 90 | for x in out[1]: |
---|
| 91 | print "%s " % x.string() |
---|
| 92 | print "\n" |
---|
| 93 | |
---|
[5d06689] | 94 | ctxt.dump_yap_db() |
---|