Add support in ConfigWatch for acl token

Fixes gh-264
This commit is contained in:
Danny Hamrick
2017-01-08 00:26:49 -05:00
committed by Spencer Gibb
parent 5eaf6aa87a
commit d115aee52b
2 changed files with 34 additions and 3 deletions

View File

@@ -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<List<GetValue>> response = this.consul.getKVValues(context,
// use the consul ACL token if found
String aclToken = properties.getAclToken();
if (StringUtils.isEmpty(aclToken)) {
aclToken = null;
}
Response<List<GetValue>> response = this.consul.getKVValues(context, aclToken,
new QueryParams(this.properties.getWatch().getWaitTime(),
currentIndex));

View File

@@ -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<GetValue> getValues = null;
@@ -86,7 +105,12 @@ public class ConfigWatchTests {
}
Response<List<GetValue>> 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);