Skip to content
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

Revert RESOLVED_CHECKSUM_SPECS mapping for SRA #4621

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA256;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.core.SelectedAuthScheme;
import software.amazon.awssdk.core.checksums.Algorithm;
import software.amazon.awssdk.core.checksums.ChecksumSpecs;
import software.amazon.awssdk.core.interceptor.ExecutionAttribute;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4FamilyHttpSigner;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner;
Expand Down Expand Up @@ -169,42 +164,6 @@ public void signingExpiration_oldAndNewAttributeAreMirrored() {
Duration.ofSeconds(10));
}

@Test
public void checksum_AttributeWriteReflectedInProperty() {
assertOldAttributeWrite_canBeReadFromNewAttributeCases(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS,
AwsV4FamilyHttpSigner.CHECKSUM_ALGORITHM,
ChecksumSpecs.builder()
.isRequestChecksumRequired(true)
.headerName("beepboop")
.isRequestStreaming(true)
.isValidationEnabled(true)
.responseValidationAlgorithms(
Collections.singletonList(Algorithm.CRC32))
.algorithm(Algorithm.SHA256)
.build(),
SHA256);
}

@Test
public void checksum_PropertyWriteReflectedInAttribute() {
// We need to set up the attribute first, so that ChecksumSpecs information is not lost
ChecksumSpecs valueToWrite = ChecksumSpecs.builder()
.isRequestChecksumRequired(true)
.headerName("beepboop")
.isRequestStreaming(true)
.isValidationEnabled(true)
.responseValidationAlgorithms(
Collections.singletonList(Algorithm.CRC32))
.algorithm(Algorithm.SHA256)
.build();
attributes.putAttribute(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS, valueToWrite);

assertNewPropertyWrite_canBeReadFromNewAttributeCases(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS,
AwsV4FamilyHttpSigner.CHECKSUM_ALGORITHM,
valueToWrite,
SHA256);
}

