Search in sources :

Example 1 with Credentials

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

Example 2 with Credentials

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

the class ProxyTest method doCredentialsProviderProxyTest.

private void doCredentialsProviderProxyTest(CredentialsProvider provider) {
    try {
        Credentials credentials = provider.getCredentials().get();
        Assert.assertNotNull(credentials);
    } catch (Exception e) {
        throw new CrtRuntimeException(e.toString());
    }
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) URISyntaxException(java.net.URISyntaxException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException)

Example 3 with Credentials

use of software.amazon.awssdk.crt.auth.credentials.Credentials in project aws-sdk-java-v2 by aws.

the class CrtCredentialProviderAdapterTest method crtCredentials_withoutSession_shouldConvert.

@Test
public void crtCredentials_withoutSession_shouldConvert() {
    AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"));
    CredentialsProvider crtCredentialsProvider = new CrtCredentialsProviderAdapter(awsCredentialsProvider).crtCredentials();
    Credentials credentials = crtCredentialsProvider.getCredentials().join();
    assertThat(credentials.getAccessKeyId()).isEqualTo("foo".getBytes(StandardCharsets.UTF_8));
    assertThat(credentials.getSecretAccessKey()).isEqualTo("bar".getBytes(StandardCharsets.UTF_8));
    assertThat(credentials.getSessionToken()).isNull();
}
Also used : AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) HttpCredentialsProvider(software.amazon.awssdk.auth.credentials.HttpCredentialsProvider) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 4 with Credentials

use of software.amazon.awssdk.crt.auth.credentials.Credentials in project aws-sdk-java-v2 by aws.

the class CrtCredentialProviderAdapterTest method crtCredentials_withoutSession_shouldConvert.

@Test
public void crtCredentials_withoutSession_shouldConvert() {
    AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"));
    CredentialsProvider crtCredentialsProvider = new CrtCredentialsProviderAdapter(awsCredentialsProvider).crtCredentials();
    Credentials credentials = crtCredentialsProvider.getCredentials().join();
    assertThat(credentials.getAccessKeyId()).isEqualTo("foo".getBytes(StandardCharsets.UTF_8));
    assertThat(credentials.getSecretAccessKey()).isEqualTo("bar".getBytes(StandardCharsets.UTF_8));
    assertThat(credentials.getSessionToken()).isNull();
}
Also used : AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) HttpCredentialsProvider(software.amazon.awssdk.auth.credentials.HttpCredentialsProvider) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.jupiter.api.Test)

Example 5 with Credentials

use of software.amazon.awssdk.crt.auth.credentials.Credentials in project aws-sdk-java-v2 by aws.

the class CrtCredentialProviderAdapterTest method crtCredentials_provideAwsCredentials_shouldInvokeResolveAndClose.

@Test
public void crtCredentials_provideAwsCredentials_shouldInvokeResolveAndClose() {
    AwsCredentialsProvider awsCredentialsProvider = Mockito.mock(HttpCredentialsProvider.class);
    AwsCredentials credentials = new AwsCredentials() {

        @Override
        public String accessKeyId() {
            return "foo";
        }

        @Override
        public String secretAccessKey() {
            return "bar";
        }
    };
    when(awsCredentialsProvider.resolveCredentials()).thenReturn(credentials);
    CrtCredentialsProviderAdapter adapter = new CrtCredentialsProviderAdapter(awsCredentialsProvider);
    CredentialsProvider crtCredentialsProvider = adapter.crtCredentials();
    Credentials crtCredentials = crtCredentialsProvider.getCredentials().join();
    assertThat(crtCredentials.getAccessKeyId()).isEqualTo("foo".getBytes(StandardCharsets.UTF_8));
    assertThat(crtCredentials.getSecretAccessKey()).isEqualTo("bar".getBytes(StandardCharsets.UTF_8));
    verify(awsCredentialsProvider).resolveCredentials();
    adapter.close();
    verify((SdkAutoCloseable) awsCredentialsProvider).close();
}
Also used : AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) HttpCredentialsProvider(software.amazon.awssdk.auth.credentials.HttpCredentialsProvider) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Credentials(software.amazon.awssdk.crt.auth.credentials.Credentials) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) SdkAutoCloseable(software.amazon.awssdk.utils.SdkAutoCloseable) Test(org.junit.jupiter.api.Test)

Aggregations

Credentials (software.amazon.awssdk.crt.auth.credentials.Credentials)10 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)7 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)7 AwsSessionCredentials (software.amazon.awssdk.auth.credentials.AwsSessionCredentials)7 Test (org.junit.jupiter.api.Test)6 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)6 HttpCredentialsProvider (software.amazon.awssdk.auth.credentials.HttpCredentialsProvider)6 StaticCredentialsProvider (software.amazon.awssdk.auth.credentials.StaticCredentialsProvider)6 CredentialsProvider (software.amazon.awssdk.crt.auth.credentials.CredentialsProvider)6 Test (org.junit.Test)2 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)2 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)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