[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 | |
---|
[709306c] | 8 | // The fault thrown by failed commands |
---|
[10f5e84] | 9 | import org.apache.axis.AxisFault; |
---|
| 10 | |
---|
[709306c] | 11 | // The ABAC commands throw this |
---|
[10f5e84] | 12 | import java.security.GeneralSecurityException; |
---|
| 13 | |
---|
[709306c] | 14 | // ABAC classes. http://abac.deterlab.net |
---|
[10f5e84] | 15 | import net.deterlab.abac.*; |
---|
[55de6a9] | 16 | |
---|
| 17 | class Create extends FeddCommand { |
---|
[10f5e84] | 18 | |
---|
[709306c] | 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 | */ |
---|
[10f5e84] | 24 | static public byte[] readNsFile(File f) throws IOException { |
---|
[709306c] | 25 | // This is tedious but straightforward |
---|
[10f5e84] | 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 | |
---|
[709306c] | 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 | */ |
---|
[10f5e84] | 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 | |
---|
[55de6a9] | 58 | /** |
---|
[709306c] | 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. |
---|
[55de6a9] | 65 | */ |
---|
| 66 | public static void main(String args[]) throws |
---|
| 67 | javax.xml.rpc.ServiceException, java.net.MalformedURLException, |
---|
| 68 | java.rmi.RemoteException { |
---|
[709306c] | 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]; |
---|
[55de6a9] | 80 | |
---|
| 81 | /* |
---|
[709306c] | 82 | * Get the Web Service for users and read the identity and topology |
---|
[55de6a9] | 83 | */ |
---|
[709306c] | 84 | FeddPortType port = getPort(urlString); |
---|
[10f5e84] | 85 | Identity AbacID = null; |
---|
| 86 | byte[] nsContents = null; |
---|
| 87 | try { |
---|
[4315c0e] | 88 | nsContents = readNsFile(new File(tclFile)); |
---|
| 89 | AbacID = new Identity(new File(certFile)); |
---|
[709306c] | 90 | } |
---|
| 91 | catch (GeneralSecurityException e) { |
---|
| 92 | System.err.println("Error reading ABAC identity " + e); |
---|
[3c7910e] | 93 | System.err.println("Make sure your certificate (in "+ certFile |
---|
| 94 | + ") is self-signed"); |
---|
[709306c] | 95 | System.exit(20); |
---|
[10f5e84] | 96 | } |
---|
| 97 | catch (IOException e) { |
---|
[709306c] | 98 | System.err.println("Cannot load file " + e); |
---|
[10f5e84] | 99 | System.exit(20); |
---|
| 100 | } |
---|
[55de6a9] | 101 | |
---|
| 102 | /* |
---|
[709306c] | 103 | * Build and send a NewRequestType Message |
---|
[55de6a9] | 104 | */ |
---|
| 105 | |
---|
[10f5e84] | 106 | NewRequestType newReq = new NewRequestType(null, |
---|
[709306c] | 107 | new IDType(null, null, null, exptName, null), |
---|
[55de6a9] | 108 | null); |
---|
[10f5e84] | 109 | NewResponseType newResp = null; |
---|
[55de6a9] | 110 | try { |
---|
[10f5e84] | 111 | newResp = port._new(newReq); |
---|
[55de6a9] | 112 | } |
---|
[709306c] | 113 | catch (AxisFault f) { |
---|
[55de6a9] | 114 | System.err.println("Error in New: " + f); |
---|
| 115 | System.exit(20); |
---|
| 116 | } |
---|
[709306c] | 117 | |
---|
| 118 | // Parse out the name of the new empty experiment, and start building |
---|
| 119 | // the CreateRequestType message. |
---|
[55de6a9] | 120 | ExperimentLabels newLabels = |
---|
[10f5e84] | 121 | new ExperimentLabels(newResp.getExperimentID()); |
---|
| 122 | |
---|
| 123 | CreateRequestType createReq = new CreateRequestType(null, |
---|
| 124 | new ExperimentDescriptionType(nsContents, null), |
---|
| 125 | null, |
---|
[709306c] | 126 | new IDType(null, null, null, newLabels.getLocalname(), null), |
---|
[10f5e84] | 127 | null); |
---|
| 128 | CreateResponseType createResp = null; |
---|
[709306c] | 129 | |
---|
| 130 | // Reloading the port clears cached SSL connections. |
---|
[10f5e84] | 131 | port = getPort("https://users.isi.deterlab.net:23235"); |
---|
| 132 | |
---|
[709306c] | 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. |
---|
[4315c0e] | 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. |
---|
[10f5e84] | 140 | try { |
---|
[709306c] | 141 | Credential c = null; |
---|
[4315c0e] | 142 | byte[][] ca = new byte[2][]; |
---|
[709306c] | 143 | |
---|
[4315c0e] | 144 | ca[0] = AbacID.getCertificate().getEncoded(); // Identity |
---|
[10f5e84] | 145 | c = delegate(AbacID, newLabels.getFedid()); |
---|
[4315c0e] | 146 | ca[1] = c.cert().getEncoded(); // Credential |
---|
[10f5e84] | 147 | createReq.setCredential(ca); |
---|
| 148 | } |
---|
[709306c] | 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 | } |
---|
[10f5e84] | 157 | |
---|
[709306c] | 158 | // The create call |
---|
[10f5e84] | 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 | } |
---|
[709306c] | 166 | |
---|
| 167 | // Tell the user we're underway |
---|
[10f5e84] | 168 | ExperimentLabels createLabels = |
---|
| 169 | new ExperimentLabels(createResp.getExperimentID()); |
---|
[709306c] | 170 | System.out.println("Success: " + createLabels.getLocalname() |
---|
| 171 | + " (" + createLabels.getFedid() + ") " |
---|
[10f5e84] | 172 | + createResp.getExperimentStatus().getValue()); |
---|
[55de6a9] | 173 | } |
---|
| 174 | } |
---|