diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java index 3fd30ed0e..a88a63c9a 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java @@ -165,9 +165,19 @@ public List getInstances(final String serviceId, private void addInstancesToList(List instances, String serviceId, QueryParams queryParams) { - Response> services = client.getHealthServices(serviceId, - this.properties.getDefaultQueryTag(), this.properties.isQueryPassing(), - queryParams); + + String aclToken = properties.getAclToken(); + Response> services; + if (StringUtils.hasText(aclToken)) { + services = client.getHealthServices(serviceId, + this.properties.getDefaultQueryTag(), + this.properties.isQueryPassing(), queryParams, aclToken); + } + else { + services = client.getHealthServices(serviceId, + this.properties.getDefaultQueryTag(), + this.properties.isQueryPassing(), queryParams); + } for (HealthService service : services.getValue()) { String host = findHost(service); instances.add(new DefaultServiceInstance(serviceId, host, service @@ -188,7 +198,14 @@ public List getAllInstances() { @Override public List getServices() { - return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue() - .keySet()); + String aclToken = properties.getAclToken(); + + if (StringUtils.hasText(aclToken)) { + return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT, aclToken).getValue() + .keySet()); + } else { + return new ArrayList<>(client.getCatalogServices(QueryParams.DEFAULT).getValue() + .keySet()); + } } } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientAclTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientAclTests.java index 5673d06be..33d1f91e1 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientAclTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientAclTests.java @@ -16,12 +16,11 @@ package org.springframework.cloud.consul.discovery; -import java.util.List; - import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @@ -30,6 +29,8 @@ import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringRunner; +import java.util.List; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @@ -55,6 +56,21 @@ public void getInstancesForThisServiceWorks() { assertFalse("instances was empty", instances.isEmpty()); } + + @Test + public void getInstancesForSecondServiceWorks() throws Exception { + + new SpringApplicationBuilder(MyTestConfig.class) + .run("--spring.application.name=testSecondServiceAcl", + "--server.port=0", + "--spring.cloud.consul.discovery.preferIpAddress=true", + "--consul.token=2d2e6b3b-1c82-40ab-8171-54609d8ad304"); + + List instances = discoveryClient.getInstances("testSecondServiceAcl"); + assertNotNull("second service instances was null", instances); + assertFalse("second service instances was empty", instances.isEmpty()); + } + @Configuration @EnableDiscoveryClient @EnableAutoConfiguration diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java index acc257273..515b98049 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientDefaultQueryTagTests.java @@ -30,6 +30,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import com.ecwid.consul.v1.ConsulClient; @@ -49,6 +50,7 @@ properties = { "spring.cloud.consul.discovery.catalogServicesWatch.enabled=false", "spring.cloud.consul.discovery.defaultQueryTag=intg"}) +@DirtiesContext public class ConsulDiscoveryClientDefaultQueryTagTests { public static final String NAME = "consulServiceDefaultTag"; diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationFailFastTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationFailFastTests.java index d2130e4dd..d91e1f5ed 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationFailFastTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationFailFastTests.java @@ -16,7 +16,9 @@ package org.springframework.cloud.consul.serviceregistry; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -24,6 +26,7 @@ import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; import org.springframework.cloud.consul.ConsulAutoConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; import com.ecwid.consul.ConsulException; import com.ecwid.consul.v1.ConsulClient; @@ -32,11 +35,18 @@ * @author Spencer Gibb * @author Venil Noronha */ +@DirtiesContext public class ConsulAutoServiceRegistrationFailFastTests { - @Test(expected = ConsulException.class) + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test public void testFailFastEnabled() { - new SpringApplicationBuilder(TestConfig.class).properties("server.port=0", "spring.cloud.consul.discovery.failFast=true").run(); + this.exception.expect(ConsulException.class); + new SpringApplicationBuilder(TestConfig.class).properties("spring.application.name=testregistrationfails-fast", + "spring.jmx.default-domain=testautoregfailfast", + "server.port=0", "spring.cloud.consul.discovery.failFast=true").run(); } @SpringBootConfiguration diff --git a/src/test/resources/consul_acl/consul_anonymous_acl.json b/src/test/resources/consul_acl/consul_anonymous_acl.json index f2485952a..61745b11c 100644 --- a/src/test/resources/consul_acl/consul_anonymous_acl.json +++ b/src/test/resources/consul_acl/consul_anonymous_acl.json @@ -18,7 +18,10 @@ \"policy\": \"write\" }, \"testConsulDiscoveryAcl\": { - \"policy\": \"read\" + \"policy\": \"deny\" + }, + \"testConsulDiscoveryAcl\": { + \"policy\": \"deny\" } } }" diff --git a/src/test/resources/consul_acl/consul_discovery_client_acl.json b/src/test/resources/consul_acl/consul_discovery_client_acl.json index a458cb15f..7af310a1a 100644 --- a/src/test/resources/consul_acl/consul_discovery_client_acl.json +++ b/src/test/resources/consul_acl/consul_discovery_client_acl.json @@ -4,8 +4,14 @@ "Type": "client", "Rules": "{ \"service\": { + \"\": { + \"policy\": \"write\" + }, \"testConsulDiscoveryAcl\": { \"policy\": \"write\" + }, + \"testSecondServiceAcl\": { + \"policy\": \"write\" } } }"