-
Notifications
You must be signed in to change notification settings - Fork 853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flexible checksum support for non-streaming cases #4376
Merged
haydenbaker
merged 5 commits into
feature/master/sra-identity-auth
from
haydenbaker/sra-ia-awssigners-flexible-checksums
Sep 6, 2023
+2,923
−329
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c16ef04
Add flexible checksum support for non-streaming cases
haydenbaker 3124558
Add more tests, address comments
haydenbaker 60cda4e
Get rid of reflection, hasTrailer(), and address other comments
haydenbaker dd67e7c
Remove DefaultChecksummer, update namings
haydenbaker a3437f7
Address last comments
haydenbaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...c/main/java/software/amazon/awssdk/http/auth/aws/internal/checksums/ConstantChecksum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.http.auth.aws.internal.checksums; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
/** | ||
* Implementation of {@link SdkChecksum} to provide a constant checksum. | ||
*/ | ||
@SdkInternalApi | ||
public class ConstantChecksum implements SdkChecksum { | ||
|
||
private final String value; | ||
|
||
public ConstantChecksum(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void update(int b) { | ||
} | ||
|
||
@Override | ||
public void update(byte[] b, int off, int len) { | ||
} | ||
|
||
@Override | ||
public long getValue() { | ||
throw new UnsupportedOperationException("Use getChecksumBytes() instead."); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
} | ||
|
||
@Override | ||
public byte[] getChecksumBytes() { | ||
return value.getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public void mark(int readLimit) { | ||
} | ||
|
||
@Override | ||
public String getChecksum() { | ||
return value; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...src/main/java/software/amazon/awssdk/http/auth/aws/internal/checksums/Crc32CChecksum.java
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.http.auth.aws.internal.checksums; | ||
|
||
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.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; | ||
|
||
/** | ||
* Creates CRT Based Crc32C checksum if Crt classpath for Crc32c is loaded, else create Sdk Implemented Crc32c | ||
*/ | ||
public Crc32CChecksum() { | ||
if (isCrtAvailable()) { | ||
crc32c = new CRC32C(); | ||
} else { | ||
crc32c = SdkCrc32CChecksum.create(); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] getChecksumBytes() { | ||
return Arrays.copyOfRange(longToByte(crc32c.getValue()), 4, 8); | ||
} | ||
|
||
|
||
@Override | ||
public void mark(int readLimit) { | ||
this.lastMarkedCrc32C = cloneChecksum(crc32c); | ||
} | ||
|
||
@Override | ||
public void update(int b) { | ||
crc32c.update(b); | ||
} | ||
|
||
@Override | ||
public void update(byte[] b, int off, int len) { | ||
crc32c.update(b, off, len); | ||
} | ||
|
||
@Override | ||
public long getValue() { | ||
return crc32c.getValue(); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
if (lastMarkedCrc32C == null) { | ||
crc32c.reset(); | ||
} else { | ||
crc32c = cloneChecksum(lastMarkedCrc32C); | ||
} | ||
} | ||
|
||
|
||
private Checksum cloneChecksum(Checksum checksum) { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (checksum instanceof CRC32C) { | ||
return (Checksum) ((CRC32C) 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(); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
.../src/main/java/software/amazon/awssdk/http/auth/aws/internal/checksums/Crc32Checksum.java
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.http.auth.aws.internal.checksums; | ||
|
||
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.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; | ||
|
||
/** | ||
* Creates CRT Based Crc32 checksum if Crt classpath for Crc32 is loaded, else create Sdk Implemented Crc32. | ||
*/ | ||
public Crc32Checksum() { | ||
if (isCrtAvailable()) { | ||
crc32 = new CRC32(); | ||
} else { | ||
crc32 = SdkCrc32Checksum.create(); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] getChecksumBytes() { | ||
return Arrays.copyOfRange(longToByte(crc32.getValue()), 4, 8); | ||
} | ||
|
||
@Override | ||
public void mark(int readLimit) { | ||
this.lastMarkedCrc32 = cloneChecksum(crc32); | ||
} | ||
|
||
@Override | ||
public void update(int b) { | ||
crc32.update(b); | ||
} | ||
|
||
@Override | ||
public void update(byte[] b, int off, int len) { | ||
crc32.update(b, off, len); | ||
} | ||
|
||
@Override | ||
public long getValue() { | ||
return crc32.getValue(); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
if ((lastMarkedCrc32 == null)) { | ||
crc32.reset(); | ||
} else { | ||
crc32 = cloneChecksum(lastMarkedCrc32); | ||
} | ||
} | ||
|
||
private Checksum cloneChecksum(Checksum checksum) { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (checksum instanceof CRC32) { | ||
return (Checksum) ((CRC32) 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(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use-case for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Touched upon here: #4376 (comment)