normalized to CryptoStreams

This commit is contained in:
Adrian Cole 2012-11-18 10:26:46 -08:00
parent ed471a2eba
commit e66219c16e
13 changed files with 30 additions and 34 deletions

View File

@ -29,7 +29,7 @@ import java.util.Map.Entry;
import javax.inject.Inject;
import org.jclouds.cloudservers.domain.Addresses;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;
@ -54,7 +54,7 @@ public class CreateServerOptions implements MapBinder {
public File(String path, byte[] contents) {
this.path = checkNotNull(path, "path");
this.contents = Base64.encodeBytes(checkNotNull(contents, "contents"));
this.contents = CryptoStreams.base64(checkNotNull(contents, "contents"));
checkArgument(path.getBytes().length < 255, String.format(
"maximum length of path is 255 bytes. Path specified %s is %d bytes", path, path.getBytes().length));
checkArgument(contents.length < 10 * 1024, String.format(

View File

@ -8,9 +8,9 @@ import javax.crypto.Cipher;
import org.jclouds.cloudstack.domain.EncryptedPasswordAndPrivateKey;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.crypto.Pems;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Charsets;
@ -46,7 +46,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
Cipher cipher = crypto.cipher("RSA/NONE/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = Base64.decode(dataAndKey.getEncryptedPassword());
byte[] cipherText = CryptoStreams.base64(dataAndKey.getEncryptedPassword());
byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charsets.US_ASCII);

View File

@ -18,13 +18,6 @@
*/
package org.jclouds.cloudstack.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import org.jclouds.encryption.internal.Base64;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

View File

@ -23,7 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.crypto.CryptoStreams;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
@ -215,7 +215,7 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
int length = checkNotNull(unencodedData, "unencodedData").length;
checkArgument(length > 0, "userData cannot be empty");
checkArgument(length <= 2 * 1024, "userData cannot be larger than 2kb");
this.queryParameters.replaceValues("userdata", ImmutableSet.of(Base64.encodeBytes(unencodedData)));
this.queryParameters.replaceValues("userdata", ImmutableSet.of(CryptoStreams.base64(unencodedData)));
return this;
}

View File

@ -26,10 +26,10 @@ import javax.crypto.Cipher;
import javax.inject.Inject;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.crypto.Pems;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Charsets;
@ -64,7 +64,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Passwo
Cipher cipher = crypto.cipher("RSA/NONE/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = Base64.decode(dataAndKey.getPasswordData().getPasswordData());
byte[] cipherText = CryptoStreams.base64(dataAndKey.getPasswordData().getPasswordData());
byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charsets.US_ASCII);

View File

@ -23,9 +23,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.ec2.domain.BlockDeviceMapping;
import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.ec2.options.internal.BaseEC2RequestOptions;
import org.jclouds.encryption.internal.Base64;
/**
* Contains options supported in the Form API for the RunInstances operation. <h2>
@ -89,7 +90,7 @@ public class RunInstancesOptions extends BaseEC2RequestOptions {
int length = checkNotNull(unencodedData, "unencodedData").length;
checkArgument(length > 0, "userData cannot be empty");
checkArgument(length <= 16 * 1024, "userData cannot be larger than 16kb");
formParameters.put("UserData", Base64.encodeBytes(unencodedData));
formParameters.put("UserData", CryptoStreams.base64(unencodedData));
return this;
}

View File

@ -18,7 +18,7 @@
*/
package org.jclouds.ec2.xml;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import com.google.common.base.Charsets;
@ -41,7 +41,7 @@ public class GetConsoleOutputResponseHandler extends ParseSax.HandlerWithResult<
@Override
public void endElement(String uri, String name, String qName) {
if (qName.equalsIgnoreCase("output")) {
this.output = new String(Base64.decode(currentText.toString().trim()), Charsets.UTF_8);
this.output = new String(CryptoStreams.base64(currentText.toString().trim()), Charsets.UTF_8);
}
currentText = new StringBuilder();
}

View File

@ -36,6 +36,7 @@ import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.domain.EC2HardwareBuilder;
import org.jclouds.ec2.compute.domain.RegionAndName;
@ -44,7 +45,6 @@ import org.jclouds.ec2.compute.options.EC2TemplateOptions;
import org.jclouds.ec2.domain.BlockDeviceMapping;
import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.ec2.options.RunInstancesOptions;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.scriptbuilder.domain.Statements;
import org.testng.annotations.Test;
@ -200,7 +200,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
assertEquals(
customize.buildFormParameters().entries(),
ImmutableMultimap.<String, String> of("InstanceType", size.getProviderId(), "SecurityGroup.1", "group",
"KeyName", systemGeneratedKeyPairName, "UserData", Base64.encodeBytes("hello".getBytes())).entries());
"KeyName", systemGeneratedKeyPairName, "UserData", CryptoStreams.base64("hello".getBytes())).entries());
assertEquals(customize.buildMatrixParameters(), ImmutableMultimap.<String, String> of());
assertEquals(customize.buildRequestHeaders(), ImmutableMultimap.<String, String> of());
assertEquals(customize.buildStringPayload(), null);

View File

@ -25,13 +25,13 @@ import static com.google.common.base.Preconditions.checkState;
import java.beans.ConstructorProperties;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.nova.domain.SecurityGroup;
import org.jclouds.rest.MapBinder;
@ -55,7 +55,7 @@ public class CreateServerOptions implements MapBinder {
public File(String path, byte[] contents) {
this.path = checkNotNull(path, "path");
this.contents = Base64.encodeBytes(checkNotNull(contents, "contents"));
this.contents = CryptoStreams.base64(checkNotNull(contents, "contents"));
checkArgument(path.getBytes().length < 255, String.format(
"maximum length of path is 255 bytes. Path specified %s is %d bytes", path, path.getBytes().length));
checkArgument(contents.length < 10 * 1024, String.format(

View File

@ -32,14 +32,16 @@ import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;
import org.jclouds.util.Preconditions2;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.collect.ForwardingObject;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@ -61,7 +63,7 @@ public class CreateServerOptions implements MapBinder {
public File(String path, byte[] contents) {
this.path = checkNotNull(path, "path");
this.contents = Base64.encodeBytes(checkNotNull(contents, "contents"));
this.contents = CryptoStreams.base64(checkNotNull(contents, "contents"));
checkArgument(
path.getBytes().length < 255,
String.format("maximum length of path is 255 bytes. Path specified %s is %d bytes", path,
@ -182,7 +184,7 @@ public class CreateServerOptions implements MapBinder {
if (keyName != null)
server.key_name = keyName;
if (userData != null)
server.user_data = Base64.encodeBytes(userData);
server.user_data = CryptoStreams.base64(userData);
if (securityGroupNames.size() > 0) {
server.securityGroupNames = Sets.newLinkedHashSet();
for (String groupName : securityGroupNames) {

View File

@ -42,8 +42,8 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.Constants;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.fujitsu.fgcp.reference.RequestParameters;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
@ -205,7 +205,7 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
try {
signer.update(stringToSign.getBytes(Charsets.UTF_8));
signed = Base64.encodeBytes(signer.sign()).replace("\n", "\r\n");
signed = CryptoStreams.base64(signer.sign()).replace("\n", "\r\n");
// signed = CryptoStreams.base64(signer.sign());
} catch (SignatureException e) {
throw new HttpException("error signing request", e);
@ -224,7 +224,7 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
String signatureData = String.format("%s&%s&%s&%s", timezone, expires,
signatureVersion, signatureMethod);
String accessKeyId = Base64.encodeBytes(signatureData.getBytes(Charsets.UTF_8));
String accessKeyId = CryptoStreams.base64(signatureData.getBytes(Charsets.UTF_8));
return accessKeyId.replace("\n", "\r\n");
}

View File

@ -23,8 +23,8 @@ import static org.testng.Assert.assertEquals;
import java.net.UnknownHostException;
import org.jclouds.aws.ec2.domain.LaunchSpecification;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.encryption.internal.Base64;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
@ -54,7 +54,7 @@ public class BindLaunchSpecificationToFormParamsTest {
assertEquals(binder.apply(spec), ImmutableMap.of("LaunchSpecification.InstanceType", "t1.micro",
"LaunchSpecification.ImageId", "ami-123", "LaunchSpecification.UserData",
Base64.encodeBytes("hello".getBytes())));
CryptoStreams.base64("hello".getBytes())));
}
@Test

View File

@ -43,6 +43,7 @@ import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.EC2TemplateBuilderTest;
import org.jclouds.ec2.compute.domain.EC2HardwareBuilder;
@ -53,7 +54,6 @@ import org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAn
import org.jclouds.ec2.domain.BlockDeviceMapping;
import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.ec2.options.RunInstancesOptions;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.scriptbuilder.domain.Statements;
import org.testng.annotations.Test;
@ -383,7 +383,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT
assertEquals(
customize.buildFormParameters().entries(),
ImmutableMultimap.<String, String> of("InstanceType", size.getProviderId(), "SecurityGroup.1", "group",
"KeyName", systemGeneratedKeyPairName, "UserData", Base64.encodeBytes("hello".getBytes())).entries());
"KeyName", systemGeneratedKeyPairName, "UserData", CryptoStreams.base64("hello".getBytes())).entries());
assertEquals(customize.buildMatrixParameters(), ImmutableMultimap.<String, String> of());
assertEquals(customize.buildRequestHeaders(), ImmutableMultimap.<String, String> of());
assertEquals(customize.buildStringPayload(), null);