[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 { |
---|
| 88 | nsContents = readNsFile(new File(args[1])); |
---|
[709306c] | 89 | AbacID = new Identity(new File("./emulab.pem")); |
---|
| 90 | } |
---|
| 91 | catch (GeneralSecurityException e) { |
---|
| 92 | System.err.println("Error reading ABAC identity " + e); |
---|
| 93 | System.exit(20); |
---|
[10f5e84] | 94 | } |
---|
| 95 | catch (IOException e) { |
---|
[709306c] | 96 | System.err.println("Cannot load file " + e); |
---|
[10f5e84] | 97 | System.exit(20); |
---|
| 98 | } |
---|
[55de6a9] | 99 | |
---|
| 100 | /* |
---|
[709306c] | 101 | * Build and send a NewRequestType Message |
---|
[55de6a9] | 102 | */ |
---|
| 103 | |
---|
[10f5e84] | 104 | NewRequestType newReq = new NewRequestType(null, |
---|
[709306c] | 105 | new IDType(null, null, null, exptName, null), |
---|
[55de6a9] | 106 | null); |
---|
[10f5e84] | 107 | NewResponseType newResp = null; |
---|
[55de6a9] | 108 | try { |
---|
[10f5e84] | 109 | newResp = port._new(newReq); |
---|
[55de6a9] | 110 | } |
---|
[709306c] | 111 | catch (AxisFault f) { |
---|
[55de6a9] | 112 | System.err.println("Error in New: " + f); |
---|
| 113 | System.exit(20); |
---|
| 114 | } |
---|
[709306c] | 115 | |
---|
| 116 | // Parse out the name of the new empty experiment, and start building |
---|
| 117 | // the CreateRequestType message. |
---|
[55de6a9] | 118 | ExperimentLabels newLabels = |
---|
[10f5e84] | 119 | new ExperimentLabels(newResp.getExperimentID()); |
---|
| 120 | |
---|
| 121 | CreateRequestType createReq = new CreateRequestType(null, |
---|
| 122 | new ExperimentDescriptionType(nsContents, null), |
---|
| 123 | null, |
---|
[709306c] | 124 | new IDType(null, null, null, newLabels.getLocalname(), null), |
---|
[10f5e84] | 125 | null); |
---|
| 126 | CreateResponseType createResp = null; |
---|
[709306c] | 127 | |
---|
| 128 | // Reloading the port clears cached SSL connections. |
---|
[10f5e84] | 129 | port = getPort("https://users.isi.deterlab.net:23235"); |
---|
| 130 | |
---|
[709306c] | 131 | // This block creates an ABAC credential telling the fedd that the |
---|
| 132 | // experiment we brought to life with the New call above can act with |
---|
| 133 | // our authority. We could keep the certificate around for other |
---|
| 134 | // commands to use, but once we tell fedd about it, fedd remembers it. |
---|
[10f5e84] | 135 | try { |
---|
[709306c] | 136 | Credential c = null; |
---|
| 137 | byte[][] ca = new byte[1][]; |
---|
| 138 | |
---|
[10f5e84] | 139 | c = delegate(AbacID, newLabels.getFedid()); |
---|
| 140 | ca[0] = c.cert().getEncoded(); |
---|
| 141 | createReq.setCredential(ca); |
---|
| 142 | } |
---|
[709306c] | 143 | catch (GeneralSecurityException e) { |
---|
| 144 | System.err.println("Failed to delegate authority: " + e); |
---|
| 145 | System.exit(20); |
---|
| 146 | } |
---|
| 147 | catch (IOException e) { |
---|
| 148 | System.err.println("Failed to delegate authority: ?!!" +e); |
---|
| 149 | System.exit(20); |
---|
| 150 | } |
---|
[10f5e84] | 151 | |
---|
[709306c] | 152 | // The create call |
---|
[10f5e84] | 153 | try { |
---|
| 154 | createResp = port.create(createReq); |
---|
| 155 | } |
---|
| 156 | catch (AxisFault f) { |
---|
| 157 | System.err.println("Error in Create: " + f); |
---|
| 158 | System.exit(20); |
---|
| 159 | } |
---|
[709306c] | 160 | |
---|
| 161 | // Tell the user we're underway |
---|
[10f5e84] | 162 | ExperimentLabels createLabels = |
---|
| 163 | new ExperimentLabels(createResp.getExperimentID()); |
---|
[709306c] | 164 | System.out.println("Success: " + createLabels.getLocalname() |
---|
| 165 | + " (" + createLabels.getFedid() + ") " |
---|
[10f5e84] | 166 | + createResp.getExperimentStatus().getValue()); |
---|
[55de6a9] | 167 | } |
---|
| 168 | } |
---|