[31b67d5] | 1 | import java.io.*; |
---|
[7ef13e3] | 2 | import java.util.*; |
---|
[31b67d5] | 3 | |
---|
| 4 | import edu.uci.ics.jung.graph.*; |
---|
| 5 | |
---|
| 6 | import net.deterlab.abac.Credential; |
---|
[84f0e7a] | 7 | import net.deterlab.abac.Context; |
---|
[31b67d5] | 8 | import net.deterlab.abac.Role; |
---|
[9725efb] | 9 | import net.deterlab.abac.Identity; |
---|
[31b67d5] | 10 | |
---|
[7ef13e3] | 11 | import java.security.KeyPair; |
---|
| 12 | |
---|
| 13 | |
---|
[31b67d5] | 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) { |
---|
[7ef13e3] | 21 | System.out.println("Usage: GraphTest <files> <role> <principal>"); |
---|
[31b67d5] | 22 | System.out.println(" runs the query role <-?- principal and prints the result"); |
---|
| 23 | System.exit(1); |
---|
| 24 | } |
---|
| 25 | |
---|
[84f0e7a] | 26 | Context ctxt = new Context(); |
---|
[5cf72cc] | 27 | Map<String, Exception> errs = new HashMap<String, Exception>(); |
---|
[31b67d5] | 28 | |
---|
[7ef13e3] | 29 | for (int i= 0; i < args.length-2; i++) { |
---|
| 30 | File f = new File(args[i]); |
---|
[31b67d5] | 31 | |
---|
[7ef13e3] | 32 | try { |
---|
| 33 | if (f.isDirectory()) |
---|
[84f0e7a] | 34 | ctxt.readDirectory(f, errs); |
---|
[be05757] | 35 | else if (f.getPath().endsWith(".pem")) |
---|
[84f0e7a] | 36 | ctxt.loadIDFile(f); |
---|
[7ef13e3] | 37 | else if (f.getPath().endsWith(".der")) |
---|
[84f0e7a] | 38 | ctxt.loadAttributeFile(f); |
---|
[5cf72cc] | 39 | else if (f.getPath().endsWith(".zip")) |
---|
[84f0e7a] | 40 | ctxt.readZipFile(f, errs); |
---|
[7ef13e3] | 41 | else |
---|
| 42 | System.out.println(f + " of unknown type"); |
---|
| 43 | } |
---|
| 44 | catch (Exception e) { |
---|
| 45 | System.err.println("Failed to process " + f + ": " +e); |
---|
| 46 | } |
---|
| 47 | } |
---|
[cae7369] | 48 | |
---|
[5cf72cc] | 49 | for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f)); |
---|
[be05757] | 50 | |
---|
[31b67d5] | 51 | // |
---|
| 52 | // run the query |
---|
| 53 | // |
---|
| 54 | |
---|
[84f0e7a] | 55 | Role role = new Role(args[args.length-2], ctxt); |
---|
| 56 | Role prin = new Role(args[args.length-1], ctxt); |
---|
| 57 | Collection<Credential> ret = ctxt.query(role.toString(), prin.toString()); |
---|
| 58 | Set<Identity> ids = new TreeSet<Identity>(); |
---|
| 59 | |
---|
[1a7e6d3] | 60 | String fn = "attr"; |
---|
| 61 | int n = 0; |
---|
| 62 | String suf = ".der"; |
---|
[84f0e7a] | 63 | for (Credential c : ret) { |
---|
| 64 | System.out.println(c.simpleString(ctxt)); |
---|
| 65 | if ( c.hasCertificate()) { |
---|
| 66 | c.write(fn + n++ + suf); |
---|
| 67 | ids.add(c.getID()); |
---|
| 68 | } |
---|
[1a7e6d3] | 69 | } |
---|
[9725efb] | 70 | |
---|
[1a7e6d3] | 71 | fn = "id"; |
---|
| 72 | n = 0; |
---|
| 73 | suf = ".pem"; |
---|
[84f0e7a] | 74 | for (Identity i: ids) { |
---|
[9725efb] | 75 | System.out.println("ID: " + i); |
---|
[1a7e6d3] | 76 | i.write(fn + n++ + suf); |
---|
| 77 | } |
---|
[84f0e7a] | 78 | ctxt.writeZipFile(new File("./testout.zip"), true, true); |
---|
[31b67d5] | 79 | } |
---|
| 80 | } |
---|