source: java/GraphTest.java @ dd5d4d9

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since dd5d4d9 was 008dc25, checked in by Ted Faber <faber@…>, 11 years ago

Scripts to create test environments in several formats and run queries on them

  • Property mode set to 100644
File size: 2.9 KB
Line 
1import java.io.*;
2import java.util.*;
3
4import edu.uci.ics.jung.graph.*;
5
6import net.deterlab.abac.Credential;
7import net.deterlab.abac.CredentialFactory;
8import net.deterlab.abac.Context;
9import net.deterlab.abac.Role;
10import net.deterlab.abac.Identity;
11
12import 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 */
19public 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        /* Set up to parse XML or X509 (XML first) */
31        try {
32            CredentialFactory cf = new CredentialFactory(
33                    new String[] { 
34                        "net.deterlab.abac.GENICredential",
35                        "net.deterlab.abac.X509Credential",
36                    });
37            ctxt.setCredentialFactory(cf);
38        }
39        catch (Exception e) {
40            e.printStackTrace();
41            System.exit(20);
42        }
43        Map<String, Exception> errs = new HashMap<String, Exception>();
44
45        for (int i= 0; i < args.length-2; i++) {
46            File f = new File(args[i]);
47
48            try {
49                if (f.isDirectory()) {
50                    ctxt.load_directory(f, errs);
51                    saveDir = f;
52                }
53                else if (f.getPath().endsWith(".pem")) 
54                    ctxt.load_id_file(f);
55                else if (f.getPath().endsWith(".der"))
56                    ctxt.load_attribute_file(f);
57                else if (f.getPath().endsWith(".zip"))
58                    ctxt.load_zip(f, errs);
59                else if (f.getPath().endsWith(".rt0"))
60                    ctxt.load_rt0(f);
61                else
62                    System.out.println(f + " of unknown type");
63            }
64            catch (Exception e) {
65                System.err.println("Failed to process " + f + ": " +e);
66            }
67        }
68
69        for (String f: errs.keySet()) System.err.println(f + " " + errs.get(f));
70
71        //
72        // run the query
73        //
74
75        Role role = new Role(args[args.length-2], ctxt);
76        Role prin = new Role(args[args.length-1], ctxt);
77        Context.QueryResult ret = ctxt.query(role.toString(), prin.toString());
78        Set<Identity> ids = new TreeSet<Identity>();
79
80        String fn = "attr";
81        int n = 0;
82        String suf = ".der";
83        System.out.println("Result: " + ret.getSuccess());
84        System.out.println("Proof");
85        for (Credential c : ret.getCredentials()) {
86            System.out.println(c.simpleString(ctxt));
87            if ( c.hasCertificate()) {
88                c.write(new File(saveDir, fn + n++ + suf).toString());
89                ids.add(c.issuer());
90            }
91        }
92
93        fn = "id";
94        n = 0;
95        suf = ".pem";
96        System.out.println("Identities");
97        for (Identity i: ids) {
98            System.out.println("ID: " + i);
99            i.write(new File(saveDir, fn + n++ + suf).toString());
100        }
101        try {
102            ctxt.write_zip(new File(saveDir, "testout.zip"), true, true);
103        }
104        catch (IOException ioe) {
105            System.err.println("Could not write ZIP: " + ioe);
106        }
107        System.out.println("rt0 with keyids");
108        ctxt.write_rt0(new OutputStreamWriter(System.out), true);
109    }
110}
Note: See TracBrowser for help on using the repository browser.