Skip to content

Commit

Permalink
remove AwsS3ClientCustomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed Dec 9, 2024
1 parent c0b77fd commit db29106
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 84 deletions.
23 changes: 7 additions & 16 deletions docs/src/main/asciidoc/s3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,14 @@ Note that this class must be listed under `org.springframework.boot.BootstrapReg
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`:
If you want to use autoconfigured `S3Client` but change underlying SDKClient or `ClientOverrideConfiguration` you will need to register bean of type `S3ClientCustomizer`:
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 io.awspring.cloud.autoconfigure.s3.S3ClientCustomizer;
import java.time.Duration;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;
Expand All @@ -501,20 +501,11 @@ class S3ClientBootstrapConfiguration implements BootstrapRegistryInitializer {
@Override
public void initialize(BootstrapRegistry registry) {
registry.register(AwsS3ClientCustomizer.class,
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();
}
});
registry.register(S3ClientCustomizer.class, context -> (builder -> {
builder.overrideConfiguration(builder.overrideConfiguration().copy(c -> {
c.apiCallTimeout(Duration.ofMillis(2001));
}));
}));
}
}
----
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package io.awspring.cloud.autoconfigure.config.s3;

import io.awspring.cloud.autoconfigure.AwsSyncClientCustomizer;
import io.awspring.cloud.autoconfigure.config.AbstractAwsConfigDataLocationResolver;
import io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerClientCustomizer;
import io.awspring.cloud.autoconfigure.core.*;
import io.awspring.cloud.autoconfigure.s3.S3ClientCustomizer;
import io.awspring.cloud.autoconfigure.s3.properties.S3Properties;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -72,23 +71,24 @@ public List<S3ConfigDataResource> resolveProfileSpecific(ConfigDataLocationResol
registerBean(resolverContext, AwsProperties.class, loadAwsProperties(resolverContext.getBinder()));
registerBean(resolverContext, S3Properties.class, s3Properties);
registerBean(resolverContext, CredentialsProperties.class,
loadCredentialsProperties(resolverContext.getBinder()));
loadCredentialsProperties(resolverContext.getBinder()));
registerBean(resolverContext, RegionProperties.class, loadRegionProperties(resolverContext.getBinder()));

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

contexts.forEach(propertySourceContext -> locations.add(new S3ConfigDataResource(propertySourceContext,
location.isOptional(), propertySources)));
contexts.forEach(propertySourceContext -> locations
.add(new S3ConfigDataResource(propertySourceContext, location.isOptional(), propertySources)));

