1 | import java.io.*; |
---|
2 | import java.util.*; |
---|
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; |
---|
10 | import net.deterlab.abac.Identity; |
---|
11 | |
---|
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 | |
---|
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 { |
---|
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 | */ |
---|
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 | |
---|
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) { |
---|
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 { |
---|
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()); |
---|
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 | } |
---|
94 | public static void main(String[] args) throws IOException { |
---|
95 | if (args.length < 3) { |
---|
96 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
97 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
98 | System.exit(1); |
---|
99 | } |
---|
100 | |
---|
101 | CredentialGraph graph = new CredentialGraph(); |
---|
102 | TreeMap<String, String> nicknames = new TreeMap<String, String>(); |
---|
103 | TreeMap<String, String> keys = new TreeMap<String, String>(); |
---|
104 | String role = args[args.length-2]; |
---|
105 | String prin = args[args.length-1]; |
---|
106 | |
---|
107 | for (int i= 0; i < args.length-2; i++) { |
---|
108 | File f = new File(args[i]); |
---|
109 | |
---|
110 | try { |
---|
111 | if (f.isDirectory()) |
---|
112 | importDir(f, graph, nicknames, keys); |
---|
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 | } |
---|
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 | } |
---|
128 | |
---|
129 | // Translate nicknames into key ids. |
---|
130 | role = replace(role, nicknames); |
---|
131 | prin = replace(prin, nicknames); |
---|
132 | // |
---|
133 | // run the query |
---|
134 | // |
---|
135 | |
---|
136 | Query q = graph.querier(); |
---|
137 | Graph<Role, Credential> ret = q.run(role, prin); |
---|
138 | String fn = "attr"; |
---|
139 | int n = 0; |
---|
140 | String suf = ".der"; |
---|
141 | for (Credential c : ret.getEdges()) { |
---|
142 | System.out.println(replace(c.toString(), keys)); |
---|
143 | c.write(fn + n++ + suf); |
---|
144 | } |
---|
145 | |
---|
146 | fn = "id"; |
---|
147 | n = 0; |
---|
148 | suf = ".pem"; |
---|
149 | for (Identity i: Credential.identities()) { |
---|
150 | System.out.println("ID: " + i); |
---|
151 | i.write(fn + n++ + suf); |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|