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 | /** |
---|
26 | * Import a credential from a der file and add it to the credential graph. |
---|
27 | */ |
---|
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 | |
---|
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 | */ |
---|
40 | protected static void importDir(File d, CredentialGraph g) { |
---|
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 { |
---|
51 | Identity id = new Identity(f); |
---|
52 | |
---|
53 | Credential.addIdentity(id); |
---|
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 | } |
---|
69 | public static void main(String[] args) throws IOException { |
---|
70 | if (args.length < 3) { |
---|
71 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
72 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
73 | System.exit(1); |
---|
74 | } |
---|
75 | |
---|
76 | CredentialGraph graph = new CredentialGraph(); |
---|
77 | TreeMap<String, String> nicknames = new TreeMap<String, String>(); |
---|
78 | TreeMap<String, String> keys = new TreeMap<String, String>(); |
---|
79 | |
---|
80 | for (int i= 0; i < args.length-2; i++) { |
---|
81 | File f = new File(args[i]); |
---|
82 | |
---|
83 | try { |
---|
84 | if (f.isDirectory()) |
---|
85 | importDir(f, graph); |
---|
86 | else if (f.getPath().endsWith(".pem")) { |
---|
87 | Identity id = new Identity(f); |
---|
88 | Credential.addIdentity(id); |
---|
89 | } |
---|
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 | } |
---|
99 | |
---|
100 | // |
---|
101 | // run the query |
---|
102 | // |
---|
103 | |
---|
104 | Role role = new Role(args[args.length-2], true); |
---|
105 | Role prin = new Role(args[args.length-1], true); |
---|
106 | Query q = graph.querier(); |
---|
107 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
108 | String fn = "attr"; |
---|
109 | int n = 0; |
---|
110 | String suf = ".der"; |
---|
111 | for (Credential c : ret.getEdges()) { |
---|
112 | System.out.println(c.simpleString()); |
---|
113 | if ( c.hasCertificate()) c.write(fn + n++ + suf); |
---|
114 | } |
---|
115 | |
---|
116 | fn = "id"; |
---|
117 | n = 0; |
---|
118 | suf = ".pem"; |
---|
119 | for (Identity i: Credential.identities()) { |
---|
120 | System.out.println("ID: " + i); |
---|
121 | i.write(fn + n++ + suf); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|