Skip to content

Commit

Permalink
Merge pull request #13 from 7SOATSquad30/fix/setup-dynamo-client-with…
Browse files Browse the repository at this point in the history
…-session-token

fix: setup dynamo client with session token
  • Loading branch information
MuriloKakazu authored Nov 23, 2024
2 parents 4398eb0 + b7777cf commit 0189395
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
package br.com.fiap.grupo30.fastfood.payments_api.infrastructure.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.extensions.AutoGeneratedTimestampRecordExtension;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Configuration
public class DynamoDbConfig {

@Value("${spring.cloud.aws.credentials.access-key}")
private String accessKeyId;

@Value("${spring.cloud.aws.credentials.secret-key}")
private String secretAccessKey;

@Value("${spring.cloud.aws.credentials.session-token:#{null}}")
private String sessionToken;

@Value("${spring.cloud.aws.region.static}")
private String region;

@Bean
public DynamoDbClient dynamoDbClient() {
StaticCredentialsProvider credentialsProvider =
sessionToken != null
? StaticCredentialsProvider.create(
AwsSessionCredentials.create(
accessKeyId, secretAccessKey, sessionToken))
: StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKeyId, secretAccessKey));

return DynamoDbClient.builder()
.credentialsProvider(credentialsProvider)
.region(Region.of(region))
.build();
}

@Bean
public static DynamoDbEnhancedClient dynamoDbEnhancedClient(DynamoDbClient dynamoDbClient) {
public DynamoDbEnhancedClient dynamoDbEnhancedClient(@Autowired DynamoDbClient dynamoDbClient) {
return DynamoDbEnhancedClient.builder()
.dynamoDbClient(dynamoDbClient)
.extensions(AutoGeneratedTimestampRecordExtension.builder().build())
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# aws
spring.cloud.aws.credentials.access-key=${AWS_ACCESS_KEY_ID}
spring.cloud.aws.credentials.secret-key=${AWS_SECRET_ACCESS_KEY}
spring.cloud.aws.credentials.session-token=${AWS_SESSION_TOKEN}

# app
integrations.mercadopago.base-url=https://api.mercadopago.com
Expand Down

0 comments on commit 0189395

Please sign in to comment.