[709306c] | 1 | // Java I/O |
---|
| 2 | import java.io.*; |
---|
| 3 | |
---|
| 4 | // The WSDL generated types for messages and components of messages. |
---|
[55de6a9] | 5 | import edu.isi.www.fedd_types.*; |
---|
| 6 | import edu.isi.www.fedd_wsdl.*; |
---|
[10f5e84] | 7 | |
---|
[b9c0090] | 8 | // Topdl classes |
---|
| 9 | import edu.isi.www.topdl.*; |
---|
| 10 | |
---|
[709306c] | 11 | // The fault thrown by failed commands |
---|
[10f5e84] | 12 | import org.apache.axis.AxisFault; |
---|
| 13 | |
---|
[709306c] | 14 | // The ABAC commands throw this |
---|
[10f5e84] | 15 | import java.security.GeneralSecurityException; |
---|
| 16 | |
---|
[709306c] | 17 | // ABAC classes. http://abac.deterlab.net |
---|
[10f5e84] | 18 | import net.deterlab.abac.*; |
---|
[55de6a9] | 19 | |
---|
| 20 | class 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"; |
---|
| 96 | |
---|
| 97 | if (args.length > 0) exptName = args[0]; |
---|
[b9c0090] | 98 | if (args.length > 1) topoFileName = args[1]; |
---|
[709306c] | 99 | if (args.length > 2) certFile = args[2]; |
---|
| 100 | if (args.length > 3) urlString = args[3]; |
---|
[55de6a9] | 101 | |
---|
| 102 | /* |
---|
[709306c] | 103 | * Get the Web Service for users and read the identity and topology |
---|
[55de6a9] | 104 | */ |
---|
[709306c] | 105 | FeddPortType port = getPort(urlString); |
---|
[b9c0090] | 106 | File topoFile = new File(topoFileName); |
---|
[10f5e84] | 107 | Identity AbacID = null; |
---|
| 108 | byte[] nsContents = null; |
---|
[b9c0090] | 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 | |
---|
[10f5e84] | 122 | try { |
---|
[4315c0e] | 123 | AbacID = new Identity(new File(certFile)); |
---|
[709306c] | 124 | } |
---|
| 125 | catch (GeneralSecurityException e) { |
---|
| 126 | System.err.println("Error reading ABAC identity " + e); |
---|
[3c7910e] | 127 | System.err.println("Make sure your certificate (in "+ certFile |
---|
| 128 | + ") is self-signed"); |
---|
[709306c] | 129 | System.exit(20); |
---|
[10f5e84] | 130 | } |
---|
| 131 | catch (IOException e) { |
---|
[b9c0090] | 132 | System.err.println("Cannot load ABAC id from " + |
---|
| 133 | certFile + ": " + e); |
---|
[10f5e84] | 134 | System.exit(20); |
---|
| 135 | } |
---|
[55de6a9] | 136 | |
---|
| 137 | /* |
---|
[709306c] | 138 | * Build and send a NewRequestType Message |
---|
[55de6a9] | 139 | */ |
---|
| 140 | |
---|
[10f5e84] | 141 | NewRequestType newReq = new NewRequestType(null, |
---|
[709306c] | 142 | new IDType(null, null, null, exptName, null), |
---|
[55de6a9] | 143 | null); |
---|
[10f5e84] | 144 | NewResponseType newResp = null; |
---|
[55de6a9] | 145 | try { |
---|
[10f5e84] | 146 | newResp = port._new(newReq); |
---|
[55de6a9] | 147 | } |
---|
[709306c] | 148 | catch (AxisFault f) { |
---|
[55de6a9] | 149 | System.err.println("Error in New: " + f); |
---|
| 150 | System.exit(20); |
---|
| 151 | } |
---|
[709306c] | 152 | |
---|
| 153 | // Parse out the name of the new empty experiment, and start building |
---|
| 154 | // the CreateRequestType message. |
---|
[55de6a9] | 155 | ExperimentLabels newLabels = |
---|
[10f5e84] | 156 | new ExperimentLabels(newResp.getExperimentID()); |
---|
| 157 | |
---|
| 158 | CreateRequestType createReq = new CreateRequestType(null, |
---|
[b9c0090] | 159 | new ExperimentDescriptionType(nsContents, topo), |
---|
[10f5e84] | 160 | null, |
---|
[709306c] | 161 | new IDType(null, null, null, newLabels.getLocalname(), null), |
---|
[10f5e84] | 162 | null); |
---|
| 163 | CreateResponseType createResp = null; |
---|
[709306c] | 164 | |
---|
| 165 | // Reloading the port clears cached SSL connections. |
---|
[10f5e84] | 166 | port = getPort("https://users.isi.deterlab.net:23235"); |
---|
| 167 | |
---|
[709306c] | 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. |
---|
[4315c0e] | 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. |
---|
[10f5e84] | 175 | try { |
---|
[709306c] | 176 | Credential c = null; |
---|
[4315c0e] | 177 | byte[][] ca = new byte[2][]; |
---|
[709306c] | 178 | |
---|
[4315c0e] | 179 | ca[0] = AbacID.getCertificate().getEncoded(); // Identity |
---|
[10f5e84] | 180 | c = delegate(AbacID, newLabels.getFedid()); |
---|
[4315c0e] | 181 | ca[1] = c.cert().getEncoded(); // Credential |
---|
[10f5e84] | 182 | createReq.setCredential(ca); |
---|
| 183 | } |
---|
[709306c] | 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 | } |
---|
[10f5e84] | 192 | |
---|
[709306c] | 193 | // The create call |
---|
[10f5e84] | 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 | } |
---|
[709306c] | 201 | |
---|
| 202 | // Tell the user we're underway |
---|
[10f5e84] | 203 | ExperimentLabels createLabels = |
---|
| 204 | new ExperimentLabels(createResp.getExperimentID()); |
---|
[709306c] | 205 | System.out.println("Success: " + createLabels.getLocalname() |
---|
| 206 | + " (" + createLabels.getFedid() + ") " |
---|
[10f5e84] | 207 | + createResp.getExperimentStatus().getValue()); |
---|
[55de6a9] | 208 | } |
---|
| 209 | } |
---|