Search in sources :

Example 1 with DelegateCredentialsProvider

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

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 Test (org.junit.Test)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 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)1 AwsSigner (software.amazon.awssdk.crt.auth.signing.AwsSigner)1 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)1