Implemented getEncryptedPasswordForVirtualMachine

This commit is contained in:
Andrei Savu 2012-02-09 11:41:26 +02:00
parent 8524022e50
commit 6704bed6dd
7 changed files with 180 additions and 15 deletions

View File

@ -0,0 +1,73 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.domain;
import com.google.gson.annotations.SerializedName;
/**
* @author Andrei Savu
*/
public class EncryptedPassword implements Comparable<EncryptedPassword> {
@SerializedName("encryptedpassword")
private String encryptedPassword;
public EncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
EncryptedPassword() { /* for serializer */ }
/**
* @return the string representation of the encrypted password
*/
public String getEncryptedPassword() {
return encryptedPassword;
}
@Override
public int hashCode() {
return encryptedPassword.hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EncryptedPassword that = (EncryptedPassword) o;
if (encryptedPassword != null ? !encryptedPassword.equals(that.encryptedPassword) : that.encryptedPassword != null)
return false;
return true;
}
@Override
public String toString() {
return "EncryptedPassword{" +
"encryptedPassword='" + encryptedPassword + '\'' +
'}';
}
@Override
public int compareTo(EncryptedPassword arg0) {
return encryptedPassword.compareTo(arg0.getEncryptedPassword());
}
}

View File

@ -20,6 +20,7 @@ package org.jclouds.cloudstack.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.EncryptedPassword;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.filters.AuthenticationFilter;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
@ -120,13 +121,13 @@ public interface VirtualMachineAsyncClient {
ListenableFuture<Long> resetPasswordForVirtualMachine(@QueryParam("id") long id);
/**
* @see VirtualMachineClient#getPasswordForVirtualMachine
* @see VirtualMachineClient#getEncryptedPasswordForVirtualMachine
*/
@GET
@QueryParams(keys = "command", values = "getVMPassword")
@SelectJson("jobid")
@SelectJson("password")
@Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<Long> getPasswordForVirtualMachine(@QueryParam("id") long id);
ListenableFuture<EncryptedPassword> getEncryptedPasswordForVirtualMachine(@QueryParam("id") long id);
/**
* @see VirtualMachineClient#changeServiceForVirtualMachine

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.EncryptedPassword;
import org.jclouds.cloudstack.domain.VirtualMachine;
import org.jclouds.cloudstack.options.DeployVirtualMachineOptions;
import org.jclouds.cloudstack.options.ListVirtualMachinesOptions;
@ -117,9 +118,9 @@ public interface VirtualMachineClient {
*
* @param id
* the ID of the virtual machine
* @return job id related to getting the encrypted password
* @return encrypted password
*/
Long getPasswordForVirtualMachine(long id);
EncryptedPassword getEncryptedPasswordForVirtualMachine(long id);
/**
* Changes the service offering for a virtual machine. The virtual machine

View File

@ -19,7 +19,9 @@
package org.jclouds.cloudstack.compute;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.domain.EncryptedPassword;
import org.jclouds.cloudstack.domain.Network;
import org.jclouds.cloudstack.domain.SshKeyPair;
import org.jclouds.cloudstack.domain.TrafficType;
import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest;
import org.jclouds.cloudstack.options.ListNetworksOptions;
@ -27,12 +29,10 @@ import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.predicates.NodePredicates;
import org.jclouds.crypto.SshKeys;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -128,13 +128,14 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
@Test
public void testCreateWindowsMachineWithKeyPairAndCheckIfTheGeneratedPasswordIsEncrypted() throws RunNodesException {
final Map<String, String> sshKey = SshKeys.generate();
final String publicKey = sshKey.get("public");
// final Map<String, String> sshKey = SshKeys.generate();
// final String publicKey = sshKey.get("public");
String keyPairName = prefix + "-windows-keypair";
client.getSSHKeyPairClient().deleteSSHKeyPair(keyPairName);
// client.getSSHKeyPairClient().registerSSHKeyPair(keyPairName, publicKey);
client.getSSHKeyPairClient().createSSHKeyPair(keyPairName);
SshKeyPair keyPair = client.getSSHKeyPairClient().createSSHKeyPair(keyPairName);
String group = prefix + "-windows-test";
Template template = computeContext.getComputeService().templateBuilder()
@ -147,11 +148,11 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
node = getOnlyElement(computeContext.getComputeService()
.createNodesInGroup(group, 1, template));
long jobId = client.getVirtualMachineClient()
.getPasswordForVirtualMachine(Long.parseLong(node.getId()));
// TODO: extrect the password from the async response
EncryptedPassword password = client.getVirtualMachineClient()
.getEncryptedPasswordForVirtualMachine(Long.parseLong(node.getId()));
Assert.fail("Password: ...");
Assert.fail("Private key:" + keyPair.getPrivateKey() + "\nPassword: " + password.getEncryptedPassword());
// TODO: decrypt the password
} finally {
if (node != null) {

View File

@ -0,0 +1,87 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudstack.features;
import com.google.common.collect.ImmutableMultimap;
import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.EncryptedPassword;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Test the CloudStack VirtualMachineClientClient
*
* @author Andrei Savu
*/
@Test(groups = "unit", testName = "VirtualMachineClientExpectTest")
public class VirtualMachineClientExpectTest extends BaseCloudStackRestClientExpectTest<VirtualMachineClient> {
public void testGetPasswordForVirtualMachineWhenResponseIs2xx() {
String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIICXgIBAAKBgQDnaPKhTNgw7qPJVp3qsT+7XhhAbip25a0AnUgq8Fb9LPcZk00p\n" +
"jm+m4JrKmDWKZWrHMNBhCNHMzvV9KrAXUMzL4s7mdEicbxTKratTYoyJM7a87bcZ\n" +
"xr+Gtoq4tm031Cix3LKyJUB0iSVU5V/Zx4QcaF5+FWcYMVI26x2Eaz+O7wIDAQAB\n" +
"AoGBAOI8sDkSL6pnJKmKjQkOEQjVjVAwZEOpd+HJ4uxX3DPY6huO7zlZj77Oh4ba\n" +
"GD4duK7VAmRbgwGAtHCSc2XYEN7ICnfkQrm+3Q8nS824Sz21WlzdCxKDFkDcC1wK\n" +
"RjE7SwXN1Kj8Xq8Vpf+z6OzHatSRZD85JM3u0/QCksOJTVIBAkEA9OpycYTuUYjC\n" +
"2pLrO5kkl0nIHbNPvFNZyle19AsHH0z/ClV8DiFtGQpwhqwCoWT0cTmSACPD/quA\n" +
"hdc2mvV+4QJBAPHiBi/7qDpJldLLvK5ALbn1yRaPSDXLccvFV4FkSS9b/2+mOM2a\n" +
"8JkolVCzImxAm0ZZDZeAGKJj1RZDsMIP188CQCfZKWus7DWZ4dI8S0e0IA75czTZ\n" +
"4uRKT3arlLAzRyJhnbFpvThzWdPULgDLZdYqndb6PfYF27LI5q1gGcNWpCECQQCB\n" +
"r8/ldiZyafW8eaQGQT7DD7brM5Nh1FyFBp+uLljW3ZqNADBAfKw3Uf0MsZ7pL5KR\n" +
"GzogWnvaxXAAafahdeEdAkEAzBT+UcxFmcPUO33PnuuiX5KIqThc6aHjjH5O7yzO\n" +
"m4Et9JwQiSgcPBmNY5NKPgmcpvUi9jDylSUV0VUu436RpQ==\n" +
"-----END RSA PRIVATE KEY-----";
VirtualMachineClient client = requestSendsResponse(
HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&" +
"command=getVMPassword&id=1&apiKey=identity&signature=SVA2r1KRj4yG03rATMLPZWS%2BKnw%3D"))
.headers(
ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json")
.build())
.build(),
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/getvmpasswordresponse.json"))
.build());
EncryptedPassword actual = client.getEncryptedPasswordForVirtualMachine(1L);
EncryptedPassword expected = new EncryptedPassword("EFOwm8icZ4sEib4y6ntVHUKHZJQrGBdyPkL1L9lpFHYhs3JfAtL5E5bxBP5Er27bJyOZPjKFcInX\r\n" +
"pQ0LZlQBZDd5/ac0NSoM6tAX3H30pYxNw4t2f9u8aJ48oOEvufgGxTTHnM9qHXD04lt+Ouql6i2q\r\n" +
"HxBqCxFkMZEla3LFieE=\r\n");
assertEquals(actual, expected);
// TODO: decrypt the returned password using the private key
}
@Override
protected VirtualMachineClient clientFrom(CloudStackContext context) {
return context.getProviderSpecificContext().getApi().getVirtualMachineClient();
}
}

View File

@ -0,0 +1,2 @@
{ "getvmpasswordresponse" : { "password" :
{"encryptedpassword":"EFOwm8icZ4sEib4y6ntVHUKHZJQrGBdyPkL1L9lpFHYhs3JfAtL5E5bxBP5Er27bJyOZPjKFcInX\r\npQ0LZlQBZDd5/ac0NSoM6tAX3H30pYxNw4t2f9u8aJ48oOEvufgGxTTHnM9qHXD04lt+Ouql6i2q\r\nHxBqCxFkMZEla3LFieE=\r\n"} } }

View File

@ -1,2 +1,2 @@
{ "registersshkeypairresponse" : { "keypair" :
{"name":"jclouds-keypair","fingerprint":"2f:4e:95:2f:f3:80:ee:21:72:a8:b4:9c:57:01:0b:3a"} } }
{"name":"jclouds-keypair","fingerprint":"8f:f1:91:2d:b1:a8:51:f1:79:cf:c4:31:c4:14:9d:81"} } }