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 7 commits
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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-metrics</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-parameter-store</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-s3</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-s3-config</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-secrets-manager</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-ses</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-sns</module>
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-sqs</module>
<module>spring-cloud-aws-samples</module>
<module>spring-cloud-aws-test</module>
<module>docs</module>
</modules>
</modules>

<dependencyManagement>
<dependencies>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-aws-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<artifactId>spring-cloud-aws-s3</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-s3-config</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>software.amazon.dax</groupId>
<artifactId>amazon-dax-client</artifactId>
Expand Down
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
@@ -0,0 +1,72 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.s3;

import io.awspring.cloud.autoconfigure.config.BootstrapLoggingHelper;
import io.awspring.cloud.s3.config.S3PropertySource;
import java.util.Collections;
import java.util.Map;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.MapPropertySource;
import org.springframework.lang.Nullable;
import software.amazon.awssdk.services.s3.S3Client;

/**
* Loads config data from AWS S3.
*
* @author Kunal Varpe
* @since 3.0.0
*/
public class S3ConfigDataLoader implements ConfigDataLoader<S3ConfigDataResource> {

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

@Override
@Nullable
public ConfigData load(ConfigDataLoaderContext context, S3ConfigDataResource resource) {
try {
// resource is disabled if s3 integration is disabled via
// spring.cloud.aws.s3.enable_import=false
MatejNedic marked this conversation as resolved.
Show resolved Hide resolved
if (resource.isEnabled()) {
S3Client s3Client = context.getBootstrapContext().get(S3Client.class);
S3PropertySource propertySource = resource.getPropertySources()
.createPropertySource(resource.getContext(), resource.isOptional(), s3Client);
if (propertySource != null) {
return new ConfigData(Collections.singletonList(propertySource));
}
else {
return null;
}
}
else {
// create dummy empty config data
return new ConfigData(Collections.singletonList(new MapPropertySource("aws-s3:" + context, Map.of())));
}
}
catch (Exception e) {
throw new ConfigDataResourceNotFoundException(resource, e);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.s3;

import io.awspring.cloud.autoconfigure.config.AbstractAwsConfigDataLocationResolver;
import io.awspring.cloud.autoconfigure.core.*;
import io.awspring.cloud.autoconfigure.s3.AwsS3ClientCustomizer;
import io.awspring.cloud.autoconfigure.s3.S3KeysMissingException;
import io.awspring.cloud.autoconfigure.s3.properties.S3Properties;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
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 software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;

/**
* Resolves config data locations in AWS S3.
*
* @author Kunal Varpe
* @author Matej Nedic
* @since 3.1.0
*/
public class S3ConfigDataLocationResolver extends AbstractAwsConfigDataLocationResolver<S3ConfigDataResource> {

/**
* AWS S3 Config Data prefix.
*/
public static final String PREFIX = "aws-s3:";

private final Log log;

public S3ConfigDataLocationResolver(DeferredLogFactory deferredLogFactory) {
this.log = deferredLogFactory.getLog(S3ConfigDataLocationResolver.class);
}

@Override
protected String getPrefix() {
return PREFIX;
}

@Override
public List<S3ConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext,
ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException {

S3Properties s3Properties = loadProperties(resolverContext.getBinder());

registerBean(resolverContext, AwsProperties.class, loadAwsProperties(resolverContext.getBinder()));
registerBean(resolverContext, S3Properties.class, s3Properties);
registerBean(resolverContext, CredentialsProperties.class,
loadCredentialsProperties(resolverContext.getBinder()));
registerBean(resolverContext, RegionProperties.class, loadRegionProperties(resolverContext.getBinder()));

registerAndPromoteBean(resolverContext, S3Client.class, this::createSystemManagementClient);

S3PropertySources propertySources = new S3PropertySources();

List<String> contexts = getCustomContexts(location.getNonPrefixedValue(PREFIX));

List<S3ConfigDataResource> locations = new ArrayList<>();
contexts.forEach(propertySourceContext -> locations.add(new S3ConfigDataResource(propertySourceContext,
location.isOptional(), s3Properties.isEnableImport(), propertySources)));

if (!location.isOptional() && locations.isEmpty()) {
throw new S3KeysMissingException("No S3 keys provided in `spring.config.import=aws-s3:` configuration.");
}

return locations;
}

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

try {
AwsS3ClientCustomizer configurer = context.get(AwsS3ClientCustomizer.class);
if (configurer != null) {
AwsClientCustomizer.apply(configurer, builder);
}
}
catch (IllegalStateException e) {
log.debug("Bean of type AwsClientConfigurerS3 is not registered: " + e.getMessage());
}
return builder.build();
}

protected S3Properties loadProperties(Binder binder) {
return binder.bind(S3Properties.PREFIX, Bindable.of(S3Properties.class)).orElseGet(S3Properties::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.s3;

import java.util.Objects;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.core.style.ToStringCreator;

/**
* Config data resource for AWS S3 integration.
*
* @author Kunal Varpe
* @since 3.0.0
*/
public class S3ConfigDataResource extends ConfigDataResource {

private final String context;

private final boolean enabled;

private final boolean optional;

private final S3PropertySources propertySources;

public S3ConfigDataResource(String context, boolean optional, S3PropertySources propertySources) {
this(context, optional, true, propertySources);
}

public S3ConfigDataResource(String context, boolean optional, boolean enabled, S3PropertySources propertySources) {
this.context = context;
this.optional = optional;
this.propertySources = propertySources;
this.enabled = enabled;
}

/**
* Returns context which is equal to S3 bucket and property file.
* @return the context
*/
public String getContext() {
return this.context;
}

/**
* If application startup should fail when secret cannot be loaded or does not exist.
* @return is optional
*/
public boolean isOptional() {
return this.optional;
}

public boolean isEnabled() {
return enabled;
}

public S3PropertySources getPropertySources() {
return this.propertySources;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
S3ConfigDataResource that = (S3ConfigDataResource) o;
return this.optional == that.optional && this.context.equals(that.context);
}

@Override
public int hashCode() {
return Objects.hash(this.optional, this.context);
}

@Override
public String toString() {
return new ToStringCreator(this).append("context", context).append("optional", optional).toString();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.awspring.cloud.autoconfigure.config.s3;

import io.awspring.cloud.autoconfigure.s3.S3KeysMissingException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

/**
* An {@link AbstractFailureAnalyzer} that performs analysis of a S3 configuration failure caused by not providing a S3
* key to `spring.config.import` property.
*
* @author Kunal Varpe
* @since 3.0.0
*/
public class S3MissingKeysFailureAnalyzer extends AbstractFailureAnalyzer<S3KeysMissingException> {

@Override
protected FailureAnalysis analyze(Throwable rootFailure, S3KeysMissingException cause) {
return new FailureAnalysis("Could not import properties from AWS S3: " + cause.getMessage(),
"Consider providing keys, for example `spring.config.import=aws-s3:bucket/application.properties`",
cause);
}

}
Loading
Loading