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 10 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
240 changes: 240 additions & 0 deletions docs/src/main/asciidoc/s3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,246 @@ The Spring Boot Starter for S3 provides the following configuration options:
| `spring.cloud.aws.s3.transfer-manager.follow-symbolic-links` | Specifies whether to follow symbolic links when traversing the file tree in `S3TransferManager#uploadDirectory` operation | No | `null` (falls back to SDK default)
|===

=== Loading External Configuration

To fetch file from S3 and add them to Spring's environment properties, add `spring.config.import` property to `application.properties`:

For example, assuming that the file name in S3 is `myApp/application.json`:

[source,properties]
----
spring.config.import=aws-s3:/myApp/application.json
----

If a file with given name does not exist in S3, application will fail to start. If file configuration is not required for the application, and it should continue to startup even when file configuration is missing, add `optional` before prefix:

[source,properties]
----
spring.config.import=optional:aws-s3:/myApp/application.json
----

To load multiple files, separate their names with `;`:

[source,properties]
----
spring.config.import=aws-s3:/myApp/application.json;/diffPath/app.yaml
----

If some files are required, and other ones are optional, list them as separate entries in `spring.config.import` property:

[source,properties]
----
spring.config.import[0]=optional:aws-s3=/myApp/application.json
spring.config.import[1]=aws-s3=/diffPath/application.yaml
----

Fetched files configuration can be referenced with `@Value`, bound to `@ConfigurationProperties` classes, or referenced in `application.properties` file.

==== S3 config import supports following models

1. Json (application.json for example)
2. Text (application.properties for example)
3. Yaml (application.yaml for example)

File resolved with `spring.config.import` can be also referenced in `application.properties`.
For example, with a file `urlDev.json` containing following JSON structure:

[source,json]
----
{
"url": "someUrl"
}
----


`spring.config.import` entry is added to `application.properties`:

[source, properties]
----
spring.config.import=aws-s3:/myPath/application.json
----

File configuration values can be referenced by JSON key names:

[source, java]
----
@Value("${url}"
private String url;
----

=== Customizing S3Client

To use custom `S3Client` in `spring.config.import`, provide an implementation of `BootstrapRegistryInitializer`. For example:

[source,java]
----
package com.app;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;

public class S3ClientBootstrapConfiguration implements BootstrapRegistryInitializer {

@Override
public void initialize(BootstrapRegistry registry) {
registry.register(S3Client.class, context -> {
AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("yourAccessKey", "yourSecretKey"));
return S3Client.builder().credentialsProvider(awsCredentialsProvider).region(Region.EU_WEST_2).build();
});
}
}
----

Note that this class must be listed under `org.springframework.boot.BootstrapRegistryInitializer` key in `META-INF/spring.factories`:

[source, properties]
----
org.springframework.boot.BootstrapRegistryInitializer=com.app.S3ClientBootstrapConfiguration
----

If you want to use autoconfigured `S3Client` but change underlying SDKClient or ClientOverrideConfiguration you will need to register bean of type `AwsS3ClientCustomizer`:
Autoconfiguration will configure `S3Client` Bean with provided values after that, for example:

[source,java]
----
package com.app;

import io.awspring.cloud.autoconfigure.config.s3.AwsS3ClientCustomizer;
import java.time.Duration;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.services.s3.S3Client;

class S3ClientBootstrapConfiguration implements BootstrapRegistryInitializer {

@Override
public void initialize(BootstrapRegistry registry) {
registry.register(AwsS3ClientCustomizer.class,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AwsS3ClientCustomizer is deprecated. Instead we should show in examples usages of S3ClientCustomizer

context -> new AwsS3ClientCustomizer() {

@Override
public ClientOverrideConfiguration overrideConfiguration() {
return ClientOverrideConfiguration.builder().apiCallTimeout(Duration.ofMillis(500))
.build();
}

@Override
public SdkHttpClient httpClient() {
return ApacheHttpClient.builder().connectionTimeout(Duration.ofMillis(1000)).build();
}
});
}
}
----

=== `PropertySource` Reload

Some applications may need to detect changes on external property sources and update their internal status to reflect the new configuration.
The reload feature of Spring Cloud AWS S3 config import integration is able to trigger an application reload when a related file value changes.

By default, this feature is disabled. You can enable it by using the `spring.cloud.aws.s3.config.reload.strategy` configuration property (for example, in the `application.properties` file) and adding following dependencies.

[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
----

The following levels of reload are supported (by setting the `spring.cloud.aws.s3.config.reload.strategy` property):

* `refresh` (default): Only configuration beans annotated with `@ConfigurationProperties` or `@RefreshScope` are reloaded.
This reload level leverages the refresh feature of Spring Cloud Context.

* `restart_context`: the whole Spring `ApplicationContext` is gracefully restarted. Beans are recreated with the new configuration.
In order for the restart context functionality to work properly you must enable and expose the restart actuator endpoint
[source,yaml]
====
----
management:
endpoint:
restart:
enabled: true
endpoints:
web:
exposure:
include: restart
----
====

Assuming that the reload feature is enabled with default settings (`refresh` mode), the following bean is refreshed when the file changes:

====
[java, source]
----
@Configuration
@ConfigurationProperties(prefix = "bean")
public class MyConfig {

private String message = "a message that can be changed live";

// getter and setters

}
----
====

To see that changes effectively happen, you can create another bean that prints the message periodically, as follows

====
[source,java]
----
@Component
public class MyBean {

@Autowired
private MyConfig config;

@Scheduled(fixedDelay = 5000)
public void hello() {
System.out.println("The message is: " + config.getMessage());
}
}
----
====

The reload feature periodically re-creates the configuration from S3 file to see if it has changed.
You can configure the polling period by using the `spring.cloud.aws.s3.config.reload.period` (default value is 1 minute).

=== Configuration

The Spring Boot Starter for S3 provides the following configuration options:

[cols="2,3,1,1"]
|===
| Name | Description | Required | Default value
| `spring.cloud.aws.s3.config.enabled` | Enables the S3 config import integration. | No | `true`
| `spring.cloud.aws.s3.config.endpoint` | Configures endpoint used by `S3Client`. | No | `null`
MatejNedic marked this conversation as resolved.
Show resolved Hide resolved
| `spring.cloud.aws.s3.config.region` | Configures region used by `S3Client`. | No | `null`
| `spring.cloud.aws.s3.config.reload.strategy` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`)
| `spring.cloud.aws.s3.config.reload.period` | `Duration`| `15s` | The period for verifying changes
| `spring.cloud.aws.s3.config.reload.max-wait-time-for-restart` | `Duration`| `2s` | The maximum time between the detection of changes in property source and the application context restart when `restart_context` strategy is used.
|===


=== IAM Permissions

Following IAM permissions are required by Spring Cloud AWS:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<module>spring-cloud-aws-test</module>
<module>spring-cloud-aws-modulith</module>
<module>docs</module>
</modules>
</modules>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.core.AwsClientCustomizer;
import software.amazon.awssdk.services.s3.S3ClientBuilder;

/**
* @author Kunal Varpe
* @since 3.2.0
*/
public interface AwsS3ClientCustomizer extends AwsClientCustomizer<S3ClientBuilder> {
maciejwalkowiak marked this conversation as resolved.
Show resolved Hide resolved
}
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.2.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);
}

}

}
Loading
Loading