[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>(); |
---|
| 37 | Map<File, Exception> errs = new HashMap<File, 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)); |
---|
[7ef13e3] | 50 | else |
---|
| 51 | System.out.println(f + " of unknown type"); |
---|
| 52 | } |
---|
| 53 | catch (Exception e) { |
---|
| 54 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 55 | } |
---|
| 56 | } |
---|
[cae7369] | 57 | |
---|
[be05757] | 58 | for (KeyPair k: kp) System.err.println(k); |
---|
| 59 | for (File f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
| 60 | |
---|
[31b67d5] | 61 | // |
---|
| 62 | // run the query |
---|
| 63 | // |
---|
| 64 | |
---|
[de63a31] | 65 | Role role = new Role(args[args.length-2], true); |
---|
| 66 | Role prin = new Role(args[args.length-1], true); |
---|
[31b67d5] | 67 | Query q = graph.querier(); |
---|
[de63a31] | 68 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
[1a7e6d3] | 69 | String fn = "attr"; |
---|
| 70 | int n = 0; |
---|
| 71 | String suf = ".der"; |
---|
| 72 | for (Credential c : ret.getEdges()) { |
---|
[de63a31] | 73 | System.out.println(c.simpleString()); |
---|
[f6789db] | 74 | if ( c.hasCertificate()) c.write(fn + n++ + suf); |
---|
[1a7e6d3] | 75 | } |
---|
[9725efb] | 76 | |
---|
[1a7e6d3] | 77 | fn = "id"; |
---|
| 78 | n = 0; |
---|
| 79 | suf = ".pem"; |
---|
| 80 | for (Identity i: Credential.identities()) { |
---|
[9725efb] | 81 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 82 | i.write(fn + n++ + suf); |
---|
| 83 | } |
---|
[31b67d5] | 84 | } |
---|
| 85 | } |
---|