Skip to content

Commit 2908ea8

Browse files
authored
Do not log warning message for STS credentials when properties are not configured. (#1444) (#1528)
1 parent 7e3c00a commit 2908ea8

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/core/CredentialsProviderAutoConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import software.amazon.awssdk.services.sts.StsClient;
4545
import software.amazon.awssdk.services.sts.auth.StsWebIdentityTokenFileCredentialsProvider;
4646

47+
import static software.amazon.awssdk.core.SdkSystemSetting.AWS_WEB_IDENTITY_TOKEN_FILE;
48+
4749
/**
4850
* {@link EnableAutoConfiguration} for {@link AwsCredentialsProvider}.
4951
*
@@ -103,7 +105,7 @@ public static AwsCredentialsProvider createCredentialsProvider(CredentialsProper
103105
}
104106

105107
StsProperties sts = properties.getSts();
106-
if (ClassUtils.isPresent(STS_WEB_IDENTITY_TOKEN_FILE_CREDENTIALS_PROVIDER, null)) {
108+
if (isWebIdentitiyTokenFileConfigured(sts) && ClassUtils.isPresent(STS_WEB_IDENTITY_TOKEN_FILE_CREDENTIALS_PROVIDER, null)) {
107109
try {
108110
providers.add(StsCredentialsProviderFactory.create(sts, regionProvider));
109111
}
@@ -124,6 +126,11 @@ else if (providers.size() == 1) {
124126
}
125127
}
126128

129+
private static boolean isWebIdentitiyTokenFileConfigured(@Nullable StsProperties sts) {
130+
// AWS_WEB_IDENTITY_TOKEN_FILE can be configured either through environment variable, system properties or `spring.cloud.aws.sts` properties.
131+
return AWS_WEB_IDENTITY_TOKEN_FILE.getStringValue().isPresent() || (sts != null && sts.getWebIdentityTokenFile() != null);
132+
}
133+
127134
private static StaticCredentialsProvider createStaticCredentialsProvider(CredentialsProperties properties) {
128135
return StaticCredentialsProvider
129136
.create(AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey()));

spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/core/CredentialsProviderAutoConfigurationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import java.io.IOException;
2222
import java.nio.file.Path;
2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.api.extension.ExtendWith;
2425
import org.junit.jupiter.api.io.TempDir;
2526
import org.springframework.boot.autoconfigure.AutoConfigurations;
2627
import org.springframework.boot.test.context.FilteredClassLoader;
2728
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
29+
import org.springframework.boot.test.system.CapturedOutput;
30+
import org.springframework.boot.test.system.OutputCaptureExtension;
2831
import org.springframework.context.annotation.Bean;
2932
import org.springframework.context.annotation.Configuration;
3033
import org.springframework.core.io.ClassPathResource;
@@ -116,6 +119,21 @@ void credentialsProvider_stsPropertiesConfigured_configuresStsWebIdentityTokenFi
116119
});
117120
}
118121

122+
@Test
123+
@ExtendWith(OutputCaptureExtension.class)
124+
void credentialsProvider_stsCredentialsProviderNotConfigured_whenWebIdentityTokenNotConfigured(CapturedOutput output)
125+
throws IOException {
126+
this.contextRunner
127+
.withPropertyValues("spring.cloud.aws.region.static:af-south-1")
128+
.run((context) -> {
129+
AwsCredentialsProvider awsCredentialsProvider = context.getBean("credentialsProvider",
130+
AwsCredentialsProvider.class);
131+
assertThat(awsCredentialsProvider).isNotNull()
132+
.isInstanceOf(DefaultCredentialsProvider.class);
133+
});
134+
assertThat(output).doesNotContain("Skipping creating `StsCredentialsProvider`");
135+
}
136+
119137
@Test
120138
void credentialsProvider_stsSystemPropertiesDefault_configuresStsWebIdentityTokenFileCredentialsProvider()
121139
throws IOException {

0 commit comments

Comments
 (0)