source: axis/Create.java @ e8979e2

compt_changes
Last change on this file since e8979e2 was e8979e2, checked in by Ted Faber <faber@…>, 12 years ago

Add example of requesting project on testbed to Create (and build)

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[709306c]1// Java I/O
2import java.io.*;
3
4// The WSDL generated types for messages and components of messages.
[55de6a9]5import edu.isi.www.fedd_types.*;
6import edu.isi.www.fedd_wsdl.*;
[10f5e84]7
[b9c0090]8// Topdl classes
9import edu.isi.www.topdl.*;
10
[709306c]11// The fault thrown by failed commands
[10f5e84]12import org.apache.axis.AxisFault;
13
[709306c]14// The ABAC commands throw this
[10f5e84]15import java.security.GeneralSecurityException;
16
[709306c]17// ABAC classes.  http://abac.deterlab.net
[10f5e84]18import net.deterlab.abac.*;
[55de6a9]19
20class Create extends FeddCommand {
[10f5e84]21
[709306c]22    /**
23     * Read a file into a byte array; used to load the topology file.
24     * @param f the File to read
25     * @throws IOException if there is an error reading the file.
26     */
[10f5e84]27    static public byte[] readNsFile(File f) throws IOException {
[709306c]28        // This is tedious but straightforward
[10f5e84]29        final int bsize = 4096;
30        byte[] buf = new byte[bsize];
31        byte[] rv = new byte[0];
32        int r = 0;
33        FileInputStream fs = new FileInputStream(f);
34
35        while ((r = fs.read(buf)) != -1 ) {
36            byte[] newRv = new byte[rv.length + r];
37            System.arraycopy(rv, 0, newRv, 0, rv.length);
38            System.arraycopy(buf, 0, newRv, rv.length, r);
39            rv = newRv;
40        }
41        fs.close();
42        return rv;
43    }
44
[b9c0090]45    /**
46     * Reads a topology file as topdl.  If this fails, just return null as
47     * later code will read the file as tcl.
48     * @param f The file to read
49     * @return the TopologyType encoded, or null of unparsable/unreadable
50     * @throws IOException if the file cannot be read
51     */
52    static public TopologyType readTopdl(File f) throws IOException {
53        try {
54            ParseTopdl p = new ParseTopdl(new FileInputStream(f), "experiment");
55            return p.getTopology();
56        }
57        catch (IOException e) { throw e; }
58        catch (Exception e) { return null; }
59    }
60
[709306c]61    /**
62     * Create an ABAC credential indicating the the given destination acts for
63     * the given Identity, and attach a certificate to it.
[b9c0090]64     * For some reason, the parse doesn't fail silently - something in the
65     * bowels of the XML parser prints an error.  Sigh.
[709306c]66     * @param id the Identity delegating authority
67     * @param dest the destination
68     * @throws IOException an I/O problem, very unlikely
69     * @throws GeneralSecurityException crypto or identity misconfiguration.
70     */
[10f5e84]71    static public Credential delegate(Identity id, String dest) 
72            throws IOException,GeneralSecurityException {
73        Credential c = new Credential(new Role(id.getKeyID()+".acting_for"),
74                new Role(dest));
75        c.make_cert(id);
76        return c;
77    }
78
[55de6a9]79    /**
[709306c]80     * Create an experiment with the given mnemonic name, from the given tcl
81     * topology file using the given identity certificate, on the given fedd.
82     * Reads the identity and topology into memory and constructs a New request
83     * for an empty experiment and a Create request to actually start it.  The
84     * start is asynchronous, so this returns when the creation begins, not
85     * when it completes. 
[55de6a9]86     */
87    public static void main(String args[]) throws 
88            javax.xml.rpc.ServiceException, java.net.MalformedURLException,
89            java.rmi.RemoteException {
[709306c]90
91        // Parse out the args
92        String exptName = "test";
[b9c0090]93        String topoFileName = "./deter-only.tcl";
[709306c]94        String certFile = "./emulab.pem";
95        String urlString = "https://users.isi.deterlab.net:23235";
[e8979e2]96        String project = null;
97        String masterTB = "deter";
[709306c]98
99        if (args.length > 0) exptName = args[0];
[b9c0090]100        if (args.length > 1) topoFileName = args[1];
[709306c]101        if (args.length > 2) certFile = args[2];
102        if (args.length > 3) urlString = args[3];
[e8979e2]103        if (args.length > 4) project = args[4];
104        if (args.length > 5) masterTB = args[5];
[55de6a9]105       
106        /*
[709306c]107         * Get the Web Service for users and read the identity and topology
[55de6a9]108         */
[709306c]109        FeddPortType port = getPort(urlString);
[b9c0090]110        File topoFile = new File(topoFileName);
[10f5e84]111        Identity AbacID = null;
112        byte[] nsContents = null;
[b9c0090]113        TopologyType topo = null;
114
115        try {
116
117            if ( (topo = readTopdl(topoFile)) == null) 
118                nsContents = readNsFile(topoFile);
119
120        }
121        catch (IOException e) {
122            System.err.println("Cannot load topology file " + e);
123            System.exit(20);
124        }
125
[10f5e84]126        try {
[4315c0e]127            AbacID = new Identity(new File(certFile));
[709306c]128        }
129        catch (GeneralSecurityException e) { 
130            System.err.println("Error reading ABAC identity " + e);
[3c7910e]131            System.err.println("Make sure your certificate (in "+ certFile
132                    + ") is self-signed");
[709306c]133            System.exit(20);
[10f5e84]134        }
135        catch (IOException e) {
[b9c0090]136            System.err.println("Cannot load ABAC id from " + 
137                    certFile + ": " + e);
[10f5e84]138            System.exit(20);
139        }
[55de6a9]140
141        /*
[709306c]142         * Build and send a NewRequestType Message
[55de6a9]143         */
144
[10f5e84]145        NewRequestType newReq = new NewRequestType(null, 
[709306c]146                new IDType(null, null, null, exptName, null), 
[55de6a9]147                null);
[10f5e84]148        NewResponseType newResp = null;
[55de6a9]149        try {
[10f5e84]150            newResp = port._new(newReq);
[55de6a9]151        }
[709306c]152        catch (AxisFault f) {
[55de6a9]153            System.err.println("Error in New: " + f);
154            System.exit(20);
155        }
[709306c]156
157        // Parse out the name of the new empty experiment, and start building
158        // the CreateRequestType message.
[55de6a9]159        ExperimentLabels newLabels = 
[10f5e84]160            new ExperimentLabels(newResp.getExperimentID());
161
[e8979e2]162        CreateServiceInfoType[] createService = null;
163        if (project != null) {
164            // If a project is requested, make a create service request to get
165            // that project.
166            createService = new CreateServiceInfoType[] {
167                new CreateServiceInfoType(null, "project_export", 
168                        new String[] { masterTB }, null, true, 
169                        new FedAttrType[] {
170                            new FedAttrType("project", project) })
171            };
172        }
173
[10f5e84]174        CreateRequestType createReq = new CreateRequestType(null, 
[b9c0090]175                new ExperimentDescriptionType(nsContents, topo),
[e8979e2]176                createService,
[709306c]177                new IDType(null, null, null, newLabels.getLocalname(), null),
[10f5e84]178                null);
179        CreateResponseType createResp = null;
[709306c]180
181        // Reloading the port clears cached SSL connections.
[cfc4d68]182        port = getPort(urlString);
[10f5e84]183
[709306c]184        // This block creates an ABAC credential telling the fedd that the
185        // experiment we brought to life with the New call above can act with
186        // our authority.  We could keep the certificate around for other
187        // commands to use, but once we tell fedd about it, fedd remembers it.
[4315c0e]188        //
189        // NB: We have to send both the identity used to sign the credential
190        // and the credential itself, so that fedd can validate it.
[10f5e84]191        try {
[709306c]192            Credential c = null;
[4315c0e]193            byte[][] ca = new byte[2][];
[709306c]194
[4315c0e]195            ca[0] = AbacID.getCertificate().getEncoded(); // Identity
[10f5e84]196            c = delegate(AbacID, newLabels.getFedid());
[4315c0e]197            ca[1] = c.cert().getEncoded(); // Credential
[10f5e84]198            createReq.setCredential(ca);
199        }
[709306c]200        catch (GeneralSecurityException e) {
201            System.err.println("Failed to delegate authority: " + e); 
202            System.exit(20);
203        }
204        catch (IOException e) { 
205            System.err.println("Failed to delegate authority: ?!!" +e);
206            System.exit(20);
207        }
[10f5e84]208
[709306c]209        // The create call
[10f5e84]210        try {
211            createResp = port.create(createReq);
212        }
213        catch (AxisFault f) {
214            System.err.println("Error in Create: " + f);
215            System.exit(20);
216        }
[709306c]217
218        // Tell the user we're underway
[10f5e84]219        ExperimentLabels createLabels = 
220            new ExperimentLabels(createResp.getExperimentID());
[709306c]221        System.out.println("Success: " + createLabels.getLocalname()
222                + " (" + createLabels.getFedid() + ") "
[10f5e84]223                + createResp.getExperimentStatus().getValue());
[55de6a9]224    }
225}
Note: See TracBrowser for help on using the repository browser.