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