Merge pull request #233 from xetys/fix-discovery-acl
* fix-discovery-acl: Uses ACL token when fetching catalog services
This commit is contained in:
@@ -165,9 +165,19 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private void addInstancesToList(List<ServiceInstance> instances, String serviceId,
|
||||
QueryParams queryParams) {
|
||||
Response<List<HealthService>> services = client.getHealthServices(serviceId,
|
||||
this.properties.getDefaultQueryTag(), this.properties.isQueryPassing(),
|
||||
queryParams);
|
||||
|
||||
String aclToken = properties.getAclToken();
|
||||
Response<List<HealthService>> 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 class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public List<String> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.Configuration;
|
||||
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 class ConsulDiscoveryClientAclTests {
|
||||
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<ServiceInstance> instances = discoveryClient.getInstances("testSecondServiceAcl");
|
||||
assertNotNull("second service instances was null", instances);
|
||||
assertFalse("second service instances was empty", instances.isEmpty());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
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 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
properties = {
|
||||
"spring.cloud.consul.discovery.catalogServicesWatch.enabled=false",
|
||||
"spring.cloud.consul.discovery.defaultQueryTag=intg"})
|
||||
@DirtiesContext
|
||||
public class ConsulDiscoveryClientDefaultQueryTagTests {
|
||||
|
||||
public static final String NAME = "consulServiceDefaultTag";
|
||||
|
||||
@@ -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.boot.builder.SpringApplicationBuilder;
|
||||
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 @@ import com.ecwid.consul.v1.ConsulClient;
|
||||
* @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
|
||||
|
||||
@@ -18,7 +18,10 @@
|
||||
\"policy\": \"write\"
|
||||
},
|
||||
\"testConsulDiscoveryAcl\": {
|
||||
\"policy\": \"read\"
|
||||
\"policy\": \"deny\"
|
||||
},
|
||||
\"testConsulDiscoveryAcl\": {
|
||||
\"policy\": \"deny\"
|
||||
}
|
||||
}
|
||||
}"
|
||||
|
||||
@@ -4,8 +4,14 @@
|
||||
"Type": "client",
|
||||
"Rules": "{
|
||||
\"service\": {
|
||||
\"\": {
|
||||
\"policy\": \"write\"
|
||||
},
|
||||
\"testConsulDiscoveryAcl\": {
|
||||
\"policy\": \"write\"
|
||||
},
|
||||
\"testSecondServiceAcl\": {
|
||||
\"policy\": \"write\"
|
||||
}
|
||||
}
|
||||
}"
|
||||
|
||||
Reference in New Issue
Block a user