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.CredentialFactory; |
---|
8 | import net.deterlab.abac.Context; |
---|
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 | public static void main(String[] args) throws IOException { |
---|
21 | File saveDir = new File("."); |
---|
22 | if (args.length < 3) { |
---|
23 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
24 | System.out.println(" runs the query role <-?- principal " |
---|
25 | + "and prints the result"); |
---|
26 | System.exit(1); |
---|
27 | } |
---|
28 | |
---|
29 | Context ctxt = new Context(); |
---|
30 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
31 | |
---|
32 | for (int i= 0; i < args.length-2; i++) { |
---|
33 | File f = new File(args[i]); |
---|
34 | |
---|
35 | try { |
---|
36 | if (f.isDirectory()) { |
---|
37 | ctxt.load_directory(f, errs); |
---|
38 | saveDir = f; |
---|
39 | } |
---|
40 | else if (f.getPath().endsWith(".pem")) |
---|
41 | ctxt.load_id_file(f); |
---|
42 | else if (f.getPath().endsWith(".der")) |
---|
43 | ctxt.load_attribute_file(f); |
---|
44 | else if (f.getPath().endsWith(".zip")) |
---|
45 | ctxt.load_zip(f, errs); |
---|
46 | else if (f.getPath().endsWith(".rt0")) |
---|
47 | ctxt.load_rt0(f); |
---|
48 | else |
---|
49 | System.out.println(f + " of unknown type"); |
---|
50 | } |
---|
51 | catch (Exception e) { |
---|
52 | System.err.println("Failed to process " + f + ": " +e); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | if (errs.keySet().size() > 0) System.err.println("Errors"); |
---|
57 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
58 | |
---|
59 | System.out.println("All read credentials"); |
---|
60 | for (Credential c: ctxt.credentials() ) |
---|
61 | System.out.println(c.simpleString(ctxt)); |
---|
62 | System.out.println("End of read credentials"); |
---|
63 | |
---|
64 | // |
---|
65 | // run the query |
---|
66 | // |
---|
67 | |
---|
68 | Role role = new Role(args[args.length-2], ctxt); |
---|
69 | Role prin = new Role(args[args.length-1], ctxt); |
---|
70 | Context.QueryResult ret = ctxt.query(role.toString(), prin.toString()); |
---|
71 | Set<Identity> ids = new TreeSet<Identity>(); |
---|
72 | |
---|
73 | String fn = "attr"; |
---|
74 | int n = 0; |
---|
75 | String suf = ".der"; |
---|
76 | System.out.println("Result: " + ret.getSuccess()); |
---|
77 | System.out.println("Proof"); |
---|
78 | for (Credential c : ret.getCredentials()) { |
---|
79 | System.out.println(c.simpleString(ctxt)); |
---|
80 | if ( c.hasCertificate()) { |
---|
81 | c.write(new File(saveDir, fn + n++ + suf).toString()); |
---|
82 | ids.add(c.issuer()); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | fn = "id"; |
---|
87 | n = 0; |
---|
88 | suf = ".pem"; |
---|
89 | System.out.println("Identities"); |
---|
90 | for (Identity i: ids) { |
---|
91 | System.out.println("ID: " + i); |
---|
92 | i.write(new File(saveDir, fn + n++ + suf).toString()); |
---|
93 | } |
---|
94 | try { |
---|
95 | ctxt.write_zip(new File(saveDir, "testout.zip"), true, true); |
---|
96 | } |
---|
97 | catch (IOException ioe) { |
---|
98 | System.err.println("Could not write ZIP: " + ioe); |
---|
99 | } |
---|
100 | System.out.println("rt0 with keyids"); |
---|
101 | ctxt.write_rt0(new OutputStreamWriter(System.out), true); |
---|
102 | } |
---|
103 | } |
---|