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 | |
---|
11 | import org.bouncycastle.openssl.PEMReader; |
---|
12 | import org.bouncycastle.jce.provider.X509CertificateObject; |
---|
13 | import java.security.KeyPair; |
---|
14 | import java.security.PublicKey; |
---|
15 | // import org.bouncycastle.util.io.pem.PemObject; |
---|
16 | |
---|
17 | |
---|
18 | /** |
---|
19 | * Simple test of the native Java implementation of ABAC. Loads credentials |
---|
20 | * from an rt0 file and runs a query against them. |
---|
21 | */ |
---|
22 | public class GraphTest { |
---|
23 | |
---|
24 | protected static void importCred(File f, CredentialGraph g) |
---|
25 | throws Exception { |
---|
26 | Credential c = new Credential(f); |
---|
27 | g.add_credential(c); |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | protected static void importDir(File d, CredentialGraph g) { |
---|
32 | Vector<File> ids = new Vector<File>(); |
---|
33 | Vector<File> creds = new Vector<File>(); |
---|
34 | |
---|
35 | for (File f: d.listFiles()) { |
---|
36 | if (f.getPath().endsWith(".pem")) ids.add(f); |
---|
37 | else if (f.getPath().endsWith(".der") ) creds.add(f); |
---|
38 | else System.out.println(f + " of unknown type"); |
---|
39 | } |
---|
40 | for (File f: ids ){ |
---|
41 | try { |
---|
42 | Credential.addIdentity(f); |
---|
43 | } |
---|
44 | catch (Exception e) { |
---|
45 | System.err.println("Cannot add " + f + ": " + e); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | for (File f: creds) { |
---|
50 | try { |
---|
51 | importCred(f, g); |
---|
52 | } |
---|
53 | catch (Exception e) { |
---|
54 | System.err.println("Cannot add " + f + ": " + e); |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | public static void main(String[] args) throws IOException { |
---|
59 | if (args.length < 3) { |
---|
60 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
61 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
62 | System.exit(1); |
---|
63 | } |
---|
64 | |
---|
65 | CredentialGraph graph = new CredentialGraph(); |
---|
66 | String role = args[args.length-2]; |
---|
67 | String prin = args[args.length-1]; |
---|
68 | |
---|
69 | for (int i= 0; i < args.length-2; i++) { |
---|
70 | File f = new File(args[i]); |
---|
71 | |
---|
72 | try { |
---|
73 | if (f.isDirectory()) |
---|
74 | importDir(f, graph); |
---|
75 | else if (f.getPath().endsWith(".pem")) |
---|
76 | Credential.addIdentity(f); |
---|
77 | else if (f.getPath().endsWith(".der")) |
---|
78 | importCred(f, graph); |
---|
79 | else |
---|
80 | System.out.println(f + " of unknown type"); |
---|
81 | } |
---|
82 | catch (Exception e) { |
---|
83 | System.err.println("Failed to process " + f + ": " +e); |
---|
84 | } |
---|
85 | } |
---|
86 | // |
---|
87 | // run the query |
---|
88 | // |
---|
89 | |
---|
90 | Query q = graph.querier(); |
---|
91 | Graph<Role, Credential> ret = q.run(role, prin); |
---|
92 | for (Credential c : ret.getEdges()) |
---|
93 | System.out.println(c.toString()); |
---|
94 | } |
---|
95 | } |
---|