import java.io.*; import java.util.*; import edu.uci.ics.jung.graph.*; import net.deterlab.abac.Role; import net.deterlab.abac.Identity; import net.deterlab.abac.Context; import net.deterlab.abac.Credential; import net.deterlab.abac.CredentialFactory; import net.deterlab.abac.GENICredential; import java.security.KeyPair; /** * Simple test of the native Java implementation of ABAC. Loads credentials * from an rt0 file and runs a query against them. */ public class CreateExample { public static void writeCombinedIdentity(Identity i, File dir) throws IOException, FileNotFoundException { FileOutputStream f = new FileOutputStream( new File(dir, i.getName() + ".pem")); i.write(f); i.writePrivateKey(f); } public static void main(String[] args) throws IOException { try { /* The type of credentials to create is a binary name for a class * on the command line. set type to that. */ String type = (args.length > 0) ? args[0] : "default"; /* A little much on the ?: but if the type came in on the command * line, make lastDir the last component of the name (.-separated). * Otherwise use "default" */ String lastDir = (args.length > 0) ? type.substring(type.lastIndexOf('.')+1) : "default"; Context ctxt = new Context(); Identity acme = new Identity("Acme"); Identity globotron = new Identity("Globotron"); Identity alice = new Identity("Alice"); Identity bob = new Identity("Bob"); Vector ids = new Vector(); Vector creds = new Vector(); Collections.addAll(ids, acme, globotron, alice, bob); File dir = new File(new File("example"), lastDir); if ( !dir.isDirectory()) { if (!dir.mkdirs()) { System.err.println("Could not create " + dir); System.exit(20); } } for ( Identity i: ids) writeCombinedIdentity(i, dir); if (!type.equals("default")) ctxt.setCredentialFactory(new CredentialFactory( new String[] {type})); Credential c = ctxt.newCredential( new Role(acme.getKeyID() + ".experiment_create"), new Role(acme.getKeyID() + ".partner.experiment_create")); c.make_cert(acme); creds.add(c); c = ctxt.newCredential( new Role(acme.getKeyID() + ".partner"), new Role(globotron.getKeyID())); c.make_cert(acme); creds.add(c); c = ctxt.newCredential( new Role(globotron.getKeyID() + ".experiment_create"), new Role(globotron.getKeyID() + ".admin.power_user")); c.make_cert(globotron); creds.add(c); c = ctxt.newCredential( new Role(globotron.getKeyID() + ".admin"), new Role(alice.getKeyID())); c.make_cert(globotron); creds.add(c); c = ctxt.newCredential( new Role(alice.getKeyID() + ".power_user"), new Role(bob.getKeyID())); c.make_cert(alice); creds.add(c); int i =0; for (Credential cc: creds) { cc.write(new File(dir, "e" + i + ".cred").toString()); i ++; } } catch (Exception e) { e.printStackTrace(); } } }