index
int64
0
0
repo_id
stringlengths
26
205
file_path
stringlengths
51
246
content
stringlengths
8
433k
__index_level_0__
int64
0
10k
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/signer/AwsV4FamilyHttpSigner.java
/* * 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.signer; import java.time.Duration; import software.amazon.awssdk.annotations.SdkPublicApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.auth.spi.signer.HttpSigner; import software.amazon.awssdk.http.auth.spi.signer.SignerProperty; import software.amazon.awssdk.identity.spi.Identity; /** * An interface shared by {@link AwsV4HttpSigner} and {@link AwsV4aHttpSigner} for defining signer properties that are common * across both signers. */ @SdkPublicApi public interface AwsV4FamilyHttpSigner<T extends Identity> extends HttpSigner<T> { /** * The name of the AWS service. This property is required. */ SignerProperty<String> SERVICE_SIGNING_NAME = SignerProperty.create(AwsV4FamilyHttpSigner.class, "ServiceSigningName"); /** * A boolean to indicate whether to double url-encode the resource path when constructing the canonical request. This property * defaults to true. */ SignerProperty<Boolean> DOUBLE_URL_ENCODE = SignerProperty.create(AwsV4FamilyHttpSigner.class, "DoubleUrlEncode"); /** * A boolean to indicate whether the resource path should be "normalized" according to RFC3986 when constructing the canonical * request. This property defaults to true. */ SignerProperty<Boolean> NORMALIZE_PATH = SignerProperty.create(AwsV4FamilyHttpSigner.class, "NormalizePath"); /** * The location where auth-related data is inserted, as a result of signing. This property defaults to HEADER. */ SignerProperty<AuthLocation> AUTH_LOCATION = SignerProperty.create(AwsV4FamilyHttpSigner.class, "AuthLocation"); /** * The duration for the request to be valid. This property defaults to null. This can be set to presign the request for * later use. The maximum allowed value for this property is 7 days. This is only supported when AuthLocation=QUERY. */ SignerProperty<Duration> EXPIRATION_DURATION = SignerProperty.create(AwsV4FamilyHttpSigner.class, "ExpirationDuration"); /** * Whether to indicate that a payload is signed or not. This property defaults to true. This can be set false to disable * payload signing. */ SignerProperty<Boolean> PAYLOAD_SIGNING_ENABLED = SignerProperty.create(AwsV4FamilyHttpSigner.class, "PayloadSigningEnabled"); /** * Whether to indicate that a payload is chunk-encoded or not. This property defaults to false. This can be set true to * enable the `aws-chunk` content-encoding */ SignerProperty<Boolean> CHUNK_ENCODING_ENABLED = SignerProperty.create(AwsV4FamilyHttpSigner.class, "ChunkEncodingEnabled"); /** * The algorithm to use for calculating a "flexible" checksum. This property is optional. */ SignerProperty<ChecksumAlgorithm> CHECKSUM_ALGORITHM = SignerProperty.create(AwsV4FamilyHttpSigner.class, "ChecksumAlgorithm"); /** * This enum represents where auth-related data is inserted, as a result of signing. */ enum AuthLocation { /** * Indicates auth-related data is inserted in HTTP headers. */ HEADER, /** * Indicates auth-related data is inserted in HTTP query-parameters. */ QUERY_STRING } }
2,300
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal/signer/EventStreamV4PayloadSigner.java
/* * 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.eventstream.internal.signer; import java.nio.ByteBuffer; import java.time.Clock; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.auth.aws.eventstream.internal.io.SigV4DataFramePublisher; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.V4PayloadSigner; import software.amazon.awssdk.http.auth.aws.internal.signer.V4RequestSigningResult; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.Validate; /** * An implementation which supports async signing of event-stream payloads. */ @SdkInternalApi public class EventStreamV4PayloadSigner implements V4PayloadSigner { private final AwsCredentialsIdentity credentials; private final CredentialScope credentialScope; private final Clock signingClock; public EventStreamV4PayloadSigner(Builder builder) { this.credentials = Validate.paramNotNull(builder.credentials, "Credentials"); this.credentialScope = Validate.paramNotNull(builder.credentialScope, "CredentialScope"); this.signingClock = Validate.paramNotNull(builder.signingClock, "SigningClock"); } public static Builder builder() { return new Builder(); } @Override public ContentStreamProvider sign(ContentStreamProvider payload, V4RequestSigningResult requestSigningResult) { throw new UnsupportedOperationException(); } @Override public Publisher<ByteBuffer> signAsync(Publisher<ByteBuffer> payload, V4RequestSigningResult requestSigningResult) { return SigV4DataFramePublisher.builder() .publisher(payload) .credentials(credentials) .credentialScope(credentialScope) .signature(requestSigningResult.getSignature()) .signingClock(signingClock) .build(); } public static class Builder { private AwsCredentialsIdentity credentials; private CredentialScope credentialScope; private Clock signingClock; public Builder credentials(AwsCredentialsIdentity credentials) { this.credentials = credentials; return this; } public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = credentialScope; return this; } public Builder signingClock(Clock signingClock) { this.signingClock = signingClock; return this; } public EventStreamV4PayloadSigner build() { return new EventStreamV4PayloadSigner(this); } } }
2,301
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal/io/TrailingDataFramePublisher.java
/* * 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.eventstream.internal.io; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.annotations.SdkInternalApi; /** * A publisher which wraps a publisher and appends a trailing frame once the wrapped publisher is finished. */ @SdkInternalApi public final class TrailingDataFramePublisher implements Publisher<ByteBuffer> { private final Publisher<ByteBuffer> trailingPublisher; public TrailingDataFramePublisher(Publisher<ByteBuffer> publisher) { this.trailingPublisher = subscriber -> { Subscriber<ByteBuffer> adaptedSubscriber = new SubscriberAdapter(subscriber); publisher.subscribe(adaptedSubscriber); }; } @Override public void subscribe(Subscriber<? super ByteBuffer> subscriber) { trailingPublisher.subscribe(subscriber); } static class SubscriberAdapter implements Subscriber<ByteBuffer> { private final AtomicBoolean upstreamDone = new AtomicBoolean(false); private final AtomicLong downstreamDemand = new AtomicLong(); private final Object lock = new Object(); private final Subscriber<? super ByteBuffer> delegate; private volatile boolean sentTrailingFrame = false; SubscriberAdapter(Subscriber<? super ByteBuffer> actual) { this.delegate = actual; } @Override public void onSubscribe(Subscription s) { delegate.onSubscribe(new Subscription() { @Override public void request(long n) { if (n <= 0) { onError(new IllegalArgumentException("n > 0 required but it was " + n)); } downstreamDemand.getAndAdd(n); if (upstreamDone.get()) { sendTrailingEmptyFrame(); } else { s.request(n); } } @Override public void cancel() { s.cancel(); } }); } @Override public void onNext(ByteBuffer byteBuffer) { downstreamDemand.decrementAndGet(); delegate.onNext(byteBuffer); } @Override public void onError(Throwable t) { upstreamDone.compareAndSet(false, true); delegate.onError(t); } @Override public void onComplete() { upstreamDone.compareAndSet(false, true); if (downstreamDemand.get() > 0) { sendTrailingEmptyFrame(); } } private void sendTrailingEmptyFrame() { // when upstream complete, send a trailing empty frame synchronized (lock) { if (!sentTrailingFrame) { sentTrailingFrame = true; delegate.onNext(ByteBuffer.wrap(new byte[] {})); delegate.onComplete(); } } } } }
2,302
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/eventstream/internal/io/SigV4DataFramePublisher.java
/* * 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.eventstream.internal.io; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.computeSignature; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.deriveSigningKey; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.hash; import java.nio.ByteBuffer; import java.time.Clock; import java.time.Instant; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import java.util.function.Function; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Logger; import software.amazon.awssdk.utils.Validate; import software.amazon.awssdk.utils.internal.MappingSubscriber; import software.amazon.eventstream.HeaderValue; import software.amazon.eventstream.Message; /** * A publisher which adapts a publisher by SigV4-signing each frame, and appends an empty trailing frame. */ @SdkInternalApi public final class SigV4DataFramePublisher implements Publisher<ByteBuffer> { private static final Logger LOG = Logger.loggerFor(SigV4DataFramePublisher.class); private static final String CHUNK_SIGNATURE = ":chunk-signature"; private static final int PAYLOAD_TRUNCATE_LENGTH = 32; private final Publisher<ByteBuffer> sigv4Publisher; private SigV4DataFramePublisher(Builder builder) { Validate.paramNotNull(builder.publisher, "Publisher"); Validate.paramNotNull(builder.credentials, "Credentials"); Validate.paramNotNull(builder.credentialScope, "CredentialScope"); Validate.paramNotNull(builder.signature, "Signature"); Validate.paramNotNull(builder.signingClock, "SigningClock"); // Adapt the publisher with a trailing-empty frame publisher Publisher<ByteBuffer> trailingPublisher = new TrailingDataFramePublisher(builder.publisher); // Map publisher with signing function this.sigv4Publisher = subscriber -> { Subscriber<ByteBuffer> adaptedSubscriber = MappingSubscriber.create(subscriber, getDataFrameSigner(builder.credentials, builder.credentialScope, builder.signature, builder.signingClock ) ); trailingPublisher.subscribe(adaptedSubscriber); }; } private static Function<ByteBuffer, ByteBuffer> getDataFrameSigner(AwsCredentialsIdentity credentials, CredentialScope credentialScope, String signature, Clock signingClock) { return new Function<ByteBuffer, ByteBuffer>() { /** * Initiate rolling signature with an initial signature */ String priorSignature = signature; @Override public ByteBuffer apply(ByteBuffer byteBuffer) { /** * Signing Date */ Map<String, HeaderValue> eventHeaders = new HashMap<>(); Instant signingInstant = signingClock.instant(); eventHeaders.put(":date", HeaderValue.fromTimestamp(signingInstant)); /** * Derive Signing Key - since a stream of events could be over a period of time, we should update * the credential scope with the new instant every time the data-frame signer is called */ CredentialScope updatedCredentialScope = new CredentialScope(credentialScope.getRegion(), credentialScope.getService(), signingInstant); byte[] signingKey = deriveSigningKey(credentials, updatedCredentialScope); /** * Calculate rolling signature */ byte[] payload = new byte[byteBuffer.remaining()]; byteBuffer.get(payload); byte[] signatureBytes = signEvent(priorSignature, signingKey, updatedCredentialScope, eventHeaders, payload); priorSignature = BinaryUtils.toHex(signatureBytes); /** * Add signing layer headers */ Map<String, HeaderValue> headers = new HashMap<>(eventHeaders); //Signature headers headers.put(CHUNK_SIGNATURE, HeaderValue.fromByteArray(signatureBytes)); /** * Wrap payload and headers in a Message object and then encode to bytes */ Message signedMessage = new Message(sortHeaders(headers), payload); if (LOG.isLoggingLevelEnabled("trace")) { LOG.trace(() -> "Signed message: " + toDebugString(signedMessage, false)); } else { LOG.debug(() -> "Signed message: " + toDebugString(signedMessage, true)); } return signedMessage.toByteBuffer(); } }; } /** * Sign an event/chunk via SigV4 * * @param priorSignature signature of previous frame * @param signingKey derived signing key * @param credentialScope the credential-scope used to provide region, service, and time * @param eventHeaders headers pertinent to the event * @param event an event of a bytes to sign * @return encoded event with signature */ private static byte[] signEvent( String priorSignature, byte[] signingKey, CredentialScope credentialScope, Map<String, HeaderValue> eventHeaders, byte[] event) { // String to sign String eventHeadersSignature = BinaryUtils.toHex(hash(Message.encodeHeaders(sortHeaders(eventHeaders).entrySet()))); String eventHash = BinaryUtils.toHex(hash(event)); String stringToSign = "AWS4-HMAC-SHA256-PAYLOAD" + SignerConstant.LINE_SEPARATOR + credentialScope.getDatetime() + SignerConstant.LINE_SEPARATOR + credentialScope.scope() + SignerConstant.LINE_SEPARATOR + priorSignature + SignerConstant.LINE_SEPARATOR + eventHeadersSignature + SignerConstant.LINE_SEPARATOR + eventHash; // calculate signature return computeSignature(stringToSign, signingKey); } /** * Sort event headers in alphabetic order, with exception that CHUNK_SIGNATURE header always at last * * @param headers unsorted event headers * @return sorted event headers */ private static TreeMap<String, HeaderValue> sortHeaders(Map<String, HeaderValue> headers) { TreeMap<String, HeaderValue> sortedHeaders = new TreeMap<>((header1, header2) -> { // CHUNK_SIGNATURE should always be the last header if (header1.equals(CHUNK_SIGNATURE)) { return 1; // put header1 at last } if (header2.equals(CHUNK_SIGNATURE)) { return -1; // put header2 at last } return header1.compareTo(header2); }); sortedHeaders.putAll(headers); return sortedHeaders; } private static String toDebugString(Message m, boolean truncatePayload) { StringBuilder sb = new StringBuilder("Message = {headers={"); Map<String, HeaderValue> headers = m.getHeaders(); Iterator<Map.Entry<String, HeaderValue>> headersIter = headers.entrySet().iterator(); while (headersIter.hasNext()) { Map.Entry<String, HeaderValue> h = headersIter.next(); sb.append(h.getKey()).append("={").append(h.getValue().toString()).append("}"); if (headersIter.hasNext()) { sb.append(", "); } } sb.append("}, payload="); byte[] payload = m.getPayload(); byte[] payloadToLog; // We don't actually need to truncate if the payload length is already within the truncate limit truncatePayload = truncatePayload && payload.length > PAYLOAD_TRUNCATE_LENGTH; if (truncatePayload) { // Would be nice if BinaryUtils.toHex() could take an array index range instead, so we don't need to copy payloadToLog = Arrays.copyOf(payload, PAYLOAD_TRUNCATE_LENGTH); } else { payloadToLog = payload; } sb.append(BinaryUtils.toHex(payloadToLog)); if (truncatePayload) { sb.append("..."); } sb.append("}"); return sb.toString(); } public static Builder builder() { return new Builder(); } @Override public void subscribe(Subscriber<? super ByteBuffer> subscriber) { sigv4Publisher.subscribe(subscriber); } public static class Builder { private Publisher<ByteBuffer> publisher; private AwsCredentialsIdentity credentials; private CredentialScope credentialScope; private String signature; private Clock signingClock; public Builder publisher(Publisher<ByteBuffer> publisher) { this.publisher = publisher; return this; } public Builder credentials(AwsCredentialsIdentity credentials) { this.credentials = credentials; return this; } public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = credentialScope; return this; } public Builder signature(String signature) { this.signature = signature; return this; } public Builder signingClock(Clock signingClock) { this.signingClock = signingClock; return this; } public SigV4DataFramePublisher build() { return new SigV4DataFramePublisher(this); } } }
2,303
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/scheme/DefaultAwsV4aAuthScheme.java
/* * 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.scheme; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.scheme.AwsV4aAuthScheme; import software.amazon.awssdk.http.auth.aws.signer.AwsV4aHttpSigner; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.identity.spi.IdentityProvider; import software.amazon.awssdk.identity.spi.IdentityProviders; /** * A default implementation of {@link AwsV4aAuthScheme}. */ @SdkInternalApi public final class DefaultAwsV4aAuthScheme implements AwsV4aAuthScheme { private static final DefaultAwsV4aAuthScheme DEFAULT = new DefaultAwsV4aAuthScheme(); /** * Returns an instance of the {@link DefaultAwsV4aAuthScheme}. */ public static DefaultAwsV4aAuthScheme create() { return DEFAULT; } @Override public String schemeId() { return SCHEME_ID; } @Override public IdentityProvider<AwsCredentialsIdentity> identityProvider(IdentityProviders providers) { return providers.identityProvider(AwsCredentialsIdentity.class); } /** * AwsV4aHttpSigner.create() returns the CRT implementation and requires the optional dependency http-auth-aws-crt to be * added. So lazily creating the instance only when this method is called. */ @Override public AwsV4aHttpSigner signer() { return SignerSingletonHolder.INSTANCE; } private static class SignerSingletonHolder { private static final AwsV4aHttpSigner INSTANCE = AwsV4aHttpSigner.create(); } }
2,304
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/scheme/DefaultAwsV4AuthScheme.java
/* * 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.scheme; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.scheme.AwsV4AuthScheme; import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.identity.spi.IdentityProvider; import software.amazon.awssdk.identity.spi.IdentityProviders; /** * A default implementation of {@link AwsV4AuthScheme}. */ @SdkInternalApi public final class DefaultAwsV4AuthScheme implements AwsV4AuthScheme { private static final DefaultAwsV4AuthScheme DEFAULT = new DefaultAwsV4AuthScheme(); private static final AwsV4HttpSigner DEFAULT_SIGNER = AwsV4HttpSigner.create(); /** * Returns an instance of the {@link DefaultAwsV4AuthScheme}. */ public static DefaultAwsV4AuthScheme create() { return DEFAULT; } @Override public String schemeId() { return SCHEME_ID; } @Override public IdentityProvider<AwsCredentialsIdentity> identityProvider(IdentityProviders providers) { return providers.identityProvider(AwsCredentialsIdentity.class); } @Override public AwsV4HttpSigner signer() { return DEFAULT_SIGNER; } }
2,305
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/PrecomputedSha256Checksummer.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CONTENT_SHA256; import java.nio.ByteBuffer; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; /** * An implementation of a checksummer that simply passes along a computed value as a checksum. Specifically, this is used in the * cases where the checksum is a pre-defined value that dictates specific behavior by the signer, and flexible checksums is not * enabled for the request (such as aws-chunked payload signing without trailers, unsigned streaming without trailers, etc.). */ @SdkInternalApi public final class PrecomputedSha256Checksummer implements Checksummer { private final Callable<String> computation; public PrecomputedSha256Checksummer(Callable<String> computation) { this.computation = computation; } @Override public void checksum(ContentStreamProvider payload, SdkHttpRequest.Builder request) { try { String checksum = computation.call(); request.putHeader(X_AMZ_CONTENT_SHA256, checksum); } catch (Exception e) { throw new RuntimeException("Could not retrieve checksum: ", e); } } @Override public CompletableFuture<Publisher<ByteBuffer>> checksum(Publisher<ByteBuffer> payload, SdkHttpRequest.Builder request) { try { String checksum = computation.call(); request.putHeader(X_AMZ_CONTENT_SHA256, checksum); return CompletableFuture.completedFuture(payload); } catch (Exception e) { throw new RuntimeException("Could not retrieve checksum: ", e); } } }
2,306
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4RequestSigner.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.V4CanonicalRequest.getCanonicalHeaders; import static software.amazon.awssdk.http.auth.aws.internal.signer.V4CanonicalRequest.getSignedHeadersString; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.AWS4_SIGNING_ALGORITHM; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CONTENT_SHA256; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.addDateHeader; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.addHostHeader; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.formatDateTime; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.getContentHash; import java.time.Duration; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant; import software.amazon.awssdk.identity.spi.AwsSessionCredentialsIdentity; import software.amazon.awssdk.utils.Pair; /** * An interface which declares an algorithm that takes a request and a content-hash and signs the request according to the SigV4 * process. */ @SdkInternalApi public interface V4RequestSigner { /** * Retrieve an implementation of a V4RequestSigner, which signs the request, but does not add authentication to the request. */ static V4RequestSigner create(V4Properties properties, String contentHash) { return new DefaultV4RequestSigner(properties, contentHash); } /** * Retrieve an implementation of a V4RequestSigner, which signs the request and adds authentication through headers. */ static V4RequestSigner header(V4Properties properties) { return requestBuilder -> { // Add pre-requisites if (properties.getCredentials() instanceof AwsSessionCredentialsIdentity) { requestBuilder.putHeader(SignerConstant.X_AMZ_SECURITY_TOKEN, ((AwsSessionCredentialsIdentity) properties.getCredentials()).sessionToken()); } addHostHeader(requestBuilder); addDateHeader(requestBuilder, formatDateTime(properties.getCredentialScope().getInstant())); V4RequestSigningResult result = create(properties, getContentHash(requestBuilder)).sign(requestBuilder); // Add the signature within an authorization header String authHeader = AWS4_SIGNING_ALGORITHM + " Credential=" + properties.getCredentialScope().scope(properties.getCredentials()) + ", SignedHeaders=" + result.getCanonicalRequest().getSignedHeadersString() + ", Signature=" + result.getSignature(); requestBuilder.putHeader(SignerConstant.AUTHORIZATION, authHeader); return result; }; } /** * Retrieve an implementation of a V4RequestSigner, which signs the request and adds authentication through query parameters. */ static V4RequestSigner query(V4Properties properties) { return requestBuilder -> { // Add pre-requisites if (properties.getCredentials() instanceof AwsSessionCredentialsIdentity) { requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SECURITY_TOKEN, ((AwsSessionCredentialsIdentity) properties.getCredentials()).sessionToken()); } // We have to add the host-header here explicitly, since query-signed request requires it in the signed-header param addHostHeader(requestBuilder); List<Pair<String, List<String>>> canonicalHeaders = getCanonicalHeaders(requestBuilder.build()); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_ALGORITHM, AWS4_SIGNING_ALGORITHM); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_DATE, properties.getCredentialScope().getDatetime()); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SIGNED_HEADERS, getSignedHeadersString(canonicalHeaders)); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_CREDENTIAL, properties.getCredentialScope().scope(properties.getCredentials())); V4RequestSigningResult result = create(properties, getContentHash(requestBuilder)).sign(requestBuilder); // Add the signature requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SIGNATURE, result.getSignature()); return result; }; } /** * Retrieve an implementation of a V4RequestSigner, which signs the request and adds authentication through query parameters, * which includes an expiration param, signalling how long a request signature is valid. */ static V4RequestSigner presigned(V4Properties properties, Duration expirationDuration) { return requestBuilder -> { // Add pre-requisites if (properties.getCredentials() instanceof AwsSessionCredentialsIdentity) { requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SECURITY_TOKEN, ((AwsSessionCredentialsIdentity) properties.getCredentials()).sessionToken()); } // We have to add the host-header here explicitly, since pre-signed request requires it in the signed-header param addHostHeader(requestBuilder); // Pre-signed requests shouldn't have the content-hash header String contentHash = getContentHash(requestBuilder); requestBuilder.removeHeader(X_AMZ_CONTENT_SHA256); List<Pair<String, List<String>>> canonicalHeaders = getCanonicalHeaders(requestBuilder.build()); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_ALGORITHM, AWS4_SIGNING_ALGORITHM); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_DATE, properties.getCredentialScope().getDatetime()); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SIGNED_HEADERS, getSignedHeadersString(canonicalHeaders)); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_CREDENTIAL, properties.getCredentialScope().scope(properties.getCredentials())); requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_EXPIRES, Long.toString(expirationDuration.getSeconds())); V4RequestSigningResult result = create(properties, contentHash).sign(requestBuilder); // Add the signature requestBuilder.putRawQueryParameter(SignerConstant.X_AMZ_SIGNATURE, result.getSignature()); return result; }; } /** * Retrieve an implementation of a V4RequestSigner to handle the anonymous credentials case, where the request is not * sigend at all. */ static V4RequestSigner anonymous(V4Properties properties) { return requestBuilder -> new V4RequestSigningResult("", new byte[] {}, null, null, requestBuilder); } /** * Given a request builder, sign the request and return a result containing the signed request and its properties. */ V4RequestSigningResult sign(SdkHttpRequest.Builder requestBuilder); }
2,307
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4RequestSigningResult.java
/* * 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.signer; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.SdkHttpRequest; /** * A container for data produced during and as a result of the SigV4 request signing process. */ @SdkInternalApi // TODO(sra-identity-auth): This is currently not @Immutable because signedRequest is a Builder. Is Builder needed? If it could // hold reference to SdkHttpRequest instead, this class would be @Immutable. public final class V4RequestSigningResult { private final String contentHash; private final byte[] signingKey; private final String signature; private final V4CanonicalRequest canonicalRequest; private final SdkHttpRequest.Builder signedRequest; public V4RequestSigningResult(String contentHash, byte[] signingKey, String signature, V4CanonicalRequest canonicalRequest, SdkHttpRequest.Builder signedRequest) { this.contentHash = contentHash; this.signingKey = signingKey.clone(); this.signature = signature; this.canonicalRequest = canonicalRequest; this.signedRequest = signedRequest; } public String getContentHash() { return contentHash; } public byte[] getSigningKey() { return signingKey.clone(); } public String getSignature() { return signature; } public V4CanonicalRequest getCanonicalRequest() { return canonicalRequest; } public SdkHttpRequest.Builder getSignedRequest() { return signedRequest; } }
2,308
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/FlexibleChecksummer.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.fromChecksumAlgorithm; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.readAll; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.getBinaryRequestPayloadStream; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Collection; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.stream.Collectors; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ChecksumInputStream; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ChecksumSubscriber; import software.amazon.awssdk.utils.Validate; /** * A "flexible" implementation of a checksummer. It takes a map of checksums and their header names, computes them efficiently by * updating each checksum while reading the payload (once), and adds the computed checksum strings to the request using the given * header names in the map. This should be used in cases where a (flexible) checksum algorithm is present during signing. */ @SdkInternalApi public final class FlexibleChecksummer implements Checksummer { private final Collection<Option> options; private final Map<Option, SdkChecksum> optionToSdkChecksum; public FlexibleChecksummer(Option... options) { this.options = Arrays.asList(options); this.optionToSdkChecksum = this.options.stream().collect( Collectors.toMap(Function.identity(), o -> fromChecksumAlgorithm(o.algorithm)) ); } @Override public void checksum(ContentStreamProvider payload, SdkHttpRequest.Builder request) { InputStream payloadStream = getBinaryRequestPayloadStream(payload); ChecksumInputStream computingStream = new ChecksumInputStream( payloadStream, optionToSdkChecksum.values() ); readAll(computingStream); addChecksums(request); } @Override public CompletableFuture<Publisher<ByteBuffer>> checksum(Publisher<ByteBuffer> payload, SdkHttpRequest.Builder request) { ChecksumSubscriber checksumSubscriber = new ChecksumSubscriber(optionToSdkChecksum.values()); if (payload == null) { addChecksums(request); return CompletableFuture.completedFuture(null); } payload.subscribe(checksumSubscriber); CompletableFuture<Publisher<ByteBuffer>> result = checksumSubscriber.completeFuture(); result.thenRun(() -> addChecksums(request)); return result; } private void addChecksums(SdkHttpRequest.Builder request) { optionToSdkChecksum.forEach( (option, sdkChecksum) -> request.putHeader( option.headerName, option.formatter.apply(sdkChecksum.getChecksumBytes())) ); } public static Option.Builder option() { return Option.builder(); } public static class Option { private final ChecksumAlgorithm algorithm; private final String headerName; private final Function<byte[], String> formatter; Option(Builder builder) { this.algorithm = Validate.paramNotNull(builder.algorithm, "algorithm"); this.headerName = Validate.paramNotNull(builder.headerName, "headerName"); this.formatter = Validate.paramNotNull(builder.formatter, "formatter"); } public static Builder builder() { return new Builder(); } public static class Builder { private ChecksumAlgorithm algorithm; private String headerName; private Function<byte[], String> formatter; public Builder algorithm(ChecksumAlgorithm algorithm) { this.algorithm = algorithm; return this; } public Builder headerName(String headerName) { this.headerName = headerName; return this; } public Builder formatter(Function<byte[], String> formatter) { this.formatter = formatter; return this; } public Option build() { return new Option(this); } } } }
2,309
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/Checksummer.java
/* * 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.signer; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA256; import static software.amazon.awssdk.http.auth.aws.internal.signer.FlexibleChecksummer.option; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.ConstantChecksumAlgorithm; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.checksumHeaderName; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CONTENT_SHA256; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.concurrent.CompletableFuture; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.utils.BinaryUtils; /** * An interface for defining how a checksum is formed from a payload synchronously and asynchronously. * <p> * The implementation may choose to also manipulate the request with the checksum, such as adding it as a header. */ @SdkInternalApi public interface Checksummer { /** * Get a default implementation of a checksummer, which calculates the SHA-256 checksum and places it in the * x-amz-content-sha256 header. */ static Checksummer create() { return new FlexibleChecksummer( option().headerName(X_AMZ_CONTENT_SHA256).algorithm(SHA256).formatter(BinaryUtils::toHex).build() ); } /** * Get a flexible checksummer that performs two checksums: the given checksum-algorithm and the SHA-256 checksum. It places * the SHA-256 checksum in x-amz-content-sha256 header, and the given checksum-algorithm in the x-amz-checksum-[name] header. */ static Checksummer forFlexibleChecksum(ChecksumAlgorithm checksumAlgorithm) { if (checksumAlgorithm != null) { return new FlexibleChecksummer( option().headerName(X_AMZ_CONTENT_SHA256).algorithm(SHA256).formatter(BinaryUtils::toHex) .build(), option().headerName(checksumHeaderName(checksumAlgorithm)).algorithm(checksumAlgorithm) .formatter(BinaryUtils::toBase64).build() ); } throw new IllegalArgumentException("Checksum Algorithm cannot be null!"); } /** * Get a precomputed checksummer which places the precomputed checksum to the x-amz-content-sha256 header. */ static Checksummer forPrecomputed256Checksum(String precomputedSha256) { return new PrecomputedSha256Checksummer(() -> precomputedSha256); } /** * Get a flexible checksummer that performs two checksums: the given checksum-algorithm and a precomputed checksum from the * given checksum string. It places the precomputed checksum in x-amz-content-sha256 header, and the given checksum-algorithm * in the x-amz-checksum-[name] header. */ static Checksummer forFlexibleChecksum(String precomputedSha256, ChecksumAlgorithm checksumAlgorithm) { if (checksumAlgorithm != null) { return new FlexibleChecksummer( option().headerName(X_AMZ_CONTENT_SHA256).algorithm(new ConstantChecksumAlgorithm(precomputedSha256)) .formatter(b -> new String(b, StandardCharsets.UTF_8)).build(), option().headerName(checksumHeaderName(checksumAlgorithm)).algorithm(checksumAlgorithm) .formatter(BinaryUtils::toBase64).build() ); } throw new IllegalArgumentException("Checksum Algorithm cannot be null!"); } static Checksummer forNoOp() { return new FlexibleChecksummer(); } /** * Given a payload, calculate a checksum and add it to the request. */ void checksum(ContentStreamProvider payload, SdkHttpRequest.Builder request); /** * Given a payload, asynchronously calculate a checksum and promise to add it to the request. */ CompletableFuture<Publisher<ByteBuffer>> checksum(Publisher<ByteBuffer> payload, SdkHttpRequest.Builder request); }
2,310
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/RollingSigner.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.computeSignature; import static software.amazon.awssdk.utils.BinaryUtils.toHex; import java.util.function.Function; import software.amazon.awssdk.annotations.SdkInternalApi; /** * A class which calculates a rolling signature of arbitrary data using HMAC-SHA256. Each time a signature is calculated, the * prior calculation is incorporated, hence "rolling". */ @SdkInternalApi public final class RollingSigner { private final byte[] signingKey; private final String seedSignature; private String previousSignature; public RollingSigner(byte[] signingKey, String seedSignature) { this.seedSignature = seedSignature; this.previousSignature = seedSignature; this.signingKey = signingKey.clone(); } /** * Using a template that incorporates the previous calculated signature, sign the string and return it. */ public String sign(Function<String, String> stringToSignTemplate) { String stringToSign = stringToSignTemplate.apply(previousSignature); byte[] bytes = computeSignature(stringToSign, signingKey); previousSignature = toHex(bytes); return previousSignature; } public void reset() { previousSignature = seedSignature; } }
2,311
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/DefaultV4RequestSigner.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.AWS4_SIGNING_ALGORITHM; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.deriveSigningKey; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.hashCanonicalRequest; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant; import software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Logger; /** * The default implementation of a v4-request-signer. It performs each step of the SigV4 signing process, but does not add the * signature or auth information to the request itself. * <p> * All signing information, such as signature, signing key, canonical request, etc. is present in result object that is returned. * This can be used by the caller to add the auth info to the request, such as adding the signature as a query parameter or * building an authorization header using the signature and canonical request headers. */ @SdkInternalApi public final class DefaultV4RequestSigner implements V4RequestSigner { private static final Logger LOG = Logger.loggerFor(DefaultV4RequestSigner.class); private final V4Properties properties; private final String contentHash; public DefaultV4RequestSigner(V4Properties properties, String contentHash) { this.properties = properties; this.contentHash = contentHash; } @Override public V4RequestSigningResult sign(SdkHttpRequest.Builder requestBuilder) { // Step 1: Create a canonical request V4CanonicalRequest canonicalRequest = createCanonicalRequest(requestBuilder.build(), contentHash); // Step 2: Create a hash of the canonical request String canonicalRequestHash = hashCanonicalRequest(canonicalRequest.getCanonicalRequestString()); // Step 2: Create a hash of the canonical request String stringToSign = createSignString(canonicalRequestHash); // Step 4: Calculate the signature byte[] signingKey = createSigningKey(); String signature = createSignature(stringToSign, signingKey); // Step 5: Return the results (including signature) of request signing return new V4RequestSigningResult(contentHash, signingKey, signature, canonicalRequest, requestBuilder); } private V4CanonicalRequest createCanonicalRequest(SdkHttpRequest request, String contentHash) { return new V4CanonicalRequest(request, contentHash, new V4CanonicalRequest.Options( properties.shouldDoubleUrlEncode(), properties.shouldNormalizePath() )); } private String createSignString(String canonicalRequestHash) { LOG.debug(() -> "AWS4 Canonical Request Hash: " + canonicalRequestHash); String stringToSign = AWS4_SIGNING_ALGORITHM + SignerConstant.LINE_SEPARATOR + properties.getCredentialScope().getDatetime() + SignerConstant.LINE_SEPARATOR + properties.getCredentialScope().scope() + SignerConstant.LINE_SEPARATOR + canonicalRequestHash; LOG.debug(() -> "AWS4 String to sign: " + stringToSign); return stringToSign; } private byte[] createSigningKey() { return deriveSigningKey(properties.getCredentials(), properties.getCredentialScope()); } private String createSignature(String stringToSign, byte[] signingKey) { return BinaryUtils.toHex(SignerUtils.computeSignature(stringToSign, signingKey)); } }
2,312
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/DefaultAwsV4HttpSigner.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.checksumHeaderName; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.CredentialUtils.sanitizeCredentials; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.OptionalDependencyLoaderUtil.getEventStreamV4PayloadSigner; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.PRESIGN_URL_MAX_EXPIRATION_DURATION; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_EVENTS_PAYLOAD; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_SIGNED_PAYLOAD; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_SIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_UNSIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.UNSIGNED_PAYLOAD; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_TRAILER; import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.concurrent.CompletableFuture; import java.util.function.Function; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.util.CredentialUtils; import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; 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.BaseSignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; /** * An implementation of a {@link AwsV4HttpSigner} that uses properties to compose v4-signers in order to delegate signing of a * request and payload (if applicable) accordingly. */ @SdkInternalApi public final class DefaultAwsV4HttpSigner implements AwsV4HttpSigner { private static final int DEFAULT_CHUNK_SIZE_IN_BYTES = 128 * 1024; @Override public SignedRequest sign(SignRequest<? extends AwsCredentialsIdentity> request) { Checksummer checksummer = checksummer(request, null); V4Properties v4Properties = v4Properties(request); V4RequestSigner v4RequestSigner = v4RequestSigner(request, v4Properties); V4PayloadSigner payloadSigner = v4PayloadSigner(request, v4Properties); return doSign(request, checksummer, v4RequestSigner, payloadSigner); } @Override public CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<? extends AwsCredentialsIdentity> request) { Checksummer checksummer = asyncChecksummer(request); V4Properties v4Properties = v4Properties(request); V4RequestSigner v4RequestSigner = v4RequestSigner(request, v4Properties); V4PayloadSigner payloadSigner = v4PayloadAsyncSigner(request, v4Properties); return doSign(request, checksummer, v4RequestSigner, payloadSigner); } private static V4Properties v4Properties(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { Clock signingClock = request.requireProperty(SIGNING_CLOCK, Clock.systemUTC()); Instant signingInstant = signingClock.instant(); AwsCredentialsIdentity credentials = sanitizeCredentials(request.identity()); String regionName = request.requireProperty(AwsV4HttpSigner.REGION_NAME); String serviceSigningName = request.requireProperty(SERVICE_SIGNING_NAME); CredentialScope credentialScope = new CredentialScope(regionName, serviceSigningName, signingInstant); boolean doubleUrlEncode = request.requireProperty(DOUBLE_URL_ENCODE, true); boolean normalizePath = request.requireProperty(NORMALIZE_PATH, true); return V4Properties.builder() .credentials(credentials) .credentialScope(credentialScope) .signingClock(signingClock) .doubleUrlEncode(doubleUrlEncode) .normalizePath(normalizePath) .build(); } private static V4RequestSigner v4RequestSigner( BaseSignRequest<?, ? extends AwsCredentialsIdentity> request, V4Properties v4Properties) { AuthLocation authLocation = request.requireProperty(AUTH_LOCATION, AuthLocation.HEADER); Duration expirationDuration = request.property(EXPIRATION_DURATION); boolean isAnonymous = CredentialUtils.isAnonymous(request.identity()); if (isAnonymous) { return V4RequestSigner.anonymous(v4Properties); } Function<V4Properties, V4RequestSigner> requestSigner; switch (authLocation) { case HEADER: if (expirationDuration != null) { throw new UnsupportedOperationException( String.format("%s is not supported for %s.", EXPIRATION_DURATION, AuthLocation.HEADER)); } requestSigner = V4RequestSigner::header; break; case QUERY_STRING: requestSigner = expirationDuration == null ? V4RequestSigner::query : properties -> V4RequestSigner.presigned(properties, validateExpirationDuration(expirationDuration)); break; default: throw new UnsupportedOperationException("Unsupported authLocation " + authLocation); } return requestSigner.apply(v4Properties); } private static Checksummer checksummer(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request, Boolean isPayloadSigningOverride) { boolean isPayloadSigning = isPayloadSigningOverride != null ? isPayloadSigningOverride : isPayloadSigning(request); boolean isEventStreaming = isEventStreaming(request.request()); boolean hasChecksumHeader = hasChecksumHeader(request); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent(); boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM) && !hasChecksumHeader; boolean isAnonymous = CredentialUtils.isAnonymous(request.identity()); if (isEventStreaming) { return Checksummer.forPrecomputed256Checksum(STREAMING_EVENTS_PAYLOAD); } if (isPayloadSigning) { if (isChunkEncoding) { if (isFlexible || isTrailing) { return Checksummer.forPrecomputed256Checksum(STREAMING_SIGNED_PAYLOAD_TRAILER); } return Checksummer.forPrecomputed256Checksum(STREAMING_SIGNED_PAYLOAD); } if (isFlexible) { return Checksummer.forFlexibleChecksum(request.property(CHECKSUM_ALGORITHM)); } return Checksummer.create(); } if (isFlexible || isTrailing) { if (isChunkEncoding) { return Checksummer.forPrecomputed256Checksum(STREAMING_UNSIGNED_PAYLOAD_TRAILER); } } if (isFlexible) { return Checksummer.forFlexibleChecksum(UNSIGNED_PAYLOAD, request.property(CHECKSUM_ALGORITHM)); } if (isAnonymous) { return Checksummer.forNoOp(); } return Checksummer.forPrecomputed256Checksum(UNSIGNED_PAYLOAD); } /** * This is needed because of the pre-existing gap (pre-SRA) in behavior where we don't treat async + streaming + http + * unsigned-payload as signed-payload (fallback). We have to do some finagling of the payload-signing options before * calling the actual checksummer() method */ private static Checksummer asyncChecksummer(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { boolean isHttp = !"https".equals(request.request().protocol()); boolean isPayloadSigning = isPayloadSigning(request); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); boolean shouldTreatAsUnsigned = isHttp && isPayloadSigning && isChunkEncoding; // set the override to false if it should be treated as unsigned, otherwise, null should be passed so that the normal // check for payload signing is done. Boolean overridePayloadSigning = shouldTreatAsUnsigned ? false : null; return checksummer(request, overridePayloadSigning); } private static V4PayloadSigner v4PayloadSigner( SignRequest<? extends AwsCredentialsIdentity> request, V4Properties properties) { boolean isPayloadSigning = isPayloadSigning(request); boolean isEventStreaming = isEventStreaming(request.request()); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent(); boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM) && !hasChecksumHeader(request); if (isEventStreaming) { if (isPayloadSigning) { return getEventStreamV4PayloadSigner( properties.getCredentials(), properties.getCredentialScope(), properties.getSigningClock() ); } throw new UnsupportedOperationException("Unsigned payload is not supported with event-streaming."); } if (useChunkEncoding(isPayloadSigning, isChunkEncoding, isTrailing || isFlexible)) { return AwsChunkedV4PayloadSigner.builder() .credentialScope(properties.getCredentialScope()) .chunkSize(DEFAULT_CHUNK_SIZE_IN_BYTES) .checksumAlgorithm(request.property(CHECKSUM_ALGORITHM)) .build(); } return V4PayloadSigner.create(); } private static V4PayloadSigner v4PayloadAsyncSigner( AsyncSignRequest<? extends AwsCredentialsIdentity> request, V4Properties properties) { boolean isPayloadSigning = request.requireProperty(PAYLOAD_SIGNING_ENABLED, true); boolean isEventStreaming = isEventStreaming(request.request()); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); if (isEventStreaming) { if (isPayloadSigning) { return getEventStreamV4PayloadSigner( properties.getCredentials(), properties.getCredentialScope(), properties.getSigningClock() ); } throw new UnsupportedOperationException("Unsigned payload is not supported with event-streaming."); } if (isChunkEncoding && isPayloadSigning) { // TODO(sra-identity-and-auth): We need to implement aws-chunk content-encoding for async. // For now, we basically have to treat this as an unsigned case because there are existing s3 use-cases for // Unsigned-payload + HTTP. These requests SHOULD be signed-payload, but are not pre-SRA, hence the problem. This // will be taken care of in HttpChecksumStage for now, so we shouldn't throw an unsupported exception here, we // should just fall through to the default since it will already encoded by the time it gets here. return V4PayloadSigner.create(); } return V4PayloadSigner.create(); } private static SignedRequest doSign(SignRequest<? extends AwsCredentialsIdentity> request, Checksummer checksummer, V4RequestSigner requestSigner, V4PayloadSigner payloadSigner) { SdkHttpRequest.Builder requestBuilder = request.request().toBuilder(); ContentStreamProvider requestPayload = request.payload().orElse(null); checksummer.checksum(requestPayload, requestBuilder); payloadSigner.beforeSigning(requestBuilder, requestPayload); V4RequestSigningResult requestSigningResult = requestSigner.sign(requestBuilder); ContentStreamProvider signedPayload = null; if (requestPayload != null) { signedPayload = payloadSigner.sign(requestPayload, requestSigningResult); } return SignedRequest.builder() .request(requestSigningResult.getSignedRequest().build()) .payload(signedPayload) .build(); } private static CompletableFuture<AsyncSignedRequest> doSign(AsyncSignRequest<? extends AwsCredentialsIdentity> request, Checksummer checksummer, V4RequestSigner requestSigner, V4PayloadSigner payloadSigner) { SdkHttpRequest.Builder requestBuilder = request.request().toBuilder(); return checksummer.checksum(request.payload().orElse(null), requestBuilder) .thenApply(payload -> { V4RequestSigningResult requestSigningResultFuture = requestSigner.sign(requestBuilder); return AsyncSignedRequest.builder() .request(requestSigningResultFuture.getSignedRequest().build()) .payload(payloadSigner.signAsync(payload, requestSigningResultFuture)) .build(); }); } private static Duration validateExpirationDuration(Duration expirationDuration) { if (!isBetweenInclusive(Duration.ofSeconds(1), expirationDuration, PRESIGN_URL_MAX_EXPIRATION_DURATION)) { throw new IllegalArgumentException( "Requests that are pre-signed by SigV4 algorithm are valid for at least 1 second and at most 7" + " days. The expiration duration set on the current request [" + expirationDuration + "]" + " does not meet these bounds." ); } return expirationDuration; } private static boolean isBetweenInclusive(Duration start, Duration x, Duration end) { return start.compareTo(x) <= 0 && x.compareTo(end) <= 0; } private static boolean isPayloadSigning(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { boolean isAnonymous = CredentialUtils.isAnonymous(request.identity()); boolean isPayloadSigningEnabled = request.requireProperty(PAYLOAD_SIGNING_ENABLED, true); boolean isEncrypted = "https".equals(request.request().protocol()); return !isAnonymous && (isPayloadSigningEnabled || !isEncrypted); } private static boolean isEventStreaming(SdkHttpRequest request) { return "application/vnd.amazon.eventstream".equals(request.firstMatchingHeader(Header.CONTENT_TYPE).orElse("")); } private static boolean hasChecksumHeader(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { ChecksumAlgorithm checksumAlgorithm = request.property(CHECKSUM_ALGORITHM); if (checksumAlgorithm != null) { String checksumHeaderName = checksumHeaderName(checksumAlgorithm); return request.request().firstMatchingHeader(checksumHeaderName).isPresent(); } return false; } private static boolean useChunkEncoding(boolean payloadSigningEnabled, boolean chunkEncodingEnabled, boolean isTrailingOrFlexible) { return (payloadSigningEnabled && chunkEncodingEnabled) || (chunkEncodingEnabled && isTrailingOrFlexible); } }
2,313
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/CredentialScope.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.AWS4_TERMINATOR; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.formatDate; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.formatDateTime; import java.time.Instant; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; @SdkInternalApi @Immutable public final class CredentialScope { private final String region; private final String service; private final Instant instant; public CredentialScope(String region, String service, Instant instant) { this.region = region; this.service = service; this.instant = instant; } public String getRegion() { return region; } public String getService() { return service; } public Instant getInstant() { return instant; } public String getDate() { return formatDate(instant); } public String getDatetime() { return formatDateTime(instant); } public String scope() { return getDate() + "/" + region + "/" + service + "/" + AWS4_TERMINATOR; } public String scope(AwsCredentialsIdentity credentials) { return credentials.accessKeyId() + "/" + scope(); } }
2,314
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4PayloadSigner.java
/* * 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.signer; import java.nio.ByteBuffer; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; /** * An interface for defining how to sign a payload via SigV4. */ @SdkInternalApi public interface V4PayloadSigner { /** * Get a default implementation of a SigV4 payload signer. */ static V4PayloadSigner create() { return new DefaultV4PayloadSigner(); } /** * Given a payload and result of request signing, sign the payload via the SigV4 process. */ ContentStreamProvider sign(ContentStreamProvider payload, V4RequestSigningResult requestSigningResult); /** * Given a payload and result of request signing, sign the payload via the SigV4 process. */ Publisher<ByteBuffer> signAsync(Publisher<ByteBuffer> payload, V4RequestSigningResult requestSigningResult); /** * Modify a request before it is signed, such as changing headers or query-parameters. */ default void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider payload) { } }
2,315
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4CanonicalRequest.java
/* * 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.signer; import static software.amazon.awssdk.utils.StringUtils.lowerCase; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant; import software.amazon.awssdk.utils.Pair; import software.amazon.awssdk.utils.StringUtils; import software.amazon.awssdk.utils.http.SdkHttpUtils; /** * A class that represents a canonical request in AWS, as documented: * <p> * https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html#create-canonical-request * </p> */ @SdkInternalApi @Immutable public final class V4CanonicalRequest { private static final List<String> HEADERS_TO_IGNORE_IN_LOWER_CASE = Arrays.asList("connection", "x-amzn-trace-id", "user-agent", "expect"); private final SdkHttpRequest request; private final String contentHash; private final Options options; // Compute these fields lazily when, and, if needed. private String canonicalUri; private SortedMap<String, List<String>> canonicalParams; private List<Pair<String, List<String>>> canonicalHeaders; private String canonicalQueryString; private String canonicalHeadersString; private String signedHeadersString; private String canonicalRequestString; /** * Create a canonical request. * <p> * Each parameter of a canonical request is set upon creation of this object. * <p> * To get such a parameter (i.e. the canonical request string), simply call the getter for that parameter (i.e. * getCanonicalRequestString()) */ public V4CanonicalRequest(SdkHttpRequest request, String contentHash, Options options) { this.request = request; this.contentHash = contentHash; this.options = options; } /** * Get the string representing which headers are part of the signing process. Header names are separated by a semicolon. */ public String getSignedHeadersString() { if (signedHeadersString == null) { signedHeadersString = getSignedHeadersString(canonicalHeaders()); } return signedHeadersString; } /** * Get the canonical request string. */ public String getCanonicalRequestString() { if (canonicalRequestString == null) { canonicalRequestString = getCanonicalRequestString(request.method().toString(), canonicalUri(), canonicalQueryString(), canonicalHeadersString(), getSignedHeadersString(), contentHash); } return canonicalRequestString; } private SortedMap<String, List<String>> canonicalQueryParams() { if (canonicalParams == null) { canonicalParams = getCanonicalQueryParams(request); } return canonicalParams; } private List<Pair<String, List<String>>> canonicalHeaders() { if (canonicalHeaders == null) { canonicalHeaders = getCanonicalHeaders(request); } return canonicalHeaders; } private String canonicalUri() { if (canonicalUri == null) { canonicalUri = getCanonicalUri(request, options); } return canonicalUri; } private String canonicalQueryString() { if (canonicalQueryString == null) { canonicalQueryString = getCanonicalQueryString(canonicalQueryParams()); } return canonicalQueryString; } private String canonicalHeadersString() { if (canonicalHeadersString == null) { canonicalHeadersString = getCanonicalHeadersString(canonicalHeaders()); } return canonicalHeadersString; } /** * Get the list of headers that are to be signed. * <p> * If calling from a site with the request object handy, this method should be used instead of passing the headers themselves, * as doing so creates a redundant copy. */ public static List<Pair<String, List<String>>> getCanonicalHeaders(SdkHttpRequest request) { List<Pair<String, List<String>>> result = new ArrayList<>(request.numHeaders()); // headers retrieved from the request are already sorted case-insensitively request.forEachHeader((key, value) -> { String lowerCaseHeader = lowerCase(key); if (!HEADERS_TO_IGNORE_IN_LOWER_CASE.contains(lowerCaseHeader)) { result.add(Pair.of(lowerCaseHeader, value)); } }); result.sort(Comparator.comparing(Pair::left)); return result; } /** * Get the list of headers that are to be signed. The supplied map of headers is expected to be sorted case-insensitively. */ public static List<Pair<String, List<String>>> getCanonicalHeaders(Map<String, List<String>> headers) { List<Pair<String, List<String>>> result = new ArrayList<>(headers.size()); headers.forEach((key, value) -> { String lowerCaseHeader = lowerCase(key); if (!HEADERS_TO_IGNORE_IN_LOWER_CASE.contains(lowerCaseHeader)) { result.add(Pair.of(lowerCaseHeader, value)); } }); result.sort(Comparator.comparing(Pair::left)); return result; } /** * Get the string representing the headers that will be signed and their values. The input list is expected to be sorted * case-insensitively. * <p> * The output string will have header names as lower-case, sorted in alphabetical order, and followed by a colon. * <p> * Values are trimmed of any leading/trailing spaces, sequential spaces are converted to single space, and multiple values are * comma separated. * <p> * Each header-value pair is separated by a newline. */ public static String getCanonicalHeadersString(List<Pair<String, List<String>>> canonicalHeaders) { StringBuilder result = new StringBuilder(512); canonicalHeaders.forEach(header -> { result.append(header.left()); result.append(":"); for (String headerValue : header.right()) { addAndTrim(result, headerValue); result.append(","); } result.setLength(result.length() - 1); result.append("\n"); }); return result.toString(); } /** * Get the string representing which headers are part of the signing process. Header names are separated by a semicolon. */ public static String getSignedHeadersString(List<Pair<String, List<String>>> canonicalHeaders) { String signedHeadersString; StringBuilder headersString = new StringBuilder(512); for (Pair<String, List<String>> header : canonicalHeaders) { headersString.append(header.left()).append(";"); } // get rid of trailing semicolon signedHeadersString = headersString.toString(); boolean trimTrailingSemicolon = signedHeadersString.length() > 1 && signedHeadersString.endsWith(";"); if (trimTrailingSemicolon) { signedHeadersString = signedHeadersString.substring(0, signedHeadersString.length() - 1); } return signedHeadersString; } /** * Get the canonical request string. * <p> * Each {@link String} parameter is separated by a newline character. */ private static String getCanonicalRequestString(String httpMethod, String canonicalUri, String canonicalParamsString, String canonicalHeadersString, String signedHeadersString, String contentHash) { return httpMethod + SignerConstant.LINE_SEPARATOR + canonicalUri + SignerConstant.LINE_SEPARATOR + canonicalParamsString + SignerConstant.LINE_SEPARATOR + canonicalHeadersString + SignerConstant.LINE_SEPARATOR + signedHeadersString + SignerConstant.LINE_SEPARATOR + contentHash; } /** * "The addAndTrim function removes excess white space before and after values, and converts sequential spaces to a single * space." * <p> * https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html * <p> * The collapse-whitespace logic is equivalent to: * <pre> * value.replaceAll("\\s+", " ") * </pre> * but does not create a Pattern object that needs to compile the match string; it also prevents us from having to make a * Matcher object as well. */ private static void addAndTrim(StringBuilder result, String value) { int lengthBefore = result.length(); boolean isStart = true; boolean previousIsWhiteSpace = false; for (int i = 0; i < value.length(); i++) { char ch = value.charAt(i); if (isWhiteSpace(ch)) { if (previousIsWhiteSpace || isStart) { continue; } result.append(' '); previousIsWhiteSpace = true; } else { result.append(ch); isStart = false; previousIsWhiteSpace = false; } } if (lengthBefore == result.length()) { return; } int lastNonWhitespaceChar = result.length() - 1; while (isWhiteSpace(result.charAt(lastNonWhitespaceChar))) { --lastNonWhitespaceChar; } result.setLength(lastNonWhitespaceChar + 1); } /** * Get the uri-encoded version of the absolute path component URL. * <p> * If the path is empty, a single-forward slash ('/') is returned. */ private static String getCanonicalUri(SdkHttpRequest request, Options options) { String path = options.normalizePath ? request.getUri().normalize().getRawPath() : request.encodedPath(); if (StringUtils.isEmpty(path)) { return "/"; } if (options.doubleUrlEncode) { path = SdkHttpUtils.urlEncodeIgnoreSlashes(path); } if (!path.startsWith("/")) { path += "/"; } // Normalization can leave a trailing slash at the end of the resource path, // even if the input path doesn't end with one. Example input: /foo/bar/. // Remove the trailing slash if the input path doesn't end with one. boolean trimTrailingSlash = options.normalizePath && path.length() > 1 && !request.getUri().getPath().endsWith("/") && path.charAt(path.length() - 1) == '/'; if (trimTrailingSlash) { path = path.substring(0, path.length() - 1); } return path; } /** * Get the sorted map of query parameters that are to be signed. */ private static SortedMap<String, List<String>> getCanonicalQueryParams(SdkHttpRequest request) { SortedMap<String, List<String>> sorted = new TreeMap<>(); // Signing protocol expects the param values also to be sorted after url // encoding in addition to sorted parameter names. request.forEachRawQueryParameter((key, values) -> { if (StringUtils.isEmpty(key)) { // Do not sign empty keys. return; } String encodedParamName = SdkHttpUtils.urlEncode(key); List<String> encodedValues = new ArrayList<>(values.size()); for (String value : values) { String encodedValue = SdkHttpUtils.urlEncode(value); // Null values should be treated as empty for the purposes of signing, not missing. // For example "?foo=" instead of "?foo". String signatureFormattedEncodedValue = encodedValue == null ? "" : encodedValue; encodedValues.add(signatureFormattedEncodedValue); } Collections.sort(encodedValues); sorted.put(encodedParamName, encodedValues); }); return sorted; } /** * Get the string representing query string parameters. Parameters are URL-encoded and separated by an ampersand. * <p> * Reserved characters are percent-encoded, names and values are encoded separately and empty parameters have an equals-sign * appended before encoding. * <p> * After encoding, parameters are sorted alphabetically by key name. * <p> * If no query string is given, an empty string ("") is returned. */ private static String getCanonicalQueryString(SortedMap<String, List<String>> canonicalParams) { if (canonicalParams.isEmpty()) { return ""; } StringBuilder stringBuilder = new StringBuilder(512); SdkHttpUtils.flattenQueryParameters(stringBuilder, canonicalParams); return stringBuilder.toString(); } private static boolean isWhiteSpace(char ch) { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\u000b' || ch == '\r' || ch == '\f'; } /** * A class for representing options used when creating a {@link V4CanonicalRequest} */ public static class Options { final boolean doubleUrlEncode; final boolean normalizePath; public Options(boolean doubleUrlEncode, boolean normalizePath) { this.doubleUrlEncode = doubleUrlEncode; this.normalizePath = normalizePath; } } }
2,316
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/DefaultV4PayloadSigner.java
/* * 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.signer; import java.nio.ByteBuffer; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; /** * A default implementation of a payload signer that is a no-op, since payloads are most commonly unsigned. */ @SdkInternalApi public class DefaultV4PayloadSigner implements V4PayloadSigner { @Override public ContentStreamProvider sign(ContentStreamProvider payload, V4RequestSigningResult requestSigningResult) { return payload; } @Override public Publisher<ByteBuffer> signAsync(Publisher<ByteBuffer> payload, V4RequestSigningResult requestSigningResult) { return payload; } }
2,317
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4Properties.java
/* * 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.signer; import java.time.Clock; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.spi.signer.BaseSignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignerProperty; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.Validate; /** * A class which contains "properties" relevant to SigV4. These properties can be derived {@link SignerProperty}'s on a * {@link BaseSignRequest}. */ @SdkInternalApi @Immutable public final class V4Properties { private final AwsCredentialsIdentity credentials; private final CredentialScope credentialScope; private final Clock signingClock; private final boolean doubleUrlEncode; private final boolean normalizePath; private V4Properties(Builder builder) { this.credentials = Validate.paramNotNull(builder.credentials, "Credentials"); this.credentialScope = Validate.paramNotNull(builder.credentialScope, "CredentialScope"); this.signingClock = Validate.paramNotNull(builder.signingClock, "SigningClock"); this.doubleUrlEncode = Validate.getOrDefault(builder.doubleUrlEncode, () -> true); this.normalizePath = Validate.getOrDefault(builder.normalizePath, () -> true); } public static Builder builder() { return new Builder(); } public AwsCredentialsIdentity getCredentials() { return credentials; } public CredentialScope getCredentialScope() { return credentialScope; } public Clock getSigningClock() { return signingClock; } public boolean shouldDoubleUrlEncode() { return doubleUrlEncode; } public boolean shouldNormalizePath() { return normalizePath; } public static class Builder { private AwsCredentialsIdentity credentials; private CredentialScope credentialScope; private Clock signingClock; private Boolean doubleUrlEncode; private Boolean normalizePath; public Builder credentials(AwsCredentialsIdentity credentials) { this.credentials = Validate.paramNotNull(credentials, "Credentials"); return this; } public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = Validate.paramNotNull(credentialScope, "CredentialScope"); return this; } public Builder signingClock(Clock signingClock) { this.signingClock = signingClock; return this; } public Builder doubleUrlEncode(Boolean doubleUrlEncode) { this.doubleUrlEncode = doubleUrlEncode; return this; } public Builder normalizePath(Boolean normalizePath) { this.normalizePath = normalizePath; return this; } public V4Properties build() { return new V4Properties(this); } } }
2,318
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/AwsChunkedV4PayloadSigner.java
/* * 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.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.checksumHeaderName; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.fromChecksumAlgorithm; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.AWS_CHUNKED; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.CONTENT_ENCODING; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_SIGNED_PAYLOAD; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_SIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_UNSIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CONTENT_SHA256; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.moveContentLength; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.reactivestreams.Publisher; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.ChecksumTrailerProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.ChunkedEncodedInputStream; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.SigV4ChunkExtensionProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.SigV4TrailerProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.TrailerProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ChecksumInputStream; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ResettableContentStreamProvider; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Pair; import software.amazon.awssdk.utils.StringInputStream; import software.amazon.awssdk.utils.Validate; /** * An implementation of a V4PayloadSigner which chunk-encodes a payload, optionally adding a chunk-signature chunk-extension, * and/or trailers representing trailing headers with their signature at the end. */ @SdkInternalApi public final class AwsChunkedV4PayloadSigner implements V4PayloadSigner { private final CredentialScope credentialScope; private final int chunkSize; private final ChecksumAlgorithm checksumAlgorithm; private final List<Pair<String, List<String>>> preExistingTrailers = new ArrayList<>(); private AwsChunkedV4PayloadSigner(Builder builder) { this.credentialScope = Validate.paramNotNull(builder.credentialScope, "CredentialScope"); this.chunkSize = Validate.isPositive(builder.chunkSize, "ChunkSize"); this.checksumAlgorithm = builder.checksumAlgorithm; } public static Builder builder() { return new Builder(); } @Override public ContentStreamProvider sign(ContentStreamProvider payload, V4RequestSigningResult requestSigningResult) { SdkHttpRequest.Builder request = requestSigningResult.getSignedRequest(); String checksum = request.firstMatchingHeader(X_AMZ_CONTENT_SHA256).orElseThrow( () -> new IllegalArgumentException(X_AMZ_CONTENT_SHA256 + " must be set!") ); ChunkedEncodedInputStream.Builder chunkedEncodedInputStreamBuilder = ChunkedEncodedInputStream .builder() .inputStream(payload.newStream()) .chunkSize(chunkSize) .header(chunk -> Integer.toHexString(chunk.length).getBytes(StandardCharsets.UTF_8)); preExistingTrailers.forEach(trailer -> chunkedEncodedInputStreamBuilder.addTrailer(() -> trailer)); switch (checksum) { case STREAMING_SIGNED_PAYLOAD: { RollingSigner rollingSigner = new RollingSigner(requestSigningResult.getSigningKey(), requestSigningResult.getSignature()); chunkedEncodedInputStreamBuilder.addExtension(new SigV4ChunkExtensionProvider(rollingSigner, credentialScope)); break; } case STREAMING_UNSIGNED_PAYLOAD_TRAILER: setupChecksumTrailerIfNeeded(chunkedEncodedInputStreamBuilder); break; case STREAMING_SIGNED_PAYLOAD_TRAILER: { RollingSigner rollingSigner = new RollingSigner(requestSigningResult.getSigningKey(), requestSigningResult.getSignature()); chunkedEncodedInputStreamBuilder.addExtension(new SigV4ChunkExtensionProvider(rollingSigner, credentialScope)); setupChecksumTrailerIfNeeded(chunkedEncodedInputStreamBuilder); chunkedEncodedInputStreamBuilder.addTrailer( new SigV4TrailerProvider(chunkedEncodedInputStreamBuilder.trailers(), rollingSigner, credentialScope) ); break; } default: throw new UnsupportedOperationException(); } return new ResettableContentStreamProvider(chunkedEncodedInputStreamBuilder::build); } @Override public Publisher<ByteBuffer> signAsync(Publisher<ByteBuffer> payload, V4RequestSigningResult requestSigningResult) { throw new UnsupportedOperationException(); } @Override public void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider payload) { long encodedContentLength = 0; long contentLength = moveContentLength(request, payload != null ? payload.newStream() : new StringInputStream("")); setupPreExistingTrailers(request); // pre-existing trailers encodedContentLength += calculateExistingTrailersLength(); String checksum = request.firstMatchingHeader(X_AMZ_CONTENT_SHA256).orElseThrow( () -> new IllegalArgumentException(X_AMZ_CONTENT_SHA256 + " must be set!") ); switch (checksum) { case STREAMING_SIGNED_PAYLOAD: { long extensionsLength = 81; // ;chunk-signature:<sigv4 hex signature, 64 bytes> encodedContentLength += calculateChunksLength(contentLength, extensionsLength); break; } case STREAMING_UNSIGNED_PAYLOAD_TRAILER: if (checksumAlgorithm != null) { encodedContentLength += calculateChecksumTrailerLength(checksumHeaderName(checksumAlgorithm)); } encodedContentLength += calculateChunksLength(contentLength, 0); break; case STREAMING_SIGNED_PAYLOAD_TRAILER: { long extensionsLength = 81; // ;chunk-signature:<sigv4 hex signature, 64 bytes> encodedContentLength += calculateChunksLength(contentLength, extensionsLength); if (checksumAlgorithm != null) { encodedContentLength += calculateChecksumTrailerLength(checksumHeaderName(checksumAlgorithm)); } encodedContentLength += 90; // x-amz-trailer-signature:<sigv4 hex signature, 64 bytes>\r\n break; } default: throw new UnsupportedOperationException(); } // terminating \r\n encodedContentLength += 2; if (checksumAlgorithm != null) { String checksumHeaderName = checksumHeaderName(checksumAlgorithm); request.appendHeader(X_AMZ_TRAILER, checksumHeaderName); } request.putHeader(Header.CONTENT_LENGTH, Long.toString(encodedContentLength)); request.appendHeader(CONTENT_ENCODING, AWS_CHUNKED); } /** * Set up a map of pre-existing trailer (headers) for the given request to be used when chunk-encoding the payload. * <p> * However, we need to validate that these are valid trailers. Since aws-chunked encoding adds the checksum as a trailer, it * isn't part of the request headers, but other trailers MUST be present in the request-headers. */ private void setupPreExistingTrailers(SdkHttpRequest.Builder request) { for (String header : request.matchingHeaders(X_AMZ_TRAILER)) { List<String> values = request.matchingHeaders(header); if (values.isEmpty()) { throw new IllegalArgumentException(header + " must be present in the request headers to be a valid trailer."); } preExistingTrailers.add(Pair.of(header, values)); request.removeHeader(header); } } private long calculateChunksLength(long contentLength, long extensionsLength) { long lengthInBytes = 0; long chunkHeaderLength = Integer.toHexString(chunkSize).length(); long numChunks = contentLength / chunkSize; // normal chunks // x<metadata>\r\n<data>\r\n lengthInBytes += numChunks * (chunkHeaderLength + extensionsLength + 2 + chunkSize + 2); // remaining chunk // x<metadata>\r\n<data>\r\n long remainingBytes = contentLength % chunkSize; if (remainingBytes > 0) { long remainingChunkHeaderLength = Long.toHexString(remainingBytes).length(); lengthInBytes += remainingChunkHeaderLength + extensionsLength + 2 + remainingBytes + 2; } // final chunk // 0<metadata>\r\n lengthInBytes += 1 + extensionsLength + 2; return lengthInBytes; } private long calculateExistingTrailersLength() { long lengthInBytes = 0; for (Pair<String, List<String>> trailer : preExistingTrailers) { // size of trailer lengthInBytes += calculateTrailerLength(trailer); } return lengthInBytes; } private long calculateTrailerLength(Pair<String, List<String>> trailer) { // size of trailer-header and colon long lengthInBytes = trailer.left().length() + 1L; // size of trailer-values for (String value : trailer.right()) { lengthInBytes += value.length(); } // size of commas between trailer-values, 1 less comma than # of values lengthInBytes += trailer.right().size() - 1; // terminating \r\n return lengthInBytes + 2; } private long calculateChecksumTrailerLength(String checksumHeaderName) { // size of checksum trailer-header and colon long lengthInBytes = checksumHeaderName.length() + 1L; // get the base checksum for the algorithm SdkChecksum sdkChecksum = fromChecksumAlgorithm(checksumAlgorithm); // size of checksum value as encoded-string lengthInBytes += BinaryUtils.toBase64(sdkChecksum.getChecksumBytes()).length(); // terminating \r\n return lengthInBytes + 2; } /** * Add the checksum as a trailer to the chunk-encoded stream. * <p> * If the checksum-algorithm is not present, then nothing is done. */ private void setupChecksumTrailerIfNeeded(ChunkedEncodedInputStream.Builder builder) { if (checksumAlgorithm == null) { return; } String checksumHeaderName = checksumHeaderName(checksumAlgorithm); SdkChecksum sdkChecksum = fromChecksumAlgorithm(checksumAlgorithm); ChecksumInputStream checksumInputStream = new ChecksumInputStream( builder.inputStream(), Collections.singleton(sdkChecksum) ); TrailerProvider checksumTrailer = new ChecksumTrailerProvider(sdkChecksum, checksumHeaderName); builder.inputStream(checksumInputStream).addTrailer(checksumTrailer); } static class Builder { private CredentialScope credentialScope; private Integer chunkSize; private ChecksumAlgorithm checksumAlgorithm; public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = credentialScope; return this; } public Builder chunkSize(Integer chunkSize) { this.chunkSize = chunkSize; return this; } public Builder checksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; } public AwsChunkedV4PayloadSigner build() { return new AwsChunkedV4PayloadSigner(this); } } }
2,319
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/OptionalDependencyLoaderUtil.java
/* * 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.signer.util; import java.time.Clock; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.crt.internal.signer.DefaultAwsCrtV4aHttpSigner; import software.amazon.awssdk.http.auth.aws.eventstream.internal.signer.EventStreamV4PayloadSigner; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.ClassLoaderHelper; import software.amazon.awssdk.utils.Logger; /** * Utilities for loading of classes and objects which have optional dependencies, and therefore need to be safely checked at * runtime in order to use. */ @SdkInternalApi public final class OptionalDependencyLoaderUtil { private static final Logger LOG = Logger.loggerFor(OptionalDependencyLoaderUtil.class); private static final String HTTP_AUTH_AWS_CRT_PATH = "software.amazon.awssdk.http.auth.aws.crt.HttpAuthAwsCrt"; private static final String HTTP_AUTH_AWS_CRT_MODULE = "software.amazon.awssdk:http-auth-aws-crt"; private static final String HTTP_AUTH_AWS_EVENT_STREAM_PATH = "software.amazon.awssdk.http.auth.aws.eventstream.HttpAuthAwsEventStream"; private static final String HTTP_AUTH_AWS_EVENT_STREAM_MODULE = "software.amazon.awssdk:http-auth-aws-eventstream"; private OptionalDependencyLoaderUtil() { } /** * A helpful method that checks that some class is available on the class-path. If it fails to load, it will throw an * exception based on why it failed to load. This should be used in cases where certain dependencies are optional, but the * dependency is used at compile-time for strong typing (i.e. {@link EventStreamV4PayloadSigner}). */ private static void requireClass(String classPath, String module, String feature) { try { ClassLoaderHelper.loadClass(classPath, false); } catch (ClassNotFoundException e) { LOG.debug(() -> "Cannot find the " + classPath + " class: ", e); String msg = String.format("Could not load class. You must add a dependency on the '%s' module to enable the %s " + "feature: ", module, feature); throw new RuntimeException(msg, e); } catch (Exception e) { throw new RuntimeException(String.format("Could not load class (%s): ", classPath), e); } } public static DefaultAwsCrtV4aHttpSigner getDefaultAwsCrtV4aHttpSigner() { requireClass(HTTP_AUTH_AWS_CRT_PATH, HTTP_AUTH_AWS_CRT_MODULE, "CRT-V4a signing"); return new DefaultAwsCrtV4aHttpSigner(); } public static EventStreamV4PayloadSigner getEventStreamV4PayloadSigner( AwsCredentialsIdentity credentials, CredentialScope credentialScope, Clock signingClock) { requireClass(HTTP_AUTH_AWS_EVENT_STREAM_PATH, HTTP_AUTH_AWS_EVENT_STREAM_MODULE, "Event-stream signing"); return EventStreamV4PayloadSigner.builder() .credentials(credentials) .credentialScope(credentialScope) .signingClock(signingClock) .build(); } }
2,320
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SigningAlgorithm.java
/* * 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.signer.util; import java.security.NoSuchAlgorithmException; import javax.crypto.Mac; import software.amazon.awssdk.annotations.SdkInternalApi; @SdkInternalApi public enum SigningAlgorithm { HMAC_SHA256("HmacSHA256"); private final String algorithmName; private final ThreadLocal<Mac> macReference; SigningAlgorithm(String algorithmName) { this.algorithmName = algorithmName; macReference = new MacThreadLocal(algorithmName); } public String getAlgorithmName() { return algorithmName; } /** * Returns the thread local reference for the crypto algorithm */ public Mac getMac() { return macReference.get(); } private static class MacThreadLocal extends ThreadLocal<Mac> { private final String algorithmName; MacThreadLocal(String algorithmName) { this.algorithmName = algorithmName; } @Override protected Mac initialValue() { try { return Mac.getInstance(algorithmName); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Unable to fetch Mac instance for Algorithm " + algorithmName + ": " + e.getMessage()); } } } }
2,321
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/BoundedLinkedHashMap.java
/* * 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.signer.util; import java.util.LinkedHashMap; import java.util.Map; import software.amazon.awssdk.annotations.SdkInternalApi; /** * A bounded linked hash map that would remove the eldest entry when the map size exceeds a configurable maximum. */ @SdkInternalApi final class BoundedLinkedHashMap<K, V> extends LinkedHashMap<K, V> { private static final long serialVersionUID = 1L; private final int maxSize; BoundedLinkedHashMap(int maxSize) { this.maxSize = maxSize; } /** * {@inheritDoc} * <p> * Returns true if the size of this map exceeds the maximum. */ @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > maxSize; } /** * Returns the maximum size of this map beyond which the eldest entry will get removed. */ int getMaxSize() { return maxSize; } }
2,322
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/FifoCache.java
/* * 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.signer.util; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.annotations.ThreadSafe; /** * A bounded cache that has a FIFO eviction policy when the cache is full. * * @param <T> value type */ @ThreadSafe @SdkInternalApi public final class FifoCache<T> { private final BoundedLinkedHashMap<String, T> map; private final ReadLock rlock; private final WriteLock wlock; /** * @param maxSize the maximum number of entries of the cache */ public FifoCache(int maxSize) { if (maxSize < 1) { throw new IllegalArgumentException("maxSize " + maxSize + " must be at least 1"); } map = new BoundedLinkedHashMap<>(maxSize); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); rlock = lock.readLock(); wlock = lock.writeLock(); } /** * Adds an entry to the cache, evicting the earliest entry if necessary. */ public T add(String key, T value) { wlock.lock(); try { return map.put(key, value); } finally { wlock.unlock(); } } /** * Returns the value of the given key; or null of no such entry exists. */ public T get(String key) { rlock.lock(); try { return map.get(key); } finally { rlock.unlock(); } } /** * Returns the current size of the cache. */ public int size() { rlock.lock(); try { return map.size(); } finally { rlock.unlock(); } } /** * Returns the maximum size of the cache. */ public int getMaxSize() { return map.getMaxSize(); } @Override public String toString() { rlock.lock(); try { return map.toString(); } finally { rlock.unlock(); } } }
2,323
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/DigestAlgorithm.java
/* * 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.signer.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import software.amazon.awssdk.annotations.SdkInternalApi; @SdkInternalApi public enum DigestAlgorithm { SHA1("SHA-1"), MD5("MD5"), SHA256("SHA-256") ; private final String algorithmName; private final DigestThreadLocal digestReference; 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 */ public MessageDigest getDigest() { MessageDigest digest = digestReference.get(); digest.reset(); return digest; } private static class DigestThreadLocal extends ThreadLocal<MessageDigest> { private final String algorithmName; DigestThreadLocal(String algorithmName) { this.algorithmName = algorithmName; } @Override protected MessageDigest initialValue() { try { return MessageDigest.getInstance(algorithmName); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Unable to fetch message digest instance for Algorithm " + algorithmName + ": " + e.getMessage(), e); } } } }
2,324
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/ChecksumUtil.java
/* * 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.signer.util; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.CRC32; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.CRC32C; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.MD5; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA1; import static software.amazon.awssdk.checksums.DefaultChecksumAlgorithm.SHA256; import java.io.InputStream; import java.util.Locale; import java.util.Map; import java.util.function.Supplier; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.ConstantChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Crc32CChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Crc32Checksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Md5Checksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Sha1Checksum; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.Sha256Checksum; import software.amazon.awssdk.utils.ImmutableMap; @SdkInternalApi public final class ChecksumUtil { private static final String CONSTANT_CHECKSUM = "CONSTANT"; private static final Map<String, Supplier<SdkChecksum>> CHECKSUM_MAP = ImmutableMap.of( SHA256.algorithmId(), Sha256Checksum::new, SHA1.algorithmId(), Sha1Checksum::new, CRC32.algorithmId(), Crc32Checksum::new, CRC32C.algorithmId(), Crc32CChecksum::new, MD5.algorithmId(), Md5Checksum::new ); private ChecksumUtil() { } /** * Get the correct checksum header name based on the checksum-algorithm. This is required to be of the form * {@code x-amz-checksum-*}, where '*' is alphanumeric checksum-algorithm-id in lower-case form. Examples include: * <p> * x-amz-checksum-sha256, x-amz-checksum-sha1, x-amz-checksum-crc32, x-amz-checksum-crc32c, x-amz-checksum-md5 * </p> */ public static String checksumHeaderName(ChecksumAlgorithm checksumAlgorithm) { return "x-amz-checksum-" + checksumAlgorithm.algorithmId().toLowerCase(Locale.US); } /** * Gets the SdkChecksum object based on the given ChecksumAlgorithm. */ public static SdkChecksum fromChecksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) { String algorithmId = checksumAlgorithm.algorithmId(); Supplier<SdkChecksum> checksumSupplier = CHECKSUM_MAP.get(algorithmId); if (checksumSupplier != null) { return checksumSupplier.get(); } if (CONSTANT_CHECKSUM.equals(algorithmId)) { return new ConstantChecksum(((ConstantChecksumAlgorithm) checksumAlgorithm).value); } throw new UnsupportedOperationException("Checksum not supported for " + algorithmId); } /** * Read the entirety of an input-stream - this is useful when the stream has side-effects (such as calculating a checksum) * when it gets read. */ public static void readAll(InputStream inputStream) { try { byte[] buffer = new byte[4096]; while (inputStream.read(buffer) > -1) { } } catch (Exception e) { throw new RuntimeException("Could not finish reading stream: ", e); } } /** * An implementation of a {@link ChecksumAlgorithm} that will map to {@link ConstantChecksum}, which provides a constant * checksum. This isn't super useful, but is needed in cases such as signing, where the content-hash (a * cryptographically-secure "checksum") can be a set of pre-defined values. */ public static class ConstantChecksumAlgorithm implements ChecksumAlgorithm { private final String value; public ConstantChecksumAlgorithm(String value) { this.value = value; } @Override public String algorithmId() { return CONSTANT_CHECKSUM; } } }
2,325
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SignerKey.java
/* * 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.signer.util; import java.time.Instant; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.DateUtils; /** * Holds the signing key and the number of days since epoch for the date for which the signing key was generated. */ @Immutable @SdkInternalApi public final class SignerKey { private final long daysSinceEpoch; private final byte[] signingKey; public SignerKey(Instant date, byte[] signingKey) { if (date == null) { throw new IllegalArgumentException( "Not able to cache signing key. Signing date to be is null"); } if (signingKey == null) { throw new IllegalArgumentException( "Not able to cache signing key. Signing Key to be cached are null"); } this.daysSinceEpoch = DateUtils.numberOfDaysSinceEpoch(date.toEpochMilli()); this.signingKey = signingKey.clone(); } public boolean isValidForDate(Instant other) { return daysSinceEpoch == DateUtils.numberOfDaysSinceEpoch(other.toEpochMilli()); } /** * Returns a copy of the signing key. */ public byte[] getSigningKey() { return signingKey.clone(); } }
2,326
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/CredentialUtils.java
/* * 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.signer.util; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.identity.spi.AwsSessionCredentialsIdentity; import software.amazon.awssdk.utils.StringUtils; @SdkInternalApi public final class CredentialUtils { private CredentialUtils() { } /** * Determine whether the provided credentials are anonymous credentials, indicating that the customer is not attempting to * authenticate themselves. */ public static boolean isAnonymous(AwsCredentialsIdentity credentials) { return credentials.secretAccessKey() == null && credentials.accessKeyId() == null; } /** * Sanitize given credentials by trimming whitespace */ public static AwsCredentialsIdentity sanitizeCredentials(AwsCredentialsIdentity credentials) { String accessKeyId = StringUtils.trim(credentials.accessKeyId()); String secretKey = StringUtils.trim(credentials.secretAccessKey()); if (credentials instanceof AwsSessionCredentialsIdentity) { AwsSessionCredentialsIdentity sessionCredentials = (AwsSessionCredentialsIdentity) credentials; return AwsSessionCredentialsIdentity.create(accessKeyId, secretKey, StringUtils.trim(sessionCredentials.sessionToken())); } // given credentials are anonymous, so don't create new instance if (accessKeyId == null && secretKey == null) { return credentials; } return AwsCredentialsIdentity.create(accessKeyId, secretKey); } }
2,327
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SignerUtils.java
/* * 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.signer.util; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CONTENT_SHA256; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_DECODED_CONTENT_LENGTH; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Optional; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Logger; import software.amazon.awssdk.utils.http.SdkHttpUtils; /** * Utility methods to be used by various AWS Signer implementations. This class is protected and subject to change. */ @SdkInternalApi public final class SignerUtils { private static final Logger LOG = Logger.loggerFor(SignerUtils.class); private static final FifoCache<SignerKey> SIGNER_CACHE = new FifoCache<>(300); private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter .ofPattern("yyyyMMdd").withZone(ZoneId.of("UTC")); private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter .ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneId.of("UTC")); private SignerUtils() { } /** * Returns a string representation of the given datetime in yyyyMMdd format. The date returned is in the UTC zone. * <p> * For example, given an Instant with millis-value of 1416863450581, this method returns "20141124" */ public static String formatDate(Instant instant) { return DATE_FORMATTER.format(instant); } /** * Returns a string representation of the given datetime in yyyyMMdd'T'HHmmss'Z' format. The date returned is in the UTC * zone. * <p> * For example, given an Instant with millis-value of 1416863450581, this method returns "20141124T211050Z" */ public static String formatDateTime(Instant instant) { return TIME_FORMATTER.format(instant); } /** * Create a hash of the canonical request string * <p> * Step 2 of the AWS Signature version 4 calculation. Refer to * https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html#create-canonical-request-hash. */ public static String hashCanonicalRequest(String canonicalRequestString) { return BinaryUtils.toHex( hash(canonicalRequestString) ); } /** * Get the signing key based on the given credentials and a credential-scope */ public static byte[] deriveSigningKey(AwsCredentialsIdentity credentials, CredentialScope credentialScope) { String cacheKey = createSigningCacheKeyName(credentials, credentialScope.getRegion(), credentialScope.getService()); SignerKey signerKey = SIGNER_CACHE.get(cacheKey); if (signerKey != null && signerKey.isValidForDate(credentialScope.getInstant())) { return signerKey.getSigningKey(); } LOG.trace(() -> "Generating a new signing key as the signing key not available in the cache for the date: " + credentialScope.getInstant().toEpochMilli()); byte[] signingKey = newSigningKey(credentials, credentialScope.getDate(), credentialScope.getRegion(), credentialScope.getService()); SIGNER_CACHE.add(cacheKey, new SignerKey(credentialScope.getInstant(), signingKey)); return signingKey; } private static String createSigningCacheKeyName(AwsCredentialsIdentity credentials, String regionName, String serviceName) { return credentials.secretAccessKey() + "-" + regionName + "-" + serviceName; } private static byte[] newSigningKey(AwsCredentialsIdentity credentials, String dateStamp, String regionName, String serviceName) { byte[] kSecret = ("AWS4" + credentials.secretAccessKey()) .getBytes(StandardCharsets.UTF_8); byte[] kDate = sign(dateStamp, kSecret); byte[] kRegion = sign(regionName, kDate); byte[] kService = sign(serviceName, kRegion); return sign(SignerConstant.AWS4_TERMINATOR, kService); } /** * Sign given data using a key. */ public static byte[] sign(String stringData, byte[] key) { try { byte[] data = stringData.getBytes(StandardCharsets.UTF_8); return sign(data, key, SigningAlgorithm.HMAC_SHA256); } catch (Exception e) { throw new RuntimeException("Unable to calculate a request signature: ", e); } } /** * Sign given data using a key and a specific algorithm */ public static byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) { try { Mac mac = algorithm.getMac(); mac.init(new SecretKeySpec(key, algorithm.toString())); return mac.doFinal(data); } catch (Exception e) { throw new RuntimeException("Unable to calculate a request signature: ", e); } } /** * Compute the signature of a string using a signing key. * <p> * Step 4 of the AWS Signature version 4 calculation. Refer to * https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html#calculate-signature. */ public static byte[] computeSignature(String stringToSign, byte[] signingKey) { return sign(stringToSign.getBytes(StandardCharsets.UTF_8), signingKey, SigningAlgorithm.HMAC_SHA256); } /** * Add the host header based on parameters of a request */ public static void addHostHeader(SdkHttpRequest.Builder requestBuilder) { // AWS4 requires that we sign the Host header, so we // have to have it in the request by the time we sign. String host = requestBuilder.host(); if (!SdkHttpUtils.isUsingStandardPort(requestBuilder.protocol(), requestBuilder.port())) { StringBuilder hostHeaderBuilder = new StringBuilder(host); hostHeaderBuilder.append(":").append(requestBuilder.port()); requestBuilder.putHeader(SignerConstant.HOST, hostHeaderBuilder.toString()); } else { requestBuilder.putHeader(SignerConstant.HOST, host); } } /** * Add a date header using a datetime string */ public static void addDateHeader(SdkHttpRequest.Builder requestBuilder, String dateTime) { requestBuilder.putHeader(SignerConstant.X_AMZ_DATE, dateTime); } /** * Move `Content-Length` to `x-amz-decoded-content-length` if not already present. If `Content-Length` is not present, then * the payload is read in its entirety to calculate the length. */ public static long moveContentLength(SdkHttpRequest.Builder request, InputStream payload) { Optional<String> decodedContentLength = request.firstMatchingHeader(X_AMZ_DECODED_CONTENT_LENGTH); if (!decodedContentLength.isPresent()) { // if the decoded length isn't present, content-length must be there String contentLength = request.firstMatchingHeader(Header.CONTENT_LENGTH).orElseGet( () -> String.valueOf(readAll(payload)) ); request.putHeader(X_AMZ_DECODED_CONTENT_LENGTH, contentLength) .removeHeader(Header.CONTENT_LENGTH); return Long.parseLong(contentLength); } // decoded header is already there, so remove content-length just to be sure it's gone request.removeHeader(Header.CONTENT_LENGTH); return Long.parseLong(decodedContentLength.get()); } private static MessageDigest getMessageDigestInstance() { return DigestAlgorithm.SHA256.getDigest(); } public static InputStream getBinaryRequestPayloadStream(ContentStreamProvider streamProvider) { try { if (streamProvider == null) { return new ByteArrayInputStream(new byte[0]); } return streamProvider.newStream(); } catch (Exception e) { throw new RuntimeException("Unable to read request payload to sign request: ", e); } } public static byte[] hash(InputStream input) { try { MessageDigest md = getMessageDigestInstance(); byte[] buf = new byte[4096]; int read = 0; while (read >= 0) { read = input.read(buf); md.update(buf, 0, read); } return md.digest(); } catch (Exception e) { throw new RuntimeException("Unable to compute hash while signing request: ", e); } } public static byte[] hash(byte[] data) { try { MessageDigest md = getMessageDigestInstance(); md.update(data); return md.digest(); } catch (Exception e) { throw new RuntimeException("Unable to compute hash while signing request: ", e); } } public static byte[] hash(String text) { return hash(text.getBytes(StandardCharsets.UTF_8)); } /** * Consume entire stream and return the number of bytes - the stream will NOT be reset upon completion, so if it needs to * be read again, the caller MUST reset the stream. */ private static int readAll(InputStream inputStream) { try { byte[] buffer = new byte[4096]; int read = 0; int offset = 0; while (read >= 0) { read = inputStream.read(buffer); if (read >= 0) { offset += read; } } return offset; } catch (Exception e) { throw new RuntimeException("Could not finish reading stream: ", e); } } public static String getContentHash(SdkHttpRequest.Builder requestBuilder) { return requestBuilder.firstMatchingHeader(X_AMZ_CONTENT_SHA256).orElseThrow( () -> new IllegalArgumentException("Content hash must be present in the '" + X_AMZ_CONTENT_SHA256 + "' header!") ); } }
2,328
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SignerConstant.java
/* * 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.signer.util; import java.time.Duration; import software.amazon.awssdk.annotations.SdkInternalApi; @SdkInternalApi public final class SignerConstant { public static final String AWS4_TERMINATOR = "aws4_request"; public static final String AWS4_SIGNING_ALGORITHM = "AWS4-HMAC-SHA256"; public static final String X_AMZ_CONTENT_SHA256 = "x-amz-content-sha256"; public static final String AUTHORIZATION = "Authorization"; public static final String CONTENT_ENCODING = "Content-Encoding"; public static final String X_AMZ_SECURITY_TOKEN = "X-Amz-Security-Token"; public static final String X_AMZ_CREDENTIAL = "X-Amz-Credential"; public static final String X_AMZ_DATE = "X-Amz-Date"; public static final String X_AMZ_EXPIRES = "X-Amz-Expires"; public static final String X_AMZ_SIGNED_HEADERS = "X-Amz-SignedHeaders"; public static final String X_AMZ_SIGNATURE = "X-Amz-Signature"; public static final String X_AMZ_ALGORITHM = "X-Amz-Algorithm"; public static final String X_AMZ_DECODED_CONTENT_LENGTH = "x-amz-decoded-content-length"; public static final String X_AMZ_TRAILER = "x-amz-trailer"; public static final String AWS_CHUNKED = "aws-chunked"; public static final String HOST = "Host"; public static final String LINE_SEPARATOR = "\n"; public static final String UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD"; public static final String STREAMING_EVENTS_PAYLOAD = "STREAMING-AWS4-HMAC-SHA256-EVENTS"; public static final String STREAMING_UNSIGNED_PAYLOAD_TRAILER = "STREAMING-UNSIGNED-PAYLOAD-TRAILER"; public static final String STREAMING_ECDSA_SIGNED_PAYLOAD = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD"; public static final String STREAMING_ECDSA_SIGNED_PAYLOAD_TRAILER = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER"; public static final String STREAMING_SIGNED_PAYLOAD = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"; public static final String STREAMING_SIGNED_PAYLOAD_TRAILER = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER"; /** * Seconds in a week, which is the max expiration time Sig-v4 accepts. */ public static final Duration PRESIGN_URL_MAX_EXPIRATION_DURATION = Duration.ofDays(7); private SignerConstant() { } }
2,329
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/ResettableContentStreamProvider.java
/* * 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.signer.io; import java.io.IOException; import java.io.InputStream; import java.util.function.Supplier; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; @SdkInternalApi public class ResettableContentStreamProvider implements ContentStreamProvider { private final Supplier<InputStream> streamSupplier; private InputStream currentStream; public ResettableContentStreamProvider(Supplier<InputStream> streamSupplier) { this.streamSupplier = streamSupplier; } @Override public InputStream newStream() { try { reset(); } catch (IOException e) { throw new RuntimeException("Could not create new stream: ", e); } return currentStream; } private void reset() throws IOException { if (currentStream != null) { currentStream.reset(); } else { currentStream = streamSupplier.get(); } } }
2,330
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/InMemoryPublisher.java
/* * 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.signer.io; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.Validate; /** * Temporarily used for buffering all data into memory. TODO(sra-identity-auth): Remove this by supporting chunked encoding. We * should not buffer everything into memory. */ @SdkInternalApi public class InMemoryPublisher implements Publisher<ByteBuffer> { private final AtomicBoolean subscribed = new AtomicBoolean(false); private final List<ByteBuffer> data; public InMemoryPublisher(List<ByteBuffer> data) { this.data = new ArrayList<>(Validate.noNullElements(data, "Data must not contain null elements.")); } @Override public void subscribe(Subscriber<? super ByteBuffer> s) { if (!subscribed.compareAndSet(false, true)) { s.onSubscribe(new NoOpSubscription()); s.onError(new IllegalStateException("InMemoryPublisher cannot be subscribed to twice.")); return; } s.onSubscribe(new Subscription() { private final AtomicBoolean sending = new AtomicBoolean(false); private final Object doneLock = new Object(); private final AtomicBoolean done = new AtomicBoolean(false); private final AtomicLong demand = new AtomicLong(0); private int position = 0; @Override public void request(long n) { if (done.get()) { return; } try { demand.addAndGet(n); fulfillDemand(); } catch (Throwable t) { finish(() -> s.onError(t)); } } private void fulfillDemand() { do { if (sending.compareAndSet(false, true)) { try { send(); } finally { sending.set(false); } } } while (!done.get() && demand.get() > 0); } private void send() { while (true) { assert position >= 0; assert position <= data.size(); if (done.get()) { break; } if (position == data.size()) { finish(s::onComplete); break; } if (demand.get() == 0) { break; } demand.decrementAndGet(); int dataIndex = position; s.onNext(data.get(dataIndex)); data.set(dataIndex, null); // We're done with this data here, so allow it to be garbage collected position++; } } @Override public void cancel() { finish(() -> { }); } private void finish(Runnable thingToDo) { synchronized (doneLock) { if (done.compareAndSet(false, true)) { thingToDo.run(); } } } }); } private static class NoOpSubscription implements Subscription { @Override public void request(long n) { } @Override public void cancel() { } } }
2,331
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/SdkLengthAwareInputStream.java
/* * 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.signer.io; import static software.amazon.awssdk.utils.NumericUtils.saturatedCast; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.Logger; import software.amazon.awssdk.utils.Validate; /** * An {@code InputStream} that is aware of its length. The main purpose of this class is to support truncating streams to a length * that is shorter than the total length of the stream. */ @SdkInternalApi public class SdkLengthAwareInputStream extends FilterInputStream { private static final Logger LOG = Logger.loggerFor(SdkLengthAwareInputStream.class); private long length; private long remaining; public SdkLengthAwareInputStream(InputStream in, long length) { super(in); this.length = Validate.isNotNegative(length, "length"); this.remaining = this.length; } @Override public int read() throws IOException { if (!hasMoreBytes()) { LOG.debug(() -> String.format("Specified InputStream length of %d has been reached. Returning EOF.", length)); return -1; } int read = super.read(); if (read != -1) { remaining--; } return read; } @Override public int read(byte[] b, int off, int len) throws IOException { if (!hasMoreBytes()) { LOG.debug(() -> String.format("Specified InputStream length of %d has been reached. Returning EOF.", length)); return -1; } len = Math.min(len, saturatedCast(remaining)); int read = super.read(b, off, len); if (read > 0) { remaining -= read; } return read; } @Override public long skip(long requestedBytesToSkip) throws IOException { requestedBytesToSkip = Math.min(requestedBytesToSkip, remaining); long skippedActual = super.skip(requestedBytesToSkip); remaining -= skippedActual; return skippedActual; } @Override public int available() throws IOException { int streamAvailable = super.available(); return Math.min(streamAvailable, saturatedCast(remaining)); } @Override public void mark(int readlimit) { super.mark(readlimit); // mark() causes reset() to change the stream's position back to the current position. Therefore, when reset() is called, // the new length of the stream will be equal to the current value of 'remaining'. length = remaining; } @Override public void reset() throws IOException { super.reset(); remaining = length; } public long remaining() { return remaining; } private boolean hasMoreBytes() { return remaining > 0; } }
2,332
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/ChecksumSubscriber.java
/* * 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.signer.io; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.zip.Checksum; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.annotations.SdkInternalApi; /** * A subscriber that takes a collection of checksums, and updates each checksum when it receives data. */ @SdkInternalApi public final class ChecksumSubscriber implements Subscriber<ByteBuffer> { private final CompletableFuture<Publisher<ByteBuffer>> checksumming = new CompletableFuture<>(); private final Collection<Checksum> checksums = new ArrayList<>(); private volatile boolean canceled = false; private volatile Subscription subscription; private final List<ByteBuffer> bufferedPayload = new ArrayList<>(); public ChecksumSubscriber(Collection<? extends Checksum> consumers) { this.checksums.addAll(consumers); checksumming.whenComplete((r, t) -> { if (t instanceof CancellationException) { synchronized (this) { canceled = true; if (subscription != null) { subscription.cancel(); } } } }); } @Override public void onSubscribe(Subscription subscription) { synchronized (this) { if (!canceled && this.subscription == null) { this.subscription = subscription; subscription.request(Long.MAX_VALUE); } else { subscription.cancel(); } } } @Override public void onNext(ByteBuffer byteBuffer) { if (!canceled) { updateChecksumsAndBuffer(byteBuffer); } } private void updateChecksumsAndBuffer(ByteBuffer buffer) { int remaining = buffer.remaining(); if (remaining <= 0) { return; } byte[] copyBuffer = new byte[remaining]; buffer.get(copyBuffer); checksums.forEach(c -> c.update(copyBuffer, 0, remaining)); bufferedPayload.add(ByteBuffer.wrap(copyBuffer)); } @Override public void onError(Throwable throwable) { checksumming.completeExceptionally(throwable); } @Override public void onComplete() { checksumming.complete(new InMemoryPublisher(bufferedPayload)); } public CompletableFuture<Publisher<ByteBuffer>> completeFuture() { return checksumming; } }
2,333
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/Releasable.java
/* * 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.signer.io; import static software.amazon.awssdk.utils.IoUtils.closeQuietly; import java.io.Closeable; import org.slf4j.Logger; import software.amazon.awssdk.annotations.SdkInternalApi; /** * Used for releasing a resource. * <p> * For example, the creation of a {@code ResettableInputStream} would entail physically opening a file. If the opened file is * meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release * method must not be called while the execution is made in other stack frames. * <p> * In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the * stream would need to explicitly disable the accidental closing via {@code ResettableInputStream#disableClose()}, so that the * release method becomes the only way to truly close the opened file. */ @SdkInternalApi public interface Releasable { /** * Releases the given {@link Closeable} especially if it was an instance of {@link Releasable}. * <p> * For example, the creation of a {@code ResettableInputStream} would entail physically opening a file. If the opened file is * meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the * release method must not be called while the execution is made in other stack frames. * <p> * In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the * stream would need to explicitly disable the accidental closing via {@code ResettableInputStream#disableClose()}, so that * the release method becomes the only way to truly close the opened file. */ static void release(Closeable is, Logger log) { closeQuietly(is, log); if (is instanceof Releasable) { Releasable r = (Releasable) is; r.release(); } } /** * Releases the allocated resource. This method should not be called except by the caller who allocated the resource at the * very top of the call stack. This allows, typically, a {@link Closeable} resource to be not unintentionally released owing * to the calling of the {@link Closeable#close()} methods by implementation deep down in the call stack. * <p> * For example, the creation of a {@code ResettableInputStream} would entail physically opening a file. If the opened file is * meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the * release method must not be called while the execution is made in other stack frames. * <p> * In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the * stream would need to explicitly disable the accidental closing via {@code ResettableInputStream#disableClose()}, so that * the release method becomes the only way to truly close the opened file. */ void release(); }
2,334
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/ChecksumInputStream.java
/* * 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.signer.io; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.zip.Checksum; import software.amazon.awssdk.annotations.SdkInternalApi; /** * An input-stream that takes a collection of checksums, and updates each checksum when it reads data. */ @SdkInternalApi public class ChecksumInputStream extends FilterInputStream { private final Collection<Checksum> checksums = new ArrayList<>(); public ChecksumInputStream(InputStream stream, Collection<? extends Checksum> checksums) { super(stream); this.checksums.addAll(checksums); } @Override public int read() throws IOException { byte[] b = new byte[1]; int read = read(b, 0, 1); if (read > 0) { checksums.forEach(checksum -> checksum.update(b, 0, 1)); } return read; } @Override public int read(byte[] b, int off, int len) throws IOException { int read = in.read(b, off, len); if (read > 0) { checksums.forEach(checksum -> checksum.update(b, off, read)); } return read; } }
2,335
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/ConstantChecksum.java
/* * 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.signer.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) { } }
2,336
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/Md5Checksum.java
/* * 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.signer.checksums; import java.security.MessageDigest; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.util.DigestAlgorithm; /** * Implementation of {@link SdkChecksum} to calculate an MD5 checksum. */ @SdkInternalApi public class Md5Checksum implements SdkChecksum { private MessageDigest digest; private MessageDigest digestLastMarked; public Md5Checksum() { this.digest = getDigest(); } @Override public void update(int b) { digest.update((byte) b); } @Override public void update(byte[] b, int off, int len) { digest.update(b, off, len); } @Override public long getValue() { throw new UnsupportedOperationException("Use getChecksumBytes() instead."); } @Override public void reset() { digest = (digestLastMarked == null) // This is necessary so that should there be a reset without a // preceding mark, the MD5 would still be computed correctly. ? getDigest() : cloneFrom(digestLastMarked); } private MessageDigest getDigest() { return DigestAlgorithm.MD5.getDigest(); } @Override public byte[] getChecksumBytes() { return digest.digest(); } @Override public void mark(int readLimit) { digestLastMarked = cloneFrom(digest); } private MessageDigest cloneFrom(MessageDigest from) { try { return (MessageDigest) from.clone(); } catch (CloneNotSupportedException e) { // should never occur throw new IllegalStateException("unexpected", e); } } }
2,337
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/Sha256Checksum.java
/* * 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.signer.checksums; import java.security.MessageDigest; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.util.DigestAlgorithm; /** * Implementation of {@link SdkChecksum} to calculate an Sha-256 Checksum. */ @SdkInternalApi public class Sha256Checksum implements SdkChecksum { private MessageDigest digest; private MessageDigest digestLastMarked; public Sha256Checksum() { this.digest = getDigest(); } @Override public void update(int b) { digest.update((byte) b); } @Override public void update(byte[] b, int off, int len) { digest.update(b, off, len); } @Override public long getValue() { throw new UnsupportedOperationException("Use getChecksumBytes() instead."); } @Override public void reset() { digest = (digestLastMarked == null) // This is necessary so that should there be a reset without a // preceding mark, the Sha-256 would still be computed correctly. ? getDigest() : cloneFrom(digestLastMarked); } private MessageDigest getDigest() { return DigestAlgorithm.SHA256.getDigest(); } @Override public byte[] getChecksumBytes() { return digest.digest(); } @Override public void mark(int readLimit) { digestLastMarked = cloneFrom(digest); } private MessageDigest cloneFrom(MessageDigest from) { try { return (MessageDigest) from.clone(); } catch (CloneNotSupportedException e) { // should never occur throw new IllegalStateException("unexpected", e); } } }
2,338
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/SdkCrc32Checksum.java
/* * 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.signer.checksums; import java.util.zip.Checksum; import software.amazon.awssdk.annotations.SdkInternalApi; /* * THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL VERSION. * * The code comes from PureJavaCrc32.java in Apache Commons Codec 1.11. * It has been modified to add a createCopy() method. * The createCopy method is used to save current c checksum state when the checksum is marked. */ @SdkInternalApi public final class SdkCrc32Checksum implements Checksum, Cloneable { /* * CRC-32 lookup tables generated by the polynomial 0xEDB88320. * See also TestPureJavaCrc32.Table. */ private static final int[] T = { /* T8_0 */ 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D, /* T8_1 */ 0x00000000, 0x191B3141, 0x32366282, 0x2B2D53C3, 0x646CC504, 0x7D77F445, 0x565AA786, 0x4F4196C7, 0xC8D98A08, 0xD1C2BB49, 0xFAEFE88A, 0xE3F4D9CB, 0xACB54F0C, 0xB5AE7E4D, 0x9E832D8E, 0x87981CCF, 0x4AC21251, 0x53D92310, 0x78F470D3, 0x61EF4192, 0x2EAED755, 0x37B5E614, 0x1C98B5D7, 0x05838496, 0x821B9859, 0x9B00A918, 0xB02DFADB, 0xA936CB9A, 0xE6775D5D, 0xFF6C6C1C, 0xD4413FDF, 0xCD5A0E9E, 0x958424A2, 0x8C9F15E3, 0xA7B24620, 0xBEA97761, 0xF1E8E1A6, 0xE8F3D0E7, 0xC3DE8324, 0xDAC5B265, 0x5D5DAEAA, 0x44469FEB, 0x6F6BCC28, 0x7670FD69, 0x39316BAE, 0x202A5AEF, 0x0B07092C, 0x121C386D, 0xDF4636F3, 0xC65D07B2, 0xED705471, 0xF46B6530, 0xBB2AF3F7, 0xA231C2B6, 0x891C9175, 0x9007A034, 0x179FBCFB, 0x0E848DBA, 0x25A9DE79, 0x3CB2EF38, 0x73F379FF, 0x6AE848BE, 0x41C51B7D, 0x58DE2A3C, 0xF0794F05, 0xE9627E44, 0xC24F2D87, 0xDB541CC6, 0x94158A01, 0x8D0EBB40, 0xA623E883, 0xBF38D9C2, 0x38A0C50D, 0x21BBF44C, 0x0A96A78F, 0x138D96CE, 0x5CCC0009, 0x45D73148, 0x6EFA628B, 0x77E153CA, 0xBABB5D54, 0xA3A06C15, 0x888D3FD6, 0x91960E97, 0xDED79850, 0xC7CCA911, 0xECE1FAD2, 0xF5FACB93, 0x7262D75C, 0x6B79E61D, 0x4054B5DE, 0x594F849F, 0x160E1258, 0x0F152319, 0x243870DA, 0x3D23419B, 0x65FD6BA7, 0x7CE65AE6, 0x57CB0925, 0x4ED03864, 0x0191AEA3, 0x188A9FE2, 0x33A7CC21, 0x2ABCFD60, 0xAD24E1AF, 0xB43FD0EE, 0x9F12832D, 0x8609B26C, 0xC94824AB, 0xD05315EA, 0xFB7E4629, 0xE2657768, 0x2F3F79F6, 0x362448B7, 0x1D091B74, 0x04122A35, 0x4B53BCF2, 0x52488DB3, 0x7965DE70, 0x607EEF31, 0xE7E6F3FE, 0xFEFDC2BF, 0xD5D0917C, 0xCCCBA03D, 0x838A36FA, 0x9A9107BB, 0xB1BC5478, 0xA8A76539, 0x3B83984B, 0x2298A90A, 0x09B5FAC9, 0x10AECB88, 0x5FEF5D4F, 0x46F46C0E, 0x6DD93FCD, 0x74C20E8C, 0xF35A1243, 0xEA412302, 0xC16C70C1, 0xD8774180, 0x9736D747, 0x8E2DE606, 0xA500B5C5, 0xBC1B8484, 0x71418A1A, 0x685ABB5B, 0x4377E898, 0x5A6CD9D9, 0x152D4F1E, 0x0C367E5F, 0x271B2D9C, 0x3E001CDD, 0xB9980012, 0xA0833153, 0x8BAE6290, 0x92B553D1, 0xDDF4C516, 0xC4EFF457, 0xEFC2A794, 0xF6D996D5, 0xAE07BCE9, 0xB71C8DA8, 0x9C31DE6B, 0x852AEF2A, 0xCA6B79ED, 0xD37048AC, 0xF85D1B6F, 0xE1462A2E, 0x66DE36E1, 0x7FC507A0, 0x54E85463, 0x4DF36522, 0x02B2F3E5, 0x1BA9C2A4, 0x30849167, 0x299FA026, 0xE4C5AEB8, 0xFDDE9FF9, 0xD6F3CC3A, 0xCFE8FD7B, 0x80A96BBC, 0x99B25AFD, 0xB29F093E, 0xAB84387F, 0x2C1C24B0, 0x350715F1, 0x1E2A4632, 0x07317773, 0x4870E1B4, 0x516BD0F5, 0x7A468336, 0x635DB277, 0xCBFAD74E, 0xD2E1E60F, 0xF9CCB5CC, 0xE0D7848D, 0xAF96124A, 0xB68D230B, 0x9DA070C8, 0x84BB4189, 0x03235D46, 0x1A386C07, 0x31153FC4, 0x280E0E85, 0x674F9842, 0x7E54A903, 0x5579FAC0, 0x4C62CB81, 0x8138C51F, 0x9823F45E, 0xB30EA79D, 0xAA1596DC, 0xE554001B, 0xFC4F315A, 0xD7626299, 0xCE7953D8, 0x49E14F17, 0x50FA7E56, 0x7BD72D95, 0x62CC1CD4, 0x2D8D8A13, 0x3496BB52, 0x1FBBE891, 0x06A0D9D0, 0x5E7EF3EC, 0x4765C2AD, 0x6C48916E, 0x7553A02F, 0x3A1236E8, 0x230907A9, 0x0824546A, 0x113F652B, 0x96A779E4, 0x8FBC48A5, 0xA4911B66, 0xBD8A2A27, 0xF2CBBCE0, 0xEBD08DA1, 0xC0FDDE62, 0xD9E6EF23, 0x14BCE1BD, 0x0DA7D0FC, 0x268A833F, 0x3F91B27E, 0x70D024B9, 0x69CB15F8, 0x42E6463B, 0x5BFD777A, 0xDC656BB5, 0xC57E5AF4, 0xEE530937, 0xF7483876, 0xB809AEB1, 0xA1129FF0, 0x8A3FCC33, 0x9324FD72, /* T8_2 */ 0x00000000, 0x01C26A37, 0x0384D46E, 0x0246BE59, 0x0709A8DC, 0x06CBC2EB, 0x048D7CB2, 0x054F1685, 0x0E1351B8, 0x0FD13B8F, 0x0D9785D6, 0x0C55EFE1, 0x091AF964, 0x08D89353, 0x0A9E2D0A, 0x0B5C473D, 0x1C26A370, 0x1DE4C947, 0x1FA2771E, 0x1E601D29, 0x1B2F0BAC, 0x1AED619B, 0x18ABDFC2, 0x1969B5F5, 0x1235F2C8, 0x13F798FF, 0x11B126A6, 0x10734C91, 0x153C5A14, 0x14FE3023, 0x16B88E7A, 0x177AE44D, 0x384D46E0, 0x398F2CD7, 0x3BC9928E, 0x3A0BF8B9, 0x3F44EE3C, 0x3E86840B, 0x3CC03A52, 0x3D025065, 0x365E1758, 0x379C7D6F, 0x35DAC336, 0x3418A901, 0x3157BF84, 0x3095D5B3, 0x32D36BEA, 0x331101DD, 0x246BE590, 0x25A98FA7, 0x27EF31FE, 0x262D5BC9, 0x23624D4C, 0x22A0277B, 0x20E69922, 0x2124F315, 0x2A78B428, 0x2BBADE1F, 0x29FC6046, 0x283E0A71, 0x2D711CF4, 0x2CB376C3, 0x2EF5C89A, 0x2F37A2AD, 0x709A8DC0, 0x7158E7F7, 0x731E59AE, 0x72DC3399, 0x7793251C, 0x76514F2B, 0x7417F172, 0x75D59B45, 0x7E89DC78, 0x7F4BB64F, 0x7D0D0816, 0x7CCF6221, 0x798074A4, 0x78421E93, 0x7A04A0CA, 0x7BC6CAFD, 0x6CBC2EB0, 0x6D7E4487, 0x6F38FADE, 0x6EFA90E9, 0x6BB5866C, 0x6A77EC5B, 0x68315202, 0x69F33835, 0x62AF7F08, 0x636D153F, 0x612BAB66, 0x60E9C151, 0x65A6D7D4, 0x6464BDE3, 0x662203BA, 0x67E0698D, 0x48D7CB20, 0x4915A117, 0x4B531F4E, 0x4A917579, 0x4FDE63FC, 0x4E1C09CB, 0x4C5AB792, 0x4D98DDA5, 0x46C49A98, 0x4706F0AF, 0x45404EF6, 0x448224C1, 0x41CD3244, 0x400F5873, 0x4249E62A, 0x438B8C1D, 0x54F16850, 0x55330267, 0x5775BC3E, 0x56B7D609, 0x53F8C08C, 0x523AAABB, 0x507C14E2, 0x51BE7ED5, 0x5AE239E8, 0x5B2053DF, 0x5966ED86, 0x58A487B1, 0x5DEB9134, 0x5C29FB03, 0x5E6F455A, 0x5FAD2F6D, 0xE1351B80, 0xE0F771B7, 0xE2B1CFEE, 0xE373A5D9, 0xE63CB35C, 0xE7FED96B, 0xE5B86732, 0xE47A0D05, 0xEF264A38, 0xEEE4200F, 0xECA29E56, 0xED60F461, 0xE82FE2E4, 0xE9ED88D3, 0xEBAB368A, 0xEA695CBD, 0xFD13B8F0, 0xFCD1D2C7, 0xFE976C9E, 0xFF5506A9, 0xFA1A102C, 0xFBD87A1B, 0xF99EC442, 0xF85CAE75, 0xF300E948, 0xF2C2837F, 0xF0843D26, 0xF1465711, 0xF4094194, 0xF5CB2BA3, 0xF78D95FA, 0xF64FFFCD, 0xD9785D60, 0xD8BA3757, 0xDAFC890E, 0xDB3EE339, 0xDE71F5BC, 0xDFB39F8B, 0xDDF521D2, 0xDC374BE5, 0xD76B0CD8, 0xD6A966EF, 0xD4EFD8B6, 0xD52DB281, 0xD062A404, 0xD1A0CE33, 0xD3E6706A, 0xD2241A5D, 0xC55EFE10, 0xC49C9427, 0xC6DA2A7E, 0xC7184049, 0xC25756CC, 0xC3953CFB, 0xC1D382A2, 0xC011E895, 0xCB4DAFA8, 0xCA8FC59F, 0xC8C97BC6, 0xC90B11F1, 0xCC440774, 0xCD866D43, 0xCFC0D31A, 0xCE02B92D, 0x91AF9640, 0x906DFC77, 0x922B422E, 0x93E92819, 0x96A63E9C, 0x976454AB, 0x9522EAF2, 0x94E080C5, 0x9FBCC7F8, 0x9E7EADCF, 0x9C381396, 0x9DFA79A1, 0x98B56F24, 0x99770513, 0x9B31BB4A, 0x9AF3D17D, 0x8D893530, 0x8C4B5F07, 0x8E0DE15E, 0x8FCF8B69, 0x8A809DEC, 0x8B42F7DB, 0x89044982, 0x88C623B5, 0x839A6488, 0x82580EBF, 0x801EB0E6, 0x81DCDAD1, 0x8493CC54, 0x8551A663, 0x8717183A, 0x86D5720D, 0xA9E2D0A0, 0xA820BA97, 0xAA6604CE, 0xABA46EF9, 0xAEEB787C, 0xAF29124B, 0xAD6FAC12, 0xACADC625, 0xA7F18118, 0xA633EB2F, 0xA4755576, 0xA5B73F41, 0xA0F829C4, 0xA13A43F3, 0xA37CFDAA, 0xA2BE979D, 0xB5C473D0, 0xB40619E7, 0xB640A7BE, 0xB782CD89, 0xB2CDDB0C, 0xB30FB13B, 0xB1490F62, 0xB08B6555, 0xBBD72268, 0xBA15485F, 0xB853F606, 0xB9919C31, 0xBCDE8AB4, 0xBD1CE083, 0xBF5A5EDA, 0xBE9834ED, /* T8_3 */ 0x00000000, 0xB8BC6765, 0xAA09C88B, 0x12B5AFEE, 0x8F629757, 0x37DEF032, 0x256B5FDC, 0x9DD738B9, 0xC5B428EF, 0x7D084F8A, 0x6FBDE064, 0xD7018701, 0x4AD6BFB8, 0xF26AD8DD, 0xE0DF7733, 0x58631056, 0x5019579F, 0xE8A530FA, 0xFA109F14, 0x42ACF871, 0xDF7BC0C8, 0x67C7A7AD, 0x75720843, 0xCDCE6F26, 0x95AD7F70, 0x2D111815, 0x3FA4B7FB, 0x8718D09E, 0x1ACFE827, 0xA2738F42, 0xB0C620AC, 0x087A47C9, 0xA032AF3E, 0x188EC85B, 0x0A3B67B5, 0xB28700D0, 0x2F503869, 0x97EC5F0C, 0x8559F0E2, 0x3DE59787, 0x658687D1, 0xDD3AE0B4, 0xCF8F4F5A, 0x7733283F, 0xEAE41086, 0x525877E3, 0x40EDD80D, 0xF851BF68, 0xF02BF8A1, 0x48979FC4, 0x5A22302A, 0xE29E574F, 0x7F496FF6, 0xC7F50893, 0xD540A77D, 0x6DFCC018, 0x359FD04E, 0x8D23B72B, 0x9F9618C5, 0x272A7FA0, 0xBAFD4719, 0x0241207C, 0x10F48F92, 0xA848E8F7, 0x9B14583D, 0x23A83F58, 0x311D90B6, 0x89A1F7D3, 0x1476CF6A, 0xACCAA80F, 0xBE7F07E1, 0x06C36084, 0x5EA070D2, 0xE61C17B7, 0xF4A9B859, 0x4C15DF3C, 0xD1C2E785, 0x697E80E0, 0x7BCB2F0E, 0xC377486B, 0xCB0D0FA2, 0x73B168C7, 0x6104C729, 0xD9B8A04C, 0x446F98F5, 0xFCD3FF90, 0xEE66507E, 0x56DA371B, 0x0EB9274D, 0xB6054028, 0xA4B0EFC6, 0x1C0C88A3, 0x81DBB01A, 0x3967D77F, 0x2BD27891, 0x936E1FF4, 0x3B26F703, 0x839A9066, 0x912F3F88, 0x299358ED, 0xB4446054, 0x0CF80731, 0x1E4DA8DF, 0xA6F1CFBA, 0xFE92DFEC, 0x462EB889, 0x549B1767, 0xEC277002, 0x71F048BB, 0xC94C2FDE, 0xDBF98030, 0x6345E755, 0x6B3FA09C, 0xD383C7F9, 0xC1366817, 0x798A0F72, 0xE45D37CB, 0x5CE150AE, 0x4E54FF40, 0xF6E89825, 0xAE8B8873, 0x1637EF16, 0x048240F8, 0xBC3E279D, 0x21E91F24, 0x99557841, 0x8BE0D7AF, 0x335CB0CA, 0xED59B63B, 0x55E5D15E, 0x47507EB0, 0xFFEC19D5, 0x623B216C, 0xDA874609, 0xC832E9E7, 0x708E8E82, 0x28ED9ED4, 0x9051F9B1, 0x82E4565F, 0x3A58313A, 0xA78F0983, 0x1F336EE6, 0x0D86C108, 0xB53AA66D, 0xBD40E1A4, 0x05FC86C1, 0x1749292F, 0xAFF54E4A, 0x322276F3, 0x8A9E1196, 0x982BBE78, 0x2097D91D, 0x78F4C94B, 0xC048AE2E, 0xD2FD01C0, 0x6A4166A5, 0xF7965E1C, 0x4F2A3979, 0x5D9F9697, 0xE523F1F2, 0x4D6B1905, 0xF5D77E60, 0xE762D18E, 0x5FDEB6EB, 0xC2098E52, 0x7AB5E937, 0x680046D9, 0xD0BC21BC, 0x88DF31EA, 0x3063568F, 0x22D6F961, 0x9A6A9E04, 0x07BDA6BD, 0xBF01C1D8, 0xADB46E36, 0x15080953, 0x1D724E9A, 0xA5CE29FF, 0xB77B8611, 0x0FC7E174, 0x9210D9CD, 0x2AACBEA8, 0x38191146, 0x80A57623, 0xD8C66675, 0x607A0110, 0x72CFAEFE, 0xCA73C99B, 0x57A4F122, 0xEF189647, 0xFDAD39A9, 0x45115ECC, 0x764DEE06, 0xCEF18963, 0xDC44268D, 0x64F841E8, 0xF92F7951, 0x41931E34, 0x5326B1DA, 0xEB9AD6BF, 0xB3F9C6E9, 0x0B45A18C, 0x19F00E62, 0xA14C6907, 0x3C9B51BE, 0x842736DB, 0x96929935, 0x2E2EFE50, 0x2654B999, 0x9EE8DEFC, 0x8C5D7112, 0x34E11677, 0xA9362ECE, 0x118A49AB, 0x033FE645, 0xBB838120, 0xE3E09176, 0x5B5CF613, 0x49E959FD, 0xF1553E98, 0x6C820621, 0xD43E6144, 0xC68BCEAA, 0x7E37A9CF, 0xD67F4138, 0x6EC3265D, 0x7C7689B3, 0xC4CAEED6, 0x591DD66F, 0xE1A1B10A, 0xF3141EE4, 0x4BA87981, 0x13CB69D7, 0xAB770EB2, 0xB9C2A15C, 0x017EC639, 0x9CA9FE80, 0x241599E5, 0x36A0360B, 0x8E1C516E, 0x866616A7, 0x3EDA71C2, 0x2C6FDE2C, 0x94D3B949, 0x090481F0, 0xB1B8E695, 0xA30D497B, 0x1BB12E1E, 0x43D23E48, 0xFB6E592D, 0xE9DBF6C3, 0x516791A6, 0xCCB0A91F, 0x740CCE7A, 0x66B96194, 0xDE0506F1, /* T8_4 */ 0x00000000, 0x3D6029B0, 0x7AC05360, 0x47A07AD0, 0xF580A6C0, 0xC8E08F70, 0x8F40F5A0, 0xB220DC10, 0x30704BC1, 0x0D106271, 0x4AB018A1, 0x77D03111, 0xC5F0ED01, 0xF890C4B1, 0xBF30BE61, 0x825097D1, 0x60E09782, 0x5D80BE32, 0x1A20C4E2, 0x2740ED52, 0x95603142, 0xA80018F2, 0xEFA06222, 0xD2C04B92, 0x5090DC43, 0x6DF0F5F3, 0x2A508F23, 0x1730A693, 0xA5107A83, 0x98705333, 0xDFD029E3, 0xE2B00053, 0xC1C12F04, 0xFCA106B4, 0xBB017C64, 0x866155D4, 0x344189C4, 0x0921A074, 0x4E81DAA4, 0x73E1F314, 0xF1B164C5, 0xCCD14D75, 0x8B7137A5, 0xB6111E15, 0x0431C205, 0x3951EBB5, 0x7EF19165, 0x4391B8D5, 0xA121B886, 0x9C419136, 0xDBE1EBE6, 0xE681C256, 0x54A11E46, 0x69C137F6, 0x2E614D26, 0x13016496, 0x9151F347, 0xAC31DAF7, 0xEB91A027, 0xD6F18997, 0x64D15587, 0x59B17C37, 0x1E1106E7, 0x23712F57, 0x58F35849, 0x659371F9, 0x22330B29, 0x1F532299, 0xAD73FE89, 0x9013D739, 0xD7B3ADE9, 0xEAD38459, 0x68831388, 0x55E33A38, 0x124340E8, 0x2F236958, 0x9D03B548, 0xA0639CF8, 0xE7C3E628, 0xDAA3CF98, 0x3813CFCB, 0x0573E67B, 0x42D39CAB, 0x7FB3B51B, 0xCD93690B, 0xF0F340BB, 0xB7533A6B, 0x8A3313DB, 0x0863840A, 0x3503ADBA, 0x72A3D76A, 0x4FC3FEDA, 0xFDE322CA, 0xC0830B7A, 0x872371AA, 0xBA43581A, 0x9932774D, 0xA4525EFD, 0xE3F2242D, 0xDE920D9D, 0x6CB2D18D, 0x51D2F83D, 0x167282ED, 0x2B12AB5D, 0xA9423C8C, 0x9422153C, 0xD3826FEC, 0xEEE2465C, 0x5CC29A4C, 0x61A2B3FC, 0x2602C92C, 0x1B62E09C, 0xF9D2E0CF, 0xC4B2C97F, 0x8312B3AF, 0xBE729A1F, 0x0C52460F, 0x31326FBF, 0x7692156F, 0x4BF23CDF, 0xC9A2AB0E, 0xF4C282BE, 0xB362F86E, 0x8E02D1DE, 0x3C220DCE, 0x0142247E, 0x46E25EAE, 0x7B82771E, 0xB1E6B092, 0x8C869922, 0xCB26E3F2, 0xF646CA42, 0x44661652, 0x79063FE2, 0x3EA64532, 0x03C66C82, 0x8196FB53, 0xBCF6D2E3, 0xFB56A833, 0xC6368183, 0x74165D93, 0x49767423, 0x0ED60EF3, 0x33B62743, 0xD1062710, 0xEC660EA0, 0xABC67470, 0x96A65DC0, 0x248681D0, 0x19E6A860, 0x5E46D2B0, 0x6326FB00, 0xE1766CD1, 0xDC164561, 0x9BB63FB1, 0xA6D61601, 0x14F6CA11, 0x2996E3A1, 0x6E369971, 0x5356B0C1, 0x70279F96, 0x4D47B626, 0x0AE7CCF6, 0x3787E546, 0x85A73956, 0xB8C710E6, 0xFF676A36, 0xC2074386, 0x4057D457, 0x7D37FDE7, 0x3A978737, 0x07F7AE87, 0xB5D77297, 0x88B75B27, 0xCF1721F7, 0xF2770847, 0x10C70814, 0x2DA721A4, 0x6A075B74, 0x576772C4, 0xE547AED4, 0xD8278764, 0x9F87FDB4, 0xA2E7D404, 0x20B743D5, 0x1DD76A65, 0x5A7710B5, 0x67173905, 0xD537E515, 0xE857CCA5, 0xAFF7B675, 0x92979FC5, 0xE915E8DB, 0xD475C16B, 0x93D5BBBB, 0xAEB5920B, 0x1C954E1B, 0x21F567AB, 0x66551D7B, 0x5B3534CB, 0xD965A31A, 0xE4058AAA, 0xA3A5F07A, 0x9EC5D9CA, 0x2CE505DA, 0x11852C6A, 0x562556BA, 0x6B457F0A, 0x89F57F59, 0xB49556E9, 0xF3352C39, 0xCE550589, 0x7C75D999, 0x4115F029, 0x06B58AF9, 0x3BD5A349, 0xB9853498, 0x84E51D28, 0xC34567F8, 0xFE254E48, 0x4C059258, 0x7165BBE8, 0x36C5C138, 0x0BA5E888, 0x28D4C7DF, 0x15B4EE6F, 0x521494BF, 0x6F74BD0F, 0xDD54611F, 0xE03448AF, 0xA794327F, 0x9AF41BCF, 0x18A48C1E, 0x25C4A5AE, 0x6264DF7E, 0x5F04F6CE, 0xED242ADE, 0xD044036E, 0x97E479BE, 0xAA84500E, 0x4834505D, 0x755479ED, 0x32F4033D, 0x0F942A8D, 0xBDB4F69D, 0x80D4DF2D, 0xC774A5FD, 0xFA148C4D, 0x78441B9C, 0x4524322C, 0x028448FC, 0x3FE4614C, 0x8DC4BD5C, 0xB0A494EC, 0xF704EE3C, 0xCA64C78C, /* T8_5 */ 0x00000000, 0xCB5CD3A5, 0x4DC8A10B, 0x869472AE, 0x9B914216, 0x50CD91B3, 0xD659E31D, 0x1D0530B8, 0xEC53826D, 0x270F51C8, 0xA19B2366, 0x6AC7F0C3, 0x77C2C07B, 0xBC9E13DE, 0x3A0A6170, 0xF156B2D5, 0x03D6029B, 0xC88AD13E, 0x4E1EA390, 0x85427035, 0x9847408D, 0x531B9328, 0xD58FE186, 0x1ED33223, 0xEF8580F6, 0x24D95353, 0xA24D21FD, 0x6911F258, 0x7414C2E0, 0xBF481145, 0x39DC63EB, 0xF280B04E, 0x07AC0536, 0xCCF0D693, 0x4A64A43D, 0x81387798, 0x9C3D4720, 0x57619485, 0xD1F5E62B, 0x1AA9358E, 0xEBFF875B, 0x20A354FE, 0xA6372650, 0x6D6BF5F5, 0x706EC54D, 0xBB3216E8, 0x3DA66446, 0xF6FAB7E3, 0x047A07AD, 0xCF26D408, 0x49B2A6A6, 0x82EE7503, 0x9FEB45BB, 0x54B7961E, 0xD223E4B0, 0x197F3715, 0xE82985C0, 0x23755665, 0xA5E124CB, 0x6EBDF76E, 0x73B8C7D6, 0xB8E41473, 0x3E7066DD, 0xF52CB578, 0x0F580A6C, 0xC404D9C9, 0x4290AB67, 0x89CC78C2, 0x94C9487A, 0x5F959BDF, 0xD901E971, 0x125D3AD4, 0xE30B8801, 0x28575BA4, 0xAEC3290A, 0x659FFAAF, 0x789ACA17, 0xB3C619B2, 0x35526B1C, 0xFE0EB8B9, 0x0C8E08F7, 0xC7D2DB52, 0x4146A9FC, 0x8A1A7A59, 0x971F4AE1, 0x5C439944, 0xDAD7EBEA, 0x118B384F, 0xE0DD8A9A, 0x2B81593F, 0xAD152B91, 0x6649F834, 0x7B4CC88C, 0xB0101B29, 0x36846987, 0xFDD8BA22, 0x08F40F5A, 0xC3A8DCFF, 0x453CAE51, 0x8E607DF4, 0x93654D4C, 0x58399EE9, 0xDEADEC47, 0x15F13FE2, 0xE4A78D37, 0x2FFB5E92, 0xA96F2C3C, 0x6233FF99, 0x7F36CF21, 0xB46A1C84, 0x32FE6E2A, 0xF9A2BD8F, 0x0B220DC1, 0xC07EDE64, 0x46EAACCA, 0x8DB67F6F, 0x90B34FD7, 0x5BEF9C72, 0xDD7BEEDC, 0x16273D79, 0xE7718FAC, 0x2C2D5C09, 0xAAB92EA7, 0x61E5FD02, 0x7CE0CDBA, 0xB7BC1E1F, 0x31286CB1, 0xFA74BF14, 0x1EB014D8, 0xD5ECC77D, 0x5378B5D3, 0x98246676, 0x852156CE, 0x4E7D856B, 0xC8E9F7C5, 0x03B52460, 0xF2E396B5, 0x39BF4510, 0xBF2B37BE, 0x7477E41B, 0x6972D4A3, 0xA22E0706, 0x24BA75A8, 0xEFE6A60D, 0x1D661643, 0xD63AC5E6, 0x50AEB748, 0x9BF264ED, 0x86F75455, 0x4DAB87F0, 0xCB3FF55E, 0x006326FB, 0xF135942E, 0x3A69478B, 0xBCFD3525, 0x77A1E680, 0x6AA4D638, 0xA1F8059D, 0x276C7733, 0xEC30A496, 0x191C11EE, 0xD240C24B, 0x54D4B0E5, 0x9F886340, 0x828D53F8, 0x49D1805D, 0xCF45F2F3, 0x04192156, 0xF54F9383, 0x3E134026, 0xB8873288, 0x73DBE12D, 0x6EDED195, 0xA5820230, 0x2316709E, 0xE84AA33B, 0x1ACA1375, 0xD196C0D0, 0x5702B27E, 0x9C5E61DB, 0x815B5163, 0x4A0782C6, 0xCC93F068, 0x07CF23CD, 0xF6999118, 0x3DC542BD, 0xBB513013, 0x700DE3B6, 0x6D08D30E, 0xA65400AB, 0x20C07205, 0xEB9CA1A0, 0x11E81EB4, 0xDAB4CD11, 0x5C20BFBF, 0x977C6C1A, 0x8A795CA2, 0x41258F07, 0xC7B1FDA9, 0x0CED2E0C, 0xFDBB9CD9, 0x36E74F7C, 0xB0733DD2, 0x7B2FEE77, 0x662ADECF, 0xAD760D6A, 0x2BE27FC4, 0xE0BEAC61, 0x123E1C2F, 0xD962CF8A, 0x5FF6BD24, 0x94AA6E81, 0x89AF5E39, 0x42F38D9C, 0xC467FF32, 0x0F3B2C97, 0xFE6D9E42, 0x35314DE7, 0xB3A53F49, 0x78F9ECEC, 0x65FCDC54, 0xAEA00FF1, 0x28347D5F, 0xE368AEFA, 0x16441B82, 0xDD18C827, 0x5B8CBA89, 0x90D0692C, 0x8DD55994, 0x46898A31, 0xC01DF89F, 0x0B412B3A, 0xFA1799EF, 0x314B4A4A, 0xB7DF38E4, 0x7C83EB41, 0x6186DBF9, 0xAADA085C, 0x2C4E7AF2, 0xE712A957, 0x15921919, 0xDECECABC, 0x585AB812, 0x93066BB7, 0x8E035B0F, 0x455F88AA, 0xC3CBFA04, 0x089729A1, 0xF9C19B74, 0x329D48D1, 0xB4093A7F, 0x7F55E9DA, 0x6250D962, 0xA90C0AC7, 0x2F987869, 0xE4C4ABCC, /* T8_6 */ 0x00000000, 0xA6770BB4, 0x979F1129, 0x31E81A9D, 0xF44F2413, 0x52382FA7, 0x63D0353A, 0xC5A73E8E, 0x33EF4E67, 0x959845D3, 0xA4705F4E, 0x020754FA, 0xC7A06A74, 0x61D761C0, 0x503F7B5D, 0xF64870E9, 0x67DE9CCE, 0xC1A9977A, 0xF0418DE7, 0x56368653, 0x9391B8DD, 0x35E6B369, 0x040EA9F4, 0xA279A240, 0x5431D2A9, 0xF246D91D, 0xC3AEC380, 0x65D9C834, 0xA07EF6BA, 0x0609FD0E, 0x37E1E793, 0x9196EC27, 0xCFBD399C, 0x69CA3228, 0x582228B5, 0xFE552301, 0x3BF21D8F, 0x9D85163B, 0xAC6D0CA6, 0x0A1A0712, 0xFC5277FB, 0x5A257C4F, 0x6BCD66D2, 0xCDBA6D66, 0x081D53E8, 0xAE6A585C, 0x9F8242C1, 0x39F54975, 0xA863A552, 0x0E14AEE6, 0x3FFCB47B, 0x998BBFCF, 0x5C2C8141, 0xFA5B8AF5, 0xCBB39068, 0x6DC49BDC, 0x9B8CEB35, 0x3DFBE081, 0x0C13FA1C, 0xAA64F1A8, 0x6FC3CF26, 0xC9B4C492, 0xF85CDE0F, 0x5E2BD5BB, 0x440B7579, 0xE27C7ECD, 0xD3946450, 0x75E36FE4, 0xB044516A, 0x16335ADE, 0x27DB4043, 0x81AC4BF7, 0x77E43B1E, 0xD19330AA, 0xE07B2A37, 0x460C2183, 0x83AB1F0D, 0x25DC14B9, 0x14340E24, 0xB2430590, 0x23D5E9B7, 0x85A2E203, 0xB44AF89E, 0x123DF32A, 0xD79ACDA4, 0x71EDC610, 0x4005DC8D, 0xE672D739, 0x103AA7D0, 0xB64DAC64, 0x87A5B6F9, 0x21D2BD4D, 0xE47583C3, 0x42028877, 0x73EA92EA, 0xD59D995E, 0x8BB64CE5, 0x2DC14751, 0x1C295DCC, 0xBA5E5678, 0x7FF968F6, 0xD98E6342, 0xE86679DF, 0x4E11726B, 0xB8590282, 0x1E2E0936, 0x2FC613AB, 0x89B1181F, 0x4C162691, 0xEA612D25, 0xDB8937B8, 0x7DFE3C0C, 0xEC68D02B, 0x4A1FDB9F, 0x7BF7C102, 0xDD80CAB6, 0x1827F438, 0xBE50FF8C, 0x8FB8E511, 0x29CFEEA5, 0xDF879E4C, 0x79F095F8, 0x48188F65, 0xEE6F84D1, 0x2BC8BA5F, 0x8DBFB1EB, 0xBC57AB76, 0x1A20A0C2, 0x8816EAF2, 0x2E61E146, 0x1F89FBDB, 0xB9FEF06F, 0x7C59CEE1, 0xDA2EC555, 0xEBC6DFC8, 0x4DB1D47C, 0xBBF9A495, 0x1D8EAF21, 0x2C66B5BC, 0x8A11BE08, 0x4FB68086, 0xE9C18B32, 0xD82991AF, 0x7E5E9A1B, 0xEFC8763C, 0x49BF7D88, 0x78576715, 0xDE206CA1, 0x1B87522F, 0xBDF0599B, 0x8C184306, 0x2A6F48B2, 0xDC27385B, 0x7A5033EF, 0x4BB82972, 0xEDCF22C6, 0x28681C48, 0x8E1F17FC, 0xBFF70D61, 0x198006D5, 0x47ABD36E, 0xE1DCD8DA, 0xD034C247, 0x7643C9F3, 0xB3E4F77D, 0x1593FCC9, 0x247BE654, 0x820CEDE0, 0x74449D09, 0xD23396BD, 0xE3DB8C20, 0x45AC8794, 0x800BB91A, 0x267CB2AE, 0x1794A833, 0xB1E3A387, 0x20754FA0, 0x86024414, 0xB7EA5E89, 0x119D553D, 0xD43A6BB3, 0x724D6007, 0x43A57A9A, 0xE5D2712E, 0x139A01C7, 0xB5ED0A73, 0x840510EE, 0x22721B5A, 0xE7D525D4, 0x41A22E60, 0x704A34FD, 0xD63D3F49, 0xCC1D9F8B, 0x6A6A943F, 0x5B828EA2, 0xFDF58516, 0x3852BB98, 0x9E25B02C, 0xAFCDAAB1, 0x09BAA105, 0xFFF2D1EC, 0x5985DA58, 0x686DC0C5, 0xCE1ACB71, 0x0BBDF5FF, 0xADCAFE4B, 0x9C22E4D6, 0x3A55EF62, 0xABC30345, 0x0DB408F1, 0x3C5C126C, 0x9A2B19D8, 0x5F8C2756, 0xF9FB2CE2, 0xC813367F, 0x6E643DCB, 0x982C4D22, 0x3E5B4696, 0x0FB35C0B, 0xA9C457BF, 0x6C636931, 0xCA146285, 0xFBFC7818, 0x5D8B73AC, 0x03A0A617, 0xA5D7ADA3, 0x943FB73E, 0x3248BC8A, 0xF7EF8204, 0x519889B0, 0x6070932D, 0xC6079899, 0x304FE870, 0x9638E3C4, 0xA7D0F959, 0x01A7F2ED, 0xC400CC63, 0x6277C7D7, 0x539FDD4A, 0xF5E8D6FE, 0x647E3AD9, 0xC209316D, 0xF3E12BF0, 0x55962044, 0x90311ECA, 0x3646157E, 0x07AE0FE3, 0xA1D90457, 0x579174BE, 0xF1E67F0A, 0xC00E6597, 0x66796E23, 0xA3DE50AD, 0x05A95B19, 0x34414184, 0x92364A30, /* T8_7 */ 0x00000000, 0xCCAA009E, 0x4225077D, 0x8E8F07E3, 0x844A0EFA, 0x48E00E64, 0xC66F0987, 0x0AC50919, 0xD3E51BB5, 0x1F4F1B2B, 0x91C01CC8, 0x5D6A1C56, 0x57AF154F, 0x9B0515D1, 0x158A1232, 0xD92012AC, 0x7CBB312B, 0xB01131B5, 0x3E9E3656, 0xF23436C8, 0xF8F13FD1, 0x345B3F4F, 0xBAD438AC, 0x767E3832, 0xAF5E2A9E, 0x63F42A00, 0xED7B2DE3, 0x21D12D7D, 0x2B142464, 0xE7BE24FA, 0x69312319, 0xA59B2387, 0xF9766256, 0x35DC62C8, 0xBB53652B, 0x77F965B5, 0x7D3C6CAC, 0xB1966C32, 0x3F196BD1, 0xF3B36B4F, 0x2A9379E3, 0xE639797D, 0x68B67E9E, 0xA41C7E00, 0xAED97719, 0x62737787, 0xECFC7064, 0x205670FA, 0x85CD537D, 0x496753E3, 0xC7E85400, 0x0B42549E, 0x01875D87, 0xCD2D5D19, 0x43A25AFA, 0x8F085A64, 0x562848C8, 0x9A824856, 0x140D4FB5, 0xD8A74F2B, 0xD2624632, 0x1EC846AC, 0x9047414F, 0x5CED41D1, 0x299DC2ED, 0xE537C273, 0x6BB8C590, 0xA712C50E, 0xADD7CC17, 0x617DCC89, 0xEFF2CB6A, 0x2358CBF4, 0xFA78D958, 0x36D2D9C6, 0xB85DDE25, 0x74F7DEBB, 0x7E32D7A2, 0xB298D73C, 0x3C17D0DF, 0xF0BDD041, 0x5526F3C6, 0x998CF358, 0x1703F4BB, 0xDBA9F425, 0xD16CFD3C, 0x1DC6FDA2, 0x9349FA41, 0x5FE3FADF, 0x86C3E873, 0x4A69E8ED, 0xC4E6EF0E, 0x084CEF90, 0x0289E689, 0xCE23E617, 0x40ACE1F4, 0x8C06E16A, 0xD0EBA0BB, 0x1C41A025, 0x92CEA7C6, 0x5E64A758, 0x54A1AE41, 0x980BAEDF, 0x1684A93C, 0xDA2EA9A2, 0x030EBB0E, 0xCFA4BB90, 0x412BBC73, 0x8D81BCED, 0x8744B5F4, 0x4BEEB56A, 0xC561B289, 0x09CBB217, 0xAC509190, 0x60FA910E, 0xEE7596ED, 0x22DF9673, 0x281A9F6A, 0xE4B09FF4, 0x6A3F9817, 0xA6959889, 0x7FB58A25, 0xB31F8ABB, 0x3D908D58, 0xF13A8DC6, 0xFBFF84DF, 0x37558441, 0xB9DA83A2, 0x7570833C, 0x533B85DA, 0x9F918544, 0x111E82A7, 0xDDB48239, 0xD7718B20, 0x1BDB8BBE, 0x95548C5D, 0x59FE8CC3, 0x80DE9E6F, 0x4C749EF1, 0xC2FB9912, 0x0E51998C, 0x04949095, 0xC83E900B, 0x46B197E8, 0x8A1B9776, 0x2F80B4F1, 0xE32AB46F, 0x6DA5B38C, 0xA10FB312, 0xABCABA0B, 0x6760BA95, 0xE9EFBD76, 0x2545BDE8, 0xFC65AF44, 0x30CFAFDA, 0xBE40A839, 0x72EAA8A7, 0x782FA1BE, 0xB485A120, 0x3A0AA6C3, 0xF6A0A65D, 0xAA4DE78C, 0x66E7E712, 0xE868E0F1, 0x24C2E06F, 0x2E07E976, 0xE2ADE9E8, 0x6C22EE0B, 0xA088EE95, 0x79A8FC39, 0xB502FCA7, 0x3B8DFB44, 0xF727FBDA, 0xFDE2F2C3, 0x3148F25D, 0xBFC7F5BE, 0x736DF520, 0xD6F6D6A7, 0x1A5CD639, 0x94D3D1DA, 0x5879D144, 0x52BCD85D, 0x9E16D8C3, 0x1099DF20, 0xDC33DFBE, 0x0513CD12, 0xC9B9CD8C, 0x4736CA6F, 0x8B9CCAF1, 0x8159C3E8, 0x4DF3C376, 0xC37CC495, 0x0FD6C40B, 0x7AA64737, 0xB60C47A9, 0x3883404A, 0xF42940D4, 0xFEEC49CD, 0x32464953, 0xBCC94EB0, 0x70634E2E, 0xA9435C82, 0x65E95C1C, 0xEB665BFF, 0x27CC5B61, 0x2D095278, 0xE1A352E6, 0x6F2C5505, 0xA386559B, 0x061D761C, 0xCAB77682, 0x44387161, 0x889271FF, 0x825778E6, 0x4EFD7878, 0xC0727F9B, 0x0CD87F05, 0xD5F86DA9, 0x19526D37, 0x97DD6AD4, 0x5B776A4A, 0x51B26353, 0x9D1863CD, 0x1397642E, 0xDF3D64B0, 0x83D02561, 0x4F7A25FF, 0xC1F5221C, 0x0D5F2282, 0x079A2B9B, 0xCB302B05, 0x45BF2CE6, 0x89152C78, 0x50353ED4, 0x9C9F3E4A, 0x121039A9, 0xDEBA3937, 0xD47F302E, 0x18D530B0, 0x965A3753, 0x5AF037CD, 0xFF6B144A, 0x33C114D4, 0xBD4E1337, 0x71E413A9, 0x7B211AB0, 0xB78B1A2E, 0x39041DCD, 0xF5AE1D53, 0x2C8E0FFF, 0xE0240F61, 0x6EAB0882, 0xA201081C, 0xA8C40105, 0x646E019B, 0xEAE10678, 0x264B06E6 }; /** * the current CRC value, bit-flipped */ private int crc; private SdkCrc32Checksum() { reset(); } private SdkCrc32Checksum(int crc) { this.crc = crc; } public static SdkCrc32Checksum create() { return new SdkCrc32Checksum(); } @Override public long getValue() { return ~crc & 0xffffffffL; } @Override public void reset() { crc = 0xffffffff; } @Override public void update(byte[] b, int offset, int len) { int localCrc = crc; int remainder = len & 0x7; int i = offset; for (int end = offset + len - remainder; i < end; i += 8) { int x = localCrc ^ (((b[i] << 24) >>> 24) + ((b[i + 1] << 24) >>> 16) + ((b[i + 2] << 24) >>> 8) + (b[i + 3] << 24)); localCrc = T[((x << 24) >>> 24) + 0x700] ^ T[((x << 16) >>> 24) + 0x600] ^ T[((x << 8) >>> 24) + 0x500] ^ T[(x >>> 24) + 0x400] ^ T[((b[i + 4] << 24) >>> 24) + 0x300] ^ T[((b[i + 5] << 24) >>> 24) + 0x200] ^ T[((b[i + 6] << 24) >>> 24) + 0x100] ^ T[(b[i + 7] << 24) >>> 24]; } /* loop unroll - duff's device style */ for (int index = 0; index < remainder; index++) { localCrc = (localCrc >>> 8) ^ T[((localCrc ^ b[i]) << 24) >>> 24]; i++; } // Publish crc out to object crc = localCrc; } @Override public void update(int b) { crc = (crc >>> 8) ^ T[((crc ^ b) << 24) >>> 24]; } @Override public SdkCrc32Checksum clone() { return new SdkCrc32Checksum(crc); } }
2,339
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/Sha1Checksum.java
/* * 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.signer.checksums; import java.security.MessageDigest; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.util.DigestAlgorithm; /** * Implementation of {@link SdkChecksum} to calculate an Sha-1 checksum. */ @SdkInternalApi public class Sha1Checksum implements SdkChecksum { private MessageDigest digest; private MessageDigest digestLastMarked; public Sha1Checksum() { this.digest = getDigest(); } @Override public void update(int b) { digest.update((byte) b); } @Override public void update(byte[] b, int off, int len) { digest.update(b, off, len); } @Override public long getValue() { throw new UnsupportedOperationException("Use getChecksumBytes() instead."); } @Override public void reset() { digest = (digestLastMarked == null) // This is necessary so that should there be a reset without a // preceding mark, the Sha-1 would still be computed correctly. ? getDigest() : cloneFrom(digestLastMarked); } private MessageDigest getDigest() { return DigestAlgorithm.SHA1.getDigest(); } @Override public byte[] getChecksumBytes() { return digest.digest(); } @Override public void mark(int readLimit) { digestLastMarked = cloneFrom(digest); } private MessageDigest cloneFrom(MessageDigest from) { try { return (MessageDigest) from.clone(); } catch (CloneNotSupportedException e) { // should never occur throw new IllegalStateException("unexpected", e); } } }
2,340
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/SdkChecksum.java
/* * 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.signer.checksums; import java.nio.ByteBuffer; import java.util.zip.Checksum; import software.amazon.awssdk.annotations.SdkInternalApi; /** * Extension of {@link Checksum} to support checksums and checksum validations used by the SDK that are not provided by the JDK. */ @SdkInternalApi public interface SdkChecksum extends Checksum { /** * Returns the computed checksum in a byte array rather than the long provided by {@link #getValue()}. * * @return byte[] containing the checksum */ byte[] getChecksumBytes(); /** * Allows marking a checksum for checksums that support the ability to mark and reset. * * @param readLimit the maximum limit of bytes that can be read before the mark position becomes invalid. */ void mark(int readLimit); /** * Updates the current checksum with the specified array of bytes. * * @param b the array of bytes to update the checksum with * @throws NullPointerException if {@code b} is {@code null} */ default void update(byte[] b) { update(b, 0, b.length); } /** * Updates the current checksum with the bytes from the specified buffer. * <p> * The checksum is updated with the remaining bytes in the buffer, starting at the buffer's position. Upon return, the * buffer's position will be updated to its limit; its limit will not have been changed. * * @param buffer the ByteBuffer to update the checksum with * @throws NullPointerException if {@code buffer} is {@code null} * @apiNote For best performance with DirectByteBuffer and other ByteBuffer implementations without a backing array * implementers of this interface should override this method. * @implSpec The default implementation has the following behavior.<br> For ByteBuffers backed by an accessible byte array. * <pre>{@code * update(buffer.array(), * buffer.position() + buffer.arrayOffset(), * buffer.remaining()); * }</pre> * For ByteBuffers not backed by an accessible byte array. * <pre>{@code * byte[] b = new byte[Math.min(buffer.remaining(), 4096)]; * while (buffer.hasRemaining()) { * int length = Math.min(buffer.remaining(), b.length); * buffer.get(b, 0, length); * update(b, 0, length); * } * }</pre> */ default void update(ByteBuffer buffer) { int pos = buffer.position(); int limit = buffer.limit(); int rem = limit - pos; if (rem <= 0) { return; } if (buffer.hasArray()) { update(buffer.array(), pos + buffer.arrayOffset(), rem); } else { byte[] b = new byte[Math.min(buffer.remaining(), 4096)]; while (buffer.hasRemaining()) { int length = Math.min(buffer.remaining(), b.length); buffer.get(b, 0, length); update(b, 0, length); } } buffer.position(limit); } }
2,341
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/SdkCrc32CChecksum.java
/* * 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.signer.checksums; import java.util.zip.Checksum; import software.amazon.awssdk.annotations.SdkInternalApi; /* * THIS FILE HAS BEEN MODIFIED FROM THE ORIGINAL VERSION. * * The code comes from PureJavaCrc32C.java in Apache Commons Codec 1.11. * It has been modified to add a createCopy() method. * The createCopy method is used to save current checksum state when the checksum is marked. */ @SdkInternalApi public final class SdkCrc32CChecksum implements Checksum, Cloneable { private static final int T8_0_START = 0; private static final int T8_1_START = 256; private static final int T8_2_START = 2 * 256; private static final int T8_3_START = 3 * 256; private static final int T_8_4_START = 4 * 256; private static final int T8_5_START = 5 * 256; private static final int T8_6_START = 6 * 256; private static final int T8_7_START = 7 * 256; // CRC polynomial tables generated by: // java -cp build/test/classes/:build/classes/ \ // org.apache.hadoop.util.TestPureJavaCrc32\$Table 82F63B78 private static final int[] T = { /* T8_0 */ 0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4, 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB, 0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B, 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24, 0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B, 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384, 0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54, 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B, 0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A, 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35, 0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5, 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA, 0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45, 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A, 0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A, 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595, 0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48, 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957, 0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687, 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198, 0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927, 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38, 0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8, 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7, 0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096, 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789, 0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859, 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46, 0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9, 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6, 0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36, 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829, 0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C, 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93, 0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043, 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C, 0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3, 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC, 0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C, 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033, 0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652, 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D, 0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D, 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982, 0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D, 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622, 0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2, 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED, 0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530, 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F, 0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF, 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0, 0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F, 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540, 0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90, 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F, 0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE, 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1, 0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321, 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E, 0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E, 0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351, /* T8_1 */ 0x00000000, 0x13A29877, 0x274530EE, 0x34E7A899, 0x4E8A61DC, 0x5D28F9AB, 0x69CF5132, 0x7A6DC945, 0x9D14C3B8, 0x8EB65BCF, 0xBA51F356, 0xA9F36B21, 0xD39EA264, 0xC03C3A13, 0xF4DB928A, 0xE7790AFD, 0x3FC5F181, 0x2C6769F6, 0x1880C16F, 0x0B225918, 0x714F905D, 0x62ED082A, 0x560AA0B3, 0x45A838C4, 0xA2D13239, 0xB173AA4E, 0x859402D7, 0x96369AA0, 0xEC5B53E5, 0xFFF9CB92, 0xCB1E630B, 0xD8BCFB7C, 0x7F8BE302, 0x6C297B75, 0x58CED3EC, 0x4B6C4B9B, 0x310182DE, 0x22A31AA9, 0x1644B230, 0x05E62A47, 0xE29F20BA, 0xF13DB8CD, 0xC5DA1054, 0xD6788823, 0xAC154166, 0xBFB7D911, 0x8B507188, 0x98F2E9FF, 0x404E1283, 0x53EC8AF4, 0x670B226D, 0x74A9BA1A, 0x0EC4735F, 0x1D66EB28, 0x298143B1, 0x3A23DBC6, 0xDD5AD13B, 0xCEF8494C, 0xFA1FE1D5, 0xE9BD79A2, 0x93D0B0E7, 0x80722890, 0xB4958009, 0xA737187E, 0xFF17C604, 0xECB55E73, 0xD852F6EA, 0xCBF06E9D, 0xB19DA7D8, 0xA23F3FAF, 0x96D89736, 0x857A0F41, 0x620305BC, 0x71A19DCB, 0x45463552, 0x56E4AD25, 0x2C896460, 0x3F2BFC17, 0x0BCC548E, 0x186ECCF9, 0xC0D23785, 0xD370AFF2, 0xE797076B, 0xF4359F1C, 0x8E585659, 0x9DFACE2E, 0xA91D66B7, 0xBABFFEC0, 0x5DC6F43D, 0x4E646C4A, 0x7A83C4D3, 0x69215CA4, 0x134C95E1, 0x00EE0D96, 0x3409A50F, 0x27AB3D78, 0x809C2506, 0x933EBD71, 0xA7D915E8, 0xB47B8D9F, 0xCE1644DA, 0xDDB4DCAD, 0xE9537434, 0xFAF1EC43, 0x1D88E6BE, 0x0E2A7EC9, 0x3ACDD650, 0x296F4E27, 0x53028762, 0x40A01F15, 0x7447B78C, 0x67E52FFB, 0xBF59D487, 0xACFB4CF0, 0x981CE469, 0x8BBE7C1E, 0xF1D3B55B, 0xE2712D2C, 0xD69685B5, 0xC5341DC2, 0x224D173F, 0x31EF8F48, 0x050827D1, 0x16AABFA6, 0x6CC776E3, 0x7F65EE94, 0x4B82460D, 0x5820DE7A, 0xFBC3FAF9, 0xE861628E, 0xDC86CA17, 0xCF245260, 0xB5499B25, 0xA6EB0352, 0x920CABCB, 0x81AE33BC, 0x66D73941, 0x7575A136, 0x419209AF, 0x523091D8, 0x285D589D, 0x3BFFC0EA, 0x0F186873, 0x1CBAF004, 0xC4060B78, 0xD7A4930F, 0xE3433B96, 0xF0E1A3E1, 0x8A8C6AA4, 0x992EF2D3, 0xADC95A4A, 0xBE6BC23D, 0x5912C8C0, 0x4AB050B7, 0x7E57F82E, 0x6DF56059, 0x1798A91C, 0x043A316B, 0x30DD99F2, 0x237F0185, 0x844819FB, 0x97EA818C, 0xA30D2915, 0xB0AFB162, 0xCAC27827, 0xD960E050, 0xED8748C9, 0xFE25D0BE, 0x195CDA43, 0x0AFE4234, 0x3E19EAAD, 0x2DBB72DA, 0x57D6BB9F, 0x447423E8, 0x70938B71, 0x63311306, 0xBB8DE87A, 0xA82F700D, 0x9CC8D894, 0x8F6A40E3, 0xF50789A6, 0xE6A511D1, 0xD242B948, 0xC1E0213F, 0x26992BC2, 0x353BB3B5, 0x01DC1B2C, 0x127E835B, 0x68134A1E, 0x7BB1D269, 0x4F567AF0, 0x5CF4E287, 0x04D43CFD, 0x1776A48A, 0x23910C13, 0x30339464, 0x4A5E5D21, 0x59FCC556, 0x6D1B6DCF, 0x7EB9F5B8, 0x99C0FF45, 0x8A626732, 0xBE85CFAB, 0xAD2757DC, 0xD74A9E99, 0xC4E806EE, 0xF00FAE77, 0xE3AD3600, 0x3B11CD7C, 0x28B3550B, 0x1C54FD92, 0x0FF665E5, 0x759BACA0, 0x663934D7, 0x52DE9C4E, 0x417C0439, 0xA6050EC4, 0xB5A796B3, 0x81403E2A, 0x92E2A65D, 0xE88F6F18, 0xFB2DF76F, 0xCFCA5FF6, 0xDC68C781, 0x7B5FDFFF, 0x68FD4788, 0x5C1AEF11, 0x4FB87766, 0x35D5BE23, 0x26772654, 0x12908ECD, 0x013216BA, 0xE64B1C47, 0xF5E98430, 0xC10E2CA9, 0xD2ACB4DE, 0xA8C17D9B, 0xBB63E5EC, 0x8F844D75, 0x9C26D502, 0x449A2E7E, 0x5738B609, 0x63DF1E90, 0x707D86E7, 0x0A104FA2, 0x19B2D7D5, 0x2D557F4C, 0x3EF7E73B, 0xD98EEDC6, 0xCA2C75B1, 0xFECBDD28, 0xED69455F, 0x97048C1A, 0x84A6146D, 0xB041BCF4, 0xA3E32483, /* T8_2 */ 0x00000000, 0xA541927E, 0x4F6F520D, 0xEA2EC073, 0x9EDEA41A, 0x3B9F3664, 0xD1B1F617, 0x74F06469, 0x38513EC5, 0x9D10ACBB, 0x773E6CC8, 0xD27FFEB6, 0xA68F9ADF, 0x03CE08A1, 0xE9E0C8D2, 0x4CA15AAC, 0x70A27D8A, 0xD5E3EFF4, 0x3FCD2F87, 0x9A8CBDF9, 0xEE7CD990, 0x4B3D4BEE, 0xA1138B9D, 0x045219E3, 0x48F3434F, 0xEDB2D131, 0x079C1142, 0xA2DD833C, 0xD62DE755, 0x736C752B, 0x9942B558, 0x3C032726, 0xE144FB14, 0x4405696A, 0xAE2BA919, 0x0B6A3B67, 0x7F9A5F0E, 0xDADBCD70, 0x30F50D03, 0x95B49F7D, 0xD915C5D1, 0x7C5457AF, 0x967A97DC, 0x333B05A2, 0x47CB61CB, 0xE28AF3B5, 0x08A433C6, 0xADE5A1B8, 0x91E6869E, 0x34A714E0, 0xDE89D493, 0x7BC846ED, 0x0F382284, 0xAA79B0FA, 0x40577089, 0xE516E2F7, 0xA9B7B85B, 0x0CF62A25, 0xE6D8EA56, 0x43997828, 0x37691C41, 0x92288E3F, 0x78064E4C, 0xDD47DC32, 0xC76580D9, 0x622412A7, 0x880AD2D4, 0x2D4B40AA, 0x59BB24C3, 0xFCFAB6BD, 0x16D476CE, 0xB395E4B0, 0xFF34BE1C, 0x5A752C62, 0xB05BEC11, 0x151A7E6F, 0x61EA1A06, 0xC4AB8878, 0x2E85480B, 0x8BC4DA75, 0xB7C7FD53, 0x12866F2D, 0xF8A8AF5E, 0x5DE93D20, 0x29195949, 0x8C58CB37, 0x66760B44, 0xC337993A, 0x8F96C396, 0x2AD751E8, 0xC0F9919B, 0x65B803E5, 0x1148678C, 0xB409F5F2, 0x5E273581, 0xFB66A7FF, 0x26217BCD, 0x8360E9B3, 0x694E29C0, 0xCC0FBBBE, 0xB8FFDFD7, 0x1DBE4DA9, 0xF7908DDA, 0x52D11FA4, 0x1E704508, 0xBB31D776, 0x511F1705, 0xF45E857B, 0x80AEE112, 0x25EF736C, 0xCFC1B31F, 0x6A802161, 0x56830647, 0xF3C29439, 0x19EC544A, 0xBCADC634, 0xC85DA25D, 0x6D1C3023, 0x8732F050, 0x2273622E, 0x6ED23882, 0xCB93AAFC, 0x21BD6A8F, 0x84FCF8F1, 0xF00C9C98, 0x554D0EE6, 0xBF63CE95, 0x1A225CEB, 0x8B277743, 0x2E66E53D, 0xC448254E, 0x6109B730, 0x15F9D359, 0xB0B84127, 0x5A968154, 0xFFD7132A, 0xB3764986, 0x1637DBF8, 0xFC191B8B, 0x595889F5, 0x2DA8ED9C, 0x88E97FE2, 0x62C7BF91, 0xC7862DEF, 0xFB850AC9, 0x5EC498B7, 0xB4EA58C4, 0x11ABCABA, 0x655BAED3, 0xC01A3CAD, 0x2A34FCDE, 0x8F756EA0, 0xC3D4340C, 0x6695A672, 0x8CBB6601, 0x29FAF47F, 0x5D0A9016, 0xF84B0268, 0x1265C21B, 0xB7245065, 0x6A638C57, 0xCF221E29, 0x250CDE5A, 0x804D4C24, 0xF4BD284D, 0x51FCBA33, 0xBBD27A40, 0x1E93E83E, 0x5232B292, 0xF77320EC, 0x1D5DE09F, 0xB81C72E1, 0xCCEC1688, 0x69AD84F6, 0x83834485, 0x26C2D6FB, 0x1AC1F1DD, 0xBF8063A3, 0x55AEA3D0, 0xF0EF31AE, 0x841F55C7, 0x215EC7B9, 0xCB7007CA, 0x6E3195B4, 0x2290CF18, 0x87D15D66, 0x6DFF9D15, 0xC8BE0F6B, 0xBC4E6B02, 0x190FF97C, 0xF321390F, 0x5660AB71, 0x4C42F79A, 0xE90365E4, 0x032DA597, 0xA66C37E9, 0xD29C5380, 0x77DDC1FE, 0x9DF3018D, 0x38B293F3, 0x7413C95F, 0xD1525B21, 0x3B7C9B52, 0x9E3D092C, 0xEACD6D45, 0x4F8CFF3B, 0xA5A23F48, 0x00E3AD36, 0x3CE08A10, 0x99A1186E, 0x738FD81D, 0xD6CE4A63, 0xA23E2E0A, 0x077FBC74, 0xED517C07, 0x4810EE79, 0x04B1B4D5, 0xA1F026AB, 0x4BDEE6D8, 0xEE9F74A6, 0x9A6F10CF, 0x3F2E82B1, 0xD50042C2, 0x7041D0BC, 0xAD060C8E, 0x08479EF0, 0xE2695E83, 0x4728CCFD, 0x33D8A894, 0x96993AEA, 0x7CB7FA99, 0xD9F668E7, 0x9557324B, 0x3016A035, 0xDA386046, 0x7F79F238, 0x0B899651, 0xAEC8042F, 0x44E6C45C, 0xE1A75622, 0xDDA47104, 0x78E5E37A, 0x92CB2309, 0x378AB177, 0x437AD51E, 0xE63B4760, 0x0C158713, 0xA954156D, 0xE5F54FC1, 0x40B4DDBF, 0xAA9A1DCC, 0x0FDB8FB2, 0x7B2BEBDB, 0xDE6A79A5, 0x3444B9D6, 0x91052BA8, /* T8_3 */ 0x00000000, 0xDD45AAB8, 0xBF672381, 0x62228939, 0x7B2231F3, 0xA6679B4B, 0xC4451272, 0x1900B8CA, 0xF64463E6, 0x2B01C95E, 0x49234067, 0x9466EADF, 0x8D665215, 0x5023F8AD, 0x32017194, 0xEF44DB2C, 0xE964B13D, 0x34211B85, 0x560392BC, 0x8B463804, 0x924680CE, 0x4F032A76, 0x2D21A34F, 0xF06409F7, 0x1F20D2DB, 0xC2657863, 0xA047F15A, 0x7D025BE2, 0x6402E328, 0xB9474990, 0xDB65C0A9, 0x06206A11, 0xD725148B, 0x0A60BE33, 0x6842370A, 0xB5079DB2, 0xAC072578, 0x71428FC0, 0x136006F9, 0xCE25AC41, 0x2161776D, 0xFC24DDD5, 0x9E0654EC, 0x4343FE54, 0x5A43469E, 0x8706EC26, 0xE524651F, 0x3861CFA7, 0x3E41A5B6, 0xE3040F0E, 0x81268637, 0x5C632C8F, 0x45639445, 0x98263EFD, 0xFA04B7C4, 0x27411D7C, 0xC805C650, 0x15406CE8, 0x7762E5D1, 0xAA274F69, 0xB327F7A3, 0x6E625D1B, 0x0C40D422, 0xD1057E9A, 0xABA65FE7, 0x76E3F55F, 0x14C17C66, 0xC984D6DE, 0xD0846E14, 0x0DC1C4AC, 0x6FE34D95, 0xB2A6E72D, 0x5DE23C01, 0x80A796B9, 0xE2851F80, 0x3FC0B538, 0x26C00DF2, 0xFB85A74A, 0x99A72E73, 0x44E284CB, 0x42C2EEDA, 0x9F874462, 0xFDA5CD5B, 0x20E067E3, 0x39E0DF29, 0xE4A57591, 0x8687FCA8, 0x5BC25610, 0xB4868D3C, 0x69C32784, 0x0BE1AEBD, 0xD6A40405, 0xCFA4BCCF, 0x12E11677, 0x70C39F4E, 0xAD8635F6, 0x7C834B6C, 0xA1C6E1D4, 0xC3E468ED, 0x1EA1C255, 0x07A17A9F, 0xDAE4D027, 0xB8C6591E, 0x6583F3A6, 0x8AC7288A, 0x57828232, 0x35A00B0B, 0xE8E5A1B3, 0xF1E51979, 0x2CA0B3C1, 0x4E823AF8, 0x93C79040, 0x95E7FA51, 0x48A250E9, 0x2A80D9D0, 0xF7C57368, 0xEEC5CBA2, 0x3380611A, 0x51A2E823, 0x8CE7429B, 0x63A399B7, 0xBEE6330F, 0xDCC4BA36, 0x0181108E, 0x1881A844, 0xC5C402FC, 0xA7E68BC5, 0x7AA3217D, 0x52A0C93F, 0x8FE56387, 0xEDC7EABE, 0x30824006, 0x2982F8CC, 0xF4C75274, 0x96E5DB4D, 0x4BA071F5, 0xA4E4AAD9, 0x79A10061, 0x1B838958, 0xC6C623E0, 0xDFC69B2A, 0x02833192, 0x60A1B8AB, 0xBDE41213, 0xBBC47802, 0x6681D2BA, 0x04A35B83, 0xD9E6F13B, 0xC0E649F1, 0x1DA3E349, 0x7F816A70, 0xA2C4C0C8, 0x4D801BE4, 0x90C5B15C, 0xF2E73865, 0x2FA292DD, 0x36A22A17, 0xEBE780AF, 0x89C50996, 0x5480A32E, 0x8585DDB4, 0x58C0770C, 0x3AE2FE35, 0xE7A7548D, 0xFEA7EC47, 0x23E246FF, 0x41C0CFC6, 0x9C85657E, 0x73C1BE52, 0xAE8414EA, 0xCCA69DD3, 0x11E3376B, 0x08E38FA1, 0xD5A62519, 0xB784AC20, 0x6AC10698, 0x6CE16C89, 0xB1A4C631, 0xD3864F08, 0x0EC3E5B0, 0x17C35D7A, 0xCA86F7C2, 0xA8A47EFB, 0x75E1D443, 0x9AA50F6F, 0x47E0A5D7, 0x25C22CEE, 0xF8878656, 0xE1873E9C, 0x3CC29424, 0x5EE01D1D, 0x83A5B7A5, 0xF90696D8, 0x24433C60, 0x4661B559, 0x9B241FE1, 0x8224A72B, 0x5F610D93, 0x3D4384AA, 0xE0062E12, 0x0F42F53E, 0xD2075F86, 0xB025D6BF, 0x6D607C07, 0x7460C4CD, 0xA9256E75, 0xCB07E74C, 0x16424DF4, 0x106227E5, 0xCD278D5D, 0xAF050464, 0x7240AEDC, 0x6B401616, 0xB605BCAE, 0xD4273597, 0x09629F2F, 0xE6264403, 0x3B63EEBB, 0x59416782, 0x8404CD3A, 0x9D0475F0, 0x4041DF48, 0x22635671, 0xFF26FCC9, 0x2E238253, 0xF36628EB, 0x9144A1D2, 0x4C010B6A, 0x5501B3A0, 0x88441918, 0xEA669021, 0x37233A99, 0xD867E1B5, 0x05224B0D, 0x6700C234, 0xBA45688C, 0xA345D046, 0x7E007AFE, 0x1C22F3C7, 0xC167597F, 0xC747336E, 0x1A0299D6, 0x782010EF, 0xA565BA57, 0xBC65029D, 0x6120A825, 0x0302211C, 0xDE478BA4, 0x31035088, 0xEC46FA30, 0x8E647309, 0x5321D9B1, 0x4A21617B, 0x9764CBC3, 0xF54642FA, 0x2803E842, /* T8_4 */ 0x00000000, 0x38116FAC, 0x7022DF58, 0x4833B0F4, 0xE045BEB0, 0xD854D11C, 0x906761E8, 0xA8760E44, 0xC5670B91, 0xFD76643D, 0xB545D4C9, 0x8D54BB65, 0x2522B521, 0x1D33DA8D, 0x55006A79, 0x6D1105D5, 0x8F2261D3, 0xB7330E7F, 0xFF00BE8B, 0xC711D127, 0x6F67DF63, 0x5776B0CF, 0x1F45003B, 0x27546F97, 0x4A456A42, 0x725405EE, 0x3A67B51A, 0x0276DAB6, 0xAA00D4F2, 0x9211BB5E, 0xDA220BAA, 0xE2336406, 0x1BA8B557, 0x23B9DAFB, 0x6B8A6A0F, 0x539B05A3, 0xFBED0BE7, 0xC3FC644B, 0x8BCFD4BF, 0xB3DEBB13, 0xDECFBEC6, 0xE6DED16A, 0xAEED619E, 0x96FC0E32, 0x3E8A0076, 0x069B6FDA, 0x4EA8DF2E, 0x76B9B082, 0x948AD484, 0xAC9BBB28, 0xE4A80BDC, 0xDCB96470, 0x74CF6A34, 0x4CDE0598, 0x04EDB56C, 0x3CFCDAC0, 0x51EDDF15, 0x69FCB0B9, 0x21CF004D, 0x19DE6FE1, 0xB1A861A5, 0x89B90E09, 0xC18ABEFD, 0xF99BD151, 0x37516AAE, 0x0F400502, 0x4773B5F6, 0x7F62DA5A, 0xD714D41E, 0xEF05BBB2, 0xA7360B46, 0x9F2764EA, 0xF236613F, 0xCA270E93, 0x8214BE67, 0xBA05D1CB, 0x1273DF8F, 0x2A62B023, 0x625100D7, 0x5A406F7B, 0xB8730B7D, 0x806264D1, 0xC851D425, 0xF040BB89, 0x5836B5CD, 0x6027DA61, 0x28146A95, 0x10050539, 0x7D1400EC, 0x45056F40, 0x0D36DFB4, 0x3527B018, 0x9D51BE5C, 0xA540D1F0, 0xED736104, 0xD5620EA8, 0x2CF9DFF9, 0x14E8B055, 0x5CDB00A1, 0x64CA6F0D, 0xCCBC6149, 0xF4AD0EE5, 0xBC9EBE11, 0x848FD1BD, 0xE99ED468, 0xD18FBBC4, 0x99BC0B30, 0xA1AD649C, 0x09DB6AD8, 0x31CA0574, 0x79F9B580, 0x41E8DA2C, 0xA3DBBE2A, 0x9BCAD186, 0xD3F96172, 0xEBE80EDE, 0x439E009A, 0x7B8F6F36, 0x33BCDFC2, 0x0BADB06E, 0x66BCB5BB, 0x5EADDA17, 0x169E6AE3, 0x2E8F054F, 0x86F90B0B, 0xBEE864A7, 0xF6DBD453, 0xCECABBFF, 0x6EA2D55C, 0x56B3BAF0, 0x1E800A04, 0x269165A8, 0x8EE76BEC, 0xB6F60440, 0xFEC5B4B4, 0xC6D4DB18, 0xABC5DECD, 0x93D4B161, 0xDBE70195, 0xE3F66E39, 0x4B80607D, 0x73910FD1, 0x3BA2BF25, 0x03B3D089, 0xE180B48F, 0xD991DB23, 0x91A26BD7, 0xA9B3047B, 0x01C50A3F, 0x39D46593, 0x71E7D567, 0x49F6BACB, 0x24E7BF1E, 0x1CF6D0B2, 0x54C56046, 0x6CD40FEA, 0xC4A201AE, 0xFCB36E02, 0xB480DEF6, 0x8C91B15A, 0x750A600B, 0x4D1B0FA7, 0x0528BF53, 0x3D39D0FF, 0x954FDEBB, 0xAD5EB117, 0xE56D01E3, 0xDD7C6E4F, 0xB06D6B9A, 0x887C0436, 0xC04FB4C2, 0xF85EDB6E, 0x5028D52A, 0x6839BA86, 0x200A0A72, 0x181B65DE, 0xFA2801D8, 0xC2396E74, 0x8A0ADE80, 0xB21BB12C, 0x1A6DBF68, 0x227CD0C4, 0x6A4F6030, 0x525E0F9C, 0x3F4F0A49, 0x075E65E5, 0x4F6DD511, 0x777CBABD, 0xDF0AB4F9, 0xE71BDB55, 0xAF286BA1, 0x9739040D, 0x59F3BFF2, 0x61E2D05E, 0x29D160AA, 0x11C00F06, 0xB9B60142, 0x81A76EEE, 0xC994DE1A, 0xF185B1B6, 0x9C94B463, 0xA485DBCF, 0xECB66B3B, 0xD4A70497, 0x7CD10AD3, 0x44C0657F, 0x0CF3D58B, 0x34E2BA27, 0xD6D1DE21, 0xEEC0B18D, 0xA6F30179, 0x9EE26ED5, 0x36946091, 0x0E850F3D, 0x46B6BFC9, 0x7EA7D065, 0x13B6D5B0, 0x2BA7BA1C, 0x63940AE8, 0x5B856544, 0xF3F36B00, 0xCBE204AC, 0x83D1B458, 0xBBC0DBF4, 0x425B0AA5, 0x7A4A6509, 0x3279D5FD, 0x0A68BA51, 0xA21EB415, 0x9A0FDBB9, 0xD23C6B4D, 0xEA2D04E1, 0x873C0134, 0xBF2D6E98, 0xF71EDE6C, 0xCF0FB1C0, 0x6779BF84, 0x5F68D028, 0x175B60DC, 0x2F4A0F70, 0xCD796B76, 0xF56804DA, 0xBD5BB42E, 0x854ADB82, 0x2D3CD5C6, 0x152DBA6A, 0x5D1E0A9E, 0x650F6532, 0x081E60E7, 0x300F0F4B, 0x783CBFBF, 0x402DD013, 0xE85BDE57, 0xD04AB1FB, 0x9879010F, 0xA0686EA3, /* T8_5 */ 0x00000000, 0xEF306B19, 0xDB8CA0C3, 0x34BCCBDA, 0xB2F53777, 0x5DC55C6E, 0x697997B4, 0x8649FCAD, 0x6006181F, 0x8F367306, 0xBB8AB8DC, 0x54BAD3C5, 0xD2F32F68, 0x3DC34471, 0x097F8FAB, 0xE64FE4B2, 0xC00C303E, 0x2F3C5B27, 0x1B8090FD, 0xF4B0FBE4, 0x72F90749, 0x9DC96C50, 0xA975A78A, 0x4645CC93, 0xA00A2821, 0x4F3A4338, 0x7B8688E2, 0x94B6E3FB, 0x12FF1F56, 0xFDCF744F, 0xC973BF95, 0x2643D48C, 0x85F4168D, 0x6AC47D94, 0x5E78B64E, 0xB148DD57, 0x370121FA, 0xD8314AE3, 0xEC8D8139, 0x03BDEA20, 0xE5F20E92, 0x0AC2658B, 0x3E7EAE51, 0xD14EC548, 0x570739E5, 0xB83752FC, 0x8C8B9926, 0x63BBF23F, 0x45F826B3, 0xAAC84DAA, 0x9E748670, 0x7144ED69, 0xF70D11C4, 0x183D7ADD, 0x2C81B107, 0xC3B1DA1E, 0x25FE3EAC, 0xCACE55B5, 0xFE729E6F, 0x1142F576, 0x970B09DB, 0x783B62C2, 0x4C87A918, 0xA3B7C201, 0x0E045BEB, 0xE13430F2, 0xD588FB28, 0x3AB89031, 0xBCF16C9C, 0x53C10785, 0x677DCC5F, 0x884DA746, 0x6E0243F4, 0x813228ED, 0xB58EE337, 0x5ABE882E, 0xDCF77483, 0x33C71F9A, 0x077BD440, 0xE84BBF59, 0xCE086BD5, 0x213800CC, 0x1584CB16, 0xFAB4A00F, 0x7CFD5CA2, 0x93CD37BB, 0xA771FC61, 0x48419778, 0xAE0E73CA, 0x413E18D3, 0x7582D309, 0x9AB2B810, 0x1CFB44BD, 0xF3CB2FA4, 0xC777E47E, 0x28478F67, 0x8BF04D66, 0x64C0267F, 0x507CEDA5, 0xBF4C86BC, 0x39057A11, 0xD6351108, 0xE289DAD2, 0x0DB9B1CB, 0xEBF65579, 0x04C63E60, 0x307AF5BA, 0xDF4A9EA3, 0x5903620E, 0xB6330917, 0x828FC2CD, 0x6DBFA9D4, 0x4BFC7D58, 0xA4CC1641, 0x9070DD9B, 0x7F40B682, 0xF9094A2F, 0x16392136, 0x2285EAEC, 0xCDB581F5, 0x2BFA6547, 0xC4CA0E5E, 0xF076C584, 0x1F46AE9D, 0x990F5230, 0x763F3929, 0x4283F2F3, 0xADB399EA, 0x1C08B7D6, 0xF338DCCF, 0xC7841715, 0x28B47C0C, 0xAEFD80A1, 0x41CDEBB8, 0x75712062, 0x9A414B7B, 0x7C0EAFC9, 0x933EC4D0, 0xA7820F0A, 0x48B26413, 0xCEFB98BE, 0x21CBF3A7, 0x1577387D, 0xFA475364, 0xDC0487E8, 0x3334ECF1, 0x0788272B, 0xE8B84C32, 0x6EF1B09F, 0x81C1DB86, 0xB57D105C, 0x5A4D7B45, 0xBC029FF7, 0x5332F4EE, 0x678E3F34, 0x88BE542D, 0x0EF7A880, 0xE1C7C399, 0xD57B0843, 0x3A4B635A, 0x99FCA15B, 0x76CCCA42, 0x42700198, 0xAD406A81, 0x2B09962C, 0xC439FD35, 0xF08536EF, 0x1FB55DF6, 0xF9FAB944, 0x16CAD25D, 0x22761987, 0xCD46729E, 0x4B0F8E33, 0xA43FE52A, 0x90832EF0, 0x7FB345E9, 0x59F09165, 0xB6C0FA7C, 0x827C31A6, 0x6D4C5ABF, 0xEB05A612, 0x0435CD0B, 0x308906D1, 0xDFB96DC8, 0x39F6897A, 0xD6C6E263, 0xE27A29B9, 0x0D4A42A0, 0x8B03BE0D, 0x6433D514, 0x508F1ECE, 0xBFBF75D7, 0x120CEC3D, 0xFD3C8724, 0xC9804CFE, 0x26B027E7, 0xA0F9DB4A, 0x4FC9B053, 0x7B757B89, 0x94451090, 0x720AF422, 0x9D3A9F3B, 0xA98654E1, 0x46B63FF8, 0xC0FFC355, 0x2FCFA84C, 0x1B736396, 0xF443088F, 0xD200DC03, 0x3D30B71A, 0x098C7CC0, 0xE6BC17D9, 0x60F5EB74, 0x8FC5806D, 0xBB794BB7, 0x544920AE, 0xB206C41C, 0x5D36AF05, 0x698A64DF, 0x86BA0FC6, 0x00F3F36B, 0xEFC39872, 0xDB7F53A8, 0x344F38B1, 0x97F8FAB0, 0x78C891A9, 0x4C745A73, 0xA344316A, 0x250DCDC7, 0xCA3DA6DE, 0xFE816D04, 0x11B1061D, 0xF7FEE2AF, 0x18CE89B6, 0x2C72426C, 0xC3422975, 0x450BD5D8, 0xAA3BBEC1, 0x9E87751B, 0x71B71E02, 0x57F4CA8E, 0xB8C4A197, 0x8C786A4D, 0x63480154, 0xE501FDF9, 0x0A3196E0, 0x3E8D5D3A, 0xD1BD3623, 0x37F2D291, 0xD8C2B988, 0xEC7E7252, 0x034E194B, 0x8507E5E6, 0x6A378EFF, 0x5E8B4525, 0xB1BB2E3C, /* T8_6 */ 0x00000000, 0x68032CC8, 0xD0065990, 0xB8057558, 0xA5E0C5D1, 0xCDE3E919, 0x75E69C41, 0x1DE5B089, 0x4E2DFD53, 0x262ED19B, 0x9E2BA4C3, 0xF628880B, 0xEBCD3882, 0x83CE144A, 0x3BCB6112, 0x53C84DDA, 0x9C5BFAA6, 0xF458D66E, 0x4C5DA336, 0x245E8FFE, 0x39BB3F77, 0x51B813BF, 0xE9BD66E7, 0x81BE4A2F, 0xD27607F5, 0xBA752B3D, 0x02705E65, 0x6A7372AD, 0x7796C224, 0x1F95EEEC, 0xA7909BB4, 0xCF93B77C, 0x3D5B83BD, 0x5558AF75, 0xED5DDA2D, 0x855EF6E5, 0x98BB466C, 0xF0B86AA4, 0x48BD1FFC, 0x20BE3334, 0x73767EEE, 0x1B755226, 0xA370277E, 0xCB730BB6, 0xD696BB3F, 0xBE9597F7, 0x0690E2AF, 0x6E93CE67, 0xA100791B, 0xC90355D3, 0x7106208B, 0x19050C43, 0x04E0BCCA, 0x6CE39002, 0xD4E6E55A, 0xBCE5C992, 0xEF2D8448, 0x872EA880, 0x3F2BDDD8, 0x5728F110, 0x4ACD4199, 0x22CE6D51, 0x9ACB1809, 0xF2C834C1, 0x7AB7077A, 0x12B42BB2, 0xAAB15EEA, 0xC2B27222, 0xDF57C2AB, 0xB754EE63, 0x0F519B3B, 0x6752B7F3, 0x349AFA29, 0x5C99D6E1, 0xE49CA3B9, 0x8C9F8F71, 0x917A3FF8, 0xF9791330, 0x417C6668, 0x297F4AA0, 0xE6ECFDDC, 0x8EEFD114, 0x36EAA44C, 0x5EE98884, 0x430C380D, 0x2B0F14C5, 0x930A619D, 0xFB094D55, 0xA8C1008F, 0xC0C22C47, 0x78C7591F, 0x10C475D7, 0x0D21C55E, 0x6522E996, 0xDD279CCE, 0xB524B006, 0x47EC84C7, 0x2FEFA80F, 0x97EADD57, 0xFFE9F19F, 0xE20C4116, 0x8A0F6DDE, 0x320A1886, 0x5A09344E, 0x09C17994, 0x61C2555C, 0xD9C72004, 0xB1C40CCC, 0xAC21BC45, 0xC422908D, 0x7C27E5D5, 0x1424C91D, 0xDBB77E61, 0xB3B452A9, 0x0BB127F1, 0x63B20B39, 0x7E57BBB0, 0x16549778, 0xAE51E220, 0xC652CEE8, 0x959A8332, 0xFD99AFFA, 0x459CDAA2, 0x2D9FF66A, 0x307A46E3, 0x58796A2B, 0xE07C1F73, 0x887F33BB, 0xF56E0EF4, 0x9D6D223C, 0x25685764, 0x4D6B7BAC, 0x508ECB25, 0x388DE7ED, 0x808892B5, 0xE88BBE7D, 0xBB43F3A7, 0xD340DF6F, 0x6B45AA37, 0x034686FF, 0x1EA33676, 0x76A01ABE, 0xCEA56FE6, 0xA6A6432E, 0x6935F452, 0x0136D89A, 0xB933ADC2, 0xD130810A, 0xCCD53183, 0xA4D61D4B, 0x1CD36813, 0x74D044DB, 0x27180901, 0x4F1B25C9, 0xF71E5091, 0x9F1D7C59, 0x82F8CCD0, 0xEAFBE018, 0x52FE9540, 0x3AFDB988, 0xC8358D49, 0xA036A181, 0x1833D4D9, 0x7030F811, 0x6DD54898, 0x05D66450, 0xBDD31108, 0xD5D03DC0, 0x8618701A, 0xEE1B5CD2, 0x561E298A, 0x3E1D0542, 0x23F8B5CB, 0x4BFB9903, 0xF3FEEC5B, 0x9BFDC093, 0x546E77EF, 0x3C6D5B27, 0x84682E7F, 0xEC6B02B7, 0xF18EB23E, 0x998D9EF6, 0x2188EBAE, 0x498BC766, 0x1A438ABC, 0x7240A674, 0xCA45D32C, 0xA246FFE4, 0xBFA34F6D, 0xD7A063A5, 0x6FA516FD, 0x07A63A35, 0x8FD9098E, 0xE7DA2546, 0x5FDF501E, 0x37DC7CD6, 0x2A39CC5F, 0x423AE097, 0xFA3F95CF, 0x923CB907, 0xC1F4F4DD, 0xA9F7D815, 0x11F2AD4D, 0x79F18185, 0x6414310C, 0x0C171DC4, 0xB412689C, 0xDC114454, 0x1382F328, 0x7B81DFE0, 0xC384AAB8, 0xAB878670, 0xB66236F9, 0xDE611A31, 0x66646F69, 0x0E6743A1, 0x5DAF0E7B, 0x35AC22B3, 0x8DA957EB, 0xE5AA7B23, 0xF84FCBAA, 0x904CE762, 0x2849923A, 0x404ABEF2, 0xB2828A33, 0xDA81A6FB, 0x6284D3A3, 0x0A87FF6B, 0x17624FE2, 0x7F61632A, 0xC7641672, 0xAF673ABA, 0xFCAF7760, 0x94AC5BA8, 0x2CA92EF0, 0x44AA0238, 0x594FB2B1, 0x314C9E79, 0x8949EB21, 0xE14AC7E9, 0x2ED97095, 0x46DA5C5D, 0xFEDF2905, 0x96DC05CD, 0x8B39B544, 0xE33A998C, 0x5B3FECD4, 0x333CC01C, 0x60F48DC6, 0x08F7A10E, 0xB0F2D456, 0xD8F1F89E, 0xC5144817, 0xAD1764DF, 0x15121187, 0x7D113D4F, /* T8_7 */ 0x00000000, 0x493C7D27, 0x9278FA4E, 0xDB448769, 0x211D826D, 0x6821FF4A, 0xB3657823, 0xFA590504, 0x423B04DA, 0x0B0779FD, 0xD043FE94, 0x997F83B3, 0x632686B7, 0x2A1AFB90, 0xF15E7CF9, 0xB86201DE, 0x847609B4, 0xCD4A7493, 0x160EF3FA, 0x5F328EDD, 0xA56B8BD9, 0xEC57F6FE, 0x37137197, 0x7E2F0CB0, 0xC64D0D6E, 0x8F717049, 0x5435F720, 0x1D098A07, 0xE7508F03, 0xAE6CF224, 0x7528754D, 0x3C14086A, 0x0D006599, 0x443C18BE, 0x9F789FD7, 0xD644E2F0, 0x2C1DE7F4, 0x65219AD3, 0xBE651DBA, 0xF759609D, 0x4F3B6143, 0x06071C64, 0xDD439B0D, 0x947FE62A, 0x6E26E32E, 0x271A9E09, 0xFC5E1960, 0xB5626447, 0x89766C2D, 0xC04A110A, 0x1B0E9663, 0x5232EB44, 0xA86BEE40, 0xE1579367, 0x3A13140E, 0x732F6929, 0xCB4D68F7, 0x827115D0, 0x593592B9, 0x1009EF9E, 0xEA50EA9A, 0xA36C97BD, 0x782810D4, 0x31146DF3, 0x1A00CB32, 0x533CB615, 0x8878317C, 0xC1444C5B, 0x3B1D495F, 0x72213478, 0xA965B311, 0xE059CE36, 0x583BCFE8, 0x1107B2CF, 0xCA4335A6, 0x837F4881, 0x79264D85, 0x301A30A2, 0xEB5EB7CB, 0xA262CAEC, 0x9E76C286, 0xD74ABFA1, 0x0C0E38C8, 0x453245EF, 0xBF6B40EB, 0xF6573DCC, 0x2D13BAA5, 0x642FC782, 0xDC4DC65C, 0x9571BB7B, 0x4E353C12, 0x07094135, 0xFD504431, 0xB46C3916, 0x6F28BE7F, 0x2614C358, 0x1700AEAB, 0x5E3CD38C, 0x857854E5, 0xCC4429C2, 0x361D2CC6, 0x7F2151E1, 0xA465D688, 0xED59ABAF, 0x553BAA71, 0x1C07D756, 0xC743503F, 0x8E7F2D18, 0x7426281C, 0x3D1A553B, 0xE65ED252, 0xAF62AF75, 0x9376A71F, 0xDA4ADA38, 0x010E5D51, 0x48322076, 0xB26B2572, 0xFB575855, 0x2013DF3C, 0x692FA21B, 0xD14DA3C5, 0x9871DEE2, 0x4335598B, 0x0A0924AC, 0xF05021A8, 0xB96C5C8F, 0x6228DBE6, 0x2B14A6C1, 0x34019664, 0x7D3DEB43, 0xA6796C2A, 0xEF45110D, 0x151C1409, 0x5C20692E, 0x8764EE47, 0xCE589360, 0x763A92BE, 0x3F06EF99, 0xE44268F0, 0xAD7E15D7, 0x572710D3, 0x1E1B6DF4, 0xC55FEA9D, 0x8C6397BA, 0xB0779FD0, 0xF94BE2F7, 0x220F659E, 0x6B3318B9, 0x916A1DBD, 0xD856609A, 0x0312E7F3, 0x4A2E9AD4, 0xF24C9B0A, 0xBB70E62D, 0x60346144, 0x29081C63, 0xD3511967, 0x9A6D6440, 0x4129E329, 0x08159E0E, 0x3901F3FD, 0x703D8EDA, 0xAB7909B3, 0xE2457494, 0x181C7190, 0x51200CB7, 0x8A648BDE, 0xC358F6F9, 0x7B3AF727, 0x32068A00, 0xE9420D69, 0xA07E704E, 0x5A27754A, 0x131B086D, 0xC85F8F04, 0x8163F223, 0xBD77FA49, 0xF44B876E, 0x2F0F0007, 0x66337D20, 0x9C6A7824, 0xD5560503, 0x0E12826A, 0x472EFF4D, 0xFF4CFE93, 0xB67083B4, 0x6D3404DD, 0x240879FA, 0xDE517CFE, 0x976D01D9, 0x4C2986B0, 0x0515FB97, 0x2E015D56, 0x673D2071, 0xBC79A718, 0xF545DA3F, 0x0F1CDF3B, 0x4620A21C, 0x9D642575, 0xD4585852, 0x6C3A598C, 0x250624AB, 0xFE42A3C2, 0xB77EDEE5, 0x4D27DBE1, 0x041BA6C6, 0xDF5F21AF, 0x96635C88, 0xAA7754E2, 0xE34B29C5, 0x380FAEAC, 0x7133D38B, 0x8B6AD68F, 0xC256ABA8, 0x19122CC1, 0x502E51E6, 0xE84C5038, 0xA1702D1F, 0x7A34AA76, 0x3308D751, 0xC951D255, 0x806DAF72, 0x5B29281B, 0x1215553C, 0x230138CF, 0x6A3D45E8, 0xB179C281, 0xF845BFA6, 0x021CBAA2, 0x4B20C785, 0x906440EC, 0xD9583DCB, 0x613A3C15, 0x28064132, 0xF342C65B, 0xBA7EBB7C, 0x4027BE78, 0x091BC35F, 0xD25F4436, 0x9B633911, 0xA777317B, 0xEE4B4C5C, 0x350FCB35, 0x7C33B612, 0x866AB316, 0xCF56CE31, 0x14124958, 0x5D2E347F, 0xE54C35A1, 0xAC704886, 0x7734CFEF, 0x3E08B2C8, 0xC451B7CC, 0x8D6DCAEB, 0x56294D82, 0x1F1530A5 }; /** * the current CRC value, bit-flipped */ private int crc; private SdkCrc32CChecksum() { reset(); } private SdkCrc32CChecksum(int crc) { this.crc = crc; } public static SdkCrc32CChecksum create() { return new SdkCrc32CChecksum(); } @Override public long getValue() { long ret = crc; return ~ret & 0xffffffffL; } @Override public void reset() { crc = 0xffffffff; } @Override public void update(byte[] b, int off, int len) { int localCrc = crc; while (len > 7) { int c0 = (b[off] ^ localCrc) & 0xff; localCrc >>>= 8; int c1 = (b[off + 1] ^ localCrc) & 0xff; localCrc >>>= 8; int c2 = (b[off + 2] ^ localCrc) & 0xff; localCrc >>>= 8; int c3 = (b[off + 3] ^ localCrc) & 0xff; localCrc = T[T8_7_START + c0] ^ T[T8_6_START + c1] ^ T[T8_5_START + c2] ^ T[T_8_4_START + c3]; int c4 = b[off + 4] & 0xff; int c5 = b[off + 5] & 0xff; int c6 = b[off + 6] & 0xff; int c7 = b[off + 7] & 0xff; localCrc ^= T[T8_3_START + c4] ^ T[T8_2_START + c5] ^ T[T8_1_START + c6] ^ T[T8_0_START + c7]; off += 8; len -= 8; } /* loop unroll - duff's device style */ for (int index = 0; index < len; index++) { localCrc = (localCrc >>> 8) ^ T[T8_0_START + ((localCrc ^ b[off]) & 0xff)]; off++; } // Publish crc out to object crc = localCrc; } @Override public void update(int b) { crc = (crc >>> 8) ^ T[T8_0_START + ((crc ^ b) & 0xff)]; } @Override public SdkCrc32CChecksum clone() { return new SdkCrc32CChecksum(crc); } }
2,342
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/Crc32CChecksum.java
/* * 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.signer.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(); } } private 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(); } @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) { if (checksum instanceof CRC32C) { return (Checksum) ((CRC32C) checksum).clone(); } if (checksum instanceof SdkCrc32CChecksum) { return (Checksum) ((SdkCrc32CChecksum) checksum).clone(); } throw new IllegalStateException("Unsupported checksum"); } }
2,343
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/checksums/Crc32Checksum.java
/* * 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.signer.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(); } } private 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(); } @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) { if (checksum instanceof CRC32) { return (Checksum) ((CRC32) checksum).clone(); } if (checksum instanceof SdkCrc32Checksum) { return (Checksum) ((SdkCrc32Checksum) checksum).clone(); } throw new IllegalStateException("Unsupported checksum"); } }
2,344
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkExtensionProvider.java
/* * 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.signer.chunkedencoding; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.Pair; /** * A functional interface for defining an extension of a chunk, where the extension is a key-value pair. * <p> * An extension usually depends on the chunk-data itself (checksum, signature, etc.), but is not required to. Per <a * href="https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.1">RFC-7230</a> The chunk-extension is defined as: * <pre> * chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] ) * chunk-ext-name = token * chunk-ext-val = token / quoted-string * </pre> */ @FunctionalInterface @SdkInternalApi public interface ChunkExtensionProvider extends Resettable { Pair<byte[], byte[]> get(byte[] chunk); }
2,345
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/SigV4TrailerProvider.java
/* * 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.signer.chunkedencoding; import static software.amazon.awssdk.http.auth.aws.internal.signer.V4CanonicalRequest.getCanonicalHeaders; import static software.amazon.awssdk.http.auth.aws.internal.signer.V4CanonicalRequest.getCanonicalHeadersString; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.hash; import static software.amazon.awssdk.utils.BinaryUtils.toHex; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.RollingSigner; import software.amazon.awssdk.utils.Pair; @SdkInternalApi public class SigV4TrailerProvider implements TrailerProvider { private final List<TrailerProvider> trailerProviders = new ArrayList<>(); private final RollingSigner signer; private final CredentialScope credentialScope; public SigV4TrailerProvider(List<TrailerProvider> trailerProviders, RollingSigner signer, CredentialScope credentialScope) { this.trailerProviders.addAll(trailerProviders); this.signer = signer; this.credentialScope = credentialScope; } @Override public void reset() { trailerProviders.forEach(TrailerProvider::reset); signer.reset(); } @Override public Pair<String, List<String>> get() { String trailerSig = signer.sign(this::getTrailersStringToSign); return Pair.of( "x-amz-trailer-signature", Collections.singletonList(trailerSig) ); } private String getTrailersStringToSign(String previousSignature) { // Get the headers by calling get() on each of the trailers Map<String, List<String>> headers = trailerProviders.stream().map(TrailerProvider::get).collect( Collectors.toMap( Pair::left, Pair::right ) ); String canonicalHeadersString = getCanonicalHeadersString(getCanonicalHeaders(headers)); String canonicalHashHex = toHex(hash(canonicalHeadersString)); // build the string-to-sign template for the rolling-signer to sign return String.join("\n", "AWS4-HMAC-SHA256-TRAILER", credentialScope.getDatetime(), credentialScope.scope(), previousSignature, canonicalHashHex ); } }
2,346
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/Resettable.java
/* * 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.signer.chunkedencoding; import software.amazon.awssdk.annotations.SdkInternalApi; @SdkInternalApi public interface Resettable { default void reset() { } }
2,347
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkInputStream.java
/* * 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.signer.chunkedencoding; import java.io.IOException; import java.io.InputStream; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.io.SdkLengthAwareInputStream; /** * A wrapped stream to represent a "chunk" of data */ @SdkInternalApi public final class ChunkInputStream extends SdkLengthAwareInputStream { public ChunkInputStream(InputStream inputStream, long length) { super(inputStream, length); } @Override public void close() throws IOException { // Drain this chunk on close, so the stream is left at the end of the chunk. long remaining = remaining(); if (remaining > 0) { if (skip(remaining) < remaining) { throw new IOException("Unable to drain stream for chunk. The underlying stream did not allow skipping the " + "whole chunk."); } } super.close(); } }
2,348
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChecksumTrailerProvider.java
/* * 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.signer.chunkedencoding; import java.util.Collections; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkChecksum; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Pair; @SdkInternalApi public class ChecksumTrailerProvider implements TrailerProvider { private final SdkChecksum checksum; private final String checksumName; public ChecksumTrailerProvider(SdkChecksum checksum, String checksumName) { this.checksum = checksum; this.checksumName = checksumName; } @Override public void reset() { checksum.reset(); } @Override public Pair<String, List<String>> get() { return Pair.of( checksumName, Collections.singletonList(BinaryUtils.toBase64(checksum.getChecksumBytes())) ); } }
2,349
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/SigV4ChunkExtensionProvider.java
/* * 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.signer.chunkedencoding; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.hash; import static software.amazon.awssdk.utils.BinaryUtils.toHex; import java.nio.charset.StandardCharsets; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.RollingSigner; import software.amazon.awssdk.utils.Pair; @SdkInternalApi public class SigV4ChunkExtensionProvider implements ChunkExtensionProvider { private static final String EMPTY_HASH = toHex(hash("")); private final RollingSigner signer; private final CredentialScope credentialScope; public SigV4ChunkExtensionProvider(RollingSigner signer, CredentialScope credentialScope) { this.signer = signer; this.credentialScope = credentialScope; } @Override public void reset() { signer.reset(); } private String getStringToSign(String previousSignature, byte[] chunk) { // build the string-to-sign template for the rolling-signer to sign return String.join("\n", "AWS4-HMAC-SHA256-PAYLOAD", credentialScope.getDatetime(), credentialScope.scope(), previousSignature, EMPTY_HASH, toHex(hash(chunk)) ); } @Override public Pair<byte[], byte[]> get(byte[] chunk) { String chunkSig = signer.sign(previousSig -> getStringToSign(previousSig, chunk)); return Pair.of( "chunk-signature".getBytes(StandardCharsets.UTF_8), chunkSig.getBytes(StandardCharsets.UTF_8) ); } }
2,350
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/Chunk.java
/* * 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.signer.chunkedencoding; import java.io.InputStream; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.SdkAutoCloseable; /** * An interface which defines a "chunk" of data. */ @SdkInternalApi public interface Chunk extends SdkAutoCloseable { /** * Get a default implementation of a chunk, which wraps a stream with a fixed size; */ static Chunk create(InputStream data, int sizeInBytes) { return new DefaultChunk(new ChunkInputStream(data, sizeInBytes)); } /** * Get the underlying stream of data for a chunk. */ InputStream stream(); /** * Whether the logical end of a chunk has been reached. */ boolean hasRemaining(); }
2,351
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkedEncodedInputStream.java
/* * 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.signer.chunkedencoding; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.Logger; import software.amazon.awssdk.utils.Pair; import software.amazon.awssdk.utils.Validate; /** * An implementation of chunk-transfer encoding, but by wrapping an {@link InputStream}. This implementation supports * chunk-headers, chunk-extensions, and trailers. * <p> * Per <a href="https://datatracker.ietf.org/doc/html/rfc7230#section-4.1">RFC-7230</a>, a chunk-transfer encoded message is * defined as: * <pre> * chunked-body = *chunk * last-chunk * trailer-part * CRLF * chunk = chunk-size [ chunk-ext ] CRLF * chunk-data CRLF * chunk-size = 1*HEXDIG * last-chunk = 1*("0") [ chunk-ext ] CRLF * chunk-data = 1*OCTET ; a sequence of chunk-size octets * </pre> */ @SdkInternalApi public final class ChunkedEncodedInputStream extends InputStream { private static final Logger LOG = Logger.loggerFor(ChunkedEncodedInputStream.class); private static final byte[] CRLF = {'\r', '\n'}; private static final byte[] END = {}; private final InputStream inputStream; private final int chunkSize; private final ChunkHeaderProvider header; private final List<ChunkExtensionProvider> extensions = new ArrayList<>(); private final List<TrailerProvider> trailers = new ArrayList<>(); private Chunk currentChunk; private boolean isFinished = false; private ChunkedEncodedInputStream(Builder builder) { this.inputStream = Validate.notNull(builder.inputStream, "Input-Stream cannot be null!"); this.chunkSize = Validate.isPositive(builder.chunkSize, "Chunk-size must be greater than 0!"); this.header = Validate.notNull(builder.header, "Header cannot be null!"); this.extensions.addAll(Validate.notNull(builder.extensions, "Extensions cannot be null!")); this.trailers.addAll(Validate.notNull(builder.trailers, "Trailers cannot be null!")); } public static Builder builder() { return new Builder(); } @Override public int read() throws IOException { if (currentChunk == null || !currentChunk.hasRemaining() && !isFinished) { currentChunk = getChunk(inputStream); } return currentChunk.stream().read(); } /** * Get an encoded chunk from the input-stream, or the final chunk if we've reached the end of the input-stream. */ private Chunk getChunk(InputStream stream) throws IOException { LOG.debug(() -> "Reading next chunk."); if (currentChunk != null) { currentChunk.close(); } // we *have* to read from the backing stream in order to figure out if it's the end or not // TODO(sra-identity-and-auth): We can likely optimize this by not copying the entire chunk of data into memory byte[] chunkData = new byte[chunkSize]; int read = read(stream, chunkData, chunkSize); if (read > 0) { // set the current chunk to the newly written chunk return getNextChunk(Arrays.copyOf(chunkData, read)); } LOG.debug(() -> "End of backing stream reached. Reading final chunk."); isFinished = true; // set the current chunk to the written final chunk return getFinalChunk(); } /** * Read from an input-stream, up to a max number of bytes, storing them in a byte-array. The actual number of bytes can be * less than the max in the event that we reach the end of the stream. * <p> * This method is necessary because we cannot assume the backing stream uses the default implementation of * {@code read(byte b[], int off, int len)} */ private int read(InputStream inputStream, byte[] buf, int maxBytesToRead) throws IOException { int read; int offset = 0; do { read = inputStream.read(buf, offset, maxBytesToRead - offset); assert read != 0; if (read > 0) { offset += read; } } while (read > 0 && offset < maxBytesToRead); return offset; } /** * Create a chunk from a byte-array, which includes the header, the extensions, and the chunk data. The input array should be * correctly sized, i.e. the number of bytes should equal its length. */ private Chunk getNextChunk(byte[] data) throws IOException { ByteArrayOutputStream chunkStream = new ByteArrayOutputStream(); writeChunk(data, chunkStream); chunkStream.write(CRLF); byte[] newChunkData = chunkStream.toByteArray(); return Chunk.create(new ByteArrayInputStream(newChunkData), newChunkData.length); } /** * Create the final chunk, which includes the header, the extensions, the chunk (if applicable), and the trailer */ private Chunk getFinalChunk() throws IOException { ByteArrayOutputStream chunkStream = new ByteArrayOutputStream(); writeChunk(END, chunkStream); writeTrailers(chunkStream); chunkStream.write(CRLF); byte[] newChunkData = chunkStream.toByteArray(); return Chunk.create(new ByteArrayInputStream(newChunkData), newChunkData.length); } private void writeChunk(byte[] chunk, ByteArrayOutputStream outputStream) throws IOException { writeHeader(chunk, outputStream); writeExtensions(chunk, outputStream); outputStream.write(CRLF); outputStream.write(chunk); } private void writeHeader(byte[] chunk, ByteArrayOutputStream outputStream) throws IOException { byte[] hdr = header.get(chunk); outputStream.write(hdr); } private void writeExtensions(byte[] chunk, ByteArrayOutputStream outputStream) throws IOException { for (ChunkExtensionProvider chunkExtensionProvider : extensions) { Pair<byte[], byte[]> ext = chunkExtensionProvider.get(chunk); outputStream.write((byte) ';'); outputStream.write(ext.left()); outputStream.write((byte) '='); outputStream.write(ext.right()); } } private void writeTrailers(ByteArrayOutputStream outputStream) throws IOException { for (TrailerProvider trailer : trailers) { Pair<String, List<String>> tlr = trailer.get(); outputStream.write(tlr.left().getBytes(StandardCharsets.UTF_8)); outputStream.write((byte) ':'); outputStream.write(String.join(",", tlr.right()).getBytes(StandardCharsets.UTF_8)); outputStream.write(CRLF); } } @Override public synchronized void reset() throws IOException { trailers.forEach(TrailerProvider::reset); extensions.forEach(ChunkExtensionProvider::reset); header.reset(); inputStream.reset(); isFinished = false; currentChunk = null; } @Override public void close() throws IOException { inputStream.close(); } public static class Builder { private final List<ChunkExtensionProvider> extensions = new ArrayList<>(); private final List<TrailerProvider> trailers = new ArrayList<>(); private InputStream inputStream; private int chunkSize; private ChunkHeaderProvider header = chunk -> Integer.toHexString(chunk.length).getBytes(StandardCharsets.UTF_8); public InputStream inputStream() { return this.inputStream; } public Builder inputStream(InputStream inputStream) { this.inputStream = inputStream; return this; } public Builder chunkSize(int chunkSize) { this.chunkSize = chunkSize; return this; } public Builder header(ChunkHeaderProvider header) { this.header = header; return this; } public Builder extensions(List<ChunkExtensionProvider> extensions) { this.extensions.clear(); extensions.forEach(this::addExtension); return this; } public Builder addExtension(ChunkExtensionProvider extension) { this.extensions.add(Validate.notNull(extension, "ExtensionProvider cannot be null!")); return this; } public List<TrailerProvider> trailers() { return new ArrayList<>(trailers); } public Builder trailers(List<TrailerProvider> trailers) { this.trailers.clear(); trailers.forEach(this::addTrailer); return this; } public Builder addTrailer(TrailerProvider trailer) { this.trailers.add(Validate.notNull(trailer, "TrailerProvider cannot be null!")); return this; } public ChunkedEncodedInputStream build() { return new ChunkedEncodedInputStream(this); } } }
2,352
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/DefaultChunk.java
/* * 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.signer.chunkedencoding; import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; import software.amazon.awssdk.annotations.SdkInternalApi; /** * An implementation of a chunk, backed by a {@link ChunkInputStream}. This allows it to have awareness of its length and * determine the endedness of the chunk. */ @SdkInternalApi final class DefaultChunk implements Chunk { private final ChunkInputStream data; DefaultChunk(ChunkInputStream data) { this.data = data; } @Override public boolean hasRemaining() { return data.remaining() > 0; } @Override public ChunkInputStream stream() { return data; } @Override public void close() { invokeSafely(data::close); } }
2,353
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/TrailerProvider.java
/* * 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.signer.chunkedencoding; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.utils.Pair; /** * A functional interface for defining a trailer, where the trailer is a header pair. * <p> * A trailer usually depends on the chunk-data itself (checksum, signature, etc.), but is not required to. Per <a * href="https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.2">RFC-7230</a>, the chunked trailer section is defined as: * <pre> * trailer-part = *( header-field CRLF ) * </pre> * An implementation of this interface is specifically an element of the {@code trailer-part}. Therefore, all occurrences of * {@code TrailerProvider}'s make up the {@code trailer-part}. */ @FunctionalInterface @SdkInternalApi public interface TrailerProvider extends Resettable { Pair<String, List<String>> get(); }
2,354
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/chunkedencoding/ChunkHeaderProvider.java
/* * 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.signer.chunkedencoding; import software.amazon.awssdk.annotations.SdkInternalApi; /** * A functional interface for defining a header of a chunk. * <p> * The header usually depends on the chunk-data itself (hex-size), but is not required to. In <a * href="https://datatracker.ietf.org/doc/html/rfc7230#section-4.1">RFC-7230</a>, the chunk-header is specifically the * {@code chunk-size}, but this interface can give us greater flexibility. */ @FunctionalInterface @SdkInternalApi public interface ChunkHeaderProvider extends Resettable { byte[] get(byte[] chunk); }
2,355
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/DefaultAwsCrtV4aHttpSigner.java
/* * 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.crt.internal.signer; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA256; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignedBodyValue.STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignedBodyValue.STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignedBodyValue.STREAMING_UNSIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSignedBodyValue.UNSIGNED_PAYLOAD; import static software.amazon.awssdk.crt.auth.signing.AwsSigningConfig.AwsSigningAlgorithm.SIGV4_ASYMMETRIC; import static software.amazon.awssdk.http.auth.aws.crt.internal.util.CrtHttpRequestConverter.toRequest; import static software.amazon.awssdk.http.auth.aws.crt.internal.util.CrtUtils.sanitizeRequest; import static software.amazon.awssdk.http.auth.aws.crt.internal.util.CrtUtils.toCredentials; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.checksumHeaderName; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.CredentialUtils.isAnonymous; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.CredentialUtils.sanitizeCredentials; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.PRESIGN_URL_MAX_EXPIRATION_DURATION; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_TRAILER; import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.concurrent.CompletableFuture; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.crt.auth.signing.AwsSigner; import software.amazon.awssdk.crt.auth.signing.AwsSigningConfig; import software.amazon.awssdk.crt.auth.signing.AwsSigningResult; import software.amazon.awssdk.crt.http.HttpRequest; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.signer.AwsV4aHttpSigner; import software.amazon.awssdk.http.auth.aws.signer.RegionSet; 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.BaseSignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.CompletableFutureUtils; /** * An implementation of a {@link AwsV4aHttpSigner} that uses properties to compose v4a-signers in order to delegate signing of a * request and payload (if applicable) accordingly. */ @SdkInternalApi public final class DefaultAwsCrtV4aHttpSigner implements AwsV4aHttpSigner { private static final int DEFAULT_CHUNK_SIZE_IN_BYTES = 128 * 1024; private static V4aProperties v4aProperties(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { Clock signingClock = request.requireProperty(SIGNING_CLOCK, Clock.systemUTC()); Instant signingInstant = signingClock.instant(); AwsCredentialsIdentity credentials = sanitizeCredentials(request.identity()); RegionSet regionSet = request.requireProperty(REGION_SET); String serviceSigningName = request.requireProperty(SERVICE_SIGNING_NAME); CredentialScope credentialScope = new CredentialScope(regionSet.asString(), serviceSigningName, signingInstant); boolean doubleUrlEncode = request.requireProperty(DOUBLE_URL_ENCODE, true); boolean normalizePath = request.requireProperty(NORMALIZE_PATH, true); return V4aProperties .builder() .credentials(credentials) .credentialScope(credentialScope) .signingClock(signingClock) .doubleUrlEncode(doubleUrlEncode) .normalizePath(normalizePath) .build(); } private static V4aPayloadSigner v4aPayloadSigner( BaseSignRequest<?, ? extends AwsCredentialsIdentity> request, V4aProperties v4aProperties) { boolean isPayloadSigning = isPayloadSigning(request); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent(); boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM); if (useChunkEncoding(isPayloadSigning, isChunkEncoding, isTrailing || isFlexible)) { return AwsChunkedV4aPayloadSigner.builder() .credentialScope(v4aProperties.getCredentialScope()) .chunkSize(DEFAULT_CHUNK_SIZE_IN_BYTES) .checksumAlgorithm(request.property(CHECKSUM_ALGORITHM)) .build(); } return V4aPayloadSigner.create(); } private static boolean useChunkEncoding(boolean payloadSigningEnabled, boolean chunkEncodingEnabled, boolean isTrailingOrFlexible) { return (payloadSigningEnabled && chunkEncodingEnabled) || (chunkEncodingEnabled && isTrailingOrFlexible); } private static Duration validateExpirationDuration(Duration expirationDuration) { if (expirationDuration.compareTo(PRESIGN_URL_MAX_EXPIRATION_DURATION) > 0) { throw new IllegalArgumentException( "Requests that are pre-signed by SigV4 algorithm are valid for at most 7" + " days. The expiration duration set on the current request [" + expirationDuration + "]" + " has exceeded this limit." ); } return expirationDuration; } private static AwsSigningConfig signingConfig( BaseSignRequest<?, ? extends AwsCredentialsIdentity> request, V4aProperties v4aProperties) { AuthLocation authLocation = request.requireProperty(AUTH_LOCATION, AuthLocation.HEADER); Duration expirationDuration = request.property(EXPIRATION_DURATION); boolean isPayloadSigning = isPayloadSigning(request); boolean isChunkEncoding = request.requireProperty(CHUNK_ENCODING_ENABLED, false); boolean isTrailing = request.request().firstMatchingHeader(X_AMZ_TRAILER).isPresent(); boolean isFlexible = request.hasProperty(CHECKSUM_ALGORITHM) && !hasChecksumHeader(request); AwsSigningConfig signingConfig = new AwsSigningConfig(); signingConfig.setCredentials(toCredentials(v4aProperties.getCredentials())); signingConfig.setService(v4aProperties.getCredentialScope().getService()); signingConfig.setRegion(v4aProperties.getCredentialScope().getRegion()); signingConfig.setAlgorithm(SIGV4_ASYMMETRIC); signingConfig.setTime(v4aProperties.getCredentialScope().getInstant().toEpochMilli()); signingConfig.setUseDoubleUriEncode(v4aProperties.shouldDoubleUrlEncode()); signingConfig.setShouldNormalizeUriPath(v4aProperties.shouldNormalizePath()); signingConfig.setSignedBodyHeader(X_AMZ_CONTENT_SHA256); switch (authLocation) { case HEADER: signingConfig.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS); if (request.hasProperty(EXPIRATION_DURATION)) { throw new UnsupportedOperationException( String.format("%s is not supported for %s.", EXPIRATION_DURATION, AuthLocation.HEADER) ); } break; case QUERY_STRING: signingConfig.setSignatureType(HTTP_REQUEST_VIA_QUERY_PARAMS); if (request.hasProperty(EXPIRATION_DURATION)) { signingConfig.setExpirationInSeconds(validateExpirationDuration(expirationDuration).getSeconds()); } break; default: throw new UnsupportedOperationException("Unknown auth-location: " + authLocation); } if (isPayloadSigning) { configurePayloadSigning(signingConfig, isChunkEncoding, isTrailing || isFlexible); } else { configureUnsignedPayload(signingConfig, isChunkEncoding, isTrailing || isFlexible); } return signingConfig; } private static boolean isPayloadSigning(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { boolean isAnonymous = isAnonymous(request.identity()); boolean isPayloadSigningEnabled = request.requireProperty(PAYLOAD_SIGNING_ENABLED, true); boolean isEncrypted = "https".equals(request.request().protocol()); return !isAnonymous && (isPayloadSigningEnabled || !isEncrypted); } private static void configureUnsignedPayload(AwsSigningConfig signingConfig, boolean isChunkEncoding, boolean isTrailingOrFlexible) { if (isChunkEncoding && isTrailingOrFlexible) { signingConfig.setSignedBodyValue(STREAMING_UNSIGNED_PAYLOAD_TRAILER); } else { signingConfig.setSignedBodyValue(UNSIGNED_PAYLOAD); } } private static void configurePayloadSigning(AwsSigningConfig signingConfig, boolean isChunkEncoding, boolean isTrailingOrFlexible) { if (isChunkEncoding) { if (isTrailingOrFlexible) { signingConfig.setSignedBodyValue(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER); } else { signingConfig.setSignedBodyValue(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD); } } // if it's non-streaming, crt should calcualte the SHA256 body-hash } private static boolean hasChecksumHeader(BaseSignRequest<?, ? extends AwsCredentialsIdentity> request) { ChecksumAlgorithm checksumAlgorithm = request.property(CHECKSUM_ALGORITHM); if (checksumAlgorithm != null) { String checksumHeaderName = checksumHeaderName(checksumAlgorithm); return request.request().firstMatchingHeader(checksumHeaderName).isPresent(); } return false; } private static SignedRequest doSign(SignRequest<? extends AwsCredentialsIdentity> request, AwsSigningConfig signingConfig, V4aPayloadSigner payloadSigner) { if (isAnonymous(request.identity())) { return SignedRequest.builder() .request(request.request()) .payload(request.payload().orElse(null)) .build(); } SdkHttpRequest.Builder requestBuilder = request.request().toBuilder(); payloadSigner.beforeSigning(requestBuilder, request.payload().orElse(null), signingConfig.getSignedBodyValue()); SdkHttpRequest sanitizedRequest = sanitizeRequest(requestBuilder.build()); HttpRequest crtRequest = toRequest(sanitizedRequest, request.payload().orElse(null)); V4aRequestSigningResult requestSigningResult = sign(requestBuilder.build(), crtRequest, signingConfig); ContentStreamProvider payload = payloadSigner.sign(request.payload().orElse(null), requestSigningResult); return SignedRequest.builder() .request(requestSigningResult.getSignedRequest().build()) .payload(payload) .build(); } private static V4aRequestSigningResult sign(SdkHttpRequest request, HttpRequest crtRequest, AwsSigningConfig signingConfig) { AwsSigningResult signingResult = CompletableFutureUtils.joinLikeSync(AwsSigner.sign(crtRequest, signingConfig)); return new V4aRequestSigningResult( toRequest(request, signingResult.getSignedRequest()).toBuilder(), signingResult.getSignature(), signingConfig); } @Override public SignedRequest sign(SignRequest<? extends AwsCredentialsIdentity> request) { V4aProperties v4aProperties = v4aProperties(request); AwsSigningConfig signingConfig = signingConfig(request, v4aProperties); V4aPayloadSigner payloadSigner = v4aPayloadSigner(request, v4aProperties); return doSign(request, signingConfig, payloadSigner); } @Override public CompletableFuture<AsyncSignedRequest> signAsync(AsyncSignRequest<? extends AwsCredentialsIdentity> request) { // There isn't currently a concept of async for crt signers throw new UnsupportedOperationException(); } }
2,356
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/AwsChunkedV4aPayloadSigner.java
/* * 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.crt.internal.signer; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.checksumHeaderName; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.ChecksumUtil.fromChecksumAlgorithm; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_ECDSA_SIGNED_PAYLOAD; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_ECDSA_SIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.STREAMING_UNSIGNED_PAYLOAD_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_TRAILER; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerUtils.moveContentLength; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.checksums.spi.ChecksumAlgorithm; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.checksums.SdkChecksum; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.ChecksumTrailerProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.ChunkedEncodedInputStream; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.TrailerProvider; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ChecksumInputStream; import software.amazon.awssdk.http.auth.aws.internal.signer.io.ResettableContentStreamProvider; import software.amazon.awssdk.utils.BinaryUtils; import software.amazon.awssdk.utils.Pair; import software.amazon.awssdk.utils.StringInputStream; import software.amazon.awssdk.utils.Validate; /** * An implementation of a V4aPayloadSigner which chunk-encodes a payload, optionally adding a chunk-signature chunk-extension, * and/or trailers representing trailing headers with their signature at the end. */ @SdkInternalApi public final class AwsChunkedV4aPayloadSigner implements V4aPayloadSigner { private final CredentialScope credentialScope; private final int chunkSize; private final ChecksumAlgorithm checksumAlgorithm; private final List<Pair<String, List<String>>> preExistingTrailers = new ArrayList<>(); private AwsChunkedV4aPayloadSigner(Builder builder) { this.credentialScope = Validate.paramNotNull(builder.credentialScope, "CredentialScope"); this.chunkSize = Validate.isPositive(builder.chunkSize, "ChunkSize"); this.checksumAlgorithm = builder.checksumAlgorithm; } public static Builder builder() { return new Builder(); } @Override public ContentStreamProvider sign(ContentStreamProvider payload, V4aRequestSigningResult requestSigningResult) { InputStream inputStream = payload != null ? payload.newStream() : new StringInputStream(""); ChunkedEncodedInputStream.Builder chunkedEncodedInputStreamBuilder = ChunkedEncodedInputStream .builder() .inputStream(inputStream) .chunkSize(chunkSize) .header(chunk -> Integer.toHexString(chunk.length).getBytes(StandardCharsets.UTF_8)); preExistingTrailers.forEach(trailer -> chunkedEncodedInputStreamBuilder.addTrailer(() -> trailer)); switch (requestSigningResult.getSigningConfig().getSignedBodyValue()) { case STREAMING_ECDSA_SIGNED_PAYLOAD: { RollingSigner rollingSigner = new RollingSigner(requestSigningResult.getSignature(), requestSigningResult.getSigningConfig()); chunkedEncodedInputStreamBuilder.addExtension(new SigV4aChunkExtensionProvider(rollingSigner, credentialScope)); break; } case STREAMING_UNSIGNED_PAYLOAD_TRAILER: setupChecksumTrailerIfNeeded(chunkedEncodedInputStreamBuilder); break; case STREAMING_ECDSA_SIGNED_PAYLOAD_TRAILER: { RollingSigner rollingSigner = new RollingSigner(requestSigningResult.getSignature(), requestSigningResult.getSigningConfig()); chunkedEncodedInputStreamBuilder.addExtension(new SigV4aChunkExtensionProvider(rollingSigner, credentialScope)); setupChecksumTrailerIfNeeded(chunkedEncodedInputStreamBuilder); chunkedEncodedInputStreamBuilder.addTrailer( new SigV4aTrailerProvider(chunkedEncodedInputStreamBuilder.trailers(), rollingSigner, credentialScope) ); break; } default: throw new UnsupportedOperationException(); } return new ResettableContentStreamProvider(chunkedEncodedInputStreamBuilder::build); } @Override public void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider payload, String checksum) { long encodedContentLength = 0; long contentLength = moveContentLength(request, payload != null ? payload.newStream() : new StringInputStream("")); setupPreExistingTrailers(request); // pre-existing trailers encodedContentLength += calculateExistingTrailersLength(); switch (checksum) { case STREAMING_ECDSA_SIGNED_PAYLOAD: { long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 144 bytes> encodedContentLength += calculateChunksLength(contentLength, extensionsLength); break; } case STREAMING_UNSIGNED_PAYLOAD_TRAILER: if (checksumAlgorithm != null) { encodedContentLength += calculateChecksumTrailerLength(checksumHeaderName(checksumAlgorithm)); } encodedContentLength += calculateChunksLength(contentLength, 0); break; case STREAMING_ECDSA_SIGNED_PAYLOAD_TRAILER: { long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 144 bytes> encodedContentLength += calculateChunksLength(contentLength, extensionsLength); if (checksumAlgorithm != null) { encodedContentLength += calculateChecksumTrailerLength(checksumHeaderName(checksumAlgorithm)); } encodedContentLength += 170; // x-amz-trailer-signature:<sigv4a-ecsda hex signature, 144 bytes>\r\n break; } default: throw new UnsupportedOperationException(); } // terminating \r\n encodedContentLength += 2; if (checksumAlgorithm != null) { String checksumHeaderName = checksumHeaderName(checksumAlgorithm); request.appendHeader(X_AMZ_TRAILER, checksumHeaderName); } request.putHeader(Header.CONTENT_LENGTH, Long.toString(encodedContentLength)); // CRT-signed request doesn't expect 'aws-chunked' Content-Encoding, so we don't add it } /** * Set up a map of pre-existing trailer (headers) for the given request to be used when chunk-encoding the payload. * <p> * However, we need to validate that these are valid trailers. Since aws-chunked encoding adds the checksum as a trailer, it * isn't part of the request headers, but other trailers MUST be present in the request-headers. */ private void setupPreExistingTrailers(SdkHttpRequest.Builder request) { for (String header : request.matchingHeaders(X_AMZ_TRAILER)) { List<String> values = request.matchingHeaders(header); if (values.isEmpty()) { throw new IllegalArgumentException(header + " must be present in the request headers to be a valid trailer."); } preExistingTrailers.add(Pair.of(header, values)); request.removeHeader(header); } } private long calculateChunksLength(long contentLength, long extensionsLength) { long lengthInBytes = 0; long chunkHeaderLength = Integer.toHexString(chunkSize).length(); long numChunks = contentLength / chunkSize; // normal chunks // x<metadata>\r\n<data>\r\n lengthInBytes += numChunks * (chunkHeaderLength + extensionsLength + 2 + chunkSize + 2); // remaining chunk // x<metadata>\r\n<data>\r\n long remainingBytes = contentLength % chunkSize; if (remainingBytes > 0) { long remainingChunkHeaderLength = Long.toHexString(remainingBytes).length(); lengthInBytes += remainingChunkHeaderLength + extensionsLength + 2 + remainingBytes + 2; } // final chunk // 0<metadata>\r\n lengthInBytes += 1 + extensionsLength + 2; return lengthInBytes; } private long calculateExistingTrailersLength() { long lengthInBytes = 0; for (Pair<String, List<String>> trailer : preExistingTrailers) { // size of trailer lengthInBytes += calculateTrailerLength(trailer); } return lengthInBytes; } private long calculateTrailerLength(Pair<String, List<String>> trailer) { // size of trailer-header and colon long lengthInBytes = trailer.left().length() + 1L; // size of trailer-values for (String value : trailer.right()) { lengthInBytes += value.length(); } // size of commas between trailer-values, 1 less comma than # of values lengthInBytes += trailer.right().size() - 1; // terminating \r\n return lengthInBytes + 2; } private long calculateChecksumTrailerLength(String checksumHeaderName) { // size of checksum trailer-header and colon long lengthInBytes = checksumHeaderName.length() + 1L; // get the base checksum for the algorithm SdkChecksum sdkChecksum = fromChecksumAlgorithm(checksumAlgorithm); // size of checksum value as encoded-string lengthInBytes += BinaryUtils.toBase64(sdkChecksum.getChecksumBytes()).length(); // terminating \r\n return lengthInBytes + 2; } /** * Add the checksum as a trailer to the chunk-encoded stream. * <p> * If the checksum-algorithm is not present, then nothing is done. */ private void setupChecksumTrailerIfNeeded(ChunkedEncodedInputStream.Builder builder) { if (checksumAlgorithm == null) { return; } String checksumHeaderName = checksumHeaderName(checksumAlgorithm); SdkChecksum sdkChecksum = fromChecksumAlgorithm(checksumAlgorithm); ChecksumInputStream checksumInputStream = new ChecksumInputStream( builder.inputStream(), Collections.singleton(sdkChecksum) ); TrailerProvider checksumTrailer = new ChecksumTrailerProvider(sdkChecksum, checksumHeaderName); builder.inputStream(checksumInputStream).addTrailer(checksumTrailer); } static final class Builder { private CredentialScope credentialScope; private Integer chunkSize; private ChecksumAlgorithm checksumAlgorithm; public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = credentialScope; return this; } public Builder chunkSize(Integer chunkSize) { this.chunkSize = chunkSize; return this; } public Builder checksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; } public AwsChunkedV4aPayloadSigner build() { return new AwsChunkedV4aPayloadSigner(this); } } }
2,357
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/V4aPayloadSigner.java
/* * 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.crt.internal.signer; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; /** * An interface for defining how to sign a payload via SigV4a. */ @SdkInternalApi public interface V4aPayloadSigner { /** * Get a default implementation of a SigV4a payload signer. */ static V4aPayloadSigner create() { return new DefaultV4aPayloadSigner(); } /** * Given a payload and result of request signing, sign the payload via the SigV4a process. */ ContentStreamProvider sign(ContentStreamProvider payload, V4aRequestSigningResult requestSigningResult); /** * Modify a request before it is signed, such as changing headers or query-parameters. */ default void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider payload, String checksum) { } }
2,358
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/SigV4aTrailerProvider.java
/* * 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.crt.internal.signer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.TrailerProvider; import software.amazon.awssdk.utils.Pair; @SdkInternalApi public class SigV4aTrailerProvider implements TrailerProvider { private final List<TrailerProvider> trailerProviders = new ArrayList<>(); private final RollingSigner signer; private final CredentialScope credentialScope; public SigV4aTrailerProvider(List<TrailerProvider> trailerProviders, RollingSigner signer, CredentialScope credentialScope) { this.trailerProviders.addAll(trailerProviders); this.signer = signer; this.credentialScope = credentialScope; } @Override public void reset() { trailerProviders.forEach(TrailerProvider::reset); signer.reset(); } @Override public Pair<String, List<String>> get() { byte[] trailerSig = signer.sign(getTrailers()); return Pair.of( "x-amz-trailer-signature", Collections.singletonList(new String(trailerSig, StandardCharsets.UTF_8)) ); } private Map<String, List<String>> getTrailers() { // Get the headers by calling get() on each of the trailers return trailerProviders.stream().map(TrailerProvider::get).collect(Collectors.toMap(Pair::left, Pair::right)); } }
2,359
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/RollingSigner.java
/* * 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.crt.internal.signer; import java.io.ByteArrayInputStream; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.crt.auth.signing.AwsSigner; import software.amazon.awssdk.crt.auth.signing.AwsSigningConfig; import software.amazon.awssdk.crt.auth.signing.AwsSigningResult; import software.amazon.awssdk.crt.http.HttpHeader; import software.amazon.awssdk.crt.http.HttpRequestBodyStream; import software.amazon.awssdk.http.auth.aws.crt.internal.io.CrtInputStream; import software.amazon.awssdk.utils.CompletableFutureUtils; /** * A class which calculates a rolling signature of arbitrary data using HMAC-SHA256. Each time a signature is calculated, the * prior calculation is incorporated, hence "rolling". */ @SdkInternalApi public final class RollingSigner { private final byte[] seedSignature; private final AwsSigningConfig signingConfig; private byte[] previousSignature; public RollingSigner(byte[] seedSignature, AwsSigningConfig signingConfig) { this.seedSignature = seedSignature.clone(); this.previousSignature = seedSignature.clone(); this.signingConfig = signingConfig; } private static byte[] signChunk(byte[] chunkBody, byte[] previousSignature, AwsSigningConfig signingConfig) { // All the config remains the same as signing config except the Signature Type. AwsSigningConfig configCopy = signingConfig.clone(); configCopy.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_CHUNK); configCopy.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.NONE); configCopy.setSignedBodyValue(null); HttpRequestBodyStream crtBody = new CrtInputStream(() -> new ByteArrayInputStream(chunkBody)); return CompletableFutureUtils.joinLikeSync(AwsSigner.signChunk(crtBody, previousSignature, configCopy)); } private static AwsSigningResult signTrailerHeaders(Map<String, List<String>> headerMap, byte[] previousSignature, AwsSigningConfig signingConfig) { List<HttpHeader> httpHeaderList = headerMap.entrySet().stream().map(entry -> new HttpHeader( entry.getKey(), String.join(",", entry.getValue()))).collect(Collectors.toList()); // All the config remains the same as signing config except the Signature Type. AwsSigningConfig configCopy = signingConfig.clone(); configCopy.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_TRAILING_HEADERS); configCopy.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.NONE); configCopy.setSignedBodyValue(null); return CompletableFutureUtils.joinLikeSync(AwsSigner.sign(httpHeaderList, previousSignature, configCopy)); } /** * Using a template that incorporates the previous calculated signature, sign the string and return it. */ public byte[] sign(byte[] chunkBody) { previousSignature = signChunk(chunkBody, previousSignature, signingConfig); return previousSignature; } public byte[] sign(Map<String, List<String>> headerMap) { AwsSigningResult result = signTrailerHeaders(headerMap, previousSignature, signingConfig); previousSignature = result != null ? result.getSignature() : new byte[0]; return previousSignature; } public void reset() { previousSignature = seedSignature; } }
2,360
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/SigV4aChunkExtensionProvider.java
/* * 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.crt.internal.signer; import java.nio.charset.StandardCharsets; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.aws.internal.signer.chunkedencoding.ChunkExtensionProvider; import software.amazon.awssdk.utils.Pair; @SdkInternalApi public class SigV4aChunkExtensionProvider implements ChunkExtensionProvider { private final RollingSigner signer; private final CredentialScope credentialScope; public SigV4aChunkExtensionProvider(RollingSigner signer, CredentialScope credentialScope) { this.signer = signer; this.credentialScope = credentialScope; } @Override public void reset() { signer.reset(); } @Override public Pair<byte[], byte[]> get(byte[] chunk) { byte[] chunkSig = signer.sign(chunk); return Pair.of( "chunk-signature".getBytes(StandardCharsets.UTF_8), chunkSig ); } }
2,361
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/V4aProperties.java
/* * 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.crt.internal.signer; import java.time.Clock; import software.amazon.awssdk.annotations.Immutable; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.auth.aws.internal.signer.CredentialScope; import software.amazon.awssdk.http.auth.spi.signer.BaseSignRequest; import software.amazon.awssdk.http.auth.spi.signer.SignerProperty; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.utils.Validate; /** * A class which contains "properties" relevant to SigV4a. These properties can be derived {@link SignerProperty}'s on a * {@link BaseSignRequest}. */ @SdkInternalApi @Immutable public final class V4aProperties { private final AwsCredentialsIdentity credentials; private final CredentialScope credentialScope; private final Clock signingClock; private final boolean doubleUrlEncode; private final boolean normalizePath; private V4aProperties(Builder builder) { this.credentials = Validate.paramNotNull(builder.credentials, "Credentials"); this.credentialScope = Validate.paramNotNull(builder.credentialScope, "CredentialScope"); this.signingClock = Validate.paramNotNull(builder.signingClock, "SigningClock"); this.doubleUrlEncode = Validate.getOrDefault(builder.doubleUrlEncode, () -> true); this.normalizePath = Validate.getOrDefault(builder.normalizePath, () -> true); } public static Builder builder() { return new Builder(); } public AwsCredentialsIdentity getCredentials() { return credentials; } public CredentialScope getCredentialScope() { return credentialScope; } public Clock getSigningClock() { return signingClock; } public boolean shouldDoubleUrlEncode() { return doubleUrlEncode; } public boolean shouldNormalizePath() { return normalizePath; } public static class Builder { private AwsCredentialsIdentity credentials; private CredentialScope credentialScope; private Clock signingClock; private Boolean doubleUrlEncode; private Boolean normalizePath; public Builder credentials(AwsCredentialsIdentity credentials) { this.credentials = Validate.paramNotNull(credentials, "Credentials"); return this; } public Builder credentialScope(CredentialScope credentialScope) { this.credentialScope = Validate.paramNotNull(credentialScope, "CredentialScope"); return this; } public Builder signingClock(Clock signingClock) { this.signingClock = signingClock; return this; } public Builder doubleUrlEncode(Boolean doubleUrlEncode) { this.doubleUrlEncode = doubleUrlEncode; return this; } public Builder normalizePath(Boolean normalizePath) { this.normalizePath = normalizePath; return this; } public V4aProperties build() { return new V4aProperties(this); } } }
2,362
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/V4aRequestSigningResult.java
/* * 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.crt.internal.signer; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.crt.auth.signing.AwsSigningConfig; import software.amazon.awssdk.http.SdkHttpRequest; /** * A container for data produced during and as a result of the SigV4a request signing with CRT. */ @SdkInternalApi public final class V4aRequestSigningResult { private final SdkHttpRequest.Builder signedRequest; private final byte[] signature; private final AwsSigningConfig signingConfig; public V4aRequestSigningResult(SdkHttpRequest.Builder signedRequest, byte[] signature, AwsSigningConfig signingConfig) { this.signedRequest = signedRequest; this.signature = signature.clone(); this.signingConfig = signingConfig; } public SdkHttpRequest.Builder getSignedRequest() { return signedRequest; } public byte[] getSignature() { return signature; } public AwsSigningConfig getSigningConfig() { return signingConfig; } }
2,363
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/DefaultV4aPayloadSigner.java
/* * 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.crt.internal.signer; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.http.ContentStreamProvider; /** * A default implementation of a payload signer that is a no-op. */ @SdkInternalApi public class DefaultV4aPayloadSigner implements V4aPayloadSigner { @Override public ContentStreamProvider sign(ContentStreamProvider payload, V4aRequestSigningResult requestSigningResult) { return payload; } }
2,364
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/util/CrtUtils.java
/* * 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.crt.internal.util; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.AUTHORIZATION; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.HOST; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_ALGORITHM; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_CREDENTIAL; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_DATE; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_EXPIRES; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_SIGNATURE; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_SIGNED_HEADERS; import java.nio.charset.StandardCharsets; import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.crt.auth.credentials.Credentials; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; import software.amazon.awssdk.identity.spi.AwsSessionCredentialsIdentity; import software.amazon.awssdk.utils.http.SdkHttpUtils; @SdkInternalApi public final class CrtUtils { private static final String BODY_HASH_NAME = "x-amz-content-sha256"; private static final String REGION_SET_NAME = "X-amz-region-set"; private static final Set<String> FORBIDDEN_HEADERS = Stream.of(BODY_HASH_NAME, X_AMZ_DATE, AUTHORIZATION, REGION_SET_NAME) .collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))); private static final Set<String> FORBIDDEN_PARAMS = Stream.of(X_AMZ_SIGNATURE, X_AMZ_DATE, X_AMZ_CREDENTIAL, X_AMZ_ALGORITHM, X_AMZ_SIGNED_HEADERS, REGION_SET_NAME, X_AMZ_EXPIRES) .collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))); private CrtUtils() { } /** * Sanitize an {@link SdkHttpRequest}, in order to prepare it for converting to a CRT request destined to be signed. * <p> * Sanitizing includes checking the path is not empty, filtering headers and query parameters that are forbidden in CRT, and * adding the host header (overriding if already presesnt). */ public static SdkHttpRequest sanitizeRequest(SdkHttpRequest request) { SdkHttpRequest.Builder builder = request.toBuilder(); // Ensure path is non-empty String path = builder.encodedPath(); if (path == null || path.isEmpty()) { builder.encodedPath("/"); } builder.clearHeaders(); // Filter headers that will cause signing to fail request.forEachHeader((name, value) -> { if (!FORBIDDEN_HEADERS.contains(name)) { builder.putHeader(name, value); } }); // Add host, which must be signed. We ignore any pre-existing Host header to match the behavior of the SigV4 signer. String hostHeader = SdkHttpUtils.isUsingStandardPort(request.protocol(), request.port()) ? request.host() : request.host() + ":" + request.port(); builder.putHeader(HOST, hostHeader); builder.clearQueryParameters(); // Filter query parameters that will cause signing to fail request.forEachRawQueryParameter((key, value) -> { if (!FORBIDDEN_PARAMS.contains(key)) { builder.putRawQueryParameter(key, value); } }); return builder.build(); } /** * Convert an {@link AwsCredentialsIdentity} to the CRT equivalent of credentials ({@link Credentials}). */ public static Credentials toCredentials(AwsCredentialsIdentity credentialsIdentity) { byte[] sessionToken = null; if (credentialsIdentity == null || credentialsIdentity.accessKeyId() == null || credentialsIdentity.secretAccessKey() == null ) { return null; } if (credentialsIdentity instanceof AwsSessionCredentialsIdentity) { sessionToken = ((AwsSessionCredentialsIdentity) credentialsIdentity) .sessionToken() .getBytes(StandardCharsets.UTF_8); } return new Credentials( credentialsIdentity.accessKeyId().getBytes(StandardCharsets.UTF_8), credentialsIdentity.secretAccessKey().getBytes(StandardCharsets.UTF_8), sessionToken ); } }
2,365
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/util/CrtHttpRequestConverter.java
/* * 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.crt.internal.util; import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.HOST; import java.io.ByteArrayInputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.crt.http.HttpHeader; import software.amazon.awssdk.crt.http.HttpRequest; import software.amazon.awssdk.crt.http.HttpRequestBodyStream; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.auth.aws.crt.internal.io.CrtInputStream; import software.amazon.awssdk.utils.StringUtils; import software.amazon.awssdk.utils.http.SdkHttpUtils; @SdkInternalApi public final class CrtHttpRequestConverter { private CrtHttpRequestConverter() { } /** * Convert an {@link SdkHttpRequest} to an {@link HttpRequest}. */ public static HttpRequest toRequest(SdkHttpRequest request, ContentStreamProvider payload) { String method = request.method().name(); String encodedPath = encodedPathToCrtFormat(request.encodedPath()); String encodedQueryString = request.encodedQueryParameters().map(value -> "?" + value).orElse(""); HttpHeader[] crtHeaderArray = createHttpHeaderArray(request); HttpRequestBodyStream crtInputStream = null; if (payload != null) { crtInputStream = new CrtInputStream(payload); } return new HttpRequest(method, encodedPath + encodedQueryString, crtHeaderArray, crtInputStream); } /** * Convert an {@link HttpRequest} to an {@link SdkHttpRequest}. */ public static SdkHttpRequest toRequest(SdkHttpRequest request, HttpRequest crtRequest) { SdkHttpRequest.Builder builder = request.toBuilder(); builder.clearHeaders(); for (HttpHeader header : crtRequest.getHeaders()) { builder.appendHeader(header.getName(), header.getValue()); } URI fullUri; try { String portString = SdkHttpUtils.isUsingStandardPort(builder.protocol(), builder.port()) ? "" : ":" + builder.port(); String encodedPath = encodedPathFromCrtFormat(request.encodedPath(), crtRequest.getEncodedPath()); String fullUriString = builder.protocol() + "://" + builder.host() + portString + encodedPath; fullUri = new URI(fullUriString); } catch (URISyntaxException e) { throw new RuntimeException("Full URI could not be formed.", e); } builder.encodedPath(fullUri.getRawPath()); String remainingQuery = fullUri.getQuery(); builder.clearQueryParameters(); while (remainingQuery != null && !remainingQuery.isEmpty()) { int nextQuery = remainingQuery.indexOf('&'); int nextAssign = remainingQuery.indexOf('='); if (nextAssign < nextQuery || (nextAssign >= 0 && nextQuery < 0)) { String queryName = remainingQuery.substring(0, nextAssign); String queryValue = remainingQuery.substring(nextAssign + 1); if (nextQuery >= 0) { queryValue = remainingQuery.substring(nextAssign + 1, nextQuery); } builder.appendRawQueryParameter(queryName, queryValue); } else { String queryName = remainingQuery; if (nextQuery >= 0) { queryName = remainingQuery.substring(0, nextQuery); } builder.appendRawQueryParameter(queryName, null); } if (nextQuery >= 0) { remainingQuery = remainingQuery.substring(nextQuery + 1); } else { break; } } return builder.build(); } private static HttpHeader[] createHttpHeaderArray(SdkHttpRequest request) { List<HttpHeader> crtHeaderList = new ArrayList<>(request.numHeaders() + 2); // Set Host Header if needed if (!request.firstMatchingHeader(HOST).isPresent()) { crtHeaderList.add(new HttpHeader(HOST, request.host())); } // Add the rest of the Headers request.forEachHeader((name, values) -> { for (String val : values) { HttpHeader h = new HttpHeader(name, val); crtHeaderList.add(h); } }); return crtHeaderList.toArray(new HttpHeader[0]); } private static String encodedPathToCrtFormat(String sdkEncodedPath) { if (StringUtils.isEmpty(sdkEncodedPath)) { return "/"; } return sdkEncodedPath; } private static String encodedPathFromCrtFormat(String sdkEncodedPath, String crtEncodedPath) { if ("/".equals(crtEncodedPath) && StringUtils.isEmpty(sdkEncodedPath)) { return ""; } return crtEncodedPath; } public static HttpRequestBodyStream toCrtStream(byte[] data) { return new CrtInputStream(() -> new ByteArrayInputStream(data)); } }
2,366
0
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal
Create_ds/aws-sdk-java-v2/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/io/CrtInputStream.java
/* * 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.crt.internal.io; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.crt.http.HttpRequestBodyStream; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.utils.FunctionalUtils; @SdkInternalApi public final class CrtInputStream implements HttpRequestBodyStream { private static final int READ_BUFFER_SIZE = 4096; private final ContentStreamProvider provider; private final int bufSize; private final byte[] readBuffer; private InputStream providerStream; public CrtInputStream(ContentStreamProvider provider) { this.provider = provider; this.bufSize = READ_BUFFER_SIZE; this.readBuffer = new byte[bufSize]; } @Override public boolean sendRequestBody(ByteBuffer bodyBytesOut) { int read; if (providerStream == null) { FunctionalUtils.invokeSafely(this::createNewStream); } int toRead = Math.min(bufSize, bodyBytesOut.remaining()); read = FunctionalUtils.invokeSafely(() -> providerStream.read(readBuffer, 0, toRead)); if (read > 0) { bodyBytesOut.put(readBuffer, 0, read); } else { FunctionalUtils.invokeSafely(providerStream::close); } return read < 0; } @Override public boolean resetPosition() { if (provider == null) { throw new IllegalStateException("Cannot reset position while provider is null"); } FunctionalUtils.invokeSafely(this::createNewStream); return true; } private void createNewStream() throws IOException { if (provider == null) { throw new IllegalStateException("Cannot create a new stream while provider is null"); } if (providerStream != null) { providerStream.close(); } providerStream = provider.newStream(); } }
2,367
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/EnumTest.java
/* * 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.protocol.tests; import static java.util.stream.Collectors.toMap; import static org.assertj.core.api.Assertions.assertThat; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import software.amazon.awssdk.services.protocolrestjson.model.AllTypesResponse; import software.amazon.awssdk.services.protocolrestjson.model.EnumType; /** * Verifies that the behavior when retrieving enums from a model behaves as expected. */ public class EnumTest { @Test public void knownEnumFieldsBehaveCorrectly() { assertThat(convertToEnumWithBuilder("EnumValue1")).isEqualTo(EnumType.ENUM_VALUE1); assertThat(convertToEnumWithBuilder((String) null)).isEqualTo(null); assertThat(convertToEnumWithBuilder(EnumType.ENUM_VALUE1)).isEqualTo(EnumType.ENUM_VALUE1); } @Test public void unknownEnumFieldsBehaveCorrectly() { assertThat(convertToEnumWithBuilder("Foo")).isEqualTo(EnumType.UNKNOWN_TO_SDK_VERSION); } @Test public void knownEnumListFieldsBehaveCorrectly() { assertThat(convertToListEnumWithBuilder("EnumValue1", "EnumValue2")) .containsExactly(EnumType.ENUM_VALUE1, EnumType.ENUM_VALUE2); assertThat(convertToListEnumWithBuilder(new String[] {null })).containsExactly(new EnumType[] {null }); assertThat(convertToMapEnumWithBuilder()).isEmpty(); } @Test public void unknownEnumListFieldsBehaveCorrectly() { assertThat(convertToListEnumWithBuilder("Foo", "EnumValue2")).containsExactly(EnumType.UNKNOWN_TO_SDK_VERSION, EnumType.ENUM_VALUE2); } @Test public void knownEnumMapFieldsBehaveCorrectly() { assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("EnumValue1", "EnumValue2"))) .containsExactly(new SimpleImmutableEntry<>(EnumType.ENUM_VALUE1, EnumType.ENUM_VALUE2)); } @Test public void unknownEnumMapFieldsBehaveCorrectly() { assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("Foo", "EnumValue2"))) .isEmpty(); assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("Foo", "Foo"))) .isEmpty(); assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("Foo", ""))) .isEmpty(); assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("EnumValue1", "Foo"))) .containsExactly(new SimpleImmutableEntry<>(EnumType.ENUM_VALUE1, EnumType.UNKNOWN_TO_SDK_VERSION)); assertThat(convertToMapEnumWithBuilder(new SimpleImmutableEntry<>("EnumValue1", ""))) .containsExactly(new SimpleImmutableEntry<>(EnumType.ENUM_VALUE1, EnumType.UNKNOWN_TO_SDK_VERSION)); } private EnumType convertToEnumWithBuilder(String value) { return AllTypesResponse.builder().enumMember(value).build().enumMember(); } private EnumType convertToEnumWithBuilder(EnumType value) { return AllTypesResponse.builder().enumMember(value).build().enumMember(); } private List<EnumType> convertToListEnumWithBuilder(String... values) { return AllTypesResponse.builder().listOfEnumsWithStrings(values).build().listOfEnums(); } @SafeVarargs private final Map<EnumType, EnumType> convertToMapEnumWithBuilder(Entry<String, String>... values) { Map<String, String> enumMap = Stream.of(values).collect(toMap(Entry::getKey, Entry::getValue)); return AllTypesResponse.builder().mapOfEnumToEnumWithStrings(enumMap).build().mapOfEnumToEnum(); } }
2,368
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/AwsJsonProtocolTest.java
/* * 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.protocol.tests; import java.io.IOException; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.model.TestCase; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; @RunWith(Parameterized.class) public class AwsJsonProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader TEST_SUITE_LOADER = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @Parameterized.Parameter public TestCase testCase; @Parameterized.Parameters(name = "{0}") public static List<TestCase> data() throws IOException { return TEST_SUITE_LOADER.load("jsonrpc-suite.json"); } @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/jsonrpc-2016-03-11-intermediate.json"); } @Test public void runProtocolTest() throws Exception { testRunner.runTest(testCase); } }
2,369
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/RestXmlEventStreamProtocolTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.any; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.ByteArrayOutputStream; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.async.SdkPublisher; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlAsyncClient; import software.amazon.awssdk.services.protocolrestxml.model.EventStream; import software.amazon.awssdk.services.protocolrestxml.model.EventStreamOperationResponse; import software.amazon.awssdk.services.protocolrestxml.model.EventStreamOperationResponseHandler; import software.amazon.awssdk.services.protocolrestxml.model.ProtocolRestXmlException; import software.amazon.eventstream.HeaderValue; import software.amazon.eventstream.Message; public class RestXmlEventStreamProtocolTest { private static final Region REGION = Region.US_WEST_2; private static final AwsCredentialsProvider CREDENTIALS = StaticCredentialsProvider.create( AwsBasicCredentials.create("akid", "skid")); @Rule public WireMockRule wireMock = new WireMockRule(); private ProtocolRestXmlAsyncClient client; @Before public void setup() { client = ProtocolRestXmlAsyncClient.builder() .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .region(REGION) .credentialsProvider(CREDENTIALS) .build(); } @After public void teardown() { client.close(); } @Test public void eventStreamOperation_unmarshallsHeaderMembersIntoResponse() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); event("SomeUnknownEvent", new byte[0]).encode(baos); String expectedValue = "HelloEventStreams"; stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200) .withHeader("Header-Member", expectedValue) .withBody(baos.toByteArray()))); TestHandler testHandler = new TestHandler(); client.eventStreamOperation(r -> {}, testHandler).join(); assertThat(testHandler.receivedResponse.headerMember()).isEqualTo(expectedValue); } @Test public void unknownEventType_unmarshallsToUnknown() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); event("SomeUnknownEvent", new byte[0]).encode(baos); stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(baos.toByteArray()))); TestHandler testHandler = new TestHandler(); client.eventStreamOperation(r -> {}, testHandler).join(); assertThat(testHandler.receivedEvents).containsExactly(EventStream.UNKNOWN); assertThat(testHandler.receivedEvents.get(0).sdkEventType()).isEqualTo(EventStream.EventType.UNKNOWN_TO_SDK_VERSION); } @Test public void eventPayloadEvent_unmarshallsToEventPayloadEvent() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); event(EventStream.EventType.EVENT_PAYLOAD_EVENT.toString(), "<Foo>bar</Foo>".getBytes(StandardCharsets.UTF_8)) .encode(baos); stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(baos.toByteArray()))); TestHandler testHandler = new TestHandler(); client.eventStreamOperation(r -> {}, testHandler).join(); assertThat(testHandler.receivedEvents).containsExactly(EventStream.eventPayloadEventBuilder().foo("<Foo>bar</Foo>").build()); assertThat(testHandler.receivedEvents.get(0).sdkEventType()).isEqualTo(EventStream.EventType.EVENT_PAYLOAD_EVENT); } @Test public void secondEventPayloadEvent_unmarshallsToSecondEventPayloadEvent() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); event(EventStream.EventType.SECOND_EVENT_PAYLOAD_EVENT.toString(), "<Foo>bar</Foo>".getBytes(StandardCharsets.UTF_8)) .encode(baos); stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(baos.toByteArray()))); TestHandler testHandler = new TestHandler(); client.eventStreamOperation(r -> {}, testHandler).join(); assertThat(testHandler.receivedEvents).containsExactly(EventStream.secondEventPayloadEventBuilder().foo("<Foo>bar</Foo>").build()); assertThat(testHandler.receivedEvents.get(0).sdkEventType()).isEqualTo(EventStream.EventType.SECOND_EVENT_PAYLOAD_EVENT); } @Test public void nonEventPayloadEvent_unmarshallsToNonEventPayloadEvent() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); event(EventStream.EventType.NON_EVENT_PAYLOAD_EVENT.toString(), "<NonEventPayloadEvent><Bar>baz</Bar></NonEventPayloadEvent>".getBytes(StandardCharsets.UTF_8)) .encode(baos); stubFor(any(anyUrl()).willReturn(aResponse().withStatus(200).withBody(baos.toByteArray()))); TestHandler testHandler = new TestHandler(); client.eventStreamOperation(r -> {}, testHandler).join(); assertThat(testHandler.receivedEvents).containsExactly(EventStream.nonEventPayloadEventBuilder().bar("baz").build()); assertThat(testHandler.receivedEvents.get(0).sdkEventType()).isEqualTo(EventStream.EventType.NON_EVENT_PAYLOAD_EVENT); } @Test public void errorResponse_unmarshalledCorrectly() { stubFor(any(anyUrl()).willReturn(aResponse().withStatus(500))); TestHandler testHandler = new TestHandler(); CompletableFuture<Void> responseFuture = client.eventStreamOperation(r -> { }, testHandler); assertThatThrownBy(responseFuture::join).hasCauseInstanceOf(ProtocolRestXmlException.class); } private static Message event(String type, byte[] payload) { Map<String, HeaderValue> headers = new HashMap<>(); headers.put(":message-type", HeaderValue.fromString("event")); headers.put(":event-type", HeaderValue.fromString(type)); return new Message(headers, payload); } private static class TestHandler implements EventStreamOperationResponseHandler { private final List<EventStream> receivedEvents = new ArrayList<>(); private EventStreamOperationResponse receivedResponse; @Override public void responseReceived(EventStreamOperationResponse response) { this.receivedResponse = response; } @Override public void onEventStream(SdkPublisher<EventStream> publisher) { publisher.subscribe(receivedEvents::add); } @Override public void exceptionOccurred(Throwable throwable) { } @Override public void complete() { } } }
2,370
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/SdkHttpResponseTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.Assertions.assertThat; import com.github.tomakehurst.wiremock.http.HttpHeader; import com.github.tomakehurst.wiremock.http.HttpHeaders; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.awscore.AwsResponse; import software.amazon.awssdk.awscore.AwsResponseMetadata; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.async.AsyncResponseTransformer; import software.amazon.awssdk.core.sync.ResponseTransformer; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.model.AllTypesResponse; import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonResponse; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; import software.amazon.awssdk.utils.builder.SdkBuilder; /** * Verify response contains correct {@link SdkHttpResponse} and {@link AwsResponseMetadata}. */ public class SdkHttpResponseTest { private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; private static final String STATUS_TEXT = "hello world"; private static final String REQUEST_ID = "uiadfsdbeqir62"; @Rule public final WireMockRule wireMock = new WireMockRule(0); private ProtocolRestJsonClient client; private ProtocolRestJsonAsyncClient asyncClient; private static final Map<String, List<String>> EXPECTED_HEADERS = new HashMap<String, List<String>>() {{ put("x-foo", Collections.singletonList("a")); put("x-bar", Collections.singletonList("b")); put("x-amzn-RequestId", Collections.singletonList(REQUEST_ID)); }}; @Before public void setupClient() { client = ProtocolRestJsonClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .build(); asyncClient = ProtocolRestJsonAsyncClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .build(); } @Test public void syncNonStreamingShouldContainSdkHttpDate() { stubWithHeaders(EXPECTED_HEADERS); AllTypesResponse response = client.allTypes(b -> b.simpleList("test")); verifySdkHttpResponse(response); verifyResponseMetadata(response); } @Test public void syncStreamingShouldContainSdkHttpDate() { stubWithHeaders(EXPECTED_HEADERS); ResponseBytes<StreamingOutputOperationResponse> responseBytes = client .streamingOutputOperation(SdkBuilder::build, ResponseTransformer.toBytes()); StreamingOutputOperationResponse response = responseBytes.response(); verifySdkHttpResponse(response); verifyResponseMetadata(response); } @Test public void asyncNonStreamingShouldContainsSdkHttpData() { stubWithHeaders(EXPECTED_HEADERS); AllTypesResponse response = asyncClient.allTypes(b -> b.simpleList("test")).join(); verifySdkHttpResponse(response); verifyResponseMetadata(response); } @Test public void asyncStreamingMethodShouldContainSdkHttpDate() { stubWithHeaders(EXPECTED_HEADERS); ResponseBytes<StreamingOutputOperationResponse> responseBytes = asyncClient .streamingOutputOperation(SdkBuilder::build, AsyncResponseTransformer.toBytes()).join(); StreamingOutputOperationResponse response = responseBytes.response(); verifySdkHttpResponse(response); verifyResponseMetadata(response); } private void stubWithHeaders(Map<String, List<String>> headers) { HttpHeaders httpHeaders = new HttpHeaders(headers.entrySet().stream().map(entry -> new HttpHeader(entry.getKey(), entry.getValue())) .collect(Collectors.toList())); stubFor(post(anyUrl()).willReturn(aResponse() .withStatus(200) .withStatusMessage(STATUS_TEXT) .withHeaders(httpHeaders) .withBody(JSON_BODY))); } private void verifySdkHttpResponse(AwsResponse response) { SdkHttpResponse sdkHttpResponse = response.sdkHttpResponse(); assertThat(sdkHttpResponse.statusCode()).isEqualTo(200); assertThat(sdkHttpResponse.statusText().get()).isEqualTo(STATUS_TEXT); EXPECTED_HEADERS.entrySet().forEach(entry -> assertThat(sdkHttpResponse.headers()).contains(entry)); } private void verifyResponseMetadata(ProtocolRestJsonResponse response) { assertThat(response.responseMetadata().requestId()).isEqualTo(REQUEST_ID); } }
2,371
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/RestJsonProtocolTest.java
/* * 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.protocol.tests; import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.model.TestCase; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; @RunWith(Parameterized.class) public class RestJsonProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader testSuiteLoader = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @Parameterized.Parameter public TestCase testCase; @Parameterized.Parameters(name = "{0}") public static List<TestCase> data() throws IOException { return testSuiteLoader.load("restjson-suite.json"); } @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/restjson-2016-03-11-intermediate.json"); } @Test public void runProtocolTest() throws Exception { testRunner.runTest(testCase); } }
2,372
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/ResponseTransformerTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.stubbing.StubMapping; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.UUID; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.core.sync.ResponseTransformer; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationRequest; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; import software.amazon.awssdk.utils.BinaryUtils; /** * Verify the end-to-end functionality of the SDK-provided {@link ResponseTransformer} implementations. */ public class ResponseTransformerTest { @Rule public WireMockRule wireMock = new WireMockRule(0); private static final String STREAMING_OUTPUT_PATH = "/2016-03-11/streamingOutputOperation"; @Test public void bytesMethodConvertsCorrectly() { stubForSuccess(); ResponseBytes<StreamingOutputOperationResponse> response = testClient().streamingOutputOperationAsBytes(StreamingOutputOperationRequest.builder().build()); byte[] arrayCopy = response.asByteArray(); assertThat(arrayCopy).containsExactly('t', 'e', 's', 't', ' ', -16, -97, -104, -126); arrayCopy[0] = 'X'; // Mutate the returned byte array to make sure it's a copy ByteBuffer buffer = response.asByteBuffer(); assertThat(buffer.isReadOnly()).isTrue(); assertThat(BinaryUtils.copyAllBytesFrom(buffer)).containsExactly('t', 'e', 's', 't', ' ', -16, -97, -104, -126); assertThat(response.asString(StandardCharsets.UTF_8)).isEqualTo("test \uD83D\uDE02"); assertThat(response.asUtf8String()).isEqualTo("test \uD83D\uDE02"); } @Test public void byteMethodDownloadFailureRetries() { stubForRetriesTimeoutReadingFromStreams(); ResponseBytes<StreamingOutputOperationResponse> response = testClient().streamingOutputOperationAsBytes(StreamingOutputOperationRequest.builder().build()); assertThat(response.asUtf8String()).isEqualTo("retried"); } @Test public void downloadToFileRetriesCorrectly() throws IOException { stubForRetriesTimeoutReadingFromStreams(); Path tmpDirectory = Files.createTempDirectory("streaming-response-handler-test"); tmpDirectory.toFile().deleteOnExit(); Path tmpFile = tmpDirectory.resolve(UUID.randomUUID().toString()); tmpFile.toFile().deleteOnExit(); testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), tmpFile); assertThat(Files.readAllLines(tmpFile)).containsExactly("retried"); } @Test public void downloadToExistingFileDoesNotRetry() throws IOException { stubForRetriesTimeoutReadingFromStreams(); assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), ResponseTransformer .toFile(new File("..")))) .isInstanceOf(SdkClientException.class); } @Test public void downloadToOutputStreamDoesNotRetry() throws IOException { stubForRetriesTimeoutReadingFromStreams(); assertThatThrownBy(() -> testClient().streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), ResponseTransformer .toOutputStream(new ByteArrayOutputStream()))) .isInstanceOf(SdkClientException.class); } @Test public void streamingCloseActuallyCloses() throws IOException { stubForSuccess(); ProtocolRestJsonClient client = testClientBuilder() .httpClientBuilder(ApacheHttpClient.builder() .connectionAcquisitionTimeout(Duration.ofSeconds(1)) .maxConnections(1)) .build(); // Two successful requests with a max of one connection means that closing the connection worked. client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).close(); client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).close(); } @Test public void streamingAbortActuallyAborts() { stubForSuccess(); ProtocolRestJsonClient client = testClientBuilder() .httpClientBuilder(ApacheHttpClient.builder() .connectionAcquisitionTimeout(Duration.ofSeconds(1)) .maxConnections(1)) .build(); // Two successful requests with a max of one connection means that closing the connection worked. client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).abort(); client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build()).abort(); } private void stubForRetriesTimeoutReadingFromStreams() { stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).inScenario("retries") .whenScenarioStateIs(STARTED) .willReturn(aResponse().withStatus(200).withBody("first") .withHeader("Content-Length", "100")) .willSetStateTo("Retry")); stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).inScenario("retries") .whenScenarioStateIs("Retry") .willReturn(aResponse().withStatus(200).withBody("retried"))); } private ProtocolRestJsonClient testClient() { return testClientBuilder().build(); } private ProtocolRestJsonClientBuilder testClientBuilder() { return ProtocolRestJsonClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .httpClientBuilder(ApacheHttpClient.builder().socketTimeout(Duration.ofSeconds(1))); } private StubMapping stubForSuccess() { return stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("test \uD83D\uDE02"))); } }
2,373
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/AsyncResponseTransformerTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import java.util.concurrent.CompletableFuture; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.awscore.AwsResponse; import software.amazon.awssdk.core.async.AsyncResponseTransformer; import software.amazon.awssdk.core.async.SdkPublisher; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolquery.ProtocolQueryAsyncClient; import software.amazon.awssdk.services.protocolquery.model.ProtocolQueryException; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonException; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; import software.amazon.awssdk.utils.builder.SdkBuilder; public class AsyncResponseTransformerTest { @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolRestJsonAsyncClient jsonClient; private ProtocolQueryAsyncClient xmlClient; @Before public void setupClient() { jsonClient = ProtocolRestJsonAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); xmlClient = ProtocolQueryAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( "akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void jsonClient_nonRetriableError_shouldNotifyAsyncResponseTransformer() { stubFor(post(anyUrl()) .willReturn(aResponse() .withStatus(400))); TestAsyncResponseTransformer<StreamingOutputOperationResponse> responseTransformer = new TestAsyncResponseTransformer<>(); assertThatThrownBy(() -> jsonClient.streamingOutputOperation(SdkBuilder::build, responseTransformer).join()) .hasCauseExactlyInstanceOf(ProtocolRestJsonException.class); assertThat(responseTransformer.exceptionOccurred).isEqualTo(true); } @Test public void xmlClient_nonRetriableError_shouldNotifyAsyncResponseTransformer() { stubFor(post(anyUrl()) .willReturn(aResponse() .withStatus(400))); TestAsyncResponseTransformer<software.amazon.awssdk.services.protocolquery.model.StreamingOutputOperationResponse> responseTransformer = new TestAsyncResponseTransformer<>(); assertThatThrownBy(() -> xmlClient.streamingOutputOperation(SdkBuilder::build, responseTransformer).join()) .hasCauseExactlyInstanceOf(ProtocolQueryException.class); assertThat(responseTransformer.exceptionOccurred).isEqualTo(true); } @Test public void jsonClient_incorrectCredentials_shouldNotifyAsyncResponseTransformer() { ProtocolRestJsonAsyncClient clientWithIncorrectCredentials = ProtocolRestJsonAsyncClient .builder() .credentialsProvider(AwsCredentialsProviderChain.of(ProfileCredentialsProvider.create("dummyprofile"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); stubFor(post(anyUrl()) .willReturn(aResponse() .withStatus(400))); TestAsyncResponseTransformer<StreamingOutputOperationResponse> responseTransformer = new TestAsyncResponseTransformer<>(); assertThatThrownBy(() -> clientWithIncorrectCredentials.streamingOutputOperation(SdkBuilder::build, responseTransformer).join()) .hasCauseExactlyInstanceOf(SdkClientException.class) .hasMessageContaining("Unable to load credentials"); assertThat(responseTransformer.exceptionOccurred).isEqualTo(true); } private class TestAsyncResponseTransformer<T extends AwsResponse> implements AsyncResponseTransformer<T, Void> { private boolean exceptionOccurred = false; @Override public CompletableFuture prepare() { return new CompletableFuture<Void>(); } @Override public void onResponse(T response) { } @Override public void exceptionOccurred(Throwable error) { exceptionOccurred = true; } @Override public void onStream(SdkPublisher publisher) { } } }
2,374
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/EventTransformTest.java
/* * 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.protocol.tests; import static org.assertj.core.api.Assertions.assertThat; import java.net.URI; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.core.SdkPojo; import software.amazon.awssdk.core.client.config.SdkClientConfiguration; import software.amazon.awssdk.core.client.config.SdkClientOption; import software.amazon.awssdk.core.http.HttpResponseHandler; import software.amazon.awssdk.core.interceptor.ExecutionAttributes; import software.amazon.awssdk.http.AbortableInputStream; import software.amazon.awssdk.http.SdkHttpFullRequest; import software.amazon.awssdk.http.SdkHttpFullResponse; import software.amazon.awssdk.protocols.json.AwsJsonProtocol; import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory; import software.amazon.awssdk.protocols.json.JsonOperationMetadata; import software.amazon.awssdk.services.protocolrestjson.model.InputEvent; import software.amazon.awssdk.services.protocolrestjson.model.InputEventStringPayload; import software.amazon.awssdk.services.protocolrestjson.transform.InputEventMarshaller; import software.amazon.awssdk.services.protocolrestjson.transform.InputEventStringPayloadMarshaller; /** * Marshalling and Unmarshalling tests for events. */ public class EventTransformTest { private static final String EXPLICIT_PAYLOAD_JSON = "{\"ExplicitPayloadMember\": \"bar\"}"; private static final String EXPLICIT_PAYLOAD_NON_JSON = "bar"; private static final String HEADER_MEMBER_NAME = "HeaderMember"; private static final String HEADER_MEMBER = "foo"; private static AwsJsonProtocolFactory protocolFactory; @BeforeAll public static void setup() { protocolFactory = AwsJsonProtocolFactory.builder() .clientConfiguration( SdkClientConfiguration.builder() .option(SdkClientOption.ENDPOINT, URI.create("http://foo.amazonaws.com")) .build()) .protocol(AwsJsonProtocol.AWS_JSON) .build(); } @ParameterizedTest @ValueSource(strings = {EXPLICIT_PAYLOAD_JSON, EXPLICIT_PAYLOAD_NON_JSON}) public void testUnmarshalling_BlobPayload(String payload) throws Exception { HttpResponseHandler<SdkPojo> responseHandler = protocolFactory .createResponseHandler(JsonOperationMetadata.builder().build(), InputEvent::builder); InputEvent unmarshalled = (InputEvent) responseHandler.handle(SdkHttpFullResponse.builder() .content(AbortableInputStream.create(SdkBytes.fromUtf8String(payload).asInputStream())) .putHeader(HEADER_MEMBER_NAME, HEADER_MEMBER) .build(), new ExecutionAttributes()); assertThat(unmarshalled.headerMember()).isEqualTo(HEADER_MEMBER); assertThat(unmarshalled.explicitPayloadMember().asUtf8String()).isEqualTo(payload); } @ParameterizedTest @ValueSource(strings = {EXPLICIT_PAYLOAD_JSON, EXPLICIT_PAYLOAD_NON_JSON}) public void testUnmarshalling_StringPayload(String payload) throws Exception { HttpResponseHandler<SdkPojo> responseHandler = protocolFactory .createResponseHandler(JsonOperationMetadata.builder().build(), InputEventStringPayload::builder); InputEventStringPayload unmarshalled = (InputEventStringPayload) responseHandler.handle(SdkHttpFullResponse.builder() .content(AbortableInputStream.create(SdkBytes.fromUtf8String(payload).asInputStream())) .putHeader(HEADER_MEMBER_NAME, HEADER_MEMBER) .build(), new ExecutionAttributes()); assertThat(unmarshalled.headerMember()).isEqualTo(HEADER_MEMBER); assertThat(unmarshalled.explicitPayloadStringMember()).isEqualTo(payload); } @ParameterizedTest @ValueSource(strings = {EXPLICIT_PAYLOAD_JSON, EXPLICIT_PAYLOAD_NON_JSON}) public void testMarshalling_BlobPayload(String payload) { InputEventMarshaller marshaller = new InputEventMarshaller(protocolFactory); InputEvent e = InputEvent.builder() .headerMember(HEADER_MEMBER) .explicitPayloadMember(SdkBytes.fromUtf8String(payload)) .build(); SdkHttpFullRequest marshalled = marshaller.marshall(e); assertThat(marshalled.headers().get(HEADER_MEMBER_NAME)).containsExactly(HEADER_MEMBER); assertThat(marshalled.contentStreamProvider().get().newStream()) .hasSameContentAs(SdkBytes.fromUtf8String(payload).asInputStream()); } @ParameterizedTest @ValueSource(strings = {EXPLICIT_PAYLOAD_JSON, EXPLICIT_PAYLOAD_NON_JSON}) public void testMarshalling_StringPayload(String payload) { InputEventStringPayloadMarshaller marshaller = new InputEventStringPayloadMarshaller(protocolFactory); InputEventStringPayload e = InputEventStringPayload.builder() .headerMember(HEADER_MEMBER) .explicitPayloadStringMember(payload) .build(); SdkHttpFullRequest marshalled = marshaller.marshall(e); assertThat(marshalled.headers().get(HEADER_MEMBER_NAME)).containsExactly(HEADER_MEMBER); assertThat(marshalled.contentStreamProvider().get().newStream()) .hasSameContentAs(SdkBytes.fromUtf8String(payload).asInputStream()); } }
2,375
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/ResourceManagementTest.java
/* * 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.protocol.tests; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR; import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; import org.junit.jupiter.api.Test; import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClientBuilder; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder; /** * Verifies that the SDK clients correctly manage resources with which they have been configured. */ public class ResourceManagementTest { @Test public void httpClientNotShutdown() { SdkHttpClient httpClient = mock(SdkHttpClient.class); syncClientBuilder().httpClient(httpClient).build().close(); verify(httpClient, never()).close(); } @Test public void asyncHttpClientNotShutdown() { SdkAsyncHttpClient httpClient = mock(SdkAsyncHttpClient.class); asyncClientBuilder().httpClient(httpClient).build().close(); verify(httpClient, never()).close(); } @Test public void httpClientFromBuilderShutdown() { SdkHttpClient httpClient = mock(SdkHttpClient.class); SdkHttpClient.Builder httpClientBuilder = mock(SdkHttpClient.Builder.class); when(httpClientBuilder.buildWithDefaults(any())).thenReturn(httpClient); syncClientBuilder().httpClientBuilder(httpClientBuilder).build().close(); verify(httpClient).close(); } @Test public void asyncHttpClientFromBuilderShutdown() { SdkAsyncHttpClient httpClient = mock(SdkAsyncHttpClient.class); SdkAsyncHttpClient.Builder httpClientBuilder = mock(SdkAsyncHttpClient.Builder.class); when(httpClientBuilder.buildWithDefaults(any())).thenReturn(httpClient); asyncClientBuilder().httpClientBuilder(httpClientBuilder).build().close(); verify(httpClient).close(); } @Test public void executorFromBuilderNotShutdown() { ExecutorService executor = mock(ExecutorService.class); asyncClientBuilder().asyncConfiguration(c -> c.advancedOption(FUTURE_COMPLETION_EXECUTOR, executor)).build().close(); verify(executor, never()).shutdown(); verify(executor, never()).shutdownNow(); } @Test public void scheduledExecutorFromBuilderNotShutdown() { ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class); // Java 19+ implements AutoCloseable for ExecutorService, and the default method is to loop until isTerminated // returns true... when(scheduledExecutorService.isTerminated()).thenReturn(true); asyncClientBuilder().overrideConfiguration(c -> c.scheduledExecutorService(scheduledExecutorService)).build().close(); verify(scheduledExecutorService, never()).shutdown(); verify(scheduledExecutorService, never()).shutdownNow(); } public ProtocolRestJsonClientBuilder syncClientBuilder() { return ProtocolRestJsonClient.builder() .region(Region.US_EAST_1) .credentialsProvider(AnonymousCredentialsProvider.create()); } public ProtocolRestJsonAsyncClientBuilder asyncClientBuilder() { return ProtocolRestJsonAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(AnonymousCredentialsProvider.create()); } }
2,376
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/QueryProtocolTest.java
/* * 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.protocol.tests; import java.io.IOException; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.model.TestCase; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; @RunWith(Parameterized.class) public class QueryProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader testSuiteLoader = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @Parameterized.Parameter public TestCase testCase; @Parameterized.Parameters(name = "{0}") public static List<TestCase> data() throws IOException { return testSuiteLoader.load("query-suite.json"); } @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/query-2016-03-11-intermediate.json"); } @Test public void runProtocolTest() throws Exception { testRunner.runTest(testCase); } }
2,377
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/ImmutableModelTest.java
/* * 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.protocol.tests; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest; import software.amazon.awssdk.services.protocolrestjson.model.SimpleStruct; import software.amazon.awssdk.utils.BinaryUtils; /** * Verifies that the models are actually immutable. */ public class ImmutableModelTest { @Test public void mapsAreImmutable() { Map<String, String> map = new HashMap<>(); map.put("key", "value"); AllTypesRequest request = AllTypesRequest.builder().mapOfStringToString(map).build(); map.put("key2", "value2"); assertThat(request.mapOfStringToString()).doesNotContainKey("key2"); assertThatExceptionOfType(UnsupportedOperationException.class) .isThrownBy(() -> request.mapOfStringToString().put("key2", "value2")); } @Test public void listsAreImmutable() { SimpleStruct struct = SimpleStruct.builder().stringMember("value").build(); SimpleStruct struct2 = SimpleStruct.builder().stringMember("value2").build(); List<SimpleStruct> list = new ArrayList<>(); list.add(struct); AllTypesRequest request = AllTypesRequest.builder().listOfStructs(list).build(); list.add(struct2); assertThat(request.listOfStructs()).doesNotContain(struct2); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> request.listOfStructs().add(struct2)); } @Test public void mapsOfListsAreImmutable() { List<Integer> list = new ArrayList<>(); list.add(1); Map<String, List<Integer>> map = Collections.singletonMap("key", list); AllTypesRequest request = AllTypesRequest.builder().mapOfStringToIntegerList(map).build(); list.add(2); assertThat(request.mapOfStringToIntegerList().get("key")).doesNotContain(2); assertThatExceptionOfType(UnsupportedOperationException.class) .isThrownBy(() -> request.mapOfStringToIntegerList().get("key").add(2)); } @Test public void byteBuffersAreImmutable() { ByteBuffer buffer = ByteBuffer.wrap("Hello".getBytes(StandardCharsets.UTF_8)); buffer.position(1); AllTypesRequest request = AllTypesRequest.builder().blobArg(SdkBytes.fromByteBuffer(buffer)).build(); buffer.array()[1] = ' '; assertThat(request.blobArg().asByteBuffer()).as("Check new read-only blob each time") .isNotSameAs(request.blobArg().asByteBuffer()); assertThat(request.blobArg().asByteBuffer().isReadOnly()).as("Check read-only").isTrue(); assertThat(BinaryUtils.copyAllBytesFrom(request.blobArg().asByteBuffer())) .as("Check copy contents").containsExactly('e', 'l', 'l', 'o'); } }
2,378
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/AsyncResponseThreadingTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR; import com.github.tomakehurst.wiremock.http.Fault; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import java.util.concurrent.Executor; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.async.AsyncResponseTransformer; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonException; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationRequest; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; public class AsyncResponseThreadingTest { @Rule public WireMockRule wireMock = new WireMockRule(0); private static final String STREAMING_OUTPUT_PATH = "/2016-03-11/streamingOutputOperation"; @Test public void completionWithNioThreadWorksCorrectly() { stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("test"))); Executor mockExecutor = Mockito.spy(new SpyableExecutor()); ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .asyncConfiguration(c -> c.advancedOption(FUTURE_COMPLETION_EXECUTOR, mockExecutor)) .build(); ResponseBytes<StreamingOutputOperationResponse> response = client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), AsyncResponseTransformer.toBytes()).join(); verify(mockExecutor).execute(any()); byte[] arrayCopy = response.asByteArray(); assertThat(arrayCopy).containsExactly('t', 'e', 's', 't'); } @Test public void connectionError_completionWithNioThreadWorksCorrectly() { stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withFault(Fault.CONNECTION_RESET_BY_PEER).withBody("test"))); Executor mockExecutor = Mockito.spy(new SpyableExecutor()); ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .asyncConfiguration(c -> c.advancedOption(FUTURE_COMPLETION_EXECUTOR, mockExecutor)) .overrideConfiguration(o -> o.retryPolicy(RetryPolicy.none())) .build(); assertThatThrownBy(() -> client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), AsyncResponseTransformer.toBytes()).join()) .hasCauseInstanceOf(SdkClientException.class); verify(mockExecutor).execute(any()); } @Test public void serverError_completionWithNioThreadWorksCorrectly() { stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(500).withBody("test"))); Executor mockExecutor = Mockito.spy(new SpyableExecutor()); ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .overrideConfiguration(o -> o.retryPolicy(RetryPolicy.none())) .asyncConfiguration(c -> c.advancedOption(FUTURE_COMPLETION_EXECUTOR, mockExecutor)) .build(); assertThatThrownBy(() -> client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), AsyncResponseTransformer.toBytes()).join()).hasCauseInstanceOf(ProtocolRestJsonException.class); verify(mockExecutor).execute(any()); } private static class SpyableExecutor implements Executor { @Override public void execute(Runnable command) { command.run(); } } }
2,379
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/RestJsonContentTypeProtocolTest.java
/* * 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.protocol.tests; import java.io.IOException; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.model.TestCase; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; @RunWith(Parameterized.class) public class RestJsonContentTypeProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader testSuiteLoader = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @Parameterized.Parameter public TestCase testCase; @Parameterized.Parameters(name = "{0}") public static List<TestCase> data() throws IOException { return testSuiteLoader.load("restjson-contenttype-suite.json"); } @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/rest-test-2021-05-13-intermediate.json"); } @Test public void runProtocolTest() throws Exception { testRunner.runTest(testCase); } }
2,380
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/AsyncFaultTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import com.github.tomakehurst.wiremock.junit5.WireMockTest; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.util.concurrent.CompletableFuture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.async.AsyncResponseTransformer; import software.amazon.awssdk.core.async.SdkPublisher; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; import software.amazon.awssdk.utils.builder.SdkBuilder; @WireMockTest public class AsyncFaultTest { private ProtocolRestJsonAsyncClient client; @BeforeEach public void setup(WireMockRuntimeInfo wiremock) { client = ProtocolRestJsonAsyncClient.builder() .region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wiremock.getHttpPort())) .credentialsProvider(() -> AwsBasicCredentials.create("akid", "skid")) .build(); } @Test public void subscriberCancel_correctExceptionThrown() { stubFor(post(anyUrl()) .willReturn(aResponse() .withStatus(200))); assertThatThrownBy(() -> client.streamingOutputOperation(SdkBuilder::build, new CancelSubscriptionTransformer()).join()) .hasRootCauseExactlyInstanceOf(SelfCancelException.class); } @ParameterizedTest @ValueSource(ints = {500, 200}) @Timeout(value = 2) public void requestContentLengthNotMatch_shouldThrowException(int statusCode) { stubFor(post(anyUrl()) .willReturn(aResponse() .withBody("hello world") .withHeader("content-length", String.valueOf(100)) .withStatus(statusCode))); assertThatThrownBy(() -> client.allTypes().join()) .hasRootCauseExactlyInstanceOf(IOException.class) .hasMessageContaining("Response had content-length of 100 bytes, but only received 11 bytes before the connection " + "was closed"); } private static class CancelSubscriptionTransformer implements AsyncResponseTransformer<StreamingOutputOperationResponse, ResponseBytes<StreamingOutputOperationResponse>> { private volatile CompletableFuture<byte[]> cf; private volatile StreamingOutputOperationResponse response; @Override public CompletableFuture<ResponseBytes<StreamingOutputOperationResponse>> prepare() { cf = new CompletableFuture<>(); return cf.thenApply(arr -> ResponseBytes.fromByteArray(response, arr)); } @Override public void onResponse(StreamingOutputOperationResponse response) { this.response = response; } @Override public void onStream(SdkPublisher<ByteBuffer> publisher) { publisher.subscribe(new CancelSubscriber(cf)); } @Override public void exceptionOccurred(Throwable error) { cf.completeExceptionally(error); } private static class CancelSubscriber implements Subscriber<ByteBuffer> { private final CompletableFuture<byte[]> resultFuture; private Subscription subscription; CancelSubscriber(CompletableFuture<byte[]> resultFuture) { this.resultFuture = resultFuture; } @Override public void onSubscribe(Subscription s) { this.subscription = s; subscription.cancel(); resultFuture.completeExceptionally(new SelfCancelException()); } @Override public void onNext(ByteBuffer byteBuffer) { } @Override public void onError(Throwable t) { resultFuture.completeExceptionally(t); } @Override public void onComplete() { resultFuture.complete("hello world".getBytes()); } } } private static class SelfCancelException extends RuntimeException { } }
2,381
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/RestJsonEventStreamProtocolTest.java
/* * 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.protocol.tests; import static org.assertj.core.api.Assertions.assertThat; import java.net.URI; import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.core.client.config.SdkClientConfiguration; import software.amazon.awssdk.core.client.config.SdkClientOption; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.SdkHttpFullRequest; import software.amazon.awssdk.protocols.json.AwsJsonProtocol; import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.BlobAndHeadersEvent; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.HeadersOnlyEvent; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.ImplicitPayloadAndHeadersEvent; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.InputEventStream; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.ProtocolRestJsonContentTypeException; import software.amazon.awssdk.services.protocolrestjsoncontenttype.model.StringAndHeadersEvent; import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.BlobAndHeadersEventMarshaller; import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.HeadersOnlyEventMarshaller; import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.ImplicitPayloadAndHeadersEventMarshaller; import software.amazon.awssdk.services.protocolrestjsoncontenttype.transform.StringAndHeadersEventMarshaller; public class RestJsonEventStreamProtocolTest { private static final String EVENT_CONTENT_TYPE_HEADER = ":content-type"; @Test public void implicitPayloadAndHeaders_payloadMemberPresent() { ImplicitPayloadAndHeadersEventMarshaller marshaller = new ImplicitPayloadAndHeadersEventMarshaller(protocolFactory()); ImplicitPayloadAndHeadersEvent event = InputEventStream.implicitPayloadAndHeadersEventBuilder() .stringMember("hello rest-json") .headerMember("hello rest-json") .build(); SdkHttpFullRequest marshalledEvent = marshaller.marshall(event); assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/json"); String content = contentAsString(marshalledEvent); assertThat(content).isEqualTo("{\"StringMember\":\"hello rest-json\"}"); } @Test public void implicitPayloadAndHeaders_payloadMemberNotPresent() { ImplicitPayloadAndHeadersEventMarshaller marshaller = new ImplicitPayloadAndHeadersEventMarshaller(protocolFactory()); ImplicitPayloadAndHeadersEvent event = InputEventStream.implicitPayloadAndHeadersEventBuilder() .headerMember("hello rest-json") .build(); SdkHttpFullRequest marshalledEvent = marshaller.marshall(event); assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/json"); String content = contentAsString(marshalledEvent); assertThat(content).isEqualTo("{}"); } @Test public void blobAndHeadersEvent() { BlobAndHeadersEventMarshaller marshaller = new BlobAndHeadersEventMarshaller(protocolFactory()); BlobAndHeadersEvent event = InputEventStream.blobAndHeadersEventBuilder() .headerMember("hello rest-json") .blobPayloadMember(SdkBytes.fromUtf8String("hello rest-json")) .build(); SdkHttpFullRequest marshalledEvent = marshaller.marshall(event); assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("application/octet-stream"); String content = contentAsString(marshalledEvent); assertThat(content).isEqualTo("hello rest-json"); } @Test public void stringAndHeadersEvent() { StringAndHeadersEventMarshaller marshaller = new StringAndHeadersEventMarshaller(protocolFactory()); StringAndHeadersEvent event = InputEventStream.stringAndHeadersEventBuilder() .headerMember("hello rest-json") .stringPayloadMember("hello rest-json") .build(); SdkHttpFullRequest marshalledEvent = marshaller.marshall(event); assertThat(marshalledEvent.headers().get(EVENT_CONTENT_TYPE_HEADER)).containsExactly("text/plain"); String content = contentAsString(marshalledEvent); assertThat(content).isEqualTo("hello rest-json"); } @Test public void headersOnly() { HeadersOnlyEventMarshaller marshaller = new HeadersOnlyEventMarshaller(protocolFactory()); HeadersOnlyEvent event = InputEventStream.headersOnlyEventBuilder() .headerMember("hello rest-json") .build(); SdkHttpFullRequest marshalledEvent = marshaller.marshall(event); assertThat(marshalledEvent.headers().keySet()).doesNotContain(EVENT_CONTENT_TYPE_HEADER); String content = contentAsString(marshalledEvent); assertThat(content).isEqualTo(""); } private static AwsJsonProtocolFactory protocolFactory() { return AwsJsonProtocolFactory.builder() .clientConfiguration( SdkClientConfiguration.builder() .option(SdkClientOption.ENDPOINT, URI.create("https://test.aws.com")) .build()) .defaultServiceExceptionSupplier(ProtocolRestJsonContentTypeException::builder) .protocol(AwsJsonProtocol.REST_JSON) .protocolVersion("1.1") .build(); } private static String contentAsString(SdkHttpFullRequest request) { return request.contentStreamProvider() .map(ContentStreamProvider::newStream) .map(s -> SdkBytes.fromInputStream(s).asString(StandardCharsets.UTF_8)) .orElse(null); } }
2,382
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/XmlMetadataTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.Assertions.assertThat; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlAsyncClient; import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlClient; import software.amazon.awssdk.services.protocolrestxml.model.AllTypesResponse; import software.amazon.awssdk.services.protocolrestxml.model.ProtocolRestXmlResponse; import software.amazon.awssdk.utils.builder.SdkBuilder; public class XmlMetadataTest { private static final String REQUEST_ID = "abcd"; @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolRestXmlClient client; private ProtocolRestXmlAsyncClient asyncClient; @Before public void setupClient() { client = ProtocolRestXmlClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); asyncClient = ProtocolRestXmlAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void requestIdInHeaderButNotXml_ShouldContainsResponseMetadata() { stubResponseWithHeaders(); AllTypesResponse allTypesResponse = asyncClient.allTypes(SdkBuilder::build).join(); verifyResponseMetadata(allTypesResponse); } @Test public void requestIdNotInXmlOrHeader_responseMetadataShouldBeUnknown() { stubResponseWithoutHeaders(); AllTypesResponse allTypesResponse = client.allTypes(SdkBuilder::build); verifyUnknownResponseMetadata(allTypesResponse); } private void verifyResponseMetadata(ProtocolRestXmlResponse xmlResponse) { assertThat(xmlResponse.responseMetadata()).isNotNull(); assertThat(xmlResponse.responseMetadata().requestId()).isEqualTo(REQUEST_ID); } private void verifyUnknownResponseMetadata(ProtocolRestXmlResponse xmlResponse) { assertThat(xmlResponse.responseMetadata()).isNotNull(); assertThat(xmlResponse.responseMetadata().requestId()).isEqualTo("UNKNOWN"); } private void stubResponseWithHeaders() { stubFor(post(anyUrl()) .willReturn(aResponse().withStatus(200) .withHeader("x-amzn-RequestId", REQUEST_ID) .withBody("<AllTypesResponse/>"))); } private void stubResponseWithoutHeaders() { stubFor(post(anyUrl()) .willReturn(aResponse().withStatus(200) .withBody("<AllTypesResponse/>"))); } }
2,383
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/ProtocolTestBase.java
/* * 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.protocol.tests; import org.junit.BeforeClass; import software.amazon.awssdk.core.util.IdempotentUtils; /** * All protocol tests should extend this class to ensure that the idempotency generator is overridden before the * client class is loaded and the generator is cached, otherwise some tests in this suite can break. */ public class ProtocolTestBase { @BeforeClass public static void overrideIdempotencyTokenGenerator() { IdempotentUtils.setGenerator(() -> "00000000-0000-4000-8000-000000000000"); } }
2,384
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/ExecutionInterceptorTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoMoreInteractions; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.util.Collections; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.junit.Rule; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mockito; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder; import software.amazon.awssdk.core.SdkRequest; import software.amazon.awssdk.core.SdkResponse; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.core.async.AsyncResponseTransformer; import software.amazon.awssdk.core.async.SdkPublisher; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.core.exception.SdkServiceException; import software.amazon.awssdk.core.interceptor.Context; import software.amazon.awssdk.core.interceptor.ExecutionAttributes; import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.model.MembersInHeadersRequest; import software.amazon.awssdk.services.protocolrestjson.model.MembersInHeadersResponse; import software.amazon.awssdk.services.protocolrestjson.model.StreamingInputOperationRequest; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationRequest; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; /** * Verify that request handler hooks are behaving as expected. */ public class ExecutionInterceptorTest { @Rule public WireMockRule wireMock = new WireMockRule(wireMockConfig().port(0), false); private static final String MEMBERS_IN_HEADERS_PATH = "/2016-03-11/membersInHeaders"; private static final String STREAMING_INPUT_PATH = "/2016-03-11/streamingInputOperation"; private static final String STREAMING_OUTPUT_PATH = "/2016-03-11/streamingOutputOperation"; @Test public void sync_success_allInterceptorMethodsCalled() { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonClient client = client(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When MembersInHeadersResponse result = client.membersInHeaders(request); // Expect expectAllMethodsCalled(interceptor, request, null, false); validateRequestResponse(result); } @Test public void async_success_allInterceptorMethodsCalled() throws Exception { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonAsyncClient client = asyncClient(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When MembersInHeadersResponse result = client.membersInHeaders(request).get(10, TimeUnit.SECONDS); // Expect expectAllMethodsCalled(interceptor, request, null, true); validateRequestResponse(result); } @Test public void sync_streamingInput_success_allInterceptorMethodsCalled() throws IOException { // Given ExecutionInterceptor interceptor = mock(NoOpInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonClient client = client(interceptor); StreamingInputOperationRequest request = StreamingInputOperationRequest.builder().build(); stubFor(post(urlPathEqualTo(STREAMING_INPUT_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When client.streamingInputOperation(request, RequestBody.fromBytes(new byte[] {0})); // Expect Context.BeforeTransmission beforeTransmissionArg = captureBeforeTransmissionArg(interceptor, false); assertThat(beforeTransmissionArg.requestBody().get().contentStreamProvider().newStream().read()).isEqualTo(0); assertThat(beforeTransmissionArg.httpRequest().firstMatchingHeader(Header.CONTENT_LENGTH).get()) .contains(Long.toString(1L)); } @Test public void async_streamingInput_success_allInterceptorMethodsCalled() throws Exception { // Given ExecutionInterceptor interceptor = mock(NoOpInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonAsyncClient client = asyncClient(interceptor); StreamingInputOperationRequest request = StreamingInputOperationRequest.builder().build(); stubFor(post(urlPathEqualTo(STREAMING_INPUT_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When client.streamingInputOperation(request, new NoOpAsyncRequestBody()).get(10, TimeUnit.SECONDS); // Expect Context.BeforeTransmission beforeTransmissionArg = captureBeforeTransmissionArg(interceptor, true); // TODO: The content should actually be empty to match responses. We can fix this by updating the StructuredJsonGenerator // to use null for NO-OP marshalling of payloads. This will break streaming POST operations for JSON because of a hack in // the MoveParametersToBodyStage, but we can move the logic from there into the query marshallers (why the hack exists) // and then everything should be good for JSON. assertThat(beforeTransmissionArg.requestBody().get().contentStreamProvider().newStream().read()).isEqualTo(-1); assertThat(beforeTransmissionArg.httpRequest().firstMatchingHeader(Header.CONTENT_LENGTH).get()) .contains(Long.toString(0L)); } @Test public void sync_streamingOutput_success_allInterceptorMethodsCalled() throws IOException { // Given ExecutionInterceptor interceptor = mock(NoOpInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonClient client = client(interceptor); StreamingOutputOperationRequest request = StreamingOutputOperationRequest.builder().build(); stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("\0"))); // When client.streamingOutputOperation(request, (r, i) -> { assertThat(i.read()).isEqualTo(0); // TODO: We have to return "r" here. We should verify other response types are cool once we switch this off of // being the unmarshaller return r; }); // Expect Context.AfterTransmission afterTransmissionArg = captureAfterTransmissionArg(interceptor); // TODO: When we don't always close the input stream, make sure we can read the service's '0' response. assertThat(afterTransmissionArg.responseBody() != null); } @Test public void async_streamingOutput_success_allInterceptorMethodsCalled() throws Exception { // Given ExecutionInterceptor interceptor = mock(NoOpInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonAsyncClient client = asyncClient(interceptor); StreamingOutputOperationRequest request = StreamingOutputOperationRequest.builder().build(); stubFor(post(urlPathEqualTo(STREAMING_OUTPUT_PATH)).willReturn(aResponse().withStatus(200).withBody("\0"))); // When client.streamingOutputOperation(request, new NoOpAsyncResponseTransformer()).get(10, TimeUnit.SECONDS); // Expect Context.AfterTransmission afterTransmissionArg = captureAfterTransmissionArgAsync(interceptor); assertThat(afterTransmissionArg.responseBody()).isNotNull(); } @Test public void sync_serviceException_failureInterceptorMethodsCalled() { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonClient client = client(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); // When assertThatExceptionOfType(SdkServiceException.class).isThrownBy(() -> client.membersInHeaders(request)); // Expect expectServiceCallErrorMethodsCalled(interceptor, false); } @Test public void async_serviceException_failureInterceptorMethodsCalled() throws Exception { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); ProtocolRestJsonAsyncClient client = asyncClient(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); // When assertThatExceptionOfType(ExecutionException.class).isThrownBy(() -> client.membersInHeaders(request).get()) .withCauseInstanceOf(SdkServiceException.class); // Expect expectServiceCallErrorMethodsCalled(interceptor, true); } @Test public void sync_interceptorException_failureInterceptorMethodsCalled() { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); RuntimeException exception = new RuntimeException("Uh oh!"); doThrow(exception).when(interceptor).afterExecution(any(), any()); ProtocolRestJsonClient client = client(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> client.membersInHeaders(request)); // Expect expectAllMethodsCalled(interceptor, request, exception, false); } @Test public void async_interceptorException_failureInterceptorMethodsCalled() { // Given ExecutionInterceptor interceptor = mock(MessageUpdatingInterceptor.class, CALLS_REAL_METHODS); RuntimeException exception = new RuntimeException("Uh oh!"); doThrow(exception).when(interceptor).afterExecution(any(), any()); ProtocolRestJsonAsyncClient client = asyncClient(interceptor); MembersInHeadersRequest request = MembersInHeadersRequest.builder().build(); stubFor(post(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)).willReturn(aResponse().withStatus(200).withBody(""))); // When assertThatExceptionOfType(ExecutionException.class).isThrownBy(() -> client.membersInHeaders(request).get()) .withCause(SdkClientException.builder() .cause(exception) .build()); // Expect expectAllMethodsCalled(interceptor, request, exception, true); } private Context.BeforeTransmission captureBeforeTransmissionArg(ExecutionInterceptor interceptor, boolean isAsync) { ArgumentCaptor<Context.BeforeTransmission> beforeTransmissionArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class); InOrder inOrder = Mockito.inOrder(interceptor); inOrder.verify(interceptor).beforeExecution(any(), any()); inOrder.verify(interceptor).modifyRequest(any(), any()); inOrder.verify(interceptor).beforeMarshalling(any(), any()); inOrder.verify(interceptor).afterMarshalling(any(), any()); inOrder.verify(interceptor).modifyAsyncHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpRequest(any(), any()); inOrder.verify(interceptor).beforeTransmission(beforeTransmissionArg.capture(), any()); inOrder.verify(interceptor).afterTransmission(any(), any()); inOrder.verify(interceptor).modifyHttpResponse(any(), any()); inOrder.verify(interceptor).modifyHttpResponseContent(any(), any()); inOrder.verify(interceptor).beforeUnmarshalling(any(), any()); if (isAsync) { inOrder.verify(interceptor).modifyAsyncHttpResponseContent(any(), any()); } inOrder.verify(interceptor).afterUnmarshalling(any(), any()); inOrder.verify(interceptor).modifyResponse(any(), any()); inOrder.verify(interceptor).afterExecution(any(), any()); verifyNoMoreInteractions(interceptor); return beforeTransmissionArg.getValue(); } private Context.AfterTransmission captureAfterTransmissionArg(ExecutionInterceptor interceptor) { ArgumentCaptor<Context.AfterTransmission> afterTransmissionArg = ArgumentCaptor.forClass(Context.AfterTransmission.class); InOrder inOrder = Mockito.inOrder(interceptor); inOrder.verify(interceptor).beforeExecution(any(), any()); inOrder.verify(interceptor).modifyRequest(any(), any()); inOrder.verify(interceptor).beforeMarshalling(any(), any()); inOrder.verify(interceptor).afterMarshalling(any(), any()); inOrder.verify(interceptor).modifyAsyncHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpRequest(any(), any()); inOrder.verify(interceptor).beforeTransmission(any(), any()); inOrder.verify(interceptor).afterTransmission(afterTransmissionArg.capture(), any()); inOrder.verify(interceptor).modifyHttpResponse(any(), any()); inOrder.verify(interceptor).modifyHttpResponseContent(any(), any()); inOrder.verify(interceptor).beforeUnmarshalling(any(), any()); inOrder.verify(interceptor).afterUnmarshalling(any(), any()); inOrder.verify(interceptor).modifyResponse(any(), any()); inOrder.verify(interceptor).afterExecution(any(), any()); verifyNoMoreInteractions(interceptor); return afterTransmissionArg.getValue(); } private Context.AfterTransmission captureAfterTransmissionArgAsync(ExecutionInterceptor interceptor) { ArgumentCaptor<Context.AfterTransmission> afterTransmissionArg = ArgumentCaptor.forClass(Context.AfterTransmission.class); InOrder inOrder = Mockito.inOrder(interceptor); inOrder.verify(interceptor).beforeExecution(any(), any()); inOrder.verify(interceptor).modifyRequest(any(), any()); inOrder.verify(interceptor).beforeMarshalling(any(), any()); inOrder.verify(interceptor).afterMarshalling(any(), any()); inOrder.verify(interceptor).modifyAsyncHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpContent(any(), any()); inOrder.verify(interceptor).modifyHttpRequest(any(), any()); inOrder.verify(interceptor).beforeTransmission(any(), any()); inOrder.verify(interceptor).afterTransmission(afterTransmissionArg.capture(), any()); inOrder.verify(interceptor).modifyHttpResponse(any(), any()); inOrder.verify(interceptor).modifyHttpResponseContent(any(), any()); inOrder.verify(interceptor).beforeUnmarshalling(any(), any()); inOrder.verify(interceptor).afterUnmarshalling(any(), any()); inOrder.verify(interceptor).modifyResponse(any(), any()); inOrder.verify(interceptor).modifyAsyncHttpResponseContent(any(), any()); inOrder.verify(interceptor).afterExecution(any(), any()); verifyNoMoreInteractions(interceptor); return afterTransmissionArg.getValue(); } private void expectAllMethodsCalled(ExecutionInterceptor interceptor, SdkRequest inputRequest, Exception expectedException, boolean isAsync) { ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class); ArgumentCaptor<Context.BeforeExecution> beforeExecutionArg = ArgumentCaptor.forClass(Context.BeforeExecution.class); ArgumentCaptor<Context.BeforeMarshalling> modifyRequestArg = ArgumentCaptor.forClass(Context.BeforeMarshalling.class); ArgumentCaptor<Context.BeforeMarshalling> beforeMarshallingArg = ArgumentCaptor.forClass(Context.BeforeMarshalling.class); ArgumentCaptor<Context.AfterMarshalling> afterMarshallingArg = ArgumentCaptor.forClass(Context.AfterMarshalling.class); ArgumentCaptor<Context.BeforeTransmission> modifyHttpRequestArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class); ArgumentCaptor<Context.BeforeTransmission> modifyHttpContentArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class); ArgumentCaptor<Context.BeforeTransmission> modifyHttpContentAsyncArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class); ArgumentCaptor<Context.BeforeTransmission> beforeTransmissionArg = ArgumentCaptor.forClass(Context.BeforeTransmission.class); ArgumentCaptor<Context.AfterTransmission> afterTransmissionArg = ArgumentCaptor.forClass(Context.AfterTransmission.class); ArgumentCaptor<Context.BeforeUnmarshalling> modifyHttpResponseArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class); ArgumentCaptor<Context.BeforeUnmarshalling> modifyHttpResponseContentArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class); ArgumentCaptor<Context.BeforeUnmarshalling> beforeUnmarshallingArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class); ArgumentCaptor<Context.BeforeUnmarshalling> modifyAsyncHttpResponseContent = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class); ArgumentCaptor<Context.AfterUnmarshalling> afterUnmarshallingArg = ArgumentCaptor.forClass(Context.AfterUnmarshalling.class); ArgumentCaptor<Context.AfterExecution> modifyResponseArg = ArgumentCaptor.forClass(Context.AfterExecution.class); ArgumentCaptor<Context.AfterExecution> afterExecutionArg = ArgumentCaptor.forClass(Context.AfterExecution.class); // Verify methods are called in the right order InOrder inOrder = Mockito.inOrder(interceptor); inOrder.verify(interceptor).beforeExecution(beforeExecutionArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyRequest(modifyRequestArg.capture(), attributes.capture()); inOrder.verify(interceptor).beforeMarshalling(beforeMarshallingArg.capture(), attributes.capture()); inOrder.verify(interceptor).afterMarshalling(afterMarshallingArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyAsyncHttpContent(modifyHttpContentAsyncArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyHttpContent(modifyHttpContentArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyHttpRequest(modifyHttpRequestArg.capture(), attributes.capture()); inOrder.verify(interceptor).beforeTransmission(beforeTransmissionArg.capture(), attributes.capture()); inOrder.verify(interceptor).afterTransmission(afterTransmissionArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyHttpResponse(modifyHttpResponseArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyHttpResponseContent(modifyHttpResponseContentArg.capture(), attributes.capture()); inOrder.verify(interceptor).beforeUnmarshalling(beforeUnmarshallingArg.capture(), attributes.capture()); if (isAsync) { inOrder.verify(interceptor).modifyAsyncHttpResponseContent(modifyAsyncHttpResponseContent.capture(), attributes.capture()); } inOrder.verify(interceptor).afterUnmarshalling(afterUnmarshallingArg.capture(), attributes.capture()); inOrder.verify(interceptor).modifyResponse(modifyResponseArg.capture(), attributes.capture()); inOrder.verify(interceptor).afterExecution(afterExecutionArg.capture(), attributes.capture()); if (expectedException != null) { ArgumentCaptor<Context.FailedExecution> failedExecutionArg = ArgumentCaptor.forClass(Context.FailedExecution.class); inOrder.verify(interceptor).modifyException(failedExecutionArg.capture(), attributes.capture()); inOrder.verify(interceptor).onExecutionFailure(failedExecutionArg.capture(), attributes.capture()); verifyFailedExecutionMethodCalled(failedExecutionArg, true); assertThat(failedExecutionArg.getValue().exception()).isEqualTo(expectedException); } verifyNoMoreInteractions(interceptor); // Verify beforeExecution gets untouched request assertThat(beforeExecutionArg.getValue().request()).isSameAs(inputRequest); // Verify methods were given correct parameters validateArgs(beforeExecutionArg.getValue(), null); validateArgs(modifyRequestArg.getValue(), null); validateArgs(beforeMarshallingArg.getValue(), "1"); validateArgs(afterMarshallingArg.getValue(), "1", null); validateArgs(modifyHttpRequestArg.getValue(), "1", null); validateArgs(beforeTransmissionArg.getValue(), "1", "2"); validateArgs(afterTransmissionArg.getValue(), "1", "2", null); validateArgs(modifyHttpResponseArg.getValue(), "1", "2", null); validateArgs(beforeUnmarshallingArg.getValue(), "1", "2", "3"); validateArgs(afterUnmarshallingArg.getValue(), "1", "2", "3", null); validateArgs(modifyResponseArg.getValue(), "1", "2", "3", null); validateArgs(afterExecutionArg.getValue(), "1", "2", "3", "4"); // Verify same execution attributes were used for all method calls assertThat(attributes.getAllValues()).containsOnly(attributes.getAllValues().get(0)); } private void validateRequestResponse(MembersInHeadersResponse outputResponse) { verify(postRequestedFor(urlPathEqualTo(MEMBERS_IN_HEADERS_PATH)) .withHeader("x-amz-string", equalTo("1")) .withHeader("x-amz-integer", equalTo("2"))); assertThat(outputResponse.integerMember()).isEqualTo(3); assertThat(outputResponse.stringMember()).isEqualTo("4"); } private void expectServiceCallErrorMethodsCalled(ExecutionInterceptor interceptor, boolean isAsync) { ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class); ArgumentCaptor<Context.BeforeUnmarshalling> beforeUnmarshallingArg = ArgumentCaptor.forClass(Context.BeforeUnmarshalling.class); ArgumentCaptor<Context.FailedExecution> failedExecutionArg = ArgumentCaptor.forClass(Context.FailedExecution.class); InOrder inOrder = Mockito.inOrder(interceptor); inOrder.verify(interceptor).beforeExecution(any(), attributes.capture()); inOrder.verify(interceptor).modifyRequest(any(), attributes.capture()); inOrder.verify(interceptor).beforeMarshalling(any(), attributes.capture()); inOrder.verify(interceptor).afterMarshalling(any(), attributes.capture()); inOrder.verify(interceptor).modifyAsyncHttpContent(any(), attributes.capture()); inOrder.verify(interceptor).modifyHttpContent(any(), attributes.capture()); inOrder.verify(interceptor).modifyHttpRequest(any(), attributes.capture()); inOrder.verify(interceptor).beforeTransmission(any(), attributes.capture()); inOrder.verify(interceptor).afterTransmission(any(), attributes.capture()); inOrder.verify(interceptor).modifyHttpResponse(any(), attributes.capture()); inOrder.verify(interceptor).modifyHttpResponseContent(any(), attributes.capture()); inOrder.verify(interceptor).beforeUnmarshalling(beforeUnmarshallingArg.capture(), attributes.capture()); if (isAsync) { inOrder.verify(interceptor).modifyAsyncHttpResponseContent(any(), attributes.capture()); } inOrder.verify(interceptor).modifyException(failedExecutionArg.capture(), attributes.capture()); inOrder.verify(interceptor).onExecutionFailure(failedExecutionArg.capture(), attributes.capture()); verifyNoMoreInteractions(interceptor); // Verify same execution attributes were used for all method calls assertThat(attributes.getAllValues()).containsOnly(attributes.getAllValues().get(0)); // Verify HTTP response assertThat(beforeUnmarshallingArg.getValue().httpResponse().statusCode()).isEqualTo(404); // Verify failed execution parameters assertThat(failedExecutionArg.getValue().exception()).isInstanceOf(SdkServiceException.class); verifyFailedExecutionMethodCalled(failedExecutionArg, false); } private void verifyFailedExecutionMethodCalled(ArgumentCaptor<Context.FailedExecution> failedExecutionArg, boolean expectResponse) { MembersInHeadersRequest failedRequest = (MembersInHeadersRequest) failedExecutionArg.getValue().request(); assertThat(failedRequest.stringMember()).isEqualTo("1"); assertThat(failedExecutionArg.getValue().httpRequest()).hasValueSatisfying(httpRequest -> { assertThat(httpRequest.firstMatchingHeader("x-amz-string")).hasValue("1"); assertThat(httpRequest.firstMatchingHeader("x-amz-integer")).hasValue("2"); }); assertThat(failedExecutionArg.getValue().httpResponse()).hasValueSatisfying(httpResponse -> { assertThat(httpResponse.firstMatchingHeader("x-amz-integer")).hasValue("3"); }); if (expectResponse) { assertThat(failedExecutionArg.getValue().response().map(MembersInHeadersResponse.class::cast)).hasValueSatisfying(response -> { assertThat(response.integerMember()).isEqualTo(3); assertThat(response.stringMember()).isEqualTo("4"); }); } else { assertThat(failedExecutionArg.getValue().response()).isNotPresent(); } } private void validateArgs(Context.BeforeExecution context, String expectedStringMemberValue) { MembersInHeadersRequest request = (MembersInHeadersRequest) context.request(); assertThat(request.stringMember()).isEqualTo(expectedStringMemberValue); } private void validateArgs(Context.AfterMarshalling context, String expectedStringMemberValue, String expectedIntegerHeaderValue) { validateArgs(context, expectedStringMemberValue); assertThat(context.httpRequest().firstMatchingHeader("x-amz-integer")) .isEqualTo(Optional.ofNullable(expectedIntegerHeaderValue)); assertThat(context.httpRequest().firstMatchingHeader("x-amz-string")) .isEqualTo(Optional.ofNullable(expectedStringMemberValue)); } private void validateArgs(Context.AfterTransmission context, String expectedStringMemberValue, String expectedIntegerHeaderValue, String expectedResponseIntegerHeaderValue) { validateArgs(context, expectedStringMemberValue, expectedIntegerHeaderValue); assertThat(context.httpResponse().firstMatchingHeader("x-amz-integer")) .isEqualTo(Optional.ofNullable(expectedResponseIntegerHeaderValue)); } private void validateArgs(Context.AfterUnmarshalling context, String expectedStringMemberValue, String expectedIntegerHeaderValue, String expectedResponseIntegerHeaderValue, String expectedResponseStringMemberValue) { validateArgs(context, expectedStringMemberValue, expectedIntegerHeaderValue, expectedResponseIntegerHeaderValue); MembersInHeadersResponse response = (MembersInHeadersResponse) context.response(); assertThat(response.integerMember()).isEqualTo(toInt(expectedResponseIntegerHeaderValue)); assertThat(response.stringMember()).isEqualTo(expectedResponseStringMemberValue); } private Integer toInt(String stringInteger) { return stringInteger == null ? null : Integer.parseInt(stringInteger); } private ProtocolRestJsonClient client(ExecutionInterceptor interceptor) { return initializeAndBuild(ProtocolRestJsonClient.builder(), interceptor); } private ProtocolRestJsonAsyncClient asyncClient(ExecutionInterceptor interceptor) { return initializeAndBuild(ProtocolRestJsonAsyncClient.builder(), interceptor); } private <T extends AwsClientBuilder<?, U>, U> U initializeAndBuild(T builder, ExecutionInterceptor interceptor) { return builder.region(Region.US_WEST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .overrideConfiguration(ClientOverrideConfiguration.builder() .addExecutionInterceptor(interceptor) .build()) .build(); } private static class MessageUpdatingInterceptor implements ExecutionInterceptor { @Override public SdkRequest modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes) { MembersInHeadersRequest request = (MembersInHeadersRequest) context.request(); return request.toBuilder() .stringMember("1") .build(); } @Override public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { SdkHttpRequest httpRequest = context.httpRequest(); return httpRequest.copy(b -> b.putHeader("x-amz-integer", "2")); } @Override public SdkHttpResponse modifyHttpResponse(Context.ModifyHttpResponse context, ExecutionAttributes executionAttributes) { SdkHttpResponse httpResponse = context.httpResponse(); return httpResponse.copy(b -> b.putHeader("x-amz-integer", Collections.singletonList("3"))); } @Override public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) { MembersInHeadersResponse response = (MembersInHeadersResponse) context.response(); return response.toBuilder() .stringMember("4") .build(); } } private static class NoOpInterceptor implements ExecutionInterceptor { } private static class NoOpAsyncRequestBody implements AsyncRequestBody { @Override public Optional<Long> contentLength() { return Optional.of(0L); } @Override public void subscribe(Subscriber<? super ByteBuffer> s) { // TODO: For content-length 0 should we require them to do this? s.onSubscribe(new Subscription() { @Override public void request(long n) { s.onComplete(); } @Override public void cancel() { } }); } } private static class NoOpAsyncResponseTransformer implements AsyncResponseTransformer<StreamingOutputOperationResponse, Object> { private volatile StreamingOutputOperationResponse response; private volatile CompletableFuture<Object> cf; @Override public CompletableFuture<Object> prepare() { cf = new CompletableFuture<>(); return cf; } @Override public void onResponse(StreamingOutputOperationResponse response) { this.response = response; } @Override public void onStream(SdkPublisher<ByteBuffer> publisher) { publisher.subscribe(new Subscriber<ByteBuffer>() { private Subscription s; @Override public void onSubscribe(Subscription s) { this.s = s; s.request(1); // TODO: Can we simplify the implementation of a "ignore everything" response handler? } @Override public void onNext(ByteBuffer byteBuffer) { s.request(1); } @Override public void onError(Throwable t) { } @Override public void onComplete() { cf.complete(response); } }); } @Override public void exceptionOccurred(Throwable throwable) { throwable.printStackTrace(); cf.completeExceptionally(throwable); } } }
2,385
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/QueryRequestTransformTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.containing; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.matching; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.http.Header; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolquery.ProtocolQueryAsyncClient; import software.amazon.awssdk.services.protocolquery.ProtocolQueryClient; import software.amazon.awssdk.services.protocolquery.model.IdempotentOperationRequest; public class QueryRequestTransformTest extends ProtocolTestBase { @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolQueryClient client; private ProtocolQueryAsyncClient asyncClient; @Before public void setupClient() { client = ProtocolQueryClient.builder() .credentialsProvider(StaticCredentialsProvider.create( AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); asyncClient = ProtocolQueryAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create( AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void syncRequest_isMovingParamsToBodyStage() { stubSimpleResponse(); IdempotentOperationRequest request = IdempotentOperationRequest.builder().idempotencyToken("test").build(); client.idempotentOperation(request); verifyResponseMetadata(); } @Test public void asyncRequest_isMovingParamsToBodyStage() { stubSimpleResponse(); IdempotentOperationRequest request = IdempotentOperationRequest.builder().idempotencyToken("test").build(); asyncClient.idempotentOperation(request).join(); verifyResponseMetadata(); } private void verifyResponseMetadata() { verify(postRequestedFor(anyUrl()) .withHeader(Header.CONTENT_TYPE, equalTo("application/x-www-form-urlencoded; charset=UTF-8")) .withHeader(Header.CONTENT_LENGTH, matching("\\d+")) .withUrl("/") .withRequestBody(containing("Action=IdempotentOperation")) .withRequestBody(containing("Version=")) .withRequestBody(containing("IdempotencyToken=test"))); } private void stubSimpleResponse() { stubFor(post(anyUrl()).willReturn(aResponse() .withStatus(200) .withBody("<IdempotentOperationResponse/>"))); } }
2,386
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/QueryMetadataTest.java
/* * 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.protocol.tests; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static org.assertj.core.api.Assertions.assertThat; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolquery.ProtocolQueryAsyncClient; import software.amazon.awssdk.services.protocolquery.ProtocolQueryClient; import software.amazon.awssdk.services.protocolquery.model.AllTypesResponse; import software.amazon.awssdk.services.protocolquery.model.ProtocolQueryResponse; import software.amazon.awssdk.utils.builder.SdkBuilder; public class QueryMetadataTest extends ProtocolTestBase { private static final String REQUEST_ID = "abcd"; @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolQueryClient client; private ProtocolQueryAsyncClient asyncClient; @Before public void setupClient() { client = ProtocolQueryClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); asyncClient = ProtocolQueryAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void syncResponse_shouldContainResponseMetadata() { stubResponseWithMetadata(); AllTypesResponse allTypesResponse = client.allTypes(SdkBuilder::build); verifyResponseMetadata(allTypesResponse); } @Test public void asyncResponse_shouldContainResponseMetadata() { stubResponseWithMetadata(); AllTypesResponse allTypesResponse = asyncClient.allTypes(SdkBuilder::build).join(); verifyResponseMetadata(allTypesResponse); } @Test public void requestIdInHeaderButNotXml_ShouldContainsResponseMetadata() { stubResponseWithHeaders(); AllTypesResponse allTypesResponse = asyncClient.allTypes(SdkBuilder::build).join(); verifyResponseMetadata(allTypesResponse); } @Test public void requestIdNotInXmlOrHeader_responseMetadataShouldBeUnknown() { stubResponseWithoutHeaders(); AllTypesResponse allTypesResponse = client.allTypes(SdkBuilder::build); verifyUnknownResponseMetadata(allTypesResponse); } private void verifyResponseMetadata(ProtocolQueryResponse allTypesResponse) { assertThat(allTypesResponse.responseMetadata()).isNotNull(); assertThat(allTypesResponse.responseMetadata().requestId()).isEqualTo(REQUEST_ID); } private void verifyUnknownResponseMetadata(ProtocolQueryResponse allTypesResponse) { assertThat(allTypesResponse.responseMetadata()).isNotNull(); assertThat(allTypesResponse.responseMetadata().requestId()).isEqualTo("UNKNOWN"); } private void stubResponseWithHeaders() { stubFor(post(anyUrl()) .willReturn(aResponse().withStatus(200) .withHeader("x-amzn-RequestId", REQUEST_ID) .withBody("<AllTypesResponse/>"))); } private void stubResponseWithMetadata() { stubFor(post(anyUrl()) .willReturn(aResponse().withStatus(200) .withBody("<AllTypesResponse>" + "<ResponseMetadata>" + "<RequestId>" + REQUEST_ID + "</RequestId>" + "</ResponseMetadata>" + "</AllTypesResponse>"))); } private void stubResponseWithoutHeaders() { stubFor(post(anyUrl()) .willReturn(aResponse().withStatus(200) .withBody("<AllTypesResponse/>"))); } }
2,387
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/XmlPayloadStringTest.java
/* * 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.protocol.tests; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.time.Duration; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.http.AbortableInputStream; import software.amazon.awssdk.http.HttpExecuteResponse; import software.amazon.awssdk.http.SdkHttpFullRequest; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlClient; import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadBlobRequest; import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadBlobResponse; import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadStringRequest; import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadStringResponse; import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; public class XmlPayloadStringTest { private final String PAYLOAD_STRING_XML = "<Field>StringPayload</Field>"; private final String PAYLOAD_STRING_NON_XML = "StringPayload"; private final SdkBytes PAYLOAD_BYTES_XML = SdkBytes.fromUtf8String(PAYLOAD_STRING_XML); private final SdkBytes PAYLOAD_BYTES_NON_XML = SdkBytes.fromUtf8String(PAYLOAD_STRING_NON_XML); private ProtocolRestXmlClient syncClient; private MockSyncHttpClient mockHttpClient; @BeforeEach public void setUp() { mockHttpClient = new MockSyncHttpClient(); syncClient = ProtocolRestXmlClient.builder() .credentialsProvider(AnonymousCredentialsProvider.create()) .region(Region.US_EAST_1) .httpClient(mockHttpClient) .build(); } @AfterEach public void reset() { mockHttpClient.reset(); } @Test public void operationWithExplicitPayloadString_xml_marshallsAndUnmarshallsCorrectly() { mockHttpClient.stubNextResponse(mockResponse(PAYLOAD_BYTES_XML), Duration.ofMillis(500)); OperationWithExplicitPayloadStringRequest request = OperationWithExplicitPayloadStringRequest.builder() .payloadMember(PAYLOAD_STRING_XML) .build(); OperationWithExplicitPayloadStringResponse response = syncClient.operationWithExplicitPayloadString(request); SdkHttpFullRequest loggedRequest = (SdkHttpFullRequest) mockHttpClient.getLastRequest(); InputStream loggedRequestStream = loggedRequest.contentStreamProvider().get().newStream(); String loggedRequestBody = new String(SdkBytes.fromInputStream(loggedRequestStream).asByteArray()); assertThat(loggedRequestBody).isEqualTo(PAYLOAD_STRING_XML); assertThat(response.payloadMember()).isEqualTo(PAYLOAD_STRING_XML); } @Test public void operationWithExplicitPayloadString_nonXml_marshallsAndUnmarshallsCorrectly() { mockHttpClient.stubNextResponse(mockResponse(PAYLOAD_BYTES_NON_XML), Duration.ofMillis(500)); OperationWithExplicitPayloadStringRequest request = OperationWithExplicitPayloadStringRequest.builder() .payloadMember(PAYLOAD_STRING_NON_XML) .build(); OperationWithExplicitPayloadStringResponse response = syncClient.operationWithExplicitPayloadString(request); SdkHttpFullRequest loggedRequest = (SdkHttpFullRequest) mockHttpClient.getLastRequest(); InputStream loggedRequestStream = loggedRequest.contentStreamProvider().get().newStream(); String loggedRequestBody = new String(SdkBytes.fromInputStream(loggedRequestStream).asByteArray()); assertThat(loggedRequestBody).isEqualTo(PAYLOAD_STRING_NON_XML); assertThat(response.payloadMember()).isEqualTo(PAYLOAD_STRING_NON_XML); } @Test public void operationWithExplicitPayloadBlob_xml_marshallsAndUnmarshallsCorrectly() { mockHttpClient.stubNextResponse(mockResponse(PAYLOAD_BYTES_XML), Duration.ofMillis(500)); OperationWithExplicitPayloadBlobRequest request = OperationWithExplicitPayloadBlobRequest.builder() .payloadMember(PAYLOAD_BYTES_XML) .build(); OperationWithExplicitPayloadBlobResponse response = syncClient.operationWithExplicitPayloadBlob(request); SdkHttpFullRequest loggedRequest = (SdkHttpFullRequest) mockHttpClient.getLastRequest(); InputStream loggedRequestStream = loggedRequest.contentStreamProvider().get().newStream(); SdkBytes loggedRequestBody = SdkBytes.fromInputStream(loggedRequestStream); assertThat(loggedRequestBody).isEqualTo(PAYLOAD_BYTES_XML); assertThat(response.payloadMember()).isEqualTo(PAYLOAD_BYTES_XML); } @Test public void operationWithExplicitPayloadBlob_nonXml_marshallsAndUnmarshallsCorrectly() { mockHttpClient.stubNextResponse(mockResponse(PAYLOAD_BYTES_NON_XML), Duration.ofMillis(500)); OperationWithExplicitPayloadBlobRequest request = OperationWithExplicitPayloadBlobRequest.builder() .payloadMember(PAYLOAD_BYTES_NON_XML) .build(); OperationWithExplicitPayloadBlobResponse response = syncClient.operationWithExplicitPayloadBlob(request); SdkHttpFullRequest loggedRequest = (SdkHttpFullRequest) mockHttpClient.getLastRequest(); InputStream loggedRequestStream = loggedRequest.contentStreamProvider().get().newStream(); SdkBytes loggedRequestBody = SdkBytes.fromInputStream(loggedRequestStream); assertThat(loggedRequestBody).isEqualTo(PAYLOAD_BYTES_NON_XML); assertThat(response.payloadMember()).isEqualTo(PAYLOAD_BYTES_NON_XML); } private HttpExecuteResponse mockResponse(SdkBytes sdkBytes) { InputStream inputStream = new ByteArrayInputStream(sdkBytes.asByteArray()); return HttpExecuteResponse.builder() .response(SdkHttpResponse.builder() .statusCode(200) .build()) .responseBody(AbortableInputStream.create(inputStream)) .build(); } }
2,388
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/Ec2ProtocolTest.java
/* * 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.protocol.tests; import org.junit.BeforeClass; import org.junit.Test; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; public class Ec2ProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader testSuiteLoader = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/ec2-2016-03-11-intermediate.json"); } @Test public void run() throws Exception { testRunner.runTests(testSuiteLoader.load("ec2-suite.json")); } }
2,389
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/TruncatedRequestStreamTest.java
/* * 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.protocol.tests; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.http.AbortableInputStream; import software.amazon.awssdk.http.ContentStreamProvider; import software.amazon.awssdk.http.ExecutableHttpRequest; import software.amazon.awssdk.http.HttpExecuteRequest; import software.amazon.awssdk.http.HttpExecuteResponse; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.model.StreamingInputOperationRequest; public class TruncatedRequestStreamTest { private ProtocolRestJsonClient client; private SdkHttpClient mockHttpClient; @BeforeEach public void setup() throws IOException { mockHttpClient = mock(SdkHttpClient.class); HttpExecuteResponse response = HttpExecuteResponse.builder() .response(SdkHttpResponse.builder() .statusCode(200) .putHeader("Content-Length", "2") .build()) .responseBody(AbortableInputStream.create( new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)))) .build(); ExecutableHttpRequest mockExecutableRequest = mock(ExecutableHttpRequest.class); when(mockExecutableRequest.call()).thenReturn(response); when(mockHttpClient.prepareRequest(any())).thenReturn(mockExecutableRequest); client = ProtocolRestJsonClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_WEST_2) .httpClient(mockHttpClient) .build(); } @AfterEach public void teardown() { client.close(); } @Test public void streamingRequest_specifiedLengthTruncatesStream_lengthHonored() throws IOException { int bodyLength = 16; client.streamingInputOperation(StreamingInputOperationRequest.builder().build(), bodyFromStream(32, bodyLength)); ArgumentCaptor<HttpExecuteRequest> requestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); verify(mockHttpClient).prepareRequest(requestCaptor.capture()); HttpExecuteRequest executeRequest = requestCaptor.getValue(); ContentStreamProvider streamProvider = executeRequest.contentStreamProvider().get(); String contentLengthHeader = executeRequest.httpRequest().firstMatchingHeader("Content-Length").get(); assertThat(contentLengthHeader).isEqualTo(Integer.toString(bodyLength)); assertThat(drainStream(streamProvider.newStream())).isEqualTo(bodyLength); } private static RequestBody bodyFromStream(int streamLength, int bodyLength) { InputStream is = new ByteArrayInputStream(new byte[streamLength]); return RequestBody.fromInputStream(is, bodyLength); } private static int drainStream(InputStream is) throws IOException { byte[] buff = new byte[32]; int totalRead = 0; while (true) { int read = is.read(buff); if (read == -1) { break; } totalRead += read; } return totalRead; } }
2,390
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/RestXmlProtocolTest.java
/* * 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.protocol.tests; import java.io.IOException; import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import software.amazon.awssdk.protocol.ProtocolTestSuiteLoader; import software.amazon.awssdk.protocol.model.TestCase; import software.amazon.awssdk.protocol.runners.ProtocolTestRunner; @RunWith(Parameterized.class) public class RestXmlProtocolTest extends ProtocolTestBase { private static final ProtocolTestSuiteLoader testSuiteLoader = new ProtocolTestSuiteLoader(); private static ProtocolTestRunner testRunner; @Parameterized.Parameter public TestCase testCase; @Parameterized.Parameters(name = "{0}") public static List<TestCase> data() throws IOException { return testSuiteLoader.load("restxml-suite.json"); } @BeforeClass public static void setupFixture() { testRunner = new ProtocolTestRunner("/models/restxml-2016-03-11-intermediate.json"); } @Test public void runProtocolTest() throws Exception { testRunner.runTest(testCase); } }
2,391
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/customservicemetadata/CustomServiceMetaDataServiceTest.java
package software.amazon.awssdk.protocol.tests.customservicemetadata; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.containing; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.common.SingleRootFileSource; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.net.URI; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocoljsonrpc.ProtocolJsonRpcClient; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesRequest; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesResponse; import software.amazon.awssdk.services.protocoljsonrpccustomized.ProtocolJsonRpcCustomizedClient; import software.amazon.awssdk.services.protocoljsonrpccustomized.model.SimpleRequest; public class CustomServiceMetaDataServiceTest { @Rule public WireMockRule mockServer = new WireMockRule(WireMockConfiguration.wireMockConfig() .port(0) .fileSource(new SingleRootFileSource("src/test/resources"))); private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; private static final String JSON_BODY_GZIP = "compressed_json_body.gz"; private static final String JSON_BODY_Crc32_CHECKSUM = "3049587505"; private static final String JSON_BODY_GZIP_Crc32_CHECKSUM = "3023995622"; private static final StaticCredentialsProvider FAKE_CREDENTIALS_PROVIDER = StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar")); @Test public void clientWithCustomizedMetadata_has_contenttype_as_mentioned_customization() { stubFor(post(urlEqualTo("/")).willReturn(aResponse() .withStatus(200) .withHeader("Content-Encoding", "gzip") .withHeader("x-amz-crc32", JSON_BODY_GZIP_Crc32_CHECKSUM) .withBodyFile(JSON_BODY_GZIP))); ProtocolJsonRpcCustomizedClient jsonRpc = ProtocolJsonRpcCustomizedClient.builder() .credentialsProvider(FAKE_CREDENTIALS_PROVIDER) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + mockServer.port())) .build(); jsonRpc.simple(SimpleRequest.builder().build()); WireMock.verify(postRequestedFor(urlPathEqualTo("/")) .withHeader("Content-Type", containing("application/json"))); } @Test public void clientWithNoCustomizedMetadata_has_default_contenttype() { stubFor(post(urlEqualTo("/")).willReturn(aResponse() .withStatus(200) .withHeader("x-amz-crc32", JSON_BODY_Crc32_CHECKSUM) .withBody(JSON_BODY))); ProtocolJsonRpcClient jsonRpc = ProtocolJsonRpcClient.builder() .credentialsProvider(FAKE_CREDENTIALS_PROVIDER) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + mockServer.port())) .build(); AllTypesResponse result = jsonRpc.allTypes(AllTypesRequest.builder().build()); WireMock.verify(postRequestedFor(urlPathEqualTo("/")) .withHeader("Content-Type", containing("application/x-amz-json-1.1"))); } }
2,392
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/retry/AsyncAwsJsonRetryTest.java
/* * 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.protocol.tests.retry; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.stubbing.Scenario; import java.net.URI; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocoljsonrpc.ProtocolJsonRpcAsyncClient; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesRequest; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesResponse; import software.amazon.awssdk.services.protocoljsonrpc.model.ProtocolJsonRpcException; public class AsyncAwsJsonRetryTest { private static final String PATH = "/"; private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolJsonRpcAsyncClient client; @Before public void setupClient() { client = ProtocolJsonRpcAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create ("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void shouldRetryOn500() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(500))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()).join(); assertThat(allTypesResponse).isNotNull(); } @Test public void shouldRetryOnRetryableAwsErrorCode() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at PriorRequestNotComplete") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(400) .withHeader("x-amzn-ErrorType", "PriorRequestNotComplete") .withBody("\"{\"__type\":\"PriorRequestNotComplete\",\"message\":\"Blah " + "error\"}\""))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at PriorRequestNotComplete") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()).join(); assertThat(allTypesResponse).isNotNull(); } @Test public void shouldRetryOnAwsThrottlingErrorCode() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at SlowDown") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(400) .withHeader("x-amzn-ErrorType", "SlowDown") .withBody("\"{\"__type\":\"SlowDown\",\"message\":\"Blah " + "error\"}\""))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at SlowDown") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()).join(); assertThat(allTypesResponse).isNotNull(); } @Test public void retryPolicyNone_shouldNotRetry() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(500))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); ProtocolJsonRpcAsyncClient clientWithNoRetry = ProtocolJsonRpcAsyncClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .overrideConfiguration(c -> c.retryPolicy(RetryPolicy.none())) .build(); assertThatThrownBy(() -> clientWithNoRetry.allTypes(AllTypesRequest.builder().build()).join()) .hasCauseInstanceOf(ProtocolJsonRpcException.class); } }
2,393
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/retry/AwsJsonRetryTest.java
/* * 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.protocol.tests.retry; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.stubbing.Scenario; import java.net.URI; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocoljsonrpc.ProtocolJsonRpcClient; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesRequest; import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesResponse; import software.amazon.awssdk.services.protocoljsonrpc.model.ProtocolJsonRpcException; public class AwsJsonRetryTest { private static final String PATH = "/"; private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; @Rule public WireMockRule wireMock = new WireMockRule(0); private ProtocolJsonRpcClient client; @Before public void setupClient() { client = ProtocolJsonRpcClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .build(); } @Test public void shouldRetryOn500() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(500))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); assertThat(allTypesResponse).isNotNull(); } @Test public void shouldRetryOnRetryableAwsErrorCode() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at PriorRequestNotComplete") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(400) .withHeader("x-amzn-ErrorType", "PriorRequestNotComplete") .withBody("\"{\"__type\":\"PriorRequestNotComplete\",\"message\":\"Blah " + "error\"}\""))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at PriorRequestNotComplete") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); assertThat(allTypesResponse).isNotNull(); } @Test public void shouldRetryOnAwsThrottlingErrorCode() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at SlowDown") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(400) .withHeader("x-amzn-ErrorType", "SlowDown") .withBody("\"{\"__type\":\"SlowDown\",\"message\":\"Blah " + "error\"}\""))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at SlowDown") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); assertThat(allTypesResponse).isNotNull(); } @Test public void retryPolicyNone_shouldNotRetry() { stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs(Scenario.STARTED) .willSetStateTo("first attempt") .willReturn(aResponse() .withStatus(500))); stubFor(post(urlEqualTo(PATH)) .inScenario("retry at 500") .whenScenarioStateIs("first attempt") .willSetStateTo("second attempt") .willReturn(aResponse() .withStatus(200) .withBody(JSON_BODY))); ProtocolJsonRpcClient clientWithNoRetry = ProtocolJsonRpcClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .endpointOverride(URI.create("http://localhost:" + wireMock.port())) .overrideConfiguration(c -> c.retryPolicy(RetryPolicy.none())) .build(); assertThatThrownBy(() -> clientWithNoRetry.allTypes(AllTypesRequest.builder().build())).isInstanceOf(ProtocolJsonRpcException.class); } }
2,394
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/connection/SyncClientConnectionInterruptionTest.java
/* * 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.protocol.tests.connection; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static org.assertj.core.api.Assertions.assertThat; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; import com.github.tomakehurst.wiremock.core.WireMockConfiguration; import java.net.URI; import java.time.Duration; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.exception.AbortedException; import software.amazon.awssdk.core.exception.ApiCallAttemptTimeoutException; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.http.SdkHttpClient; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.metrics.MetricCollection; import software.amazon.awssdk.metrics.MetricPublisher; import software.amazon.awssdk.metrics.MetricRecord; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClientBuilder; import software.amazon.awssdk.services.protocolrestjson.model.AllTypesResponse; /** * Tests to verify Interruption of Threads while Http Connection is in progress to make sure Resources are released. */ class SyncClientConnectionInterruptionTest { public static final String SAMPLE_BODY = "{\"StringMember" + "\":\"resultString\"}"; private final WireMockServer mockServer = new WireMockServer(new WireMockConfiguration() .bindAddress("localhost").dynamicPort()); private static final ExecutorService executorService = Executors.newCachedThreadPool(); @BeforeEach public void setup() { mockServer.start(); stubPostRequest(".*", aResponse(), "{}"); } @AfterAll public static void cleanUp(){ executorService.shutdownNow(); } @Test void connectionPoolsGetsReusedWhenInterruptedWith_1_MaxConnection() throws Exception { Integer responseDelay = 1500; String urlRegex = "/2016-03-11/allTypes"; stubPostRequest(urlRegex, aResponse().withFixedDelay(responseDelay), SAMPLE_BODY); SdkHttpClient httpClient = ApacheHttpClient.builder().maxConnections(1).build(); ProtocolRestJsonClient client = getClient(httpClient, Duration.ofMillis(2L * responseDelay)).build(); Future<?> toBeInterruptedFuture = executorService.submit(() -> client.allTypes()); unInterruptedSleep(responseDelay - responseDelay / 5); toBeInterruptedFuture.cancel(true); // Make sure thread start the Http connections unInterruptedSleep(50); AllTypesResponse allTypesResponse = client.allTypes(); assertThat(allTypesResponse.stringMember()).isEqualTo("resultString"); executorService.shutdownNow(); } @Test void interruptionWhenWaitingForLease_AbortsImmediately() throws InterruptedException { Integer responseDelay = 50000; ExceptionInThreadRun exceptionInThreadRun = new ExceptionInThreadRun(); AtomicLong leaseWaitingTime = new AtomicLong(responseDelay); stubPostRequest("/2016-03-11/allTypes", aResponse().withFixedDelay(responseDelay), SAMPLE_BODY); SdkHttpClient httpClient = ApacheHttpClient.builder().maxConnections(1).build(); ProtocolRestJsonClient client = getClient(httpClient, Duration.ofMillis(2L * responseDelay)).build(); executorService.submit(() -> client.allTypes()); // 1 Sec sleep to make sure Thread 1 is picked for executing Http connection unInterruptedSleep(1000); Thread leaseWaitingThread = new Thread(() -> { try { client.allTypes(l -> l.overrideConfiguration( b -> b .addMetricPublisher(new MetricPublisher() { @Override public void publish(MetricCollection metricCollection) { Optional<MetricRecord<?>> apiCallDuration = metricCollection.stream().filter(o -> "ApiCallDuration".equals(o.metric().name())).findAny(); leaseWaitingTime.set(Duration.parse(apiCallDuration.get().value().toString()).toMillis()); } @Override public void close() { } }) )); } catch (Exception exception) { exceptionInThreadRun.setException(exception); } }); leaseWaitingThread.start(); // 1 sec sleep to make sure Http connection execution is initialized for Thread 2 , in this case it will wait for lease // and immediately terminate on interrupt unInterruptedSleep(1000); leaseWaitingThread.interrupt(); leaseWaitingThread.join(); assertThat(leaseWaitingTime.get()).isNotEqualTo(responseDelay.longValue()); assertThat(leaseWaitingTime.get()).isLessThan(responseDelay.longValue()); assertThat(exceptionInThreadRun.getException()).isInstanceOf(AbortedException.class); client.close(); } /** * Service Latency is set to high value say X. * Api timeout value id set to 1/3 of X. * And we interrupt the thread at 90% of X. * In this case since the ApiTimeOut first happened we should get ApiTimeOut Exception and not the interrupt. */ @Test void interruptionDueToApiTimeOut_followed_byInterruptCausesOnlyTimeOutException() throws InterruptedException { SdkHttpClient httpClient = ApacheHttpClient.create(); Integer responseDelay = 3000; stubPostRequest("/2016-03-11/allTypes", aResponse().withFixedDelay(responseDelay), SAMPLE_BODY); ExceptionInThreadRun exception = new ExceptionInThreadRun(); ProtocolRestJsonClient client = getClient(httpClient, Duration.ofMillis(10)).overrideConfiguration(o -> o.retryPolicy(RetryPolicy.none())).build(); unInterruptedSleep(100); // We need to creat a separate thread to interrupt it externally. Thread leaseWaitingThread = new Thread(() -> { try { client.allTypes(l -> l.overrideConfiguration(b -> b.apiCallAttemptTimeout(Duration.ofMillis(responseDelay / 3)))); } catch (Exception e) { exception.setException(e); } }); leaseWaitingThread.start(); unInterruptedSleep(responseDelay - responseDelay / 10); leaseWaitingThread.interrupt(); leaseWaitingThread.join(); assertThat(exception.getException()).isInstanceOf(ApiCallAttemptTimeoutException.class); client.close(); } private class ExceptionInThreadRun { private Exception exception; public Exception getException() { return exception; } public void setException(Exception exception) { this.exception = exception; } } static void unInterruptedSleep(long millis){ try { Thread.sleep(millis); } catch (InterruptedException e) { throw new IllegalStateException("This test sleep is not be interrupted"); } } private void stubPostRequest(String urlRegex, ResponseDefinitionBuilder LONG_DELAY, String body) { mockServer.stubFor(post(urlMatching(urlRegex)) .willReturn(LONG_DELAY .withStatus(200) .withBody(body))); } private ProtocolRestJsonClientBuilder getClient(SdkHttpClient httpClient, Duration timeOutDuration) { return ProtocolRestJsonClient.builder() .credentialsProvider( StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .endpointOverride(URI.create("http://localhost:" + mockServer.port())) .httpClient(httpClient) .overrideConfiguration(o -> o.apiCallTimeout(timeOutDuration)); } }
2,395
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/connection/SyncClientConnectionTest.java
/* * 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.protocol.tests.connection; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; import java.io.InputStream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.sync.ResponseTransformer; import software.amazon.awssdk.http.AbortableInputStream; import software.amazon.awssdk.http.HttpExecuteResponse; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.protocol.tests.util.ClosableStringInputStream; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest; import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonException; import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; import software.amazon.awssdk.utils.builder.SdkBuilder; /** * Tests to verify the correction of connection closure. */ public class SyncClientConnectionTest { private ProtocolRestJsonClient client; private MockSyncHttpClient mockHttpClient; @BeforeEach public void setupClient() { mockHttpClient = new MockSyncHttpClient(); client = ProtocolRestJsonClient.builder() .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))) .region(Region.US_EAST_1) .httpClient(mockHttpClient) .build(); } @Test public void nonStreaming_exception_shouldCloseConnection() throws IOException { ClosableStringInputStream inputStream = new ClosableStringInputStream("{\"__type\":\"SomeUnknownType\"}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 400)); assertThatThrownBy(() -> client.allTypes(AllTypesRequest.builder().build())) .isExactlyInstanceOf(ProtocolRestJsonException.class); assertThat(inputStream.isClosed()).isTrue(); } @Test public void nonStreaming_successfulResponse_shouldCloseConnection() { ClosableStringInputStream inputStream = new ClosableStringInputStream("{}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 200)); client.allTypes(AllTypesRequest.builder().build()); assertThat(inputStream.isClosed()).isTrue(); } @Test public void streamingOut_successfulResponse_shouldCloseConnection() { ClosableStringInputStream inputStream = new ClosableStringInputStream("{}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 200)); client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes()); assertThat(inputStream.isClosed()).isTrue(); } @Test public void streamingOut_errorResponse_shouldCloseConnection() { ClosableStringInputStream inputStream = new ClosableStringInputStream("{\"__type\":\"SomeUnknownType\"}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 400)); assertThatThrownBy(() -> client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toBytes())) .isExactlyInstanceOf(ProtocolRestJsonException.class); assertThat(inputStream.isClosed()).isTrue(); } @Test public void streamingOut_connectionLeftOpen_shouldNotCloseStream() { ClosableStringInputStream inputStream = new ClosableStringInputStream("{}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 200)); client.streamingOutputOperation(b -> b.build(), new ResponseTransformer<StreamingOutputOperationResponse, Object>() { @Override public Object transform(StreamingOutputOperationResponse response, AbortableInputStream inputStream) throws Exception { return null; } @Override public boolean needsConnectionLeftOpen() { return true; } }); assertThat(inputStream.isClosed()).isFalse(); } @Test public void streamingOut_toInputStream_closeResponseStreamShouldCloseUnderlyingStream() throws IOException { ClosableStringInputStream inputStream = new ClosableStringInputStream("{}"); mockHttpClient.stubNextResponse(mockResponse(inputStream, 200)); ResponseInputStream<StreamingOutputOperationResponse> responseInputStream = client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toInputStream()); assertThat(inputStream.isClosed()).isFalse(); responseInputStream.close(); assertThat(inputStream.isClosed()).isTrue(); } @Test public void streamingIn_providedRequestBody_shouldNotCloseRequestStream() { ClosableStringInputStream responseStream = new ClosableStringInputStream("{}"); ClosableStringInputStream requestStream = new ClosableStringInputStream("{}"); mockHttpClient.stubNextResponse(mockResponse(responseStream, 200)); RequestBody requestBody = RequestBody.fromInputStream(requestStream, requestStream.getString().getBytes().length); client.streamingInputOperation(SdkBuilder::build, requestBody); assertThat(responseStream.isClosed()).isTrue(); assertThat(requestStream.isClosed()).isFalse(); } @Test public void closeClient_nonManagedHttpClient_shouldNotCloseUnderlyingHttpClient() { client.close(); assertThat(mockHttpClient.isClosed()).isFalse(); } private HttpExecuteResponse mockResponse(InputStream inputStream, int statusCode) { return HttpExecuteResponse.builder() .response(SdkHttpResponse.builder().statusCode(statusCode).build()) .responseBody(AbortableInputStream.create(inputStream)) .build(); } }
2,396
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/util/ClosableStringInputStream.java
/* * 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.protocol.tests.util; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; /** * Closable StringInputStream. Once closed, cannot be read again */ public class ClosableStringInputStream extends ByteArrayInputStream { private final String string; private boolean isClosed; public ClosableStringInputStream(String s) { super(s.getBytes(StandardCharsets.UTF_8)); this.string = s; } @Override public void close() { isClosed = true; } public boolean isClosed() { return isClosed; } public String getString() { return string; } }
2,397
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/util
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/util/exception/ExceptionTestUtils.java
/* * 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.protocol.tests.util.exception; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; public class ExceptionTestUtils { /** * Convenience method to configure WireMock to stub a 404 response for the specified path with * the given body. * * @param path Matching path to stub for * @param body Body to return in 404 response. */ public static void stub404Response(String path, String body) { stubFor(post(urlEqualTo(path)).willReturn(aResponse().withStatus(404).withBody(body))); } }
2,398
0
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests
Create_ds/aws-sdk-java-v2/test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/timeout/BaseApiCallTimeoutTest.java
/* * 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.protocol.tests.timeout; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import java.time.Duration; import org.junit.jupiter.api.Test; import software.amazon.awssdk.utils.Pair; /** * Contains common scenarios to test timeout feature. */ public abstract class BaseApiCallTimeoutTest extends BaseTimeoutTest { protected static final Duration TIMEOUT = Duration.ofMillis(300); protected static final Duration DELAY_BEFORE_TIMEOUT = Duration.ofMillis(10); protected static final Duration DELAY_AFTER_TIMEOUT = Duration.ofMillis(500); @Test public void nonstreamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception { stubSuccessResponse(DELAY_BEFORE_TIMEOUT); verifySuccessResponseNotTimedOut(); } @Test public void nonstreamingOperation_notFinishedWithinTime_shouldTimeout() { stubSuccessResponse(DELAY_AFTER_TIMEOUT); verifyTimedOut(); } @Test public void nonstreamingOperation500_notFinishedWithinTime_shouldTimeout() { stubErrorResponse(DELAY_AFTER_TIMEOUT); verifyTimedOut(); } @Test public void nonstreamingOperation500_finishedWithinTime_shouldNotTimeout() throws Exception { stubErrorResponse(DELAY_BEFORE_TIMEOUT); verifyFailedResponseNotTimedOut(); } @Test public void streamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception { stubSuccessResponse(DELAY_BEFORE_TIMEOUT); verifySuccessResponseNotTimedOut(); } @Test public void streamingOperation_notFinishedWithinTime_shouldTimeout() { stubSuccessResponse(DELAY_AFTER_TIMEOUT); verifyTimedOut(); } @Test public void nonstreamingOperation_retrySucceeded_FinishedWithinTime_shouldNotTimeout() throws Exception { mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT), Pair.of(mockResponse(200), DELAY_BEFORE_TIMEOUT)); assertThat(retryableCallable().call()).isNotNull(); verifyRequestCount(2); } @Test public void nonstreamingOperation_retryWouldSucceed_notFinishedWithinTime_shouldTimeout() { mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT), Pair.of(mockResponse(200), DELAY_AFTER_TIMEOUT)); verifyRetryableTimeout(); verifyRequestCount(2); } }
2,399