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