[31b67d5] | 1 | import java.io.*; |
---|
[7ef13e3] | 2 | import java.util.*; |
---|
[31b67d5] | 3 | |
---|
| 4 | import edu.uci.ics.jung.graph.*; |
---|
| 5 | |
---|
| 6 | import net.deterlab.abac.Credential; |
---|
[65dbf06] | 7 | import net.deterlab.abac.CredentialFactory; |
---|
[84f0e7a] | 8 | import net.deterlab.abac.Context; |
---|
[31b67d5] | 9 | import net.deterlab.abac.Role; |
---|
[9725efb] | 10 | import net.deterlab.abac.Identity; |
---|
[31b67d5] | 11 | |
---|
[7ef13e3] | 12 | import java.security.KeyPair; |
---|
| 13 | |
---|
| 14 | |
---|
[31b67d5] | 15 | /** |
---|
| 16 | * Simple test of the native Java implementation of ABAC. Loads credentials |
---|
| 17 | * from an rt0 file and runs a query against them. |
---|
| 18 | */ |
---|
| 19 | public class GraphTest { |
---|
| 20 | public static void main(String[] args) throws IOException { |
---|
[008dc25] | 21 | File saveDir = new File("."); |
---|
[31b67d5] | 22 | if (args.length < 3) { |
---|
[7ef13e3] | 23 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[008dc25] | 24 | System.out.println(" runs the query role <-?- principal " |
---|
| 25 | + "and prints the result"); |
---|
[31b67d5] | 26 | System.exit(1); |
---|
| 27 | } |
---|
| 28 | |
---|
[84f0e7a] | 29 | Context ctxt = new Context(); |
---|
[5cf72cc] | 30 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
[31b67d5] | 31 | |
---|
[7ef13e3] | 32 | for (int i= 0; i < args.length-2; i++) { |
---|
| 33 | File f = new File(args[i]); |
---|
[31b67d5] | 34 | |
---|
[7ef13e3] | 35 | try { |
---|
[008dc25] | 36 | if (f.isDirectory()) { |
---|
[6432e35] | 37 | ctxt.load_directory(f, errs); |
---|
[008dc25] | 38 | saveDir = f; |
---|
| 39 | } |
---|
[be05757] | 40 | else if (f.getPath().endsWith(".pem")) |
---|
[6432e35] | 41 | ctxt.load_id_file(f); |
---|
[7ef13e3] | 42 | else if (f.getPath().endsWith(".der")) |
---|
[6432e35] | 43 | ctxt.load_attribute_file(f); |
---|
[5cf72cc] | 44 | else if (f.getPath().endsWith(".zip")) |
---|
[39526ce] | 45 | ctxt.load_zip(f, errs); |
---|
[ad24705] | 46 | else if (f.getPath().endsWith(".rt0")) |
---|
[02d9eec] | 47 | ctxt.load_rt0(f); |
---|
[7ef13e3] | 48 | else |
---|
| 49 | System.out.println(f + " of unknown type"); |
---|
| 50 | } |
---|
| 51 | catch (Exception e) { |
---|
| 52 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 53 | } |
---|
| 54 | } |
---|
[cae7369] | 55 | |
---|
[4822072] | 56 | if (errs.keySet().size() > 0) System.err.println("Errors"); |
---|
[5cf72cc] | 57 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
[be05757] | 58 | |
---|
[4822072] | 59 | System.out.println("All read credentials"); |
---|
| 60 | for (Credential c: ctxt.credentials() ) |
---|
[7f614c1] | 61 | System.out.println(c.simpleString(ctxt) + " " + c.expiration()); |
---|
[4822072] | 62 | System.out.println("End of read credentials"); |
---|
| 63 | |
---|
[31b67d5] | 64 | // |
---|
| 65 | // run the query |
---|
| 66 | // |
---|
| 67 | |
---|
[84f0e7a] | 68 | Role role = new Role(args[args.length-2], ctxt); |
---|
| 69 | Role prin = new Role(args[args.length-1], ctxt); |
---|
[1a80501] | 70 | Context.QueryResult ret = ctxt.query(role.toString(), prin.toString()); |
---|
[84f0e7a] | 71 | Set<Identity> ids = new TreeSet<Identity>(); |
---|
| 72 | |
---|
[1a7e6d3] | 73 | String fn = "attr"; |
---|
| 74 | int n = 0; |
---|
| 75 | String suf = ".der"; |
---|
[1a80501] | 76 | System.out.println("Result: " + ret.getSuccess()); |
---|
[ad24705] | 77 | System.out.println("Proof"); |
---|
[1a80501] | 78 | for (Credential c : ret.getCredentials()) { |
---|
[84f0e7a] | 79 | System.out.println(c.simpleString(ctxt)); |
---|
| 80 | if ( c.hasCertificate()) { |
---|
[008dc25] | 81 | c.write(new File(saveDir, fn + n++ + suf).toString()); |
---|
[d69593c] | 82 | ids.add(c.issuer()); |
---|
[84f0e7a] | 83 | } |
---|
[1a7e6d3] | 84 | } |
---|
[9725efb] | 85 | |
---|
[1a7e6d3] | 86 | fn = "id"; |
---|
| 87 | n = 0; |
---|
| 88 | suf = ".pem"; |
---|
[ad24705] | 89 | System.out.println("Identities"); |
---|
[84f0e7a] | 90 | for (Identity i: ids) { |
---|
[9725efb] | 91 | System.out.println("ID: " + i); |
---|
[008dc25] | 92 | i.write(new File(saveDir, fn + n++ + suf).toString()); |
---|
[1a7e6d3] | 93 | } |
---|
[ad24705] | 94 | try { |
---|
[008dc25] | 95 | ctxt.write_zip(new File(saveDir, "testout.zip"), true, true); |
---|
[ad24705] | 96 | } |
---|
| 97 | catch (IOException ioe) { |
---|
| 98 | System.err.println("Could not write ZIP: " + ioe); |
---|
| 99 | } |
---|
| 100 | System.out.println("rt0 with keyids"); |
---|
| 101 | ctxt.write_rt0(new OutputStreamWriter(System.out), true); |
---|
[31b67d5] | 102 | } |
---|
| 103 | } |
---|