[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 { |
---|
[cae7369] | 20 | /** |
---|
| 21 | * Import a directory full of files, using the suffixes as determinants. |
---|
| 22 | * First import all the identities (pem), then the credentials (der) into |
---|
| 23 | * the credential graph then any alias files into the two maps. |
---|
| 24 | */ |
---|
[de63a31] | 25 | protected static void importDir(File d, CredentialGraph g) { |
---|
[7ef13e3] | 26 | } |
---|
[be05757] | 27 | |
---|
[31b67d5] | 28 | public static void main(String[] args) throws IOException { |
---|
| 29 | if (args.length < 3) { |
---|
[7ef13e3] | 30 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[31b67d5] | 31 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
| 32 | System.exit(1); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | CredentialGraph graph = new CredentialGraph(); |
---|
[be05757] | 36 | Vector<KeyPair> kp = new Vector<KeyPair>(); |
---|
[5cf72cc] | 37 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
[31b67d5] | 38 | |
---|
[7ef13e3] | 39 | for (int i= 0; i < args.length-2; i++) { |
---|
| 40 | File f = new File(args[i]); |
---|
[31b67d5] | 41 | |
---|
[7ef13e3] | 42 | try { |
---|
| 43 | if (f.isDirectory()) |
---|
[be05757] | 44 | for (Credential c :Credential.readDirectory(f, kp, errs)) |
---|
| 45 | graph.add_credential(c); |
---|
| 46 | else if (f.getPath().endsWith(".pem")) |
---|
| 47 | Credential.addIdentity(new Identity(f)); |
---|
[7ef13e3] | 48 | else if (f.getPath().endsWith(".der")) |
---|
[be05757] | 49 | graph.add_credential(new Credential(f)); |
---|
[5cf72cc] | 50 | else if (f.getPath().endsWith(".zip")) |
---|
[e1c49ce] | 51 | for (Credential c :Credential.readZipFile(f, kp, errs)) |
---|
[5cf72cc] | 52 | graph.add_credential(c); |
---|
[7ef13e3] | 53 | else |
---|
| 54 | System.out.println(f + " of unknown type"); |
---|
| 55 | } |
---|
| 56 | catch (Exception e) { |
---|
| 57 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 58 | } |
---|
| 59 | } |
---|
[cae7369] | 60 | |
---|
[be05757] | 61 | for (KeyPair k: kp) System.err.println(k); |
---|
[5cf72cc] | 62 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
[be05757] | 63 | |
---|
[31b67d5] | 64 | // |
---|
| 65 | // run the query |
---|
| 66 | // |
---|
| 67 | |
---|
[de63a31] | 68 | Role role = new Role(args[args.length-2], true); |
---|
| 69 | Role prin = new Role(args[args.length-1], true); |
---|
[31b67d5] | 70 | Query q = graph.querier(); |
---|
[de63a31] | 71 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
[1a7e6d3] | 72 | String fn = "attr"; |
---|
| 73 | int n = 0; |
---|
| 74 | String suf = ".der"; |
---|
| 75 | for (Credential c : ret.getEdges()) { |
---|
[de63a31] | 76 | System.out.println(c.simpleString()); |
---|
[f6789db] | 77 | if ( c.hasCertificate()) c.write(fn + n++ + suf); |
---|
[1a7e6d3] | 78 | } |
---|
[9725efb] | 79 | |
---|
[1a7e6d3] | 80 | fn = "id"; |
---|
| 81 | n = 0; |
---|
| 82 | suf = ".pem"; |
---|
| 83 | for (Identity i: Credential.identities()) { |
---|
[9725efb] | 84 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 85 | i.write(fn + n++ + suf); |
---|
| 86 | } |
---|
[5cf72cc] | 87 | Credential.writeZipFile(graph.credentials(), |
---|
| 88 | new File("./testout.zip"), true); |
---|
[31b67d5] | 89 | } |
---|
| 90 | } |
---|