source: java/CreateExample.java @ 0100d7b

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

Polymorphic example creation

  • Property mode set to 100644
File size: 3.0 KB
Line 
1import java.io.*;
2import java.util.*;
3
4import edu.uci.ics.jung.graph.*;
5
6import net.deterlab.abac.Role;
7import net.deterlab.abac.Identity;
8import net.deterlab.abac.Context;
9import net.deterlab.abac.Credential;
10import net.deterlab.abac.CredentialFactory;
11import net.deterlab.abac.GENICredential;
12
13import java.security.KeyPair;
14
15
16/**
17 * Simple test of the native Java implementation of ABAC. Loads credentials
18 * from an rt0 file and runs a query against them.
19 */
20public class CreateExample {
21
22    public static void writeCombinedIdentity(Identity i, File dir) 
23            throws IOException, FileNotFoundException {
24        FileOutputStream f = new FileOutputStream(
25                new File(dir,  i.getName() + ".pem"));
26
27        i.write(f);
28        i.writePrivateKey(f);
29    }
30    public static void main(String[] args) throws IOException {
31        try {
32            /* The type of credentials to create is a binary name for a class
33             * on the command line.  set type to that.
34             */
35            String type = (args.length > 0) ? args[0] : "default";
36
37            /* A little much on the ?: but if the type came in on the command
38             * line, make lastDir the last component of the name (.-separated).
39             * Otherwise use "default" */
40            String lastDir = (args.length > 0) ? 
41                type.substring(type.lastIndexOf('.')+1) : "default";
42
43            Context ctxt = new Context();
44            Identity acme = new Identity("Acme");
45            Identity globotron = new Identity("Globotron");
46            Identity alice = new Identity("Alice");
47            Identity bob = new Identity("Bob");
48            Vector<Identity> ids = new Vector<Identity>();
49            Vector<Credential> creds = new Vector<Credential>();
50            Collections.addAll(ids, acme, globotron, alice, bob);
51
52            File dir = new File(new File("example"), lastDir);
53
54            if ( !dir.isDirectory()) {
55                if (!dir.mkdirs()) {
56                    System.err.println("Could not create " + dir);
57                    System.exit(20);
58                }
59            }
60
61            for ( Identity i: ids) 
62                writeCombinedIdentity(i, dir);
63
64            if (!type.equals("default"))
65                ctxt.setCredentialFactory(new CredentialFactory(
66                            new String[] {type}));
67
68
69            Credential c = ctxt.newCredential(
70                    new Role(acme.getKeyID() + ".experiment_create"),
71                    new Role(acme.getKeyID() + ".partner.experiment_create"));
72            c.make_cert(acme);
73            creds.add(c);
74            c = ctxt.newCredential(
75                    new Role(acme.getKeyID() + ".partner"),
76                    new Role(globotron.getKeyID()));
77            c.make_cert(acme);
78            creds.add(c);
79            c = ctxt.newCredential(
80                    new Role(globotron.getKeyID() + ".experiment_create"),
81                    new Role(globotron.getKeyID() + ".admin.power_user"));
82            c.make_cert(globotron);
83            creds.add(c);
84            c = ctxt.newCredential(
85                    new Role(globotron.getKeyID() + ".admin"),
86                    new Role(alice.getKeyID()));
87            c.make_cert(globotron);
88            creds.add(c);
89            c = ctxt.newCredential(
90                    new Role(alice.getKeyID() + ".power_user"),
91                    new Role(bob.getKeyID()));
92            c.make_cert(alice);
93            creds.add(c);
94
95            int i =0;
96            for (Credential cc: creds) {
97                cc.write(new File(dir, "e" + i + ".cred").toString());
98                i ++;
99            }
100
101        }
102        catch (Exception e) {
103            e.printStackTrace();
104        }
105
106    }
107}
Note: See TracBrowser for help on using the repository browser.