1 | import java.io.*; |
---|
2 | import java.util.*; |
---|
3 | |
---|
4 | import edu.uci.ics.jung.graph.*; |
---|
5 | |
---|
6 | import net.deterlab.abac.Role; |
---|
7 | import net.deterlab.abac.Identity; |
---|
8 | import net.deterlab.abac.Context; |
---|
9 | import net.deterlab.abac.Credential; |
---|
10 | import net.deterlab.abac.CredentialFactory; |
---|
11 | import net.deterlab.abac.GENICredential; |
---|
12 | |
---|
13 | import 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 | */ |
---|
20 | public 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 | } |
---|