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
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// Topdl classes
9import edu.isi.www.topdl.*;
10
11// The fault thrown by failed commands
12import org.apache.axis.AxisFault;
13
14// The ABAC commands throw this
15import java.security.GeneralSecurityException;
16
17// ABAC classes.  http://abac.deterlab.net
18import net.deterlab.abac.*;
19
20class Create extends FeddCommand {
21
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     */
27    static public byte[] readNsFile(File f) throws IOException {
28        // This is tedious but straightforward
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
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
61    /**
62     * Create an ABAC credential indicating the the given destination acts for
63     * the given Identity, and attach a certificate to it.
64     * For some reason, the parse doesn't fail silently - something in the
65     * bowels of the XML parser prints an error.  Sigh.
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     */
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
79    /**
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. 
86     */
87    public static void main(String args[]) throws 
88            javax.xml.rpc.ServiceException, java.net.MalformedURLException,
89            java.rmi.RemoteException {
90
91        // Parse out the args
92        String exptName = "test";
93        String topoFileName = "./deter-only.tcl";
94        String certFile = "./emulab.pem";
95        String urlString = "https://users.isi.deterlab.net:23235";
96        String project = null;
97        String masterTB = "deter";
98
99        if (args.length > 0) exptName = args[0];
100        if (args.length > 1) topoFileName = args[1];
101        if (args.length > 2) certFile = args[2];
102        if (args.length > 3) urlString = args[3];
103        if (args.length > 4) project = args[4];
104        if (args.length > 5) masterTB = args[5];
105       
106        /*
107         * Get the Web Service for users and read the identity and topology
108         */
109        FeddPortType port = getPort(urlString);
110        File topoFile = new File(topoFileName);
111        Identity AbacID = null;
112        byte[] nsContents = null;
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
126        try {
127            AbacID = new Identity(new File(certFile));
128        }
129        catch (GeneralSecurityException e) { 
130            System.err.println("Error reading ABAC identity " + e);
131            System.err.println("Make sure your certificate (in "+ certFile
132                    + ") is self-signed");
133            System.exit(20);
134        }
135        catch (IOException e) {
136            System.err.println("Cannot load ABAC id from " + 
137                    certFile + ": " + e);
138            System.exit(20);
139        }
140
141        /*
142         * Build and send a NewRequestType Message
143         */
144
145        NewRequestType newReq = new NewRequestType(null, 
146                new IDType(null, null, null, exptName, null), 
147                null);
148        NewResponseType newResp = null;
149        try {
150            newResp = port._new(newReq);
151        }
152        catch (AxisFault f) {
153            System.err.println("Error in New: " + f);
154            System.exit(20);
155        }
156
157        // Parse out the name of the new empty experiment, and start building
158        // the CreateRequestType message.
159        ExperimentLabels newLabels = 
160            new ExperimentLabels(newResp.getExperimentID());
161
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
174        CreateRequestType createReq = new CreateRequestType(null, 
175                new ExperimentDescriptionType(nsContents, topo),
176                createService,
177                new IDType(null, null, null, newLabels.getLocalname(), null),
178                null);
179        CreateResponseType createResp = null;
180
181        // Reloading the port clears cached SSL connections.
182        port = getPort(urlString);
183
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.
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.
191        try {
192            Credential c = null;
193            byte[][] ca = new byte[2][];
194
195            ca[0] = AbacID.getCertificate().getEncoded(); // Identity
196            c = delegate(AbacID, newLabels.getFedid());
197            ca[1] = c.cert().getEncoded(); // Credential
198            createReq.setCredential(ca);
199        }
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        }
208
209        // The create call
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        }
217
218        // Tell the user we're underway
219        ExperimentLabels createLabels = 
220            new ExperimentLabels(createResp.getExperimentID());
221        System.out.println("Success: " + createLabels.getLocalname()
222                + " (" + createLabels.getFedid() + ") "
223                + createResp.getExperimentStatus().getValue());
224    }
225}
Note: See TracBrowser for help on using the repository browser.