diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConfigWatch.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConfigWatch.java index d7e174d3..18c7b50c 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConfigWatch.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConfigWatch.java @@ -31,6 +31,7 @@ import org.springframework.cloud.endpoint.event.RefreshEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.util.StringUtils; import lombok.Data; import lombok.extern.apachecommons.CommonsLog; @@ -84,7 +85,13 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware { currentIndex = -1L; } - Response> response = this.consul.getKVValues(context, + // use the consul ACL token if found + String aclToken = properties.getAclToken(); + if (StringUtils.isEmpty(aclToken)) { + aclToken = null; + } + + Response> response = this.consul.getKVValues(context, aclToken, new QueryParams(this.properties.getWatch().getWaitTime(), currentIndex)); diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConfigWatchTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConfigWatchTests.java index 5c271bda..0d23e898 100644 --- a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConfigWatchTests.java +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConfigWatchTests.java @@ -24,11 +24,13 @@ import org.junit.Before; import org.junit.Test; import org.springframework.cloud.endpoint.event.RefreshEvent; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.util.StringUtils; import java.util.Arrays; import java.util.List; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -49,6 +51,15 @@ public class ConfigWatchTests { configProperties = new ConsulConfigProperties(); } + @Test + public void watchPublishesEventWithAcl() { + ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); + + setupWatch(eventPublisher, new GetValue(), "2ee647bd-bd69-4118-9f34-b9a6e9e60746"); + + verify(eventPublisher, times(1)).publishEvent(any(RefreshEvent.class)); + } + @Test public void watchPublishesEvent() { ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class); @@ -77,7 +88,15 @@ public class ConfigWatchTests { verify(eventPublisher, times(1)).publishEvent(any(RefreshEvent.class)); } - private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, ConsulConfigProperties configProperties, String context ) { + private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, String aclToken) { + setupWatch(eventPublisher, getValue, this.configProperties, "/app/"); + } + + private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, ConsulConfigProperties configProperties, String context) { + setupWatch(eventPublisher, getValue, configProperties, context, null); + } + + private void setupWatch(ApplicationEventPublisher eventPublisher, GetValue getValue, ConsulConfigProperties configProperties, String context, String aclToken) { ConsulClient consul = mock(ConsulClient.class); List getValues = null; @@ -86,7 +105,12 @@ public class ConfigWatchTests { } Response> response = new Response<>(getValues, 1L, false, 1L); - when(consul.getKVValues(eq(context), any(QueryParams.class))).thenReturn(response); + when(consul.getKVValues(eq(context), anyString(), any(QueryParams.class))).thenReturn(response); + + ConsulConfigProperties properties = new ConsulConfigProperties(); + if (StringUtils.hasText(aclToken)) { + properties.setAclToken(aclToken); + } ConfigWatch watch = new ConfigWatch(configProperties, Arrays.asList(context), consul); watch.setApplicationEventPublisher(eventPublisher);