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