If consul token is not set, default to CONSUL_TOKEN env var
This matches the behaviour of ConsulPropertySourceLocator which depends on ConsulConfigProperties that uses `@Value` which is not recognized in ConfigData. Fixes gh-738
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -26,7 +26,7 @@
|
||||
<spring-cloud-deployer.version>1.0.3.RELEASE</spring-cloud-deployer.version>
|
||||
<spring-cloud-openfeign.version>3.1.7-SNAPSHOT</spring-cloud-openfeign.version>
|
||||
<spring-cloud-stream.version>3.2.8-SNAPSHOT</spring-cloud-stream.version>
|
||||
<testcontainers.version>1.15.1</testcontainers.version>
|
||||
<testcontainers.version>1.17.6</testcontainers.version>
|
||||
</properties>
|
||||
|
||||
<scm>
|
||||
|
||||
@@ -212,9 +212,14 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol
|
||||
.bind(ConsulConfigProperties.PREFIX, Bindable.of(ConsulConfigProperties.class), bindHandler)
|
||||
.orElseGet(ConsulConfigProperties::new);
|
||||
|
||||
if (StringUtils.isEmpty(properties.getName())) {
|
||||
if (!StringUtils.hasText(properties.getName())) {
|
||||
properties.setName(binder.bind("spring.application.name", String.class).orElse("application"));
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(properties.getAclToken())) {
|
||||
properties.setAclToken(binder.bind("spring.cloud.consul.token", String.class)
|
||||
.orElse(binder.bind("consul.token", String.class).orElse(null)));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
|
||||
import org.springframework.boot.DefaultBootstrapContext;
|
||||
@@ -30,6 +32,7 @@ import org.springframework.boot.context.config.ConfigDataLocationResolverContext
|
||||
import org.springframework.boot.context.config.Profiles;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.cloud.consul.ConsulProperties;
|
||||
import org.springframework.core.env.SystemEnvironmentPropertySource;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
@@ -81,6 +84,28 @@ public class ConsulConfigDataLocationResolverTests {
|
||||
assertThat(properties.getPort()).isEqualTo(8502);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "consul.token", "CONSUL_TOKEN", "spring.cloud.consul.token", "SPRING_CLOUD_CONSUL_TOKEN",
|
||||
"spring.cloud.consul.config.acl-token" })
|
||||
public void testLoadConfigProperties(String property) {
|
||||
MockEnvironment mockEnvironment = new MockEnvironment();
|
||||
String tokenValue = "mytoken";
|
||||
if (property.contains("_")) {
|
||||
SystemEnvironmentPropertySource envPS = new SystemEnvironmentPropertySource("mocksysenv",
|
||||
Collections.singletonMap(property, tokenValue));
|
||||
mockEnvironment.getPropertySources().addLast(envPS);
|
||||
}
|
||||
else {
|
||||
mockEnvironment.setProperty(property, tokenValue);
|
||||
}
|
||||
Binder binder = Binder.get(mockEnvironment);
|
||||
ConfigDataLocationResolverContext resolverContext = mock(ConfigDataLocationResolverContext.class);
|
||||
when(resolverContext.getBinder()).thenReturn(binder);
|
||||
when(resolverContext.getBootstrapContext()).thenReturn(new DefaultBootstrapContext());
|
||||
ConsulConfigProperties properties = createResolver().loadConfigProperties(resolverContext);
|
||||
assertThat(properties.getAclToken()).isEqualTo(tokenValue);
|
||||
}
|
||||
|
||||
private List<String> toContexts(List<ConsulConfigDataResource> locations) {
|
||||
return locations.stream().map(ConsulConfigDataResource::getContext).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user