source: axis/Create.java @ 2ef3584

axis_examplecompt_changesinfo-ops
Last change on this file since 2ef3584 was 4315c0e, checked in by Ted Faber <faber@…>, 13 years ago

Send the Identity cert with the credential!! (Also fix parameters)

  • Property mode set to 100644
File size: 5.3 KB
Line 
1// Java I/O
2import java.io.*;
3
4// The WSDL generated types for messages and components of messages.
5import edu.isi.www.fedd_types.*;
6import edu.isi.www.fedd_wsdl.*;
7
8// The fault thrown by failed commands
9import org.apache.axis.AxisFault;
10
11// The ABAC commands throw this
12import java.security.GeneralSecurityException;
13
14// ABAC classes.  http://abac.deterlab.net
15import net.deterlab.abac.*;
16
17class Create extends FeddCommand {
18
19    /**
20     * Read a file into a byte array; used to load the topology file.
21     * @param f the File to read
22     * @throws IOException if there is an error reading the file.
23     */
24    static public byte[] readNsFile(File f) throws IOException {
25        // This is tedious but straightforward
26        final int bsize = 4096;
27        byte[] buf = new byte[bsize];
28        byte[] rv = new byte[0];
29        int r = 0;
30        FileInputStream fs = new FileInputStream(f);
31
32        while ((r = fs.read(buf)) != -1 ) {
33            byte[] newRv = new byte[rv.length + r];
34            System.arraycopy(rv, 0, newRv, 0, rv.length);
35            System.arraycopy(buf, 0, newRv, rv.length, r);
36            rv = newRv;
37        }
38        fs.close();
39        return rv;
40    }
41
42    /**
43     * Create an ABAC credential indicating the the given destination acts for
44     * the given Identity, and attach a certificate to it.
45     * @param id the Identity delegating authority
46     * @param dest the destination
47     * @throws IOException an I/O problem, very unlikely
48     * @throws GeneralSecurityException crypto or identity misconfiguration.
49     */
50    static public Credential delegate(Identity id, String dest) 
51            throws IOException,GeneralSecurityException {
52        Credential c = new Credential(new Role(id.getKeyID()+".acting_for"),
53                new Role(dest));
54        c.make_cert(id);
55        return c;
56    }
57
58    /**
59     * Create an experiment with the given mnemonic name, from the given tcl
60     * topology file using the given identity certificate, on the given fedd.
61     * Reads the identity and topology into memory and constructs a New request
62     * for an empty experiment and a Create request to actually start it.  The
63     * start is asynchronous, so this returns when the creation begins, not
64     * when it completes. 
65     */
66    public static void main(String args[]) throws 
67            javax.xml.rpc.ServiceException, java.net.MalformedURLException,
68            java.rmi.RemoteException {
69
70        // Parse out the args
71        String exptName = "test";
72        String tclFile = "./deter-only.tcl";
73        String certFile = "./emulab.pem";
74        String urlString = "https://users.isi.deterlab.net:23235";
75
76        if (args.length > 0) exptName = args[0];
77        if (args.length > 1) tclFile = args[1];
78        if (args.length > 2) certFile = args[2];
79        if (args.length > 3) urlString = args[3];
80       
81        /*
82         * Get the Web Service for users and read the identity and topology
83         */
84        FeddPortType port = getPort(urlString);
85        Identity AbacID = null;
86        byte[] nsContents = null;
87        try {
88            nsContents = readNsFile(new File(tclFile));
89            AbacID = new Identity(new File(certFile));
90        }
91        catch (GeneralSecurityException e) { 
92            System.err.println("Error reading ABAC identity " + e);
93            System.exit(20);
94        }
95        catch (IOException e) {
96            System.err.println("Cannot load file " + e);
97            System.exit(20);
98        }
99
100        /*
101         * Build and send a NewRequestType Message
102         */
103
104        NewRequestType newReq = new NewRequestType(null, 
105                new IDType(null, null, null, exptName, null), 
106                null);
107        NewResponseType newResp = null;
108        try {
109            newResp = port._new(newReq);
110        }
111        catch (AxisFault f) {
112            System.err.println("Error in New: " + f);
113            System.exit(20);
114        }
115
116        // Parse out the name of the new empty experiment, and start building
117        // the CreateRequestType message.
118        ExperimentLabels newLabels = 
119            new ExperimentLabels(newResp.getExperimentID());
120
121        CreateRequestType createReq = new CreateRequestType(null, 
122                new ExperimentDescriptionType(nsContents, null),
123                null,
124                new IDType(null, null, null, newLabels.getLocalname(), null),
125                null);
126        CreateResponseType createResp = null;
127
128        // Reloading the port clears cached SSL connections.
129        port = getPort("https://users.isi.deterlab.net:23235");
130
131        // This block creates an ABAC credential telling the fedd that the
132        // experiment we brought to life with the New call above can act with
133        // our authority.  We could keep the certificate around for other
134        // commands to use, but once we tell fedd about it, fedd remembers it.
135        //
136        // NB: We have to send both the identity used to sign the credential
137        // and the credential itself, so that fedd can validate it.
138        try {
139            Credential c = null;
140            byte[][] ca = new byte[2][];
141
142            ca[0] = AbacID.getCertificate().getEncoded(); // Identity
143            c = delegate(AbacID, newLabels.getFedid());
144            ca[1] = c.cert().getEncoded(); // Credential
145            createReq.setCredential(ca);
146            System.err.println(c);
147        }
148        catch (GeneralSecurityException e) {
149            System.err.println("Failed to delegate authority: " + e); 
150            System.exit(20);
151        }
152        catch (IOException e) { 
153            System.err.println("Failed to delegate authority: ?!!" +e);
154            System.exit(20);
155        }
156
157        // The create call
158        try {
159            createResp = port.create(createReq);
160        }
161        catch (AxisFault f) {
162            System.err.println("Error in Create: " + f);
163            System.exit(20);
164        }
165
166        // Tell the user we're underway
167        ExperimentLabels createLabels = 
168            new ExperimentLabels(createResp.getExperimentID());
169        System.out.println("Success: " + createLabels.getLocalname()
170                + " (" + createLabels.getFedid() + ") "
171                + createResp.getExperimentStatus().getValue());
172    }
173}
Note: See TracBrowser for help on using the repository browser.