Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for spring.config.import with S3 backend #849

Merged
merged 22 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/**
* {@link org.springframework.boot.context.config.ConfigDataLoader} implementation for AWS Parameter Store.
MatejNedic marked this conversation as resolved.
Show resolved Hide resolved
* {@link org.springframework.boot.context.config.ConfigDataLoader} implementation for AWS S3.
*/
@org.springframework.lang.NonNullApi
@org.springframework.lang.NonNullFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class S3ConfigDataLoader implements ConfigDataLoader<S3ConfigDataResource> {

public S3ConfigDataLoader(DeferredLogFactory logFactory) {
BootstrapLoggingHelper.reconfigureLoggers(logFactory, "io.awspring.cloud.S3PropertySource",
BootstrapLoggingHelper.reconfigureLoggers(logFactory, "io.awspring.cloud.s3.config.S3PropertySource",
"io.awspring.cloud.autoconfigure.config.s3.S3PropertySources");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@
import io.awspring.cloud.autoconfigure.s3.AwsS3ClientCustomizer;
import io.awspring.cloud.autoconfigure.s3.S3KeysMissingException;
import io.awspring.cloud.autoconfigure.s3.properties.S3Properties;
import io.awspring.cloud.s3.crossregion.CrossRegionS3Client;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.util.ClassUtils;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;

Expand Down Expand Up @@ -75,8 +71,7 @@ public List<S3ConfigDataResource> resolveProfileSpecific(ConfigDataLocationResol
loadCredentialsProperties(resolverContext.getBinder()));
registerBean(resolverContext, RegionProperties.class, loadRegionProperties(resolverContext.getBinder()));

registerAndPromoteBean(resolverContext, S3Client.class, BootstrapRegistry.InstanceSupplier.of(
S3ClientFactory.s3Client(createS3ClientBuilder(s3Properties, resolverContext.getBootstrapContext()))));
registerAndPromoteBean(resolverContext, S3Client.class, this::createSystemManagementClient);

S3PropertySources propertySources = new S3PropertySources();

Expand All @@ -93,12 +88,8 @@ public List<S3ConfigDataResource> resolveProfileSpecific(ConfigDataLocationResol
return locations;
}

private S3ClientBuilder createS3ClientBuilder(S3Properties s3Properties, BootstrapContext context) {
S3ClientBuilder builder = S3Client.builder();
Optional.ofNullable(s3Properties.getCrossRegionEnabled()).ifPresent(builder::crossRegionAccessEnabled);
builder.serviceConfiguration(s3Properties.toS3Configuration());

builder = configure(builder, s3Properties, context);
private S3Client createSystemManagementClient(BootstrapContext context) {
S3ClientBuilder builder = configure(S3Client.builder(), context.get(S3Properties.class), context);

try {
AwsS3ClientCustomizer configurer = context.get(AwsS3ClientCustomizer.class);
Expand All @@ -107,25 +98,12 @@ private S3ClientBuilder createS3ClientBuilder(S3Properties s3Properties, Bootstr
}
}
catch (IllegalStateException e) {
log.debug("Bean of type AwsClientConfigurerParameterStore is not registered: " + e.getMessage());
log.debug("Bean of type AwsClientConfigurerS3 is not registered: " + e.getMessage());
}
return builder;
return builder.build();
}

protected S3Properties loadProperties(Binder binder) {
return binder.bind(S3Properties.PREFIX, Bindable.of(S3Properties.class)).orElseGet(S3Properties::new);
}

static class S3ClientFactory {

static S3Client s3Client(S3ClientBuilder builder) {
if (ClassUtils.isPresent("io.awspring.cloud.s3.crossregion.CrossRegionS3Client", null)) {
return new CrossRegionS3Client(builder);
}
else {
return builder.build();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ public S3PropertySource createPropertySource(String context, boolean optional, S
return propertySource;
}
catch (Exception e) {
LOG.warn("Unable to load AWS S3 object from " + context + ". " + e.getMessage());
if (!optional) {
throw new AwsS3PropertySourceNotFoundException(e);
}
else {
LOG.warn("Unable to load AWS S3 object from " + context + ". " + e.getMessage());
}
}
return null;
}
Expand Down
Loading
Loading