-
Notifications
You must be signed in to change notification settings - Fork 970
Fixed an issue that could cause checksum mismatch errors in S3 uploads. #5836
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48d19b5
Fixed an issue that could cause checksum mismatch errors in S3 uploads.
millems 18f00ca
Fixed an issue where SignerUtils was using the internal class of anot…
millems 03d0084
Update clone() failure message. Move to LinkedBlockingDeque for Diges…
millems 6a01566
Added direct testing of the DigestAlgorithm cache.
millems 4bd1ee1
Fixed a transient issue that could occur when the first attempt fully…
millems 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 hidden or 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,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "Amazon S3", | ||
| "contributor": "", | ||
| "description": "Fixed an issue that could cause checksum mismatch errors when performing parallel uploads with the async S3 client and the SHA1 or SHA256 checksum algorithms selected." | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,51 +17,115 @@ | |
|
|
||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.Deque; | ||
| import java.util.concurrent.LinkedBlockingDeque; | ||
| import java.util.function.Supplier; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.annotations.SdkTestInternalApi; | ||
| import software.amazon.awssdk.utils.SdkAutoCloseable; | ||
|
|
||
| @SdkInternalApi | ||
| public enum DigestAlgorithm { | ||
|
|
||
| SHA1("SHA-1"), | ||
|
|
||
| MD5("MD5"), | ||
| SHA256("SHA-256") | ||
| ; | ||
|
|
||
| private static final Supplier<MessageDigest> CLOSED_DIGEST = () -> { | ||
| throw new IllegalStateException("This message digest is closed."); | ||
| }; | ||
|
|
||
| private static final int MAX_CACHED_DIGESTS = 10_000; | ||
| private final String algorithmName; | ||
| private final DigestThreadLocal digestReference; | ||
| private final Deque<MessageDigest> digestCache = new LinkedBlockingDeque<>(MAX_CACHED_DIGESTS); // LIFO | ||
|
|
||
| DigestAlgorithm(String algorithmName) { | ||
| this.algorithmName = algorithmName; | ||
| digestReference = new DigestThreadLocal(algorithmName); | ||
| } | ||
|
|
||
| public String getAlgorithmName() { | ||
| return algorithmName; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the thread local reference for the {@link MessageDigest} algorithm | ||
| * Returns a {@link CloseableMessageDigest} to use for this algorithm. | ||
| */ | ||
| public MessageDigest getDigest() { | ||
| MessageDigest digest = digestReference.get(); | ||
| digest.reset(); | ||
| return digest; | ||
| public CloseableMessageDigest getDigest() { | ||
| MessageDigest digest = digestCache.pollFirst(); | ||
| if (digest != null) { | ||
| digest.reset(); | ||
| return new CloseableMessageDigest(digest); | ||
| } | ||
| return new CloseableMessageDigest(newDigest()); | ||
| } | ||
|
|
||
| private static class DigestThreadLocal extends ThreadLocal<MessageDigest> { | ||
| private final String algorithmName; | ||
| private MessageDigest newDigest() { | ||
| try { | ||
| return MessageDigest.getInstance(algorithmName); | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new RuntimeException("Unable to fetch message digest instance for Algorithm " | ||
| + algorithmName + ": " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| @SdkTestInternalApi | ||
| static void clearCaches() { | ||
| for (DigestAlgorithm value : values()) { | ||
| value.digestCache.clear(); | ||
| } | ||
| } | ||
|
|
||
| public final class CloseableMessageDigest implements SdkAutoCloseable, Cloneable { | ||
|
|
||
| private Supplier<MessageDigest> digest; | ||
| private byte[] messageDigest; | ||
|
|
||
| private CloseableMessageDigest(MessageDigest digest) { | ||
| this.digest = () -> digest; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the message digest instance. | ||
| */ | ||
| public MessageDigest messageDigest() { | ||
| return digest.get(); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the message digest bytes. This will close the message digest when invoked. This is because the underlying | ||
| * message digest is reset on read, and we'd rather fail future interactions with the digest than act on the wrong data. | ||
| */ | ||
| public byte[] digest() { | ||
| if (messageDigest != null) { | ||
| return messageDigest; | ||
| } | ||
| messageDigest = messageDigest().digest(); | ||
| close(); | ||
| return messageDigest; | ||
| } | ||
|
|
||
| /** | ||
| * Release this message digest back to the cache. Once released, you must not use the digest anymore. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| if (digest == CLOSED_DIGEST) { | ||
| return; | ||
| } | ||
|
|
||
| // Drop this digest is the cache is full. | ||
| digestCache.offerFirst(digest.get()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
|
|
||
| DigestThreadLocal(String algorithmName) { | ||
| this.algorithmName = algorithmName; | ||
| digest = CLOSED_DIGEST; | ||
| } | ||
|
|
||
| @Override | ||
| protected MessageDigest initialValue() { | ||
| public CloseableMessageDigest clone() { | ||
| try { | ||
| return MessageDigest.getInstance(algorithmName); | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new RuntimeException("Unable to fetch message digest instance for Algorithm " | ||
| + algorithmName + ": " + e.getMessage(), e); | ||
| return new CloseableMessageDigest((MessageDigest) digest.get().clone()); | ||
| } catch (CloneNotSupportedException e) { | ||
| throw new IllegalStateException("Clone was not supported by this digest type.", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
81 changes: 0 additions & 81 deletions
81
core/checksums/src/main/java/software/amazon/awssdk/checksums/internal/Sha1Checksum.java
This file was deleted.
Oops, something went wrong.
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.
Question: where do we close messageDigest in this method?
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.
Good catch, that needs a test added.