source: axis/Create.java @ 3c7910e

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

typo

  • Property mode set to 100644
File size: 5.4 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.err.println("Make sure your certificate (in "+ certFile
94                    + ") is self-signed");
95            System.exit(20);
96        }
97        catch (IOException e) {
98            System.err.println("Cannot load file " + e);
99            System.exit(20);
100        }
101
102        /*
103         * Build and send a NewRequestType Message
104         */
105
106        NewRequestType newReq = new NewRequestType(null, 
107                new IDType(null, null, null, exptName, null), 
108                null);
109        NewResponseType newResp = null;
110        try {
111            newResp = port._new(newReq);
112        }
113        catch (AxisFault f) {
114            System.err.println("Error in New: " + f);
115            System.exit(20);
116        }
117
118        // Parse out the name of the new empty experiment, and start building
119        // the CreateRequestType message.
120        ExperimentLabels newLabels = 
121            new ExperimentLabels(newResp.getExperimentID());
122
123        CreateRequestType createReq = new CreateRequestType(null, 
124                new ExperimentDescriptionType(nsContents, null),
125                null,
126                new IDType(null, null, null, newLabels.getLocalname(), null),
127                null);
128        CreateResponseType createResp = null;
129
130        // Reloading the port clears cached SSL connections.
131        port = getPort("https://users.isi.deterlab.net:23235");
132
133        // This block creates an ABAC credential telling the fedd that the
134        // experiment we brought to life with the New call above can act with
135        // our authority.  We could keep the certificate around for other
136        // commands to use, but once we tell fedd about it, fedd remembers it.
137        //
138        // NB: We have to send both the identity used to sign the credential
139        // and the credential itself, so that fedd can validate it.
140        try {
141            Credential c = null;
142            byte[][] ca = new byte[2][];
143
144            ca[0] = AbacID.getCertificate().getEncoded(); // Identity
145            c = delegate(AbacID, newLabels.getFedid());
146            ca[1] = c.cert().getEncoded(); // Credential
147            createReq.setCredential(ca);
148        }
149        catch (GeneralSecurityException e) {
150            System.err.println("Failed to delegate authority: " + e); 
151            System.exit(20);
152        }
153        catch (IOException e) { 
154            System.err.println("Failed to delegate authority: ?!!" +e);
155            System.exit(20);
156        }
157
158        // The create call
159        try {
160            createResp = port.create(createReq);
161        }
162        catch (AxisFault f) {
163            System.err.println("Error in Create: " + f);
164            System.exit(20);
165        }
166
167        // Tell the user we're underway
168        ExperimentLabels createLabels = 
169            new ExperimentLabels(createResp.getExperimentID());
170        System.out.println("Success: " + createLabels.getLocalname()
171                + " (" + createLabels.getFedid() + ") "
172                + createResp.getExperimentStatus().getValue());
173    }
174}
Note: See TracBrowser for help on using the repository browser.