Skip to content

Commit 8622414

Browse files
committed
Merge branch 'main' of github.com:awspring/spring-cloud-aws into awspringgh-1246
2 parents 64ee4dc + a08ebc1 commit 8622414

File tree

40 files changed

+1891
-8
lines changed

40 files changed

+1891
-8
lines changed

docs/src/main/asciidoc/_configprops.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,23 @@
4646
|spring.cloud.aws.s3.accelerate-mode-enabled | | Option to enable using the accelerate endpoint when accessing S3. Accelerate endpoints allow faster transfer of objects by using Amazon CloudFront's globally distributed edge locations.
4747
|spring.cloud.aws.s3.checksum-validation-enabled | | Option to disable doing a validation of the checksum of an object stored in S3.
4848
|spring.cloud.aws.s3.chunked-encoding-enabled | | Option to enable using chunked encoding when signing the request payload for {@link software.amazon.awssdk.services.s3.model.PutObjectRequest} and {@link software.amazon.awssdk.services.s3.model.UploadPartRequest}.
49+
|spring.cloud.aws.s3.config.enabled | `+++true+++` | Enables S3 Config File import integration.
50+
|spring.cloud.aws.s3.config.reload.max-wait-for-restart | `+++2s+++` | If {@link ReloadStrategy#RESTART_CONTEXT} is configured, maximum waiting time for server restart.
51+
|spring.cloud.aws.s3.config.reload.period | `+++1m+++` | Refresh period for {@link PollingAwsPropertySourceChangeDetector}.
52+
|spring.cloud.aws.s3.config.reload.strategy | | Reload strategy to run when properties change.
4953
|spring.cloud.aws.s3.cross-region-enabled | | Enables cross-region bucket access.
5054
|spring.cloud.aws.s3.crt.initial-read-buffer-size-in-bytes | | Configure the starting buffer size the client will use to buffer the parts downloaded from S3. Maintain a larger window to keep up a high download throughput; parts cannot download in parallel unless the window is large enough to hold multiple parts. Maintain a smaller window to limit the amount of data buffered in memory.
5155
|spring.cloud.aws.s3.crt.max-concurrency | | Specifies the maximum number of S3 connections that should be established during transfer.
5256
|spring.cloud.aws.s3.crt.minimum-part-size-in-bytes | | Sets the minimum part size for transfer parts. Decreasing the minimum part size causes multipart transfer to be split into a larger number of smaller parts. Setting this value too low has a negative effect on transfer speeds, causing extra latency and network communication for each part.
5357
|spring.cloud.aws.s3.crt.target-throughput-in-gbps | | The target throughput for transfer requests. Higher value means more S3 connections will be opened. Whether the transfer manager can achieve the configured target throughput depends on various factors such as the network bandwidth of the environment and the configured `max-concurrency`.
5458
|spring.cloud.aws.s3.enabled | `+++true+++` | Enables S3 integration.
59+
|spring.cloud.aws.s3.encryption.enable-delayed-authentication-mode | `+++false+++` |
60+
|spring.cloud.aws.s3.encryption.enable-legacy-unauthenticated-modes | `+++false+++` |
61+
|spring.cloud.aws.s3.encryption.enable-multipart-put-object | `+++false+++` |
62+
|spring.cloud.aws.s3.encryption.key-id | |
5563
|spring.cloud.aws.s3.endpoint | | Overrides the default endpoint.
5664
|spring.cloud.aws.s3.path-style-access-enabled | | Option to enable using path style access for accessing S3 objects instead of DNS style access. DNS style access is preferred as it will result in better load balancing when accessing S3.
65+
|spring.cloud.aws.s3.plugin.enable-fallback | `+++false+++` | If set to false if Access Grants does not find/return permissions, S3Client won't try to determine if policies grant access If set to true fallback policies S3/IAM will be evaluated.
5766
|spring.cloud.aws.s3.region | | Overrides the default region.
5867
|spring.cloud.aws.s3.transfer-manager.follow-symbolic-links | | Specifies whether to follow symbolic links when traversing the file tree in `S3TransferManager#uploadDirectory` operation.
5968
|spring.cloud.aws.s3.transfer-manager.max-depth | | Specifies the maximum number of levels of directories to visit in `S3TransferManager#uploadDirectory` operation.

docs/src/main/asciidoc/core.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,12 @@ There can be multiple customizer beans present in single application context and
260260

261261
Client-specific customizations can be applied through client-specific customizer interfaces (for example `S3ClientCustomizer` for S3). See integrations documentation for details.
262262

263+
264+
=== GraalVM Native Image
265+
266+
Since version 3.3.0 the framework provides **experimental** support for GraalVM Native Image build.
267+
268+
Known issues are:
269+
270+
- in DynamoDB integration, `StaticTableSchema` must be used instead of `DynamicTableSchema` (see https://github.com/aws/aws-sdk-java-v2/issues/2445)
271+
- in S3 integration, when working with CRT client, following guide must be followed: https://github.com/awslabs/aws-crt-java?tab=readme-ov-file#graalvm-support

docs/src/main/asciidoc/s3.adoc

+225
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,231 @@ There can be multiple `S3ClientCustomizer` beans present in single application c
378378

379379
Note that `S3ClientCustomizer` beans are applied **after** `AwsSyncClientCustomizer` beans and therefore can overwrite previously set configurations.
380380

381+
=== Loading External Configuration
382+
383+
Just like Spring Boot supports configuring application through `application.properties` stored in the file system, Spring Cloud AWS S3 integration extends this capability with fetching application configuration the S3 bucket through `spring.config.import` property.
384+
385+
For example, assuming that there is a file `config.properties` in a bucket named `bucket-name`, to include it as Spring Boot configuration, add a following property to `application.properties` or `application.yml`:
386+
387+
[source,properties]
388+
----
389+
spring.config.import=aws-s3:/bucket-name/config.properties
390+
----
391+
392+
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:
393+
394+
[source,properties]
395+
----
396+
spring.config.import=optional:aws-s3:/bucket-name/config.properties
397+
----
398+
399+
To load multiple files, separate their names with `;`:
400+
401+
[source,properties]
402+
----
403+
spring.config.import=aws-s3:/bucket-name/config.properties;/another-name/config.yml
404+
----
405+
406+
If some files are required, and other ones are optional, list them as separate entries in `spring.config.import` property:
407+
408+
[source,properties]
409+
----
410+
spring.config.import[0]=optional:bucket-name/config.properties
411+
spring.config.import[1]=aws-s3=/another-name/config.yml
412+
----
413+
414+
Fetched files configuration can be referenced with `@Value`, bound to `@ConfigurationProperties` classes, or referenced in `application.properties` file.
415+
416+
`JSON`, Java Properties and `YAML` configuration file formats are supported.
417+
418+
File resolved with `spring.config.import` can be also referenced in `application.properties`.
419+
For example, with a file `config.json` containing following JSON structure:
420+
421+
[source,json]
422+
----
423+
{
424+
"url": "someUrl"
425+
}
426+
----
427+
428+
429+
`spring.config.import` entry is added to `application.properties`:
430+
431+
[source, properties]
432+
----
433+
spring.config.import=aws-s3:/bucket-name/config.json
434+
----
435+
436+
File configuration values can be referenced by JSON key names:
437+
438+
[source, java]
439+
----
440+
@Value("${url}"
441+
private String url;
442+
----
443+
444+
=== Customizing S3Client
445+
446+
To use custom `S3Client` in `spring.config.import`, provide an implementation of `BootstrapRegistryInitializer`. For example:
447+
448+
[source,java]
449+
----
450+
package com.app;
451+
452+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
453+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
454+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
455+
import software.amazon.awssdk.regions.Region;
456+
import software.amazon.awssdk.services.s3.S3Client;
457+
458+
import org.springframework.boot.BootstrapRegistry;
459+
import org.springframework.boot.BootstrapRegistryInitializer;
460+
461+
public class S3ClientBootstrapConfiguration implements BootstrapRegistryInitializer {
462+
463+
@Override
464+
public void initialize(BootstrapRegistry registry) {
465+
registry.register(S3Client.class, context -> {
466+
AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create("yourAccessKey", "yourSecretKey"));
467+
return S3Client.builder().credentialsProvider(awsCredentialsProvider).region(Region.EU_WEST_2).build();
468+
});
469+
}
470+
}
471+
----
472+
473+
Note that this class must be listed under `org.springframework.boot.BootstrapRegistryInitializer` key in `META-INF/spring.factories`:
474+
475+
[source, properties]
476+
----
477+
org.springframework.boot.BootstrapRegistryInitializer=com.app.S3ClientBootstrapConfiguration
478+
----
479+
480+
If you want to use autoconfigured `S3Client` but change underlying SDKClient or `ClientOverrideConfiguration` you will need to register bean of type `S3ClientCustomizer`:
481+
Autoconfiguration will configure `S3Client` Bean with provided values after that, for example:
482+
483+
[source,java]
484+
----
485+
package com.app;
486+
487+
import io.awspring.cloud.autoconfigure.s3.S3ClientCustomizer;
488+
import java.time.Duration;
489+
import org.springframework.boot.BootstrapRegistry;
490+
import org.springframework.boot.BootstrapRegistryInitializer;
491+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
492+
import software.amazon.awssdk.http.SdkHttpClient;
493+
import software.amazon.awssdk.http.apache.ApacheHttpClient;
494+
import software.amazon.awssdk.services.s3.S3Client;
495+
496+
class S3ClientBootstrapConfiguration implements BootstrapRegistryInitializer {
497+
498+
@Override
499+
public void initialize(BootstrapRegistry registry) {
500+
registry.register(S3ClientCustomizer.class, context -> (builder -> {
501+
builder.overrideConfiguration(builder.overrideConfiguration().copy(c -> {
502+
c.apiCallTimeout(Duration.ofMillis(2001));
503+
}));
504+
}));
505+
}
506+
}
507+
----
508+
509+
=== `PropertySource` Reload
510+
511+
Some applications may need to detect changes on external property sources and update their internal status to reflect the new configuration.
512+
The reload feature of Spring Cloud AWS S3 config import integration is able to trigger an application reload when a related file value changes.
513+
514+
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.
515+
516+
[source,xml]
517+
----
518+
<dependency>
519+
<groupId>org.springframework.boot</groupId>
520+
<artifactId>spring-boot-starter-actuator</artifactId>
521+
</dependency>
522+
<dependency>
523+
<groupId>org.springframework.cloud</groupId>
524+
<artifactId>spring-cloud-commons</artifactId>
525+
</dependency>
526+
<dependency>
527+
<groupId>org.springframework.cloud</groupId>
528+
<artifactId>spring-cloud-context</artifactId>
529+
</dependency>
530+
----
531+
532+
The following levels of reload are supported (by setting the `spring.cloud.aws.s3.config.reload.strategy` property):
533+
534+
* `refresh` (default): Only configuration beans annotated with `@ConfigurationProperties` or `@RefreshScope` are reloaded.
535+
This reload level leverages the refresh feature of Spring Cloud Context.
536+
537+
* `restart_context`: the whole Spring `ApplicationContext` is gracefully restarted. Beans are recreated with the new configuration.
538+
In order for the restart context functionality to work properly you must enable and expose the restart actuator endpoint
539+
[source,yaml]
540+
====
541+
----
542+
management:
543+
endpoint:
544+
restart:
545+
enabled: true
546+
endpoints:
547+
web:
548+
exposure:
549+
include: restart
550+
----
551+
====
552+
553+
Assuming that the reload feature is enabled with default settings (`refresh` mode), the following bean is refreshed when the file changes:
554+
555+
====
556+
[java, source]
557+
----
558+
@Configuration
559+
@ConfigurationProperties(prefix = "bean")
560+
public class MyConfig {
561+
562+
private String message = "a message that can be changed live";
563+
564+
// getter and setters
565+
566+
}
567+
----
568+
====
569+
570+
To see that changes effectively happen, you can create another bean that prints the message periodically, as follows
571+
572+
====
573+
[source,java]
574+
----
575+
@Component
576+
public class MyBean {
577+
578+
@Autowired
579+
private MyConfig config;
580+
581+
@Scheduled(fixedDelay = 5000)
582+
public void hello() {
583+
System.out.println("The message is: " + config.getMessage());
584+
}
585+
}
586+
----
587+
====
588+
589+
The reload feature periodically re-creates the configuration from S3 file to see if it has changed.
590+
You can configure the polling period by using the `spring.cloud.aws.s3.config.reload.period` (default value is 1 minute).
591+
592+
=== Configuration
593+
594+
The Spring Boot Starter for S3 provides the following configuration options:
595+
596+
[cols="2,3,1,1"]
597+
|===
598+
| Name | Description | Required | Default value
599+
| `spring.cloud.aws.s3.config.enabled` | Enables the S3 config import integration. | No | `true`
600+
| `spring.cloud.aws.s3.config.reload.strategy` | `Enum` | `refresh` | The strategy to use when firing a reload (`refresh`, `restart_context`)
601+
| `spring.cloud.aws.s3.config.reload.period` | `Duration`| `15s` | The period for verifying changes
602+
| `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.
603+
|===
604+
605+
381606
=== IAM Permissions
382607

383608
Following IAM permissions are required by Spring Cloud AWS:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.autoconfigure.config;
17+
18+
import io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStorePropertySources;
19+
import io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerPropertySources;
20+
import io.awspring.cloud.parameterstore.ParameterStorePropertySource;
21+
import io.awspring.cloud.secretsmanager.SecretsManagerPropertySource;
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.util.ClassUtils;
27+
28+
public class ConfigStoreRuntimeHints implements RuntimeHintsRegistrar {
29+
30+
@Override
31+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
32+
if (ClassUtils.isPresent("io.awspring.cloud.parameterstore.ParameterStorePropertySource", classLoader)) {
33+
hints.reflection().registerType(TypeReference.of(ParameterStorePropertySources.class),
34+
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
35+
MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
36+
hints.reflection().registerType(TypeReference.of(ParameterStorePropertySource.class),
37+
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
38+
MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
39+
}
40+
41+
if (ClassUtils.isPresent("io.awspring.cloud.secretsmanager.SecretsManagerPropertySource", classLoader)) {
42+
hints.reflection().registerType(TypeReference.of(SecretsManagerPropertySources.class),
43+
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
44+
MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
45+
46+
hints.reflection().registerType(TypeReference.of(SecretsManagerPropertySource.class),
47+
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
48+
MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
49+
}
50+
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.autoconfigure.config.s3;
17+
18+
import io.awspring.cloud.autoconfigure.config.BootstrapLoggingHelper;
19+
import io.awspring.cloud.s3.config.S3PropertySource;
20+
import java.util.Collections;
21+
import java.util.Map;
22+
import org.springframework.boot.context.config.ConfigData;
23+
import org.springframework.boot.context.config.ConfigDataLoader;
24+
import org.springframework.boot.context.config.ConfigDataLoaderContext;
25+
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
26+
import org.springframework.boot.logging.DeferredLogFactory;
27+
import org.springframework.core.env.MapPropertySource;
28+
import org.springframework.lang.Nullable;
29+
import software.amazon.awssdk.services.s3.S3Client;
30+
31+
/**
32+
* Loads config data from AWS S3.
33+
*
34+
* @author Kunal Varpe
35+
* @since 3.3.0
36+
*/
37+
public class S3ConfigDataLoader implements ConfigDataLoader<S3ConfigDataResource> {
38+
39+
public S3ConfigDataLoader(DeferredLogFactory logFactory) {
40+
BootstrapLoggingHelper.reconfigureLoggers(logFactory, "io.awspring.cloud.s3.config.S3PropertySource",
41+
"io.awspring.cloud.autoconfigure.config.s3.S3PropertySources");
42+
}
43+
44+
@Override
45+
@Nullable
46+
public ConfigData load(ConfigDataLoaderContext context, S3ConfigDataResource resource) {
47+
try {
48+
// resource is disabled if s3 integration is disabled via
49+
// spring.cloud.aws.s3.config.enabled=false
50+
if (resource.isEnabled()) {
51+
S3Client s3Client = context.getBootstrapContext().get(S3Client.class);
52+
S3PropertySource propertySource = resource.getPropertySources()
53+
.createPropertySource(resource.getContext(), resource.isOptional(), s3Client);
54+
if (propertySource != null) {
55+
return new ConfigData(Collections.singletonList(propertySource));
56+
}
57+
else {
58+
return null;
59+
}
60+
}
61+
else {
62+
// create dummy empty config data
63+
return new ConfigData(Collections.singletonList(new MapPropertySource("aws-s3:" + context, Map.of())));
64+
}
65+
}
66+
catch (Exception e) {
67+
throw new ConfigDataResourceNotFoundException(resource, e);
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)