mirror of
https://github.com/apache/jclouds.git
synced 2025-02-06 18:19:03 +00:00
Move to Guava 14 BaseEncoding and Hashing
This commit is contained in:
parent
2ce689cc1a
commit
359b6b88d0
@ -20,6 +20,8 @@ package org.jclouds.chef.binders;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static com.google.common.primitives.Bytes.toArray;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -27,12 +29,9 @@ import java.util.Set;
|
||||
import javax.inject.Singleton;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.rest.binders.BindToStringPayload;
|
||||
|
||||
import com.google.common.primitives.Bytes;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
@ -51,7 +50,7 @@ public class BindChecksumsToJsonPayload extends BindToStringPayload {
|
||||
builder.append("{\"checksums\":{");
|
||||
|
||||
for (List<Byte> md5 : md5s)
|
||||
builder.append(String.format("\"%s\":null,", CryptoStreams.hex(Bytes.toArray(md5))));
|
||||
builder.append(String.format("\"%s\":null,", base16().lowerCase().encode(toArray(md5))));
|
||||
builder.deleteCharAt(builder.length() - 1);
|
||||
builder.append("}}");
|
||||
super.bindToRequest(request, builder.toString());
|
||||
|
@ -18,7 +18,12 @@
|
||||
*/
|
||||
package org.jclouds.chef.filters;
|
||||
|
||||
import static com.google.common.base.Charsets.UTF_8;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.hash.Hashing.sha1;
|
||||
import static com.google.common.io.BaseEncoding.base64;
|
||||
import static com.google.common.io.ByteStreams.asByteSource;
|
||||
import static com.google.common.io.ByteStreams.toByteArray;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.util.NoSuchElementException;
|
||||
@ -30,15 +35,13 @@ import javax.inject.Provider;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.Constants;
|
||||
import org.jclouds.crypto.Crypto;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.date.TimeStamp;
|
||||
import org.jclouds.http.HttpException;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpRequestFilter;
|
||||
import org.jclouds.http.HttpUtils;
|
||||
import org.jclouds.http.internal.SignatureWire;
|
||||
import org.jclouds.io.InputSuppliers;
|
||||
import org.jclouds.io.ByteSources;
|
||||
import org.jclouds.io.Payload;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.io.payloads.MultipartForm;
|
||||
@ -55,7 +58,6 @@ import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
/**
|
||||
* Ported from mixlib-authentication in order to sign Chef requests.
|
||||
@ -72,7 +74,6 @@ public class SignedHeaderAuth implements HttpRequestFilter {
|
||||
private final String userId;
|
||||
private final PrivateKey privateKey;
|
||||
private final Provider<String> timeStampProvider;
|
||||
private final Crypto crypto;
|
||||
private final String emptyStringHash;
|
||||
private final HttpUtils utils;
|
||||
|
||||
@ -82,12 +83,11 @@ public class SignedHeaderAuth implements HttpRequestFilter {
|
||||
|
||||
@Inject
|
||||
public SignedHeaderAuth(SignatureWire signatureWire, @Identity String userId, PrivateKey privateKey,
|
||||
@TimeStamp Provider<String> timeStampProvider, Crypto crypto, HttpUtils utils) {
|
||||
@TimeStamp Provider<String> timeStampProvider, HttpUtils utils) {
|
||||
this.signatureWire = signatureWire;
|
||||
this.userId = userId;
|
||||
this.privateKey = privateKey;
|
||||
this.timeStampProvider = timeStampProvider;
|
||||
this.crypto = crypto;
|
||||
this.emptyStringHash = hashBody(Payloads.newStringPayload(""));
|
||||
this.utils = utils;
|
||||
}
|
||||
@ -134,7 +134,7 @@ public class SignedHeaderAuth implements HttpRequestFilter {
|
||||
@VisibleForTesting
|
||||
String hashPath(String path) {
|
||||
try {
|
||||
return CryptoStreams.base64(CryptoStreams.digest(InputSuppliers.of(canonicalPath(path)), crypto.sha1()));
|
||||
return base64().encode(asByteSource(canonicalPath(path).getBytes(UTF_8)).hash(sha1()).asBytes());
|
||||
} catch (Exception e) {
|
||||
Throwables.propagateIfPossible(e);
|
||||
throw new HttpException("error creating sigature for path: " + path, e);
|
||||
@ -159,7 +159,7 @@ public class SignedHeaderAuth implements HttpRequestFilter {
|
||||
checkArgument(payload != null, "payload was null");
|
||||
checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload);
|
||||
try {
|
||||
return CryptoStreams.base64(CryptoStreams.digest(payload, crypto.sha1()));
|
||||
return base64().encode(ByteSources.asByteSource(payload.getInput()).hash(sha1()).asBytes());
|
||||
} catch (Exception e) {
|
||||
Throwables.propagateIfPossible(e);
|
||||
throw new HttpException("error creating sigature for payload: " + payload, e);
|
||||
@ -187,9 +187,8 @@ public class SignedHeaderAuth implements HttpRequestFilter {
|
||||
|
||||
public String sign(String toSign) {
|
||||
try {
|
||||
byte[] encrypted = ByteStreams.toByteArray(new RSAEncryptingPayload(Payloads.newStringPayload(toSign),
|
||||
privateKey));
|
||||
return CryptoStreams.base64(encrypted);
|
||||
byte[] encrypted = toByteArray(new RSAEncryptingPayload(Payloads.newStringPayload(toSign), privateKey));
|
||||
return base64().encode(encrypted);
|
||||
} catch (Exception e) {
|
||||
throw new HttpException("error signing request", e);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.jclouds.ohai.functions;
|
||||
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.collect.Lists.partition;
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static com.google.common.primitives.Bytes.asList;
|
||||
import static com.google.common.primitives.Bytes.toArray;
|
||||
|
||||
@ -27,8 +28,6 @@ import java.util.List;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
@ -47,7 +46,7 @@ public class ByteArrayToMacAddress implements Function<byte[], String> {
|
||||
|
||||
@Override
|
||||
public String apply(List<Byte> from) {
|
||||
return CryptoStreams.hex(toArray(from));
|
||||
return base16().lowerCase().encode(toArray(from));
|
||||
}
|
||||
|
||||
}));
|
||||
|
@ -18,6 +18,8 @@
|
||||
*/
|
||||
package org.jclouds.chef;
|
||||
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static com.google.common.primitives.Bytes.asList;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -49,7 +51,6 @@ import org.jclouds.chef.functions.ParseSearchNodesFromJson;
|
||||
import org.jclouds.chef.functions.ParseSearchRolesFromJson;
|
||||
import org.jclouds.chef.options.CreateClientOptions;
|
||||
import org.jclouds.chef.options.SearchOptions;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.date.TimeStamp;
|
||||
import org.jclouds.fallbacks.MapHttp4xxCodesToExceptions;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
@ -66,7 +67,6 @@ import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import com.google.inject.Module;
|
||||
|
||||
/**
|
||||
@ -97,10 +97,11 @@ public class ChefAsyncApiTest extends BaseAsyncApiTest<ChefAsyncApi> {
|
||||
|
||||
public void testGetUploadSandboxForChecksums() throws SecurityException, NoSuchMethodException, IOException {
|
||||
Method method = ChefAsyncApi.class.getMethod("getUploadSandboxForChecksums", Set.class);
|
||||
GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableSet.of(Bytes
|
||||
.asList(CryptoStreams.hex("0189e76ccc476701d6b374e5a1a27347")), Bytes.asList(CryptoStreams
|
||||
.hex("0c5ecd7788cf4f6c7de2a57193897a6c")), Bytes.asList(CryptoStreams
|
||||
.hex("1dda05ed139664f1f89b9dec482b77c0"))));
|
||||
GeneratedHttpRequest httpRequest = processor.createRequest(
|
||||
method,
|
||||
ImmutableSet.of(asList(base16().lowerCase().decode("0189e76ccc476701d6b374e5a1a27347")),
|
||||
asList(base16().lowerCase().decode("0c5ecd7788cf4f6c7de2a57193897a6c")),
|
||||
asList(base16().lowerCase().decode("1dda05ed139664f1f89b9dec482b77c0"))));
|
||||
assertRequestLineEquals(httpRequest, "POST http://localhost:4000/sandboxes HTTP/1.1");
|
||||
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: " + ChefAsyncApi.VERSION + "-test\n");
|
||||
assertPayloadEquals(
|
||||
@ -113,7 +114,6 @@ public class ChefAsyncApiTest extends BaseAsyncApiTest<ChefAsyncApi> {
|
||||
assertFallbackClassEquals(method, null);
|
||||
|
||||
checkFilters(httpRequest);
|
||||
|
||||
}
|
||||
|
||||
public void testUploadContent() throws SecurityException, NoSuchMethodException, IOException {
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.jclouds.chef.binders;
|
||||
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
@ -26,7 +27,6 @@ import javax.ws.rs.HttpMethod;
|
||||
|
||||
import org.jclouds.chef.ChefAsyncApi;
|
||||
import org.jclouds.chef.config.ChefParserModule;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
import org.jclouds.rest.annotations.ApiVersion;
|
||||
@ -62,7 +62,7 @@ public class BindHexEncodedMD5sToJsonPayloadTest {
|
||||
@Test(enabled = false)
|
||||
public void testCorrect() {
|
||||
HttpRequest request = HttpRequest.builder().method(HttpMethod.POST).endpoint("http://localhost").build();
|
||||
binder.bindToRequest(request, ImmutableSet.of(CryptoStreams.hex("abddef"), CryptoStreams.hex("1234")));
|
||||
binder.bindToRequest(request, ImmutableSet.of(base16().lowerCase().decode("abddef"), base16().lowerCase().decode("1234")));
|
||||
assertEquals(request.getPayload().getRawContent(), "{\"checksums\":{\"abddef\":null,\"1234\":null}}");
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,6 @@ import javax.ws.rs.core.HttpHeaders;
|
||||
|
||||
import org.jclouds.ContextBuilder;
|
||||
import org.jclouds.chef.ChefApiMetadata;
|
||||
import org.jclouds.crypto.Crypto;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpUtils;
|
||||
import org.jclouds.http.internal.SignatureWire;
|
||||
@ -170,7 +169,6 @@ public class SignedHeaderAuthTest {
|
||||
}
|
||||
|
||||
private SignedHeaderAuth signing_obj;
|
||||
private Crypto crypto;
|
||||
|
||||
/**
|
||||
* before class, as we need to ensure that the filter is threadsafe.
|
||||
@ -184,7 +182,6 @@ public class SignedHeaderAuthTest {
|
||||
Injector injector = ContextBuilder.newBuilder(new ChefApiMetadata()).credentials(USER_ID, PRIVATE_KEY).modules(
|
||||
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule())).buildInjector();
|
||||
|
||||
crypto = injector.getInstance(Crypto.class);
|
||||
HttpUtils utils = injector.getInstance(HttpUtils.class);
|
||||
|
||||
PrivateKey privateKey = injector.getInstance(PrivateKey.class);
|
||||
@ -196,7 +193,7 @@ public class SignedHeaderAuthTest {
|
||||
return TIMESTAMP_ISO8601;
|
||||
}
|
||||
|
||||
}, crypto, utils);
|
||||
}, utils);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.jclouds.chef.functions;
|
||||
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -29,7 +30,6 @@ import org.jclouds.chef.domain.Attribute;
|
||||
import org.jclouds.chef.domain.CookbookVersion;
|
||||
import org.jclouds.chef.domain.Metadata;
|
||||
import org.jclouds.chef.domain.Resource;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.functions.ParseJson;
|
||||
import org.jclouds.json.Json;
|
||||
@ -149,13 +149,13 @@ public class ParseCookbookVersionFromJsonTest {
|
||||
"README",
|
||||
URI
|
||||
.create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/checksum-11637f98942eafbf49c71b7f2f048b78?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277766181&Signature=zgpNl6wSxjTNovqZu2nJq0JztU8%3D"),
|
||||
CryptoStreams.hex("11637f98942eafbf49c71b7f2f048b78"), "README",
|
||||
base16().lowerCase().decode("11637f98942eafbf49c71b7f2f048b78"), "README",
|
||||
"default"),
|
||||
new Resource(
|
||||
"Rakefile",
|
||||
URI
|
||||
.create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/checksum-ebcf925a1651b4e04b9cd8aac2bc54eb?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277766181&Signature=EFzzDSKKytTl7b%2FxrCeNLh05zj4%3D"),
|
||||
CryptoStreams.hex("ebcf925a1651b4e04b9cd8aac2bc54eb"), "Rakefile",
|
||||
base16().lowerCase().decode("ebcf925a1651b4e04b9cd8aac2bc54eb"), "Rakefile",
|
||||
"default"))));
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,8 @@
|
||||
*/
|
||||
package org.jclouds.chef.functions;
|
||||
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static com.google.common.primitives.Bytes.asList;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -28,7 +30,6 @@ import org.jclouds.chef.ChefAsyncApi;
|
||||
import org.jclouds.chef.config.ChefParserModule;
|
||||
import org.jclouds.chef.domain.ChecksumStatus;
|
||||
import org.jclouds.chef.domain.UploadSandbox;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.functions.ParseJson;
|
||||
import org.jclouds.json.config.GsonModule;
|
||||
@ -37,7 +38,6 @@ import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.primitives.Bytes;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
@ -81,13 +81,11 @@ public class ParseUploadSandboxFromJsonTest {
|
||||
.create("https://api.opscode.com/organizations/jclouds/sandboxes/d454f71e2a5f400c808d0c5d04c2c88c"),
|
||||
ImmutableMap
|
||||
.<List<Byte>, ChecksumStatus> of(
|
||||
Bytes.asList(CryptoStreams.hex("0c5ecd7788cf4f6c7de2a57193897a6c")),
|
||||
asList(base16().lowerCase().decode("0c5ecd7788cf4f6c7de2a57193897a6c")),
|
||||
new ChecksumStatus(
|
||||
URI
|
||||
.create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/sandbox-d454f71e2a5f400c808d0c5d04c2c88c/checksum-0c5ecd7788cf4f6c7de2a57193897a6c?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277344702&Signature=FtKyqvYEjhhEKmRY%2B0M8aGPMM7g%3D"),
|
||||
true), Bytes.asList(CryptoStreams
|
||||
.hex("0189e76ccc476701d6b374e5a1a27347")), new ChecksumStatus(),
|
||||
Bytes.asList(CryptoStreams.hex("1dda05ed139664f1f89b9dec482b77c0")),
|
||||
URI.create("https://s3.amazonaws.com/opscode-platform-production-data/organization-486ca3ac66264fea926aa0b4ff74341c/sandbox-d454f71e2a5f400c808d0c5d04c2c88c/checksum-0c5ecd7788cf4f6c7de2a57193897a6c?AWSAccessKeyId=AKIAJOZTD2N26S7W6APA&Expires=1277344702&Signature=FtKyqvYEjhhEKmRY%2B0M8aGPMM7g%3D"),
|
||||
true), asList(base16().lowerCase().decode("0189e76ccc476701d6b374e5a1a27347")), new ChecksumStatus(),
|
||||
asList(base16().lowerCase().decode("1dda05ed139664f1f89b9dec482b77c0")),
|
||||
new ChecksumStatus()), "d454f71e2a5f400c808d0c5d04c2c88c"));
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@
|
||||
package org.jclouds.chef.internal;
|
||||
|
||||
import static com.google.common.base.Throwables.propagate;
|
||||
import static com.google.common.hash.Hashing.md5;
|
||||
import static org.jclouds.io.ByteSources.asByteSource;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertFalse;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
@ -46,9 +48,7 @@ import org.jclouds.chef.domain.SearchResult;
|
||||
import org.jclouds.chef.domain.UploadSandbox;
|
||||
import org.jclouds.chef.options.CreateClientOptions;
|
||||
import org.jclouds.chef.options.SearchOptions;
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.jclouds.crypto.Pems;
|
||||
import org.jclouds.io.InputSuppliers;
|
||||
import org.jclouds.io.Payloads;
|
||||
import org.jclouds.io.payloads.FilePayload;
|
||||
import org.jclouds.predicates.RetryablePredicate;
|
||||
@ -167,7 +167,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
|
||||
.addAll(cookbookO.getTemplates()).build()) {
|
||||
try {
|
||||
InputStream stream = chefApi.getResourceContents(resource);
|
||||
byte[] md5 = CryptoStreams.md5(InputSuppliers.of(stream));
|
||||
byte[] md5 = asByteSource(stream).hash(md5()).asBytes();
|
||||
assertEquals(md5, resource.getChecksum());
|
||||
} catch (NullPointerException e) {
|
||||
assert false : "resource not found: " + resource;
|
||||
|
@ -18,9 +18,9 @@
|
||||
*/
|
||||
package org.jclouds.ohai.functions;
|
||||
|
||||
import static com.google.common.io.BaseEncoding.base16;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import org.jclouds.crypto.CryptoStreams;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
@ -32,7 +32,7 @@ import org.testng.annotations.Test;
|
||||
public class ByteArrayToMacAddressTest {
|
||||
|
||||
public void test() {
|
||||
assertEquals(new ByteArrayToMacAddress().apply(CryptoStreams.hex("0026bb09e6c4")),
|
||||
assertEquals(new ByteArrayToMacAddress().apply(base16().lowerCase().decode("0026bb09e6c4")),
|
||||
"00:26:bb:09:e6:c4");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user