if (!location.isOptional() && locations.isEmpty()) {
throw new S3KeysMissingException("No S3 keys provided in `spring.config.import=aws-s3:` configuration.");
throw new S3KeysMissingException(
"No S3 keys provided in `spring.config.import=aws-s3:` configuration.");
}
} else {
}
else {
// create dummy resources with enabled flag set to false,
// because returned locations cannot be empty
contexts.forEach(
propertySourceContext -> locations.add(new S3ConfigDataResource(propertySourceContext,
location.isOptional(), false, propertySources)));
contexts.forEach(propertySourceContext -> locations.add(
new S3ConfigDataResource(propertySourceContext, location.isOptional(), false, propertySources)));
}
return locations;
}
Expand All @@ -97,27 +97,17 @@ private S3Client createS3Client(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());
}
try {
AwsSyncClientCustomizer awsSyncClientCustomizer = context.get(AwsSyncClientCustomizer.class);
if (awsSyncClientCustomizer != null) {
awsSyncClientCustomizer.customize(builder);
S3ClientCustomizer s3ClientCustomizer = context.get(S3ClientCustomizer.class);
if (s3ClientCustomizer != null) {
s3ClientCustomizer.customize(builder);
}
}
catch (IllegalStateException e) {
log.debug("Bean of type AwsSyncClientCustomizer is not registered: " + e.getMessage());
}

try {
S3ManagerClientCustomizer secretsManagerClientCustomizer = context
.get(S3ManagerClientCustomizer.class);
S3ManagerClientCustomizer secretsManagerClientCustomizer = context.get(S3ManagerClientCustomizer.class);
if (secretsManagerClientCustomizer != null) {
secretsManagerClientCustomizer.customize(builder);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013-2024 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.AwsClientCustomizer;
Expand All @@ -10,6 +25,5 @@
* @since 3.4.0
*/
@FunctionalInterface
public interface S3ManagerClientCustomizer extends AwsClientCustomizer<S3ClientBuilder> {
public interface S3ManagerClientCustomizer extends AwsClientCustomizer<S3ClientBuilder> {
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
@EnableConfigurationProperties(S3Properties.class)
@ConditionalOnClass({ EndpointAutoConfiguration.class, RestartEndpoint.class, ContextRefresher.class })
@AutoConfigureAfter({ InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class,
RefreshAutoConfiguration.class })
RefreshAutoConfiguration.class })
@ConditionalOnProperty(value = S3Properties.PREFIX + ".config.reload.strategy")
@ConditionalOnBean(ContextRefresher.class)
public class S3ReloadAutoConfiguration {
Expand Down Expand Up @@ -81,7 +81,7 @@ public ConfigurationChangeDetector<S3PropertySource> s3PollingAwsPropertySourceC
@Qualifier("s3TaskScheduler") TaskSchedulerWrapper<TaskScheduler> taskScheduler,
ConfigurableEnvironment environment) {

return new PollingAwsPropertySourceChangeDetector<>(properties.getConfig().getReload(), S3PropertySource.class, strategy,
taskScheduler.getTaskScheduler(), environment);
return new PollingAwsPropertySourceChangeDetector<>(properties.getConfig().getReload(), S3PropertySource.class,
strategy, taskScheduler.getTaskScheduler(), environment);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2013-2024 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.s3.properties;

import io.awspring.cloud.autoconfigure.config.reload.ReloadProperties;
Expand All @@ -18,7 +33,6 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}


public ReloadProperties getReload() {
return reload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public void setEncryption(S3EncryptionProperties encryption) {
@NestedConfigurationProperty
private S3ConfigProperties config = new S3ConfigProperties();


public S3ConfigProperties getConfig() {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;

import io.awspring.cloud.autoconfigure.AwsSyncClientCustomizer;
import io.awspring.cloud.autoconfigure.ConfiguredAwsClient;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;

import io.awspring.cloud.autoconfigure.AwsSyncClientCustomizer;
import io.awspring.cloud.autoconfigure.ConfiguredAwsClient;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.BootstrapRegistry;
Expand Down Expand Up @@ -90,12 +89,11 @@ void resolvesPropertyFromS3ComplexPath() {
uploadFileToBucket("key1=value1", "myPath/unusual/application.properties", TEXT_TYPE);

try (ConfigurableApplicationContext context = runApplication(application,
"aws-s3:test-bucket/myPath/unusual/application.properties")) {
"aws-s3:test-bucket/myPath/unusual/application.properties")) {
assertThat(context.getEnvironment().getProperty("key1")).isEqualTo("value1");
}
}


@Test
void resolvesYamlFromS3() {
SpringApplication application = new SpringApplication(S3ConfigDataLoaderIntegrationTests.App.class);
Expand Down Expand Up @@ -138,10 +136,10 @@ void clientIsConfiguredWithCustomizerProvidedToBootstrapRegistry() throws JsonPr
application.setWebApplicationType(WebApplicationType.NONE);
application.addBootstrapRegistryInitializer(new S3ConfigDataLoaderIntegrationTests.CustomizerConfiguration());
uploadFileToBucket(new ObjectMapper().writeValueAsString(Map.of("key1", "value1")), "application.json",
JSON_TYPE);
JSON_TYPE);

try (ConfigurableApplicationContext context = runApplication(application,
"aws-s3:test-bucket/application.json")) {
"aws-s3:test-bucket/application.json")) {
ConfiguredAwsClient client = new ConfiguredAwsClient(context.getBean(S3Client.class));
assertThat(client.getApiCallTimeout()).isEqualTo(Duration.ofMillis(2001));
assertThat(client.getSyncHttpClient()).isNotNull();
Expand All @@ -156,7 +154,8 @@ void reloadPropertiesFromS3() {

try (ConfigurableApplicationContext context = application.run(
"--spring.config.import=aws-s3:test-bucket/reload.properties",
"--spring.cloud.aws.s3.config.reload.strategy=refresh", "--spring.cloud.aws.s3.config.reload.period=PT1S",
"--spring.cloud.aws.s3.config.reload.strategy=refresh",
"--spring.cloud.aws.s3.config.reload.period=PT1S",
"--spring.cloud.aws.s3.region=" + localstack.getRegion(),
"--spring.cloud.aws.endpoint=" + localstack.getEndpoint(),
"--spring.cloud.aws.credentials.access-key=noop", "--spring.cloud.aws.credentials.secret-key=noop",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class S3ReloadAutoConfigurationTests {

@Test
void createsBeansForRefreshStrategy() {
this.contextRunner.withPropertyValues("spring.cloud.aws.s3.config.reload.strategy:refresh").run(this::createsBeans);
this.contextRunner.withPropertyValues("spring.cloud.aws.s3.config.reload.strategy:refresh")
.run(this::createsBeans);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.modulith.events.Externalized;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;
Expand Down Expand Up @@ -64,6 +63,7 @@ DynamicPropertyRegistrar dynamicPropertyRegistrar(LocalStackContainer localstack
registry.add("spring.cloud.aws.region.static", localstack::getRegion);
};
}

@Bean
LocalStackContainer localStackContainer() {
return new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.8.1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.modulith.events.Externalized;
import org.springframework.test.context.DynamicPropertyRegistrar;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;
Expand Down Expand Up @@ -62,6 +61,7 @@ DynamicPropertyRegistrar dynamicPropertyRegistrar(LocalStackContainer localstack
registry.add("spring.cloud.aws.region.static", localstack::getRegion);
};
}

@Bean
LocalStackContainer localStackContainer() {
return new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.8.1"));
Expand Down

0 comments on commit db29106

Please sign in to comment.