Skip to content

Commit

Permalink
Uses ACL token when fetching catalog services
Browse files Browse the repository at this point in the history
Adds ACL token to consul client when fetching catalog services

fixes spring-cloudgh-201
  • Loading branch information
David Steiman authored and spencergibb committed Feb 20, 2017
1 parent 0a0b10a commit 3329f97
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,19 @@ public List<ServiceInstance> getInstances(final String serviceId,

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
Expand All @@ -188,7 +198,14 @@ public List<ServiceInstance> getAllInstances() {

@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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<ServiceInstance> instances = discoveryClient.getInstances("testSecondServiceAcl");
assertNotNull("second service instances was null", instances);
assertFalse("second service instances was empty", instances.isEmpty());
}

@Configuration
@EnableDiscoveryClient
@EnableAutoConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

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;
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;
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/consul_acl/consul_anonymous_acl.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
\"policy\": \"write\"
},
\"testConsulDiscoveryAcl\": {
\"policy\": \"read\"
\"policy\": \"deny\"
},
\"testConsulDiscoveryAcl\": {
\"policy\": \"deny\"
}
}
}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"Type": "client",
"Rules": "{
\"service\": {
\"\": {
\"policy\": \"write\"
},
\"testConsulDiscoveryAcl\": {
\"policy\": \"write\"
},
\"testSecondServiceAcl\": {
\"policy\": \"write\"
}
}
}"
Expand Down

0 comments on commit 3329f97

Please sign in to comment.