Always pass acl token (even if null). Add consul.token shortcut.
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.cloud.consul.config;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import lombok.Data;
|
||||
@@ -50,6 +51,7 @@ public class ConsulConfigProperties {
|
||||
@NotEmpty
|
||||
private String dataKey = "data";
|
||||
|
||||
@Value("${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}")
|
||||
private String aclToken;
|
||||
|
||||
private Watch watch = new Watch();
|
||||
|
||||
@@ -58,24 +58,18 @@ public class ConsulPropertySource extends EnumerablePropertySource<ConsulClient>
|
||||
}
|
||||
|
||||
public void init() {
|
||||
Response<List<GetValue>> response;
|
||||
if (configProperties.getAclToken() == null) {
|
||||
response = source.getKVValues(context, QueryParams.DEFAULT);
|
||||
}
|
||||
else {
|
||||
response = source.getKVValues(context, configProperties.getAclToken(),
|
||||
QueryParams.DEFAULT);
|
||||
}
|
||||
Response<List<GetValue>> response = source.getKVValues(context,
|
||||
configProperties.getAclToken(), QueryParams.DEFAULT);
|
||||
|
||||
final List<GetValue> values = response.getValue();
|
||||
ConsulConfigProperties.Format format = configProperties.getFormat();
|
||||
switch (format) {
|
||||
case KEY_VALUE:
|
||||
parsePropertiesInKeyValueFormat(values);
|
||||
break;
|
||||
case PROPERTIES:
|
||||
case YAML:
|
||||
parsePropertiesWithNonKeyValueFormat(values, format);
|
||||
case KEY_VALUE:
|
||||
parsePropertiesInKeyValueFormat(values);
|
||||
break;
|
||||
case PROPERTIES:
|
||||
case YAML:
|
||||
parsePropertiesWithNonKeyValueFormat(values, format);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,13 +113,15 @@ public class ConsulPropertySource extends EnumerablePropertySource<ConsulClient>
|
||||
final Properties props = generateProperties(value, format);
|
||||
|
||||
for (Map.Entry entry : props.entrySet()) {
|
||||
properties.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
properties
|
||||
.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Properties generateProperties(String value, ConsulConfigProperties.Format format) {
|
||||
private Properties generateProperties(String value,
|
||||
ConsulConfigProperties.Format format) {
|
||||
final Properties props = new Properties();
|
||||
|
||||
if (format == ConsulConfigProperties.Format.PROPERTIES) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.util.InetUtils;
|
||||
|
||||
@@ -45,6 +46,7 @@ public class ConsulDiscoveryProperties {
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private InetUtils.HostInfo hostInfo;
|
||||
|
||||
@Value("${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}")
|
||||
private String aclToken;
|
||||
|
||||
/** Tags to use when registering service */
|
||||
|
||||
@@ -145,11 +145,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
|
||||
|
||||
protected void register(NewService newService) {
|
||||
log.info("Registering service with consul: {}", newService.toString());
|
||||
if (properties.getAclToken() == null) {
|
||||
client.agentServiceRegister(newService);
|
||||
} else {
|
||||
client.agentServiceRegister(newService, properties.getAclToken());
|
||||
}
|
||||
client.agentServiceRegister(newService, properties.getAclToken());
|
||||
if (ttlConfig.isEnabled() && ttlScheduler != null) {
|
||||
ttlScheduler.add(newService);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
@SpringApplicationConfiguration(classes = ConsulDiscoveryClientAclTests.MyTestConfig.class)
|
||||
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscoveryAcl",
|
||||
"spring.cloud.consul.discovery.preferIpAddress=true",
|
||||
"spring.cloud.consul.discovery.aclToken=2d2e6b3b-1c82-40ab-8171-54609d8ad304"}, randomPort = true)
|
||||
"consul.token=2d2e6b3b-1c82-40ab-8171-54609d8ad304"}, randomPort = true)
|
||||
public class ConsulDiscoveryClientAclTests {
|
||||
|
||||
@Autowired
|
||||
|
||||
Reference in New Issue
Block a user