cloudstack doesn't need a strict bouncycastle dep

This commit is contained in:
Adrian Cole 2012-12-20 09:15:39 -08:00
parent ba72ed8a62
commit 6fb7aa27f2
5 changed files with 111 additions and 88 deletions

View File

@ -23,25 +23,22 @@ import java.beans.ConstructorProperties;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/**
* Class EncryptedPasswordAndPrivateKey
*
* @author Andrei Savu
*/
public class EncryptedPasswordAndPrivateKey {
public final class EncryptedPasswordAndPrivateKey {
public static Builder<?> builder() {
return new ConcreteBuilder();
public static Builder builder() {
return new Builder();
}
public Builder<?> toBuilder() {
return new ConcreteBuilder().fromEncryptedPasswordAndPrivateKey(this);
public Builder toBuilder() {
return builder().fromEncryptedPasswordAndPrivateKey(this);
}
public static abstract class Builder<T extends Builder<T>> {
protected abstract T self();
public final static class Builder {
protected String encryptedPassword;
protected String privateKey;
@ -49,44 +46,36 @@ public class EncryptedPasswordAndPrivateKey {
/**
* @see EncryptedPasswordAndPrivateKey#getEncryptedPassword()
*/
public T encryptedPassword(String encryptedPassword) {
public Builder encryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
return self();
return this;
}
/**
* @see EncryptedPasswordAndPrivateKey#getPrivateKey()
*/
public T privateKey(String privateKey) {
public Builder privateKey(String privateKey) {
this.privateKey = privateKey;
return self();
return this;
}
public EncryptedPasswordAndPrivateKey build() {
return new EncryptedPasswordAndPrivateKey(encryptedPassword, privateKey);
}
public T fromEncryptedPasswordAndPrivateKey(EncryptedPasswordAndPrivateKey in) {
return this
.encryptedPassword(in.getEncryptedPassword())
public Builder fromEncryptedPasswordAndPrivateKey(EncryptedPasswordAndPrivateKey in) {
return encryptedPassword(in.getEncryptedPassword())
.privateKey(in.getPrivateKey());
}
}
private static class ConcreteBuilder extends Builder<ConcreteBuilder> {
@Override
protected ConcreteBuilder self() {
return this;
}
}
private final String encryptedPassword;
private final String privateKey;
@ConstructorProperties({
"encryptedPassword", "privateKey"
})
protected EncryptedPasswordAndPrivateKey(@Nullable String encryptedPassword, @Nullable String privateKey) {
public EncryptedPasswordAndPrivateKey(@Nullable String encryptedPassword, @Nullable String privateKey) {
this.encryptedPassword = encryptedPassword;
this.privateKey = privateKey;
}
@ -121,14 +110,10 @@ public class EncryptedPasswordAndPrivateKey {
&& Objects.equal(this.privateKey, that.privateKey);
}
protected ToStringHelper string() {
return Objects.toStringHelper(this)
.add("encryptedPassword", encryptedPassword).add("privateKey", privateKey);
}
@Override
public String toString() {
return string().toString();
return Objects.toStringHelper(this).omitNullValues()
.add("encryptedPassword", encryptedPassword).add("privateKey", privateKey).toString();
}
}

View File

@ -1,3 +1,21 @@
/**
* 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.functions;
import java.nio.charset.Charset;
@ -44,7 +62,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
KeyFactory kf = crypto.rsaKeyFactory();
PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher cipher = crypto.cipher("RSA/NONE/PKCS1Padding");
Cipher cipher = crypto.cipher("RSA");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = Base64.decode(dataAndKey.getEncryptedPassword());
byte[] plainText = cipher.doFinal(cipherText);
@ -60,4 +78,4 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
throw Throwables.propagate(e);
}
}
}
}

View File

@ -20,33 +20,24 @@ package org.jclouds.cloudstack.compute;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Sets.newTreeSet;
import static org.jclouds.cloudstack.options.CreateNetworkOptions.Builder.vlan;
import static org.jclouds.cloudstack.options.ListNetworkOfferingsOptions.Builder.specifyVLAN;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.domain.EncryptedPasswordAndPrivateKey;
import org.jclouds.cloudstack.domain.Network;
import org.jclouds.cloudstack.domain.SshKeyPair;
import org.jclouds.cloudstack.domain.TrafficType;
import org.jclouds.cloudstack.internal.BaseCloudStackClientLiveTest;
import org.jclouds.cloudstack.functions.WindowsLoginCredentialsFromEncryptedData;
import org.jclouds.cloudstack.options.ListNetworksOptions;
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.Crypto;
import org.jclouds.encryption.bouncycastle.BouncyCastleCrypto;
import org.testng.annotations.Test;
/**
@ -130,46 +121,4 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
}
}
@Test(enabled = false)
public void testCreateWindowsMachineWithKeyPairAndCheckIfTheGeneratedPasswordIsEncrypted()
throws RunNodesException, NoSuchAlgorithmException, CertificateException {
// 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);
SshKeyPair keyPair = client.getSSHKeyPairClient().createSSHKeyPair(keyPairName);
String group = prefix + "-windows-test";
Template template = view.getComputeService().templateBuilder()
.imageId("290").locationId("1")
.options(new CloudStackTemplateOptions().setupStaticNat(false).keyPair(keyPairName))
.build();
NodeMetadata node = null;
try {
node = getOnlyElement(view.getComputeService()
.createNodesInGroup(group, 1, template));
String encryptedPassword = client.getVirtualMachineClient()
.getEncryptedPasswordForVirtualMachine(node.getId());
Crypto crypto = new BouncyCastleCrypto();
WindowsLoginCredentialsFromEncryptedData passwordDecrypt = new WindowsLoginCredentialsFromEncryptedData(crypto);
assertEquals(passwordDecrypt.apply(
EncryptedPasswordAndPrivateKey.builder().encryptedPassword(encryptedPassword).privateKey(keyPair.getPrivateKey()).build())
.getPassword(), "bX7vvptvw");
} finally {
if (node != null) {
view.getComputeService().destroyNode(node.getId());
}
}
}
}
}

View File

@ -28,8 +28,7 @@ import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.EncryptedPasswordAndPrivateKey;
import org.jclouds.cloudstack.functions.WindowsLoginCredentialsFromEncryptedData;
import org.jclouds.cloudstack.internal.BaseCloudStackExpectTest;
import org.jclouds.crypto.Crypto;
import org.jclouds.encryption.bouncycastle.BouncyCastleCrypto;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test;
@ -79,8 +78,7 @@ public class VirtualMachineClientExpectTest extends BaseCloudStackExpectTest<Vir
assertEquals(actual, expected);
Crypto crypto = new BouncyCastleCrypto();
WindowsLoginCredentialsFromEncryptedData passwordDecrypt = new WindowsLoginCredentialsFromEncryptedData(crypto);
WindowsLoginCredentialsFromEncryptedData passwordDecrypt = new WindowsLoginCredentialsFromEncryptedData(new JCECrypto());
assertEquals(passwordDecrypt.apply(
EncryptedPasswordAndPrivateKey.builder().encryptedPassword(actual).privateKey(privateKey).build()).getPassword(), "bX7vvptvw");

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.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import org.jclouds.cloudstack.domain.EncryptedPasswordAndPrivateKey;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.encryption.internal.JCECrypto;
import org.testng.annotations.Test;
/**
* @author Richard Downer
*/
public class WindowsLoginCredentialsFromEncryptedDataTest {
private static final String PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEowIBAAKCAQEAmN6GOSMnyGNWN19ETBh11tJB5OGs3Dps8kPWqAhF9RyL/mKwkW26vH+h/5Z5\n" +
"cA5T80pK72kNnXObFaMHNoX3lavrc6yXF+8F3f1tlFX2Z+iB1pYXz1oBPqT6oOmc2XzcsJuJRakd\n" +
"zwRwHDaqljpaW7+TZlxhMa1DmUkD/HHMxDCK8jbUIZDc6BZSrnj2uPwHwW737NRE4aC3fcu4LMwf\n" +
"b2VotbNGNiAnNmrb/vtIIGkFE8NYEMpiz0WYTWX4eVKpJImv1PR6G1fMLSvudJs0ARObuLDvuonn\n" +
"SCFFdkibrwMKYbHVGGh6FoY1Vy0sqI55dgQU1kSNouiDgOGxgx+TIwIDAQABAoIBAHCS/nk5QGS7\n" +
"cpRYXa1EHhNSxx/MaUXM6MoH1x3q6cm1egqdlrWh/vAtdZkIsOkqQ/xX65Me493dcomegwNN6KOZ\n" +
"9Uw7/xCq/sEZjga8vzaJ7IOgCGy0NVJyn/a70rv+zW5pO8/G2KLI+95rC3iSBFSoYd3xjcnNdIh/\n" +
"UqYnD8oxYpKmf7418pMPsBrkglkFlbVBPiDX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J4MaxxFYagzAWTnDX+kzTlkZ2\n" +
"-----END RSA PRIVATE KEY-----";
private static final String ENCRYPTED_PASSWORD = "gO1oMoIjjIifv2iqcfIKiQD7ziOTVXsuaBJFEQrZdb8uJH/LsAiJXZeGKEeXlHl/oMoR3HEIoYuHxl+p5iHdrpP889RmxWBDGOWC5iTUzK6CRa5mFmF1I5Lpt7v2YeVoQWihSM8B19BEdBdY1svQp9nyhPB4AqLDrY28x/OrmRh/qYq953i6Y4Z8c76OHqqGcUYM4ePysRlcizSgQjdkEDmKC10Ak3OFRRx3/LqYsFIMiOHeg47APg+UANNTyRiTIia5FDhSeHJzaeYCBRQ7UYH0z2rg4cX3YjOz/MoznjHiaaN4MO+5N3v84VawnqwKOvlwPyI2bmz0+9Tr6DKzqA==";
protected final DateService dateService = new SimpleDateFormatDateService();
@Test
public void testApply() throws Exception {
WindowsLoginCredentialsFromEncryptedData f = new WindowsLoginCredentialsFromEncryptedData(new JCECrypto());
LoginCredentials credentials = f.apply(new EncryptedPasswordAndPrivateKey(ENCRYPTED_PASSWORD, PRIVATE_KEY));
assertEquals(credentials.getUser(), "Administrator");
assertEquals(credentials.getPassword(), "u4.y9mb;nR.");
assertFalse(credentials.getOptionalPrivateKey().isPresent());
}
}