[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; |
---|
| 7 | import net.deterlab.abac.CredentialGraph; |
---|
| 8 | import net.deterlab.abac.Query; |
---|
| 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 | |
---|
| 27 | CredentialGraph graph = new CredentialGraph(); |
---|
[5cf72cc] | 28 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
[31b67d5] | 29 | |
---|
[7ef13e3] | 30 | for (int i= 0; i < args.length-2; i++) { |
---|
| 31 | File f = new File(args[i]); |
---|
[31b67d5] | 32 | |
---|
[7ef13e3] | 33 | try { |
---|
| 34 | if (f.isDirectory()) |
---|
[e9360e2] | 35 | for (Credential c :Credential.readDirectory(f, errs)) |
---|
[be05757] | 36 | graph.add_credential(c); |
---|
| 37 | else if (f.getPath().endsWith(".pem")) |
---|
| 38 | Credential.addIdentity(new Identity(f)); |
---|
[7ef13e3] | 39 | else if (f.getPath().endsWith(".der")) |
---|
[be05757] | 40 | graph.add_credential(new Credential(f)); |
---|
[5cf72cc] | 41 | else if (f.getPath().endsWith(".zip")) |
---|
[e9360e2] | 42 | for (Credential c :Credential.readZipFile(f, errs)) |
---|
[5cf72cc] | 43 | graph.add_credential(c); |
---|
[7ef13e3] | 44 | else |
---|
| 45 | System.out.println(f + " of unknown type"); |
---|
| 46 | } |
---|
| 47 | catch (Exception e) { |
---|
| 48 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 49 | } |
---|
| 50 | } |
---|
[cae7369] | 51 | |
---|
[5cf72cc] | 52 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
[be05757] | 53 | |
---|
[31b67d5] | 54 | // |
---|
| 55 | // run the query |
---|
| 56 | // |
---|
| 57 | |
---|
[de63a31] | 58 | Role role = new Role(args[args.length-2], true); |
---|
| 59 | Role prin = new Role(args[args.length-1], true); |
---|
[31b67d5] | 60 | Query q = graph.querier(); |
---|
[de63a31] | 61 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
[1a7e6d3] | 62 | String fn = "attr"; |
---|
| 63 | int n = 0; |
---|
| 64 | String suf = ".der"; |
---|
| 65 | for (Credential c : ret.getEdges()) { |
---|
[de63a31] | 66 | System.out.println(c.simpleString()); |
---|
[f6789db] | 67 | if ( c.hasCertificate()) c.write(fn + n++ + suf); |
---|
[1a7e6d3] | 68 | } |
---|
[9725efb] | 69 | |
---|
[1a7e6d3] | 70 | fn = "id"; |
---|
| 71 | n = 0; |
---|
| 72 | suf = ".pem"; |
---|
| 73 | for (Identity i: Credential.identities()) { |
---|
[9725efb] | 74 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 75 | i.write(fn + n++ + suf); |
---|
| 76 | } |
---|
[5cf72cc] | 77 | Credential.writeZipFile(graph.credentials(), |
---|
[8a93b41] | 78 | new File("./testout.zip"), true, true); |
---|
[31b67d5] | 79 | } |
---|
| 80 | } |
---|