Search in sources :

Example 6 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class Aws4EventStreamSignerTest method testEventStreamSigning.

@Test
public void testEventStreamSigning() {
    TestVector testVector = generateTestVector();
    SdkHttpFullRequest.Builder request = testVector.httpFullRequest();
    AwsBasicCredentials credentials = AwsBasicCredentials.create("access", "secret");
    SdkHttpFullRequest signedRequest = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingClock(), "us-east-1");
    AsyncRequestBody transformedPublisher = SignerTestUtils.signAsyncRequest(signer, signedRequest, testVector.requestBodyPublisher(), credentials, "demo", signingClock(), "us-east-1");
    TestSubscriber testSubscriber = TestSubscriber.create();
    Flowable.fromPublisher(transformedPublisher).flatMap(new Function<ByteBuffer, Publisher<?>>() {

        Queue<Message> messages = new LinkedList<>();

        MessageDecoder decoder = new MessageDecoder(message -> messages.offer(message));

        @Override
        public Publisher<?> apply(ByteBuffer byteBuffer) throws Exception {
            decoder.feed(byteBuffer.array());
            List<Message> messageList = new ArrayList<>();
            while (!messages.isEmpty()) {
                messageList.add(messages.poll());
            }
            return Flowable.fromIterable(messageList);
        }
    }).subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertComplete();
    testSubscriber.assertValueSequence(testVector.expectedMessagePublisher().blockingIterable());
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BiFunction(io.reactivex.functions.BiFunction) SignerTestUtils(software.amazon.awssdk.auth.signer.internal.SignerTestUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Message(software.amazon.eventstream.Message) EVENT_STREAM_DATE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_DATE) ArrayList(java.util.ArrayList) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BinaryUtils(software.amazon.awssdk.utils.BinaryUtils) TestSubscriber(io.reactivex.subscribers.TestSubscriber) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) ZoneOffset(java.time.ZoneOffset) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Mockito.times(org.mockito.Mockito.times) EVENT_STREAM_SIGNATURE(software.amazon.awssdk.auth.signer.internal.BaseEventStreamAsyncAws4Signer.EVENT_STREAM_SIGNATURE) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) MessageDecoder(software.amazon.eventstream.MessageDecoder) Mockito.never(org.mockito.Mockito.never) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Function(io.reactivex.functions.Function) Lists(org.assertj.core.util.Lists) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Subscription(org.reactivestreams.Subscription) Clock(java.time.Clock) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) HeaderValue(software.amazon.eventstream.HeaderValue) Message(software.amazon.eventstream.Message) ArrayList(java.util.ArrayList) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ByteBuffer(java.nio.ByteBuffer) BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) MessageDecoder(software.amazon.eventstream.MessageDecoder) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Queue(java.util.Queue) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 7 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignatureWithChecksum.

@Test
void test_sign_computesCorrectSignatureWithChecksum() {
    Aws4Signer aws4Signer = Aws4Signer.create();
    AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
    byte[] content = "abc".getBytes(StandardCharsets.UTF_8);
    ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
    AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
    SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
    AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
    Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).checksumParams(SignerChecksumParams.builder().algorithm(Algorithm.SHA256).checksumHeaderName("x-amzn-header").isStreamingRequest(true).build()).build();
    List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
    final SdkHttpFullRequest sdkHttpFullRequest = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join();
    List<String> asyncSignature = sdkHttpFullRequest.headers().get("Authorization");
    assertThat(asyncSignature).isEqualTo(syncSignature);
    final List<String> stringList = sdkHttpFullRequest.headers().get("x-amzn-header");
    assertThat(stringList.stream().findFirst()).hasValue("ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=");
    assertThat(sdkHttpFullRequest.firstMatchingHeader(("x-amzn-trailer"))).isNotPresent();
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.jupiter.api.Test)

Example 8 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class NonStreamingAsyncBodyAws4SignerTest method test_sign_publisherThrows_exceptionPropagated.

@Test
void test_sign_publisherThrows_exceptionPropagated() {
    AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
    RuntimeException error = new RuntimeException("error");
    Flowable<ByteBuffer> errorPublisher = Flowable.error(error);
    AsyncRequestBody asyncBody = new AsyncRequestBody() {

        @Override
        public Optional<Long> contentLength() {
            return Optional.of(42L);
        }

        @Override
        public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
            errorPublisher.subscribe(subscriber);
        }
    };
    SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").build();
    AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
    Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
    assertThatThrownBy(asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams)::join).hasCause(error);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) Subscriber(org.reactivestreams.Subscriber) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 9 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignature.

@Test
void test_sign_computesCorrectSignature() {
    Aws4Signer aws4Signer = Aws4Signer.create();
    AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
    byte[] content = "Hello AWS!".getBytes(StandardCharsets.UTF_8);
    ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
    AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
    SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
    AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
    Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
    List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
    List<String> asyncSignature = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join().headers().get("Authorization");
    assertThat(asyncSignature).isEqualTo(syncSignature);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) Test(org.junit.jupiter.api.Test)

Example 10 with AsyncRequestBody

use of software.amazon.awssdk.core.async.AsyncRequestBody in project aws-sdk-java-v2 by aws.

the class EventStreamInitialRequestInterceptorTest method testInitialRequestEvent.

@Test
public void testInitialRequestEvent() {
    ModifyHttpRequest context = buildContext(bytePublisher, payload);
    Optional<AsyncRequestBody> modifiedBody = interceptor.modifyAsyncHttpContent(context, attr);
    List<Message> messages = Flowable.fromPublisher(modifiedBody.get()).map(Message::decode).toList().blockingGet();
    Message initialRequestEvent = messages.get(0);
    assertArrayEquals(payload, initialRequestEvent.getPayload());
    assertEquals(RPC_CONTENT_TYPE, initialRequestEvent.getHeaders().get(":content-type").getString());
}
Also used : Message(software.amazon.eventstream.Message) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody) ModifyHttpRequest(software.amazon.awssdk.core.interceptor.Context.ModifyHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)51 ByteBuffer (java.nio.ByteBuffer)25 Test (org.junit.jupiter.api.Test)24 List (java.util.List)17 Test (org.junit.Test)13 Subscriber (org.reactivestreams.Subscriber)12 CompletableFuture (java.util.concurrent.CompletableFuture)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)11 Publisher (org.reactivestreams.Publisher)9 Flowable (io.reactivex.Flowable)8 Consumer (java.util.function.Consumer)8 AsyncAws4Signer (software.amazon.awssdk.auth.signer.AsyncAws4Signer)8 AsyncResponseTransformer (software.amazon.awssdk.core.async.AsyncResponseTransformer)8 Signer (software.amazon.awssdk.core.signer.Signer)8 MetricCollector (software.amazon.awssdk.metrics.MetricCollector)8 Collections (java.util.Collections)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Generated (software.amazon.awssdk.annotations.Generated)7 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)7