Search in sources :

Example 1 with StaticCredentialsProvider

use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider 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 StaticCredentialsProvider

use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider 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 StaticCredentialsProvider

use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider in project aws-crt-java by awslabs.

the class SigningTest method testSigningFailureBodyStreamException.

@Test(expected = CrtRuntimeException.class)
public void testSigningFailureBodyStreamException() throws Exception {
    try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
        // request is missing Host header
        HttpRequest request = createBadBodyStreamRequest("POST", "/bad");
        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.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA256);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            result.get();
        }
    } catch (Exception e) {
        Throwable cause = e.getCause();
        assertTrue(cause != null);
        assertTrue(cause.getClass() == CrtRuntimeException.class);
        CrtRuntimeException crt = (CrtRuntimeException) cause;
        assertTrue(crt.errorName.equals("AWS_ERROR_HTTP_CALLBACK_FAILURE"));
        throw crt;
    }
}
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 4 with StaticCredentialsProvider

use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider in project aws-crt-java by awslabs.

the class SigningTest method testSigningBasicSigv4Test.

@Test
public void testSigningBasicSigv4Test() 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_HEADERS);
            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);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            HttpRequest signedRequest = result.get();
            assertNotNull(signedRequest);
            assertTrue(hasHeaderWithValue(signedRequest, "X-Amz-Date", "20150830T123600Z"));
            // expected authorization value for post-vanilla-query test
            assertTrue(hasHeaderWithValue(signedRequest, "Authorization", "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=28038455d6de14eafc1f9222cf5aa6f1a96197d7deb8263271d420d138af7f11"));
        }
    }
}
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 5 with StaticCredentialsProvider

use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider in project aws-crt-java by awslabs.

the class SigningTest method testSigningFailureBadRequest.

@Test(expected = CrtRuntimeException.class)
public void testSigningFailureBadRequest() throws Exception {
    try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
        // request is missing Host header
        HttpRequest request = createUnsignableRequest("POST", "/bad");
        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.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            result.get();
        }
    } catch (Exception e) {
        Throwable cause = e.getCause();
        assertTrue(cause != null);
        assertTrue(cause.getClass() == CrtRuntimeException.class);
        CrtRuntimeException crt = (CrtRuntimeException) cause;
        assertTrue(crt.errorName.equals("AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER"));
        throw crt;
    }
}
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)

Aggregations

Test (org.junit.Test)5 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)5 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)5 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)5 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Predicate (java.util.function.Predicate)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 software.amazon.awssdk.crt (software.amazon.awssdk.crt)1 Credentials (software.amazon.awssdk.crt.auth.credentials.Credentials)1 DelegateCredentialsHandler (software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsHandler)1 DelegateCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.DelegateCredentialsProvider)1