Search in sources :

Example 1 with AwsSigningConfig

use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.

the class SigningTest method testSigningSuccess.

@Test
public void testSigningSuccess() throws Exception {
    try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
        HttpRequest request = createSimpleRequest("https://www.example.com", "POST", "/derp", "<body>Hello</body>");
        Predicate<String> filterParam = param -> !param.equals("bad-param");
        try (AwsSigningConfig config = new AwsSigningConfig()) {
            config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
            config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS);
            config.setRegion("us-east-1");
            config.setService("service");
            config.setTime(System.currentTimeMillis());
            config.setCredentialsProvider(provider);
            config.setShouldSignHeader(filterParam);
            config.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            HttpRequest signedRequest = result.get();
            assertNotNull(signedRequest);
            assertTrue(hasHeader(signedRequest, "X-Amz-Date"));
            assertTrue(hasHeader(signedRequest, "Authorization"));
        }
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) Arrays(java.util.Arrays) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) SimpleDateFormat(java.text.SimpleDateFormat) CompletableFuture(java.util.concurrent.CompletableFuture) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) AwsSigningResult(software.amazon.awssdk.crt.auth.signing.AwsSigningResult) URI(java.net.URI) DelegateCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsProvider) software.amazon.awssdk.crt(software.amazon.awssdk.crt) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) TimeZone(java.util.TimeZone) Predicate(java.util.function.Predicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) Assert.assertTrue(org.junit.Assert.assertTrue) DelegateCredentialsHandler(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsHandler) Test(org.junit.Test) ByteBufferUtils.transferData(software.amazon.awssdk.crt.utils.ByteBufferUtils.transferData) StandardCharsets(java.nio.charset.StandardCharsets) AwsSigner(software.amazon.awssdk.crt.auth.signing.AwsSigner) List(java.util.List) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) AwsSigningUtils(software.amazon.awssdk.crt.auth.signing.AwsSigningUtils) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) Test(org.junit.Test)

Example 2 with AwsSigningConfig

use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.

the class SigningTest method testQuerySigningSuccess.

@Test
public void testQuerySigningSuccess() throws Exception {
    try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
        HttpRequest request = createSigv4TestSuiteRequest();
        try (AwsSigningConfig config = new AwsSigningConfig()) {
            config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
            config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS);
            config.setRegion("us-east-1");
            config.setService("service");
            config.setTime(DATE_FORMAT.parse("2015-08-30T12:36:00Z").getTime());
            config.setCredentialsProvider(provider);
            config.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
            config.setExpirationInSeconds(60);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            HttpRequest signedRequest = result.get();
            assertNotNull(signedRequest);
            String path = signedRequest.getEncodedPath();
            assertTrue(path.contains("X-Amz-Signature="));
            assertTrue(path.contains("X-Amz-SignedHeaders=host"));
            assertTrue(path.contains("X-Amz-Credential=AKIDEXAMPLE%2F20150830%2F"));
            assertTrue(path.contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
            assertTrue(path.contains("X-Amz-Expires=60"));
        }
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) Test(org.junit.Test)

Example 3 with AwsSigningConfig

use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.

the class SigningTest method createChunkedRequestSigningConfig.

private AwsSigningConfig createChunkedRequestSigningConfig() throws Exception {
    AwsSigningConfig config = new AwsSigningConfig();
    config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
    config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS);
    config.setRegion(CHUNKED_TEST_REGION);
    config.setService(CHUNKED_TEST_SERVICE);
    config.setTime(DATE_FORMAT.parse(CHUNKED_TEST_SIGNING_TIME).getTime());
    config.setUseDoubleUriEncode(false);
    config.setShouldNormalizeUriPath(true);
    config.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA256);
    config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.STREAMING_AWS4_HMAC_SHA256_PAYLOAD);
    config.setCredentials(createChunkedTestCredentials());
    return config;
}
Also used : AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)

Example 4 with AwsSigningConfig

use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.

the class SigningTest method testTrailingHeadersSigv4Signing.

