Skip to content

Commit

Permalink
Get rid of reflection, hasTrailer(), and address other comments
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenbaker committed Sep 1, 2023
1 parent 3124558 commit 60cda4e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,32 @@

package software.amazon.awssdk.http.auth.aws.internal.checksums;

import static software.amazon.awssdk.http.auth.aws.internal.checksums.factory.CrtBasedChecksumProvider.longToByte;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.Checksum;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.crt.checksums.CRC32C;
import software.amazon.awssdk.http.auth.aws.internal.checksums.factory.CrtBasedChecksumProvider;
import software.amazon.awssdk.http.auth.aws.internal.checksums.factory.SdkCrc32C;
import software.amazon.awssdk.utils.ClassLoaderHelper;

/**
* Implementation of {@link SdkChecksum} to calculate an CRC32C checksum.
*/
@SdkInternalApi
public class Crc32CChecksum implements SdkChecksum {

private static final String CRT_CLASSPATH_FOR_CRC32C = "software.amazon.awssdk.crt.checksums.CRC32C";

private Checksum crc32c;
private Checksum lastMarkedCrc32C;
private final boolean isCrtBasedChecksum;

/**
* Creates CRT Based Crc32C checksum if Crt classpath for Crc32c is loaded, else create Sdk Implemented Crc32c
*/
public Crc32CChecksum() {
crc32c = CrtBasedChecksumProvider.createCrc32C();
isCrtBasedChecksum = crc32c != null;
if (!isCrtBasedChecksum) {
crc32c = SdkCrc32C.create();
if (isCrtAvailable()) {
crc32c = new CRC32C();
} else {
crc32c = SdkCrc32CChecksum.create();
}
}

Expand Down Expand Up @@ -82,11 +81,30 @@ public void reset() {


private Checksum cloneChecksum(Checksum checksum) {
if (isCrtBasedChecksum) {
if (checksum instanceof CRC32C) {
return (Checksum) ((CRC32C) checksum).clone();
}

return (Checksum) ((SdkCrc32C) checksum).clone();
if (checksum instanceof SdkCrc32CChecksum) {
return (Checksum) ((SdkCrc32CChecksum) checksum).clone();
}

throw new IllegalStateException("Unsupported checksum");
}

public static boolean isCrtAvailable() {
try {
ClassLoaderHelper.loadClass(CRT_CLASSPATH_FOR_CRC32C, false);
} catch (ClassNotFoundException e) {
return false;
}

return true;
}

private static byte[] longToByte(Long input) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(input);
return buffer.array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,32 @@

package software.amazon.awssdk.http.auth.aws.internal.checksums;

import static software.amazon.awssdk.http.auth.aws.internal.checksums.factory.CrtBasedChecksumProvider.longToByte;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.Checksum;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.crt.checksums.CRC32;
import software.amazon.awssdk.http.auth.aws.internal.checksums.factory.CrtBasedChecksumProvider;
import software.amazon.awssdk.http.auth.aws.internal.checksums.factory.SdkCrc32;
import software.amazon.awssdk.utils.ClassLoaderHelper;

/**
* Implementation of {@link SdkChecksum} to calculate an CRC32 checksum.
*/
@SdkInternalApi
public class Crc32Checksum implements SdkChecksum {

private static final String CRT_CLASSPATH_FOR_CRC32 = "software.amazon.awssdk.crt.checksums.CRC32";

private Checksum crc32;
private Checksum lastMarkedCrc32;
private final boolean isCrtBasedChecksum;

/**
* Creates CRT Based Crc32 checksum if Crt classpath for Crc32 is loaded, else create Sdk Implemented Crc32.
*/
public Crc32Checksum() {
crc32 = CrtBasedChecksumProvider.createCrc32();
isCrtBasedChecksum = crc32 != null;
if (!isCrtBasedChecksum) {
crc32 = SdkCrc32.create();
if (isCrtAvailable()) {
crc32 = new CRC32();
} else {
crc32 = SdkCrc32Checksum.create();
}
}

Expand Down Expand Up @@ -80,10 +79,30 @@ public void reset() {
}

private Checksum cloneChecksum(Checksum checksum) {
if (isCrtBasedChecksum) {
if (checksum instanceof CRC32) {
return (Checksum) ((CRC32) checksum).clone();
}

return (Checksum) ((SdkCrc32) checksum).clone();
if (checksum instanceof SdkCrc32Checksum) {
return (Checksum) ((SdkCrc32Checksum) checksum).clone();
}

throw new IllegalStateException("Unsupported checksum");
}

public static boolean isCrtAvailable() {
try {
ClassLoaderHelper.loadClass(CRT_CLASSPATH_FOR_CRC32, false);
} catch (ClassNotFoundException e) {
return false;
}

return true;
}

private static byte[] longToByte(Long input) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(input);
return buffer.array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.aws.internal.checksums.factory;
package software.amazon.awssdk.http.auth.aws.internal.checksums;

import java.util.zip.Checksum;
import software.amazon.awssdk.annotations.SdkInternalApi;
Expand All @@ -27,7 +27,7 @@
* The createCopy method is used to save current checksum state when the checksum is marked.
*/
@SdkInternalApi
public final class SdkCrc32C implements Checksum, Cloneable {
public final class SdkCrc32CChecksum implements Checksum, Cloneable {

private static final int T8_0_START = 0 * 256;
private static final int T8_1_START = 1 * 256;
Expand Down Expand Up @@ -568,16 +568,16 @@ public final class SdkCrc32C implements Checksum, Cloneable {
*/
private int crc;

private SdkCrc32C() {
private SdkCrc32CChecksum() {
reset();
}

private SdkCrc32C(int crc) {
private SdkCrc32CChecksum(int crc) {
this.crc = crc;
}

public static SdkCrc32C create() {
return new SdkCrc32C();
public static SdkCrc32CChecksum create() {
return new SdkCrc32CChecksum();
}

@Override
Expand Down Expand Up @@ -633,6 +633,6 @@ public void update(int b) {

@Override
public Object clone() {
return new SdkCrc32C(crc);
return new SdkCrc32CChecksum(crc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.auth.aws.internal.checksums.factory;
package software.amazon.awssdk.http.auth.aws.internal.checksums;

import java.util.zip.Checksum;
import software.amazon.awssdk.annotations.SdkInternalApi;
Expand All @@ -26,7 +26,7 @@
* The createCopy method is used to save current c checksum state when the checksum is marked.
*/
@SdkInternalApi
public final class SdkCrc32 implements Checksum, Cloneable {
public final class SdkCrc32Checksum implements Checksum, Cloneable {

/*
* CRC-32 lookup tables generated by the polynomial 0xEDB88320.
Expand Down Expand Up @@ -559,18 +559,18 @@ public final class SdkCrc32 implements Checksum, Cloneable {
*/
private int crc;

private SdkCrc32() {
private SdkCrc32Checksum() {
reset();
}


private SdkCrc32(int crc) {
private SdkCrc32Checksum(int crc) {
this.crc = crc;

}

public static SdkCrc32 create() {
return new SdkCrc32();
public static SdkCrc32Checksum create() {
return new SdkCrc32Checksum();
}

@Override
Expand Down Expand Up @@ -617,6 +617,6 @@ public void update(int b) {

@Override
public Object clone() {
return new SdkCrc32(crc);
return new SdkCrc32Checksum(crc);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ private static Checksummer checksummer(SignRequest<?, ? extends AwsCredentialsId
boolean isPayloadSigning = request.requireProperty(PAYLOAD_SIGNING_ENABLED, true);
boolean isEventStreaming = isEventStreaming(request.request());
boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false);
boolean isTrailing = hasTrailer(request);
boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent();
boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM);

if (isEventStreaming) {
return new PrecomputedChecksummer(() -> STREAMING_EVENTS_PAYLOAD);
}

if (isPayloadSigning) {
if (isChunkEncoding) {
if (isTrailing) {
if (isFlexible || isTrailing) {
return new PrecomputedChecksummer(() -> STREAMING_SIGNED_PAYLOAD_TRAILER);
}
return new PrecomputedChecksummer(() -> STREAMING_SIGNED_PAYLOAD);
Expand All @@ -128,7 +129,7 @@ private static Checksummer checksummer(SignRequest<?, ? extends AwsCredentialsId
}

if (isChunkEncoding) {
if (isTrailing) {
if (isFlexible || isTrailing) {
return new PrecomputedChecksummer(() -> STREAMING_UNSIGNED_PAYLOAD_TRAILER);
}
throw new UnsupportedOperationException("Chunk-Encoding without Payload-Signing must have a trailer!");
Expand Down Expand Up @@ -230,12 +231,6 @@ private static boolean isEventStreaming(SdkHttpRequest request) {
return "application/vnd.amazon.eventstream".equals(request.firstMatchingHeader(Header.CONTENT_TYPE).orElse(""));
}

private static boolean hasTrailer(SignRequest<?, ? extends AwsCredentialsIdentity> request) {
// Flexible checksums dictates adding a trailer when signing, but there may be existing trailers on
// the request (in the future)
return request.hasProperty(CHECKSUM_ALGORITHM) || request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent();
}

/**
* A class-loader for the event-stream signer, which throws exceptions if it can't load the class (it's likely not on the
* classpath, so it should be added), or if it can't instantiate the signer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class ChecksumInputStreamTest {

@Test
public void test_computesCorrectSha256() {
public void read_computesCorrectSha256() {
String testString = "AWS SDK for Java";
String expectedDigest = "004c6bbd87e7fe70109b3bc23c8b1ab8f18a8bede0ed38c9233f6cdfd4f7b5d6";

Expand All @@ -46,7 +46,7 @@ public void test_computesCorrectSha256() {
}

@Test
public void test_withMultipleChecksums_shouldComputeCorrectChecksums() {
public void read_withMultipleChecksums_shouldComputeCorrectChecksums() {
String testString = "AWS SDK for Java";
String expectedSha256Digest = "004c6bbd87e7fe70109b3bc23c8b1ab8f18a8bede0ed38c9233f6cdfd4f7b5d6";
String expectedCrc32Digest = "4ac37ece";
Expand Down
Loading

0 comments on commit 60cda4e

Please sign in to comment.