FINERACT-2118: Use S3 service accounts

Allow usage of S3 service accounts and support any S3 compatible storage
endpoints
This commit is contained in:
Naphlin Akena
2025-07-28 11:42:29 +02:00
committed by Adam Saghy
parent a8d23ee747
commit dadc36b6b4
4 changed files with 236 additions and 3 deletions
@@ -19,13 +19,19 @@
package org.apache.fineract.infrastructure.core.config;
import com.google.common.base.Strings;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;
@Slf4j
@Configuration
@@ -34,8 +40,22 @@ public class ContentS3Config {
@Bean
@ConditionalOnProperty("fineract.content.s3.enabled")
public S3Client contentS3Client(FineractProperties fineractProperties) {
return S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials
.create(fineractProperties.getContent().getS3().getAccessKey(), fineractProperties.getContent().getS3().getSecretKey())))
.build();
S3ClientBuilder builder = S3Client.builder().credentialsProvider(getCredentialProvider(fineractProperties.getContent().getS3()))
.region(Region.of(fineractProperties.getContent().getS3().getRegion()));
if (!Strings.isNullOrEmpty(fineractProperties.getContent().getS3().getEndpoint())) {
builder.endpointOverride(URI.create(fineractProperties.getContent().getS3().getEndpoint()))
.forcePathStyle(fineractProperties.getContent().getS3().getPathStyleAddressingEnabled());
}
return builder.build();
}
private AwsCredentialsProvider getCredentialProvider(FineractProperties.FineractContentS3Properties s3Properties) {
if (Strings.isNullOrEmpty(s3Properties.getAccessKey()) || Strings.isNullOrEmpty(s3Properties.getSecretKey())) {
return DefaultCredentialsProvider.create();
}
return StaticCredentialsProvider.create(AwsBasicCredentials.create(s3Properties.getAccessKey(), s3Properties.getSecretKey()));
}
}
@@ -165,6 +165,9 @@ fineract.content.s3.enabled=${FINERACT_CONTENT_S3_ENABLED:false}
fineract.content.s3.bucketName=${FINERACT_CONTENT_S3_BUCKET_NAME:}
fineract.content.s3.accessKey=${FINERACT_CONTENT_S3_ACCESS_KEY:}
fineract.content.s3.secretKey=${FINERACT_CONTENT_S3_SECRET_KEY:}
fineract.content.s3.region=${FINERACT_CONTENT_S3_REGION:us-east-1}
fineract.content.s3.endpoint=${FINERACT_CONTENT_S3_ENDPOINT:}
fineract.content.s3.path-style-addressing-enabled=${FINERACT_CONTENT_S3_PATH_STYLE_ADDRESSING_ENABLED:false}
fineract.template.regex-whitelist-enabled=${FINERACT_TEMPLATE_REGEX_WHITELIST_ENABLED:true}
fineract.template.regex-whitelist=${FINERACT_TEMPLATE_REGEX_WHITELIST:}