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 java.security.KeyPair; |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | * Simple test of the native Java implementation of ABAC. Loads credentials |
---|
17 | * from an rt0 file and runs a query against them. |
---|
18 | */ |
---|
19 | public class GraphTest { |
---|
20 | /** |
---|
21 | * Import a directory full of files, using the suffixes as determinants. |
---|
22 | * First import all the identities (pem), then the credentials (der) into |
---|
23 | * the credential graph then any alias files into the two maps. |
---|
24 | */ |
---|
25 | protected static void importDir(File d, CredentialGraph g) { |
---|
26 | } |
---|
27 | |
---|
28 | public static void main(String[] args) throws IOException { |
---|
29 | if (args.length < 3) { |
---|
30 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
31 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
32 | System.exit(1); |
---|
33 | } |
---|
34 | |
---|
35 | CredentialGraph graph = new CredentialGraph(); |
---|
36 | Vector<KeyPair> kp = new Vector<KeyPair>(); |
---|
37 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
38 | |
---|
39 | for (int i= 0; i < args.length-2; i++) { |
---|
40 | File f = new File(args[i]); |
---|
41 | |
---|
42 | try { |
---|
43 | if (f.isDirectory()) |
---|
44 | for (Credential c :Credential.readDirectory(f, kp, errs)) |
---|
45 | graph.add_credential(c); |
---|
46 | else if (f.getPath().endsWith(".pem")) |
---|
47 | Credential.addIdentity(new Identity(f)); |
---|
48 | else if (f.getPath().endsWith(".der")) |
---|
49 | graph.add_credential(new Credential(f)); |
---|
50 | else if (f.getPath().endsWith(".zip")) |
---|
51 | for (Credential c :Credential.readZipFile(f, kp, errs)) |
---|
52 | graph.add_credential(c); |
---|
53 | else |
---|
54 | System.out.println(f + " of unknown type"); |
---|
55 | } |
---|
56 | catch (Exception e) { |
---|
57 | System.err.println("Failed to process " + f + ": " +e); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | for (KeyPair k: kp) System.err.println(k); |
---|
62 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
63 | |
---|
64 | // |
---|
65 | // run the query |
---|
66 | // |
---|
67 | |
---|
68 | Role role = new Role(args[args.length-2], true); |
---|
69 | Role prin = new Role(args[args.length-1], true); |
---|
70 | Query q = graph.querier(); |
---|
71 | Graph<Role, Credential> ret = q.run(role.toString(), prin.toString()); |
---|
72 | String fn = "attr"; |
---|
73 | int n = 0; |
---|
74 | String suf = ".der"; |
---|
75 | for (Credential c : ret.getEdges()) { |
---|
76 | System.out.println(c.simpleString()); |
---|
77 | if ( c.hasCertificate()) c.write(fn + n++ + suf); |
---|
78 | } |
---|
79 | |
---|
80 | fn = "id"; |
---|
81 | n = 0; |
---|
82 | suf = ".pem"; |
---|
83 | for (Identity i: Credential.identities()) { |
---|
84 | System.out.println("ID: " + i); |
---|
85 | i.write(fn + n++ + suf); |
---|
86 | } |
---|
87 | Credential.writeZipFile(graph.credentials(), |
---|
88 | new File("./testout.zip"), true); |
---|
89 | } |
---|
90 | } |
---|