Skip to content

Commit

Permalink
Use AwsCredentialsProvider and AwsRegionProvider interfaces for bean …
Browse files Browse the repository at this point in the history
…declaration instead of their Chain classes
  • Loading branch information
shduke committed Apr 17, 2021
1 parent 222f602 commit c4c63a3
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package io.micronaut.aws.sdk.v2;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.env.Environment;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;

Expand All @@ -41,9 +42,8 @@ public class CredentialsAndRegionFactory {
* @return An {@link AwsCredentialsProviderChain} that attempts to read the values from the Micronaut environment
* first, then delegates to {@link DefaultCredentialsProvider}.
*/
@Bean(preDestroy = "close")
@Singleton
public AwsCredentialsProviderChain awsCredentialsProvider(Environment environment) {
public AwsCredentialsProvider awsCredentialsProvider(Environment environment) {
return AwsCredentialsProviderChain.of(
EnvironmentAwsCredentialsProvider.create(environment),
DefaultCredentialsProvider.create()
Expand All @@ -56,7 +56,7 @@ public AwsCredentialsProviderChain awsCredentialsProvider(Environment environmen
* first, then delegates to {@link DefaultAwsRegionProviderChain}.
*/
@Singleton
public AwsRegionProviderChain awsRegionProvider(Environment environment) {
public AwsRegionProvider awsRegionProvider(Environment environment) {
return new AwsRegionProviderChain(
new EnvironmentAwsRegionProvider(environment),
new DefaultAwsRegionProviderChain()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package io.micronaut.aws.sdk.v2.service;

import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.awscore.client.builder.AwsAsyncClientBuilder;
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;
import software.amazon.awssdk.awscore.client.builder.AwsSyncClientBuilder;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;

/**
* Abstract class that eases creation of AWS client factories.
Expand All @@ -37,16 +37,16 @@
*/
public abstract class AwsClientFactory<SB extends AwsSyncClientBuilder<SB, SC> & AwsClientBuilder<SB, SC>, AB extends AwsAsyncClientBuilder<AB, AC> & AwsClientBuilder<AB, AC>, SC, AC extends SdkClient> {

protected final AwsCredentialsProviderChain credentialsProvider;
protected final AwsRegionProviderChain regionProvider;
protected final AwsCredentialsProvider credentialsProvider;
protected final AwsRegionProvider regionProvider;

/**
* Constructor.
*
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected AwsClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected AwsClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
this.credentialsProvider = credentialsProvider;
this.regionProvider = regionProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClientBuilder;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
Expand All @@ -45,7 +45,7 @@ public class DynamoDbClientFactory extends AwsClientFactory<DynamoDbClientBuilde
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected DynamoDbClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected DynamoDbClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3AsyncClientBuilder;
import software.amazon.awssdk.services.s3.S3Client;
Expand All @@ -48,7 +48,7 @@ public class S3ClientFactory extends AwsClientFactory<S3ClientBuilder, S3AsyncCl
* @param regionProvider The region provider
* @param configuration The service configuration
*/
public S3ClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider,
public S3ClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider,
S3ConfigurationProperties configuration) {
super(credentialsProvider, regionProvider);
this.configuration = configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerAsyncClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerAsyncClientBuilder;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
Expand All @@ -46,7 +46,7 @@ public class SecretsManagerClientFactory extends AwsClientFactory<SecretsManager
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected SecretsManagerClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected SecretsManagerClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.ses.SesAsyncClient;
import software.amazon.awssdk.services.ses.SesAsyncClientBuilder;
import software.amazon.awssdk.services.ses.SesClient;
Expand All @@ -45,7 +45,7 @@ public class SesClientFactory extends AwsClientFactory<SesClientBuilder, SesAsyn
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected SesClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected SesClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.sns.SnsAsyncClient;
import software.amazon.awssdk.services.sns.SnsAsyncClientBuilder;
import software.amazon.awssdk.services.sns.SnsClient;
Expand All @@ -45,7 +45,7 @@ public class SnsClientFactory extends AwsClientFactory<SnsClientBuilder, SnsAsyn
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected SnsClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected SnsClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
import software.amazon.awssdk.services.sqs.SqsAsyncClientBuilder;
import software.amazon.awssdk.services.sqs.SqsClient;
Expand All @@ -45,7 +45,7 @@ public class SqsClientFactory extends AwsClientFactory<SqsClientBuilder, SqsAsyn
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected SqsClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected SqsClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.ssm.SsmAsyncClient;
import software.amazon.awssdk.services.ssm.SsmAsyncClientBuilder;
import software.amazon.awssdk.services.ssm.SsmClient;
Expand All @@ -47,7 +47,7 @@ public class SsmClientFactory extends AwsClientFactory<SsmClientBuilder, SsmAsyn
* @param credentialsProvider The credentials provider
* @param regionProvider The region provider
*/
protected SsmClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected SsmClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package io.micronaut.aws.sdk.v2

import io.micronaut.context.ApplicationContext
import software.amazon.awssdk.auth.credentials.AwsCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties
Expand All @@ -38,7 +38,7 @@ class EnvironmentAwsCredentialsProviderSpec extends Specification {

when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProviderChain).resolveCredentials()
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()

then:
awsCredentials.accessKeyId() == TEST_KEY_ID
Expand All @@ -59,7 +59,7 @@ class EnvironmentAwsCredentialsProviderSpec extends Specification {

when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProviderChain).resolveCredentials()
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()

then:
awsCredentials.accessKeyId() == TEST_KEY_ID
Expand All @@ -79,7 +79,7 @@ class EnvironmentAwsCredentialsProviderSpec extends Specification {

when:
AwsSessionCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProviderChain).resolveCredentials() as AwsSessionCredentials
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials() as AwsSessionCredentials

then:
awsCredentials.accessKeyId() == TEST_KEY_ID
Expand All @@ -96,7 +96,7 @@ class EnvironmentAwsCredentialsProviderSpec extends Specification {

when:
AwsSessionCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProviderChain).resolveCredentials() as AwsSessionCredentials
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials() as AwsSessionCredentials

then:
awsCredentials.accessKeyId() == "yamlAccessKeyId"
Expand All @@ -117,7 +117,7 @@ class EnvironmentAwsCredentialsProviderSpec extends Specification {

when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProviderChain).resolveCredentials()
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()

then:
awsCredentials.accessKeyId() == TEST_KEY_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.regions.providers.AwsRegionProviderChain;
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
import software.amazon.awssdk.services.rekognition.RekognitionAsyncClient;
import software.amazon.awssdk.services.rekognition.RekognitionAsyncClientBuilder;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
Expand All @@ -20,7 +20,7 @@
@Factory
public class RekognitionClientFactory extends AwsClientFactory<RekognitionClientBuilder, RekognitionAsyncClientBuilder, RekognitionClient, RekognitionAsyncClient> {

protected RekognitionClientFactory(AwsCredentialsProviderChain credentialsProvider, AwsRegionProviderChain regionProvider) {
protected RekognitionClientFactory(AwsCredentialsProvider credentialsProvider, AwsRegionProvider regionProvider) {
super(credentialsProvider, regionProvider);
}

Expand Down

0 comments on commit c4c63a3

Please sign in to comment.