[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 | * Translate either keys to nicknames or vice versa. Break the string into |
---|
| 26 | * space separated tokens and then each of them into period separated |
---|
| 27 | * strings. If any of the smallest strings is in the map, replace it with |
---|
| 28 | * the value. |
---|
| 29 | */ |
---|
| 30 | protected static String replace(String is, Map<String, String> m) { |
---|
| 31 | String rv = ""; |
---|
| 32 | for (String tok: is.split(" ")) { |
---|
| 33 | String term = ""; |
---|
| 34 | for (String s: tok.split("\\.")) { |
---|
| 35 | String next = m.containsKey(s) ? m.get(s) : s; |
---|
| 36 | |
---|
| 37 | if (term.isEmpty()) term = next; |
---|
| 38 | else term += "." + next; |
---|
| 39 | } |
---|
| 40 | if (rv.isEmpty()) rv = term; |
---|
| 41 | else rv += " " + term; |
---|
| 42 | } |
---|
| 43 | return rv; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Import a credential from a der file and add it to the credential graph. |
---|
| 49 | */ |
---|
[7ef13e3] | 50 | protected static void importCred(File f, CredentialGraph g) |
---|
| 51 | throws Exception { |
---|
| 52 | Credential c = new Credential(f); |
---|
| 53 | g.add_credential(c); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
[cae7369] | 57 | /** |
---|
| 58 | * Import a directory full of files, using the suffixes as determinants. |
---|
| 59 | * First import all the identities (pem), then the credentials (der) into |
---|
| 60 | * the credential graph then any alias files into the two maps. |
---|
| 61 | */ |
---|
| 62 | protected static void importDir(File d, CredentialGraph g, |
---|
| 63 | Map<String, String> nick, Map<String, String> keys) { |
---|
[7ef13e3] | 64 | Vector<File> ids = new Vector<File>(); |
---|
| 65 | Vector<File> creds = new Vector<File>(); |
---|
| 66 | |
---|
| 67 | for (File f: d.listFiles()) { |
---|
| 68 | if (f.getPath().endsWith(".pem")) ids.add(f); |
---|
| 69 | else if (f.getPath().endsWith(".der") ) creds.add(f); |
---|
| 70 | else System.out.println(f + " of unknown type"); |
---|
| 71 | } |
---|
| 72 | for (File f: ids ){ |
---|
| 73 | try { |
---|
[9725efb] | 74 | Identity id = new Identity(f); |
---|
| 75 | |
---|
| 76 | Credential.addIdentity(id); |
---|
| 77 | nick.put(id.getName(), id.getKeyID()); |
---|
| 78 | keys.put(id.getKeyID(),id.getName()); |
---|
[7ef13e3] | 79 | } |
---|
| 80 | catch (Exception e) { |
---|
| 81 | System.err.println("Cannot add " + f + ": " + e); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | for (File f: creds) { |
---|
| 86 | try { |
---|
| 87 | importCred(f, g); |
---|
| 88 | } |
---|
| 89 | catch (Exception e) { |
---|
| 90 | System.err.println("Cannot add " + f + ": " + e); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
[31b67d5] | 94 | public static void main(String[] args) throws IOException { |
---|
| 95 | if (args.length < 3) { |
---|
[7ef13e3] | 96 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[31b67d5] | 97 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
| 98 | System.exit(1); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | CredentialGraph graph = new CredentialGraph(); |
---|
[cae7369] | 102 | TreeMap<String, String> nicknames = new TreeMap<String, String>(); |
---|
| 103 | TreeMap<String, String> keys = new TreeMap<String, String>(); |
---|
[7ef13e3] | 104 | String role = args[args.length-2]; |
---|
| 105 | String prin = args[args.length-1]; |
---|
[31b67d5] | 106 | |
---|
[7ef13e3] | 107 | for (int i= 0; i < args.length-2; i++) { |
---|
| 108 | File f = new File(args[i]); |
---|
[31b67d5] | 109 | |
---|
[7ef13e3] | 110 | try { |
---|
| 111 | if (f.isDirectory()) |
---|
[cae7369] | 112 | importDir(f, graph, nicknames, keys); |
---|
[9725efb] | 113 | else if (f.getPath().endsWith(".pem")) { |
---|
| 114 | Identity id = new Identity(f); |
---|
| 115 | Credential.addIdentity(id); |
---|
| 116 | nicknames.put(id.getName(), id.getKeyID()); |
---|
| 117 | keys.put(id.getKeyID(),id.getName()); |
---|
| 118 | } |
---|
[7ef13e3] | 119 | else if (f.getPath().endsWith(".der")) |
---|
| 120 | importCred(f, graph); |
---|
| 121 | else |
---|
| 122 | System.out.println(f + " of unknown type"); |
---|
| 123 | } |
---|
| 124 | catch (Exception e) { |
---|
| 125 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 126 | } |
---|
| 127 | } |
---|
[cae7369] | 128 | |
---|
| 129 | // Translate nicknames into key ids. |
---|
| 130 | role = replace(role, nicknames); |
---|
| 131 | prin = replace(prin, nicknames); |
---|
[31b67d5] | 132 | // |
---|
| 133 | // run the query |
---|
| 134 | // |
---|
| 135 | |
---|
| 136 | Query q = graph.querier(); |
---|
| 137 | Graph<Role, Credential> ret = q.run(role, prin); |
---|
[1a7e6d3] | 138 | String fn = "attr"; |
---|
| 139 | int n = 0; |
---|
| 140 | String suf = ".der"; |
---|
| 141 | for (Credential c : ret.getEdges()) { |
---|
[cae7369] | 142 | System.out.println(replace(c.toString(), keys)); |
---|
[1a7e6d3] | 143 | c.write(fn + n++ + suf); |
---|
| 144 | } |
---|
[9725efb] | 145 | |
---|
[1a7e6d3] | 146 | fn = "id"; |
---|
| 147 | n = 0; |
---|
| 148 | suf = ".pem"; |
---|
| 149 | for (Identity i: Credential.identities()) { |
---|
[9725efb] | 150 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 151 | i.write(fn + n++ + suf); |
---|
| 152 | } |
---|
[31b67d5] | 153 | } |
---|
| 154 | } |
---|