[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 { |
---|
| 21 | if (args.length < 3) { |
---|
[7ef13e3] | 22 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[31b67d5] | 23 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
| 24 | System.exit(1); |
---|
| 25 | } |
---|
| 26 | |
---|
[84f0e7a] | 27 | Context ctxt = new Context(); |
---|
[d06c453] | 28 | /* Set up to parse XML or X509 (XML first) */ |
---|
| 29 | try { |
---|
| 30 | CredentialFactory cf = new CredentialFactory( |
---|
| 31 | new String[] { |
---|
| 32 | "net.deterlab.abac.GENICredential", |
---|
| 33 | "net.deterlab.abac.X509Credential", |
---|
| 34 | }); |
---|
| 35 | ctxt.setCredentialFactory(cf); |
---|
| 36 | } |
---|
| 37 | catch (Exception e) { |
---|
| 38 | e.printStackTrace(); |
---|
| 39 | System.exit(20); |
---|
| 40 | } |
---|
[5cf72cc] | 41 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
[31b67d5] | 42 | |
---|
[7ef13e3] | 43 | for (int i= 0; i < args.length-2; i++) { |
---|
| 44 | File f = new File(args[i]); |
---|
[31b67d5] | 45 | |
---|
[7ef13e3] | 46 | try { |
---|
| 47 | if (f.isDirectory()) |
---|
[6432e35] | 48 | ctxt.load_directory(f, errs); |
---|
[be05757] | 49 | else if (f.getPath().endsWith(".pem")) |
---|
[6432e35] | 50 | ctxt.load_id_file(f); |
---|
[7ef13e3] | 51 | else if (f.getPath().endsWith(".der")) |
---|
[6432e35] | 52 | ctxt.load_attribute_file(f); |
---|
[5cf72cc] | 53 | else if (f.getPath().endsWith(".zip")) |
---|
[39526ce] | 54 | ctxt.load_zip(f, errs); |
---|
[ad24705] | 55 | else if (f.getPath().endsWith(".rt0")) |
---|
[02d9eec] | 56 | ctxt.load_rt0(f); |
---|
[7ef13e3] | 57 | else |
---|
| 58 | System.out.println(f + " of unknown type"); |
---|
| 59 | } |
---|
| 60 | catch (Exception e) { |
---|
| 61 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 62 | } |
---|
| 63 | } |
---|
[cae7369] | 64 | |
---|
[5cf72cc] | 65 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
[be05757] | 66 | |
---|
[31b67d5] | 67 | // |
---|
| 68 | // run the query |
---|
| 69 | // |
---|
| 70 | |
---|
[84f0e7a] | 71 | Role role = new Role(args[args.length-2], ctxt); |
---|
| 72 | Role prin = new Role(args[args.length-1], ctxt); |
---|
[1a80501] | 73 | Context.QueryResult ret = ctxt.query(role.toString(), prin.toString()); |
---|
[84f0e7a] | 74 | Set<Identity> ids = new TreeSet<Identity>(); |
---|
| 75 | |
---|
[1a7e6d3] | 76 | String fn = "attr"; |
---|
| 77 | int n = 0; |
---|
| 78 | String suf = ".der"; |
---|
[1a80501] | 79 | System.out.println("Result: " + ret.getSuccess()); |
---|
[ad24705] | 80 | System.out.println("Proof"); |
---|
[1a80501] | 81 | for (Credential c : ret.getCredentials()) { |
---|
[84f0e7a] | 82 | System.out.println(c.simpleString(ctxt)); |
---|
| 83 | if ( c.hasCertificate()) { |
---|
| 84 | c.write(fn + n++ + suf); |
---|
[d69593c] | 85 | ids.add(c.issuer()); |
---|
[84f0e7a] | 86 | } |
---|
[1a7e6d3] | 87 | } |
---|
[9725efb] | 88 | |
---|
[1a7e6d3] | 89 | fn = "id"; |
---|
| 90 | n = 0; |
---|
| 91 | suf = ".pem"; |
---|
[ad24705] | 92 | System.out.println("Identities"); |
---|
[84f0e7a] | 93 | for (Identity i: ids) { |
---|
[9725efb] | 94 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 95 | i.write(fn + n++ + suf); |
---|
| 96 | } |
---|
[ad24705] | 97 | try { |
---|
[39526ce] | 98 | ctxt.write_zip(new File("./testout.zip"), true, true); |
---|
[ad24705] | 99 | } |
---|
| 100 | catch (IOException ioe) { |
---|
| 101 | System.err.println("Could not write ZIP: " + ioe); |
---|
| 102 | } |
---|
| 103 | System.out.println("rt0 with keyids"); |
---|
| 104 | ctxt.write_rt0(new OutputStreamWriter(System.out), true); |
---|
[31b67d5] | 105 | } |
---|
| 106 | } |
---|