Search in sources :

Example 1 with Aws4SignerParams

use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project hibernate-search by hibernate.

the class AwsSigningRequestInterceptor method sign.

private void sign(HttpRequest request, HttpContext context, HttpEntityContentStreamProvider contentStreamProvider) {
    SdkHttpFullRequest awsRequest = toAwsRequest(request, context, contentStreamProvider);
    if (log.isTraceEnabled()) {
        log.tracef("HTTP request (before signing): %s", request);
        log.tracef("AWS request (before signing): %s", awsRequest);
    }
    AwsCredentials credentials = credentialsProvider.resolveCredentials();
    log.tracef("AWS credentials: %s", credentials);
    Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingRegion(region).signingName(ELASTICSEARCH_SERVICE_NAME).build();
    awsRequest = signer.sign(awsRequest, signerParams);
    // We don't expect signing to affect anything else (path, query, content, ...).
    for (Map.Entry<String, List<String>> header : awsRequest.headers().entrySet()) {
        String name = header.getKey();
        boolean first = true;
        for (String value : header.getValue()) {
            if (first) {
                request.setHeader(name, value);
                first = false;
            } else {
                request.addHeader(name, value);
            }
        }
    }
    if (log.isTraceEnabled()) {
        log.tracef("AWS request (after signing): %s", awsRequest);
        log.tracef("HTTP request (after signing): %s", request);
    }
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams) List(java.util.List) Map(java.util.Map)

Example 2 with Aws4SignerParams

use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams 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 3 with Aws4SignerParams

use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams 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 4 with Aws4SignerParams

use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams 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 5 with Aws4SignerParams

use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project aws-sdk-java-v2 by aws.

the class SignerTestUtils method signAsyncRequest.

public static AsyncRequestBody signAsyncRequest(BaseAsyncAws4Signer signer, SdkHttpFullRequest request, AsyncRequestBody asyncRequestBody, AwsCredentials credentials, String signingName, Clock signingDateOverride, String region) {
    Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingName(signingName).signingClockOverride(signingDateOverride).signingRegion(Region.of(region)).build();
    final Aws4SignerRequestParams requestParams = new Aws4SignerRequestParams(signerParams);
    return signer.signAsync(request, asyncRequestBody, requestParams, signerParams);
}
Also used : Aws4SignerParams(software.amazon.awssdk.auth.signer.params.Aws4SignerParams)

Aggregations

Aws4SignerParams (software.amazon.awssdk.auth.signer.params.Aws4SignerParams)11 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)5 Test (org.junit.jupiter.api.Test)4 URI (java.net.URI)3 List (java.util.List)3 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)3 ByteBuffer (java.nio.ByteBuffer)2 Instant (java.time.Instant)2 Map (java.util.Map)2 Nullable (org.jetbrains.annotations.Nullable)2 HttpHeader (org.mvnsearch.http.model.HttpHeader)2 Subscriber (org.reactivestreams.Subscriber)2 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)2 SdkHttpMethod (software.amazon.awssdk.http.SdkHttpMethod)2 Flowable (io.reactivex.Flowable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1