source: axis/Create.java @ cedf721

axis_examplecompt_changesinfo-ops
Last change on this file since cedf721 was b9c0090, checked in by Ted Faber <faber@…>, 14 years ago

Parses topdl to the point where it will successfully create.

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