[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 org.bouncycastle.openssl.PEMReader; |
---|
| 13 | import org.bouncycastle.jce.provider.X509CertificateObject; |
---|
| 14 | import java.security.KeyPair; |
---|
| 15 | import java.security.PublicKey; |
---|
| 16 | // import org.bouncycastle.util.io.pem.PemObject; |
---|
| 17 | |
---|
| 18 | |
---|
[31b67d5] | 19 | /** |
---|
| 20 | * Simple test of the native Java implementation of ABAC. Loads credentials |
---|
| 21 | * from an rt0 file and runs a query against them. |
---|
| 22 | */ |
---|
| 23 | public class GraphTest { |
---|
[cae7369] | 24 | |
---|
| 25 | /** |
---|
| 26 | * Import a credential from a der file and add it to the credential graph. |
---|
| 27 | */ |
---|
[7ef13e3] | 28 | protected static void importCred(File f, CredentialGraph g) |
---|
| 29 | throws Exception { |
---|
| 30 | Credential c = new Credential(f); |
---|
| 31 | g.add_credential(c); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
[cae7369] | 35 | /** |
---|
| 36 | * Import a directory full of files, using the suffixes as determinants. |
---|
| 37 | * First import all the identities (pem), then the credentials (der) into |
---|
| 38 | * the credential graph then any alias files into the two maps. |
---|
| 39 | */ |
---|
[de63a31] | 40 | protected static void importDir(File d, CredentialGraph g) { |
---|
[7ef13e3] | 41 | Vector<File> ids = new Vector<File>(); |
---|
| 42 | Vector<File> creds = new Vector<File>(); |
---|
| 43 | |
---|
| 44 | for (File f: d.listFiles()) { |
---|
| 45 | if (f.getPath().endsWith(".pem")) ids.add(f); |
---|
| 46 | else if (f.getPath().endsWith(".der") ) creds.add(f); |
---|
| 47 | else System.out.println(f + " of unknown type"); |
---|
| 48 | } |
---|
| 49 | for (File f: ids ){ |
---|
| 50 | try { |
---|
[9725efb] | 51 | Identity id = new Identity(f); |
---|
| 52 | |
---|
| 53 | Credential.addIdentity(id); |
---|
[7ef13e3] | 54 | } |
---|
| 55 | catch (Exception e) { |
---|
| 56 | System.err.println("Cannot add " + f + ": " + e); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | for (File f: creds) { |
---|
| 61 | try { |
---|
| 62 | importCred(f, g); |
---|
| 63 | } |
---|
| 64 | catch (Exception e) { |
---|
| 65 | System.err.println("Cannot add " + f + ": " + e); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | } |
---|
[31b67d5] | 69 | public static void main(String[] args) throws IOException { |
---|
| 70 | if (args.length < 3) { |
---|
[7ef13e3] | 71 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[31b67d5] | 72 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
| 73 | System.exit(1); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | CredentialGraph graph = new CredentialGraph(); |
---|
[cae7369] | 77 | TreeMap<String, String> nicknames = new TreeMap<String, String>(); |
---|
| 78 | TreeMap<String, String> keys = new TreeMap<String, String>(); |
---|
[31b67d5] | 79 | |
---|
[7ef13e3] | 80 | for (int i= 0; i < args.length-2; i++) { |
---|
| 81 | File f = new File(args[i]); |
---|
[31b67d5] | 82 | |
---|
[7ef13e3] | 83 | try { |
---|
| 84 | if (f.isDirectory()) |
---|
[de63a31] | 85 | importDir(f, graph); |
---|
[9725efb] | 86 | else if (f.getPath().endsWith(".pem")) { |
---|
| 87 | Identity id = new Identity(f); |
---|
| 88 | Credential.addIdentity(id); |
---|
| 89 | } |
---|
[7ef13e3] | 90 | else if (f.getPath().endsWith(".der")) |
---|
| 91 | importCred(f, graph); |
---|
| 92 | else |
---|
| 93 | System.out.println(f + " of unknown type"); |
---|
| 94 | } |
---|
| 95 | catch (Exception e) { |
---|
| 96 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 97 | } |
---|
| 98 | } |
---|
[cae7369] | 99 | |
---|
[31b67d5] | 100 | // |
---|
| 101 | // run the query |
---|
| 102 | // |
---|
| 103 | |
---|
[de63a31] | 104 | Role role = new Role(args[args.length-2], true); |
---|
| 105 | Role prin = new Role(args[args.length-1], true); |
---|
[31b67d5] | 106 | Query q = graph.querier(); |
---|
[de63a31] | 107 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
[1a7e6d3] | 108 | String fn = "attr"; |
---|
| 109 | int n = 0; |
---|
| 110 | String suf = ".der"; |
---|
| 111 | for (Credential c : ret.getEdges()) { |
---|
[de63a31] | 112 | System.out.println(c.simpleString()); |
---|
[1a7e6d3] | 113 | c.write(fn + n++ + suf); |
---|
| 114 | } |
---|
[9725efb] | 115 | |
---|
[1a7e6d3] | 116 | fn = "id"; |
---|
| 117 | n = 0; |
---|
| 118 | suf = ".pem"; |
---|
| 119 | for (Identity i: Credential.identities()) { |
---|
[9725efb] | 120 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 121 | i.write(fn + n++ + suf); |
---|
| 122 | } |
---|
[31b67d5] | 123 | } |
---|
| 124 | } |
---|