Skip to content

Commit b755a62

Browse files
Ensure property-defined Vault token is used when auth is TOKEN (#3178)
* Ensure property-defined Vault token is used when auth is TOKEN Signed-off-by: johnycho <shunnn215@gmail.com> * Fix usage of wrong assertion tool --------- Signed-off-by: johnycho <shunnn215@gmail.com> Co-authored-by: Ryan Baxter <ryan.baxter@broadcom.com>
1 parent 21ba671 commit b755a62

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/vault/SpringVaultTemplateBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ public VaultTemplate build(VaultEnvironmentProperties vaultProperties) {
5858
}
5959

6060
private boolean isStaticToken(VaultEnvironmentProperties vaultProperties) {
61-
return vaultProperties.getAuthentication() == null && StringUtils.hasText(vaultProperties.getToken());
61+
boolean hasToken = StringUtils.hasText(vaultProperties.getToken());
62+
boolean isDefaultAuth = vaultProperties.getAuthentication() == null;
63+
boolean isTokenAuth = vaultProperties
64+
.getAuthentication() == VaultEnvironmentProperties.AuthenticationMethod.TOKEN;
65+
66+
return hasToken && (isDefaultAuth || isTokenAuth);
6267
}
6368

6469
}

spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/vault/SpringVaultTemplateBuilderTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.cloud.config.server.environment.ConfigTokenProvider;
2929
import org.springframework.cloud.config.server.environment.VaultEnvironmentProperties;
3030
import org.springframework.cloud.config.server.environment.vault.authentication.AppRoleClientAuthenticationProvider;
31+
import org.springframework.context.ApplicationContext;
3132
import org.springframework.context.support.StaticApplicationContext;
3233
import org.springframework.http.client.SimpleClientHttpRequestFactory;
3334
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -43,6 +44,9 @@
4344
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
4445
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
4546
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
47+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
48+
import static org.mockito.Mockito.mock;
49+
import static org.mockito.Mockito.verifyNoInteractions;
4650

4751
/**
4852
* @author Kaveh Shamsi
@@ -124,6 +128,21 @@ void shouldUseAppRoleToken() {
124128
""")));
125129
}
126130

131+
@Test
132+
void buildShouldUseStaticTokenWhenAuthenticationIsToken() {
133+
VaultEnvironmentProperties properties = new VaultEnvironmentProperties();
134+
properties.setToken("my-static-token");
135+
properties.setAuthentication(VaultEnvironmentProperties.AuthenticationMethod.TOKEN);
136+
137+
ConfigTokenProvider defaultTokenProvider = mock(ConfigTokenProvider.class);
138+
ApplicationContext mockContext = mock(ApplicationContext.class);
139+
140+
SpringVaultTemplateBuilder builder = new SpringVaultTemplateBuilder(defaultTokenProvider,
141+
Collections.emptyList(), mockContext);
142+
assertThatThrownBy(() -> builder.build(properties)).isInstanceOf(Exception.class);
143+
verifyNoInteractions(defaultTokenProvider);
144+
}
145+
127146
private static StaticApplicationContext givenApplicationContext(ConfigTokenProvider defaultTokenProvider) {
128147
var context = new StaticApplicationContext();
129148
context.getBeanFactory()

0 commit comments

Comments
 (0)