private void assertOldAndNewBooleanAttributesAreMirrored(ExecutionAttribute<Boolean> attribute,
SignerProperty<Boolean> property) {
assertOldAndNewAttributesAreMirrored(attribute, property, true);
Expand Down
15 changes: 0 additions & 15 deletions core/sdk-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,6 @@
<artifactId>http-auth-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>http-auth-aws</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>checksums-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>checksums</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>identity-spi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,17 @@

package software.amazon.awssdk.core.interceptor;

import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.CRC32;
import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.CRC32C;
import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA1;
import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA256;

import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm;
import software.amazon.awssdk.core.ClientType;
import software.amazon.awssdk.core.SelectedAuthScheme;
import software.amazon.awssdk.core.ServiceConfiguration;
import software.amazon.awssdk.core.checksums.Algorithm;
import software.amazon.awssdk.core.checksums.ChecksumSpecs;
import software.amazon.awssdk.core.checksums.ChecksumValidation;
import software.amazon.awssdk.core.signer.Signer;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4FamilyHttpSigner;
import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption;
import software.amazon.awssdk.http.auth.spi.signer.AsyncSignRequest;
import software.amazon.awssdk.http.auth.spi.signer.AsyncSignedRequest;
import software.amazon.awssdk.http.auth.spi.signer.HttpSigner;
import software.amazon.awssdk.http.auth.spi.signer.SignRequest;
import software.amazon.awssdk.http.auth.spi.signer.SignedRequest;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.utils.CompletableFutureUtils;
import software.amazon.awssdk.utils.ImmutableMap;

/**
* Contains attributes attached to the execution. This information is available to {@link ExecutionInterceptor}s and
Expand Down Expand Up @@ -119,12 +101,7 @@ public class SdkExecutionAttribute {
* The RESOLVED_CHECKSUM_SPECS holds the final checksum which will be used for checksum computation.
*/
public static final ExecutionAttribute<ChecksumSpecs> RESOLVED_CHECKSUM_SPECS =
ExecutionAttribute.mappedBuilder("ResolvedChecksumSpecs",
ChecksumSpecs.class,
() -> SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME)
.readMapping(SdkExecutionAttribute::signerChecksumReadMapping)
.writeMapping(SdkExecutionAttribute::signerChecksumWriteMapping)
.build();
new ExecutionAttribute<>("ResolvedChecksumSpecs");

/**
* The Algorithm used for checksum validation of a response.
Expand All @@ -138,86 +115,6 @@ public class SdkExecutionAttribute {
public static final ExecutionAttribute<ChecksumValidation> HTTP_RESPONSE_CHECKSUM_VALIDATION = new ExecutionAttribute<>(
"HttpResponseChecksumValidation");

private static final ImmutableMap<Algorithm, ChecksumAlgorithm> CHECKSUM_ALGORITHM_MAP = ImmutableMap.of(
Algorithm.SHA256, SHA256,
Algorithm.SHA1, SHA1,
Algorithm.CRC32, CRC32,
Algorithm.CRC32C, CRC32C
);

private static final ImmutableMap<ChecksumAlgorithm, Algorithm> ALGORITHM_MAP = ImmutableMap.of(
SHA256, Algorithm.SHA256,
SHA1, Algorithm.SHA1,
CRC32, Algorithm.CRC32,
CRC32C, Algorithm.CRC32C
);

protected SdkExecutionAttribute() {
}

private static <T extends Identity> ChecksumSpecs signerChecksumReadMapping(ChecksumSpecs checksumSpecs,
SelectedAuthScheme<T> authScheme) {
if (authScheme == null) {
return null;
}

ChecksumAlgorithm checksumAlgorithm =
authScheme.authSchemeOption().signerProperty(AwsV4FamilyHttpSigner.CHECKSUM_ALGORITHM);

if (checksumAlgorithm == null) {
return null;
}


return ChecksumSpecs.builder()
.algorithm(ALGORITHM_MAP.getOrDefault(checksumAlgorithm, null))
.isRequestStreaming(checksumSpecs != null && checksumSpecs.isRequestStreaming())
.isRequestChecksumRequired(checksumSpecs != null && checksumSpecs.isRequestChecksumRequired())
.isValidationEnabled(checksumSpecs != null && checksumSpecs.isValidationEnabled())
.headerName(checksumSpecs != null ? checksumSpecs.headerName() : null)
.responseValidationAlgorithms(checksumSpecs != null ? checksumSpecs.responseValidationAlgorithms()
: null)
.build();
}

private static <T extends Identity> SelectedAuthScheme<?> signerChecksumWriteMapping(SelectedAuthScheme<T> authScheme,
ChecksumSpecs checksumSpecs) {
ChecksumAlgorithm checksumAlgorithm =
checksumSpecs == null ? null
: CHECKSUM_ALGORITHM_MAP.getOrDefault(checksumSpecs.algorithm(), null);

if (authScheme == null) {
// This is an unusual use-case.
// Let's assume they're setting the checksum-algorithm so that they can call the signer directly. If that's true,
// then it doesn't really matter what we store other than the checksum-algorithm.
return new SelectedAuthScheme<>(CompletableFuture.completedFuture(new UnsetIdentity()),
new UnsetHttpSigner(),
AuthSchemeOption.builder()
.schemeId("unset")
.putSignerProperty(AwsV4FamilyHttpSigner.CHECKSUM_ALGORITHM,
checksumAlgorithm)
.build());
}

return new SelectedAuthScheme<>(authScheme.identity(),
authScheme.signer(),
authScheme.authSchemeOption()
.copy(o -> o.putSignerProperty(AwsV4FamilyHttpSigner.CHECKSUM_ALGORITHM,
checksumAlgorithm)));
}

private static class UnsetIdentity implements Identity {
}

private static class UnsetHttpSigner implements HttpSigner<UnsetIdentity> {
@Override
public SignedRequest sign(SignRequest<? extends UnsetIdentity> request) {
throw new IllegalStateException("A signer was not configured.");
}

@Override
public CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<? extends UnsetIdentity> request) {
return CompletableFutureUtils.failedFuture(new IllegalStateException("A signer was not configured."));
}
}
}
Loading