@Test
public void testTrailingHeadersSigv4Signing() throws Exception {
    HttpRequest request = createChunkedTestRequest();
    CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, createChunkedRequestSigningConfig());
    HttpRequest signedRequest = result.get();
    assertNotNull(signedRequest);
    assertTrue(hasHeaderWithValue(signedRequest, "Authorization", EXPECTED_CHUNK_REQUEST_AUTHORIZATION_HEADER));
    /*
        * If the authorization header is equal then certainly we can assume the
        * signature value
        */
    byte[] signature = EXPECTED_REQUEST_SIGNATURE;
    HttpRequestBodyStream chunk1 = createChunk1Stream();
    CompletableFuture<AwsSigningResult> chunk1Result = AwsSigner.sign(chunk1, signature, createChunkSigningConfig());
    signature = chunk1Result.get().getSignature();
    assertTrue(Arrays.equals(signature, EXPECTED_FIRST_CHUNK_SIGNATURE));
    HttpRequestBodyStream chunk2 = createChunk2Stream();
    CompletableFuture<AwsSigningResult> chunk2Result = AwsSigner.sign(chunk2, signature, createChunkSigningConfig());
    signature = chunk2Result.get().getSignature();
    assertTrue(Arrays.equals(signature, EXPECTED_SECOND_CHUNK_SIGNATURE));
    CompletableFuture<AwsSigningResult> finalChunkResult = AwsSigner.sign((HttpRequestBodyStream) null, signature, createChunkSigningConfig());
    signature = finalChunkResult.get().getSignature();
    assertTrue(Arrays.equals(signature, EXPECTED_FINAL_CHUNK_SIGNATURE));
    List<HttpHeader> trailingHeaders = createTrailingHeaders();
    AwsSigningConfig trailingHeadersSigningConfig = createTrailingHeadersSigningConfig();
    CompletableFuture<AwsSigningResult> trailingHeadersResult = AwsSigner.sign(trailingHeaders, signature, trailingHeadersSigningConfig);
    signature = trailingHeadersResult.get().getSignature();
    assertTrue(Arrays.equals(signature, EXPECTED_TRAILING_HEADERS_SIGNATURE));
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) AwsSigningResult(software.amazon.awssdk.crt.auth.signing.AwsSigningResult) Test(org.junit.Test)

Example 5 with AwsSigningConfig

use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.

the class SigningTest method testSigningException.

@Test
public void testSigningException() throws Exception {
    DelegateCredentialsHandler credentialsHandler = new DelegateCredentialsHandler() {

        @Override
        public Credentials getCredentials() {
            throw new RuntimeException("failed");
        }
    };
    boolean failed = false;
    try (DelegateCredentialsProvider provider = new DelegateCredentialsProvider.DelegateCredentialsProviderBuilder().withHandler(credentialsHandler).build()) {
        HttpRequest request = createSimpleRequest("https://www.example.com", "POST", "/derp", "<body>Hello</body>");
        Predicate<String> filterParam = param -> !param.equals("bad-param");
        try (AwsSigningConfig config = new AwsSigningConfig()) {
            config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
            config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS);
            config.setRegion("us-east-1");
            config.setService("service");
            config.setTime(System.currentTimeMillis());
            config.setCredentialsProvider(provider);
            config.setShouldSignHeader(filterParam);
            config.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            HttpRequest signedRequest = result.get();
            assertNotNull(signedRequest);
            assertTrue(hasHeader(signedRequest, "X-Amz-Date"));
            assertTrue(hasHeader(signedRequest, "Authorization"));
        }
    } catch (Exception ex) {
        failed = true;
    }
    assertTrue(failed);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) Arrays(java.util.Arrays) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) SimpleDateFormat(java.text.SimpleDateFormat) CompletableFuture(java.util.concurrent.CompletableFuture) HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) AwsSigningResult(software.amazon.awssdk.crt.auth.signing.AwsSigningResult) URI(java.net.URI) DelegateCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsProvider) software.amazon.awssdk.crt(software.amazon.awssdk.crt) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) TimeZone(java.util.TimeZone) Predicate(java.util.function.Predicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) Assert.assertTrue(org.junit.Assert.assertTrue) DelegateCredentialsHandler(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsHandler) Test(org.junit.Test) ByteBufferUtils.transferData(software.amazon.awssdk.crt.utils.ByteBufferUtils.transferData) StandardCharsets(java.nio.charset.StandardCharsets) AwsSigner(software.amazon.awssdk.crt.auth.signing.AwsSigner) List(java.util.List) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) AwsSigningUtils(software.amazon.awssdk.crt.auth.signing.AwsSigningUtils) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) DelegateCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsProvider) DelegateCredentialsHandler(software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsHandler) Test(org.junit.Test)

Aggregations

AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)43 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)14 Test (org.junit.Test)12 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)12 Test (org.junit.jupiter.api.Test)10 SigningTestCase (software.amazon.awssdk.authcrt.signer.SigningTestCase)10 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)8 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)7 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)6 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)6 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)5 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Credentials (software.amazon.awssdk.crt.auth.credentials.Credentials)3 AwsSigner (software.amazon.awssdk.crt.auth.signing.AwsSigner)3 URI (java.net.URI)2 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Instant (java.time.Instant)2