Skip to content

Commit

Permalink
disable configsource if no host or hosts are set
Browse files Browse the repository at this point in the history
  • Loading branch information
rikcarve committed Apr 12, 2020
1 parent cd777a5 commit 8eb72b6
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
target/

.idea/
*.iml
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ The eclipse microprofile config framework is a simple yet powerful configuration
<dependency>
<groupId>ch.carve</groupId>
<artifactId>mp-config-consul</artifactId>
<version>0.8</version>
<version>0.9</version>
</dependency>
```

## Configuration
Currently there are 6 values you can configure, either through Java system properties or environment variables:
* **configsource.consul.host** url of your consul agent instance, e.g. "192.168.99.100", default value is "localhost", variable substitution available.
* **configsource.consul.host** url of your consul agent instance, e.g. "192.168.99.100", default empty, variable substitution available.
* **configsource.consul.hosts** list of consul servers, e.g. "192.168.99.100,192.168.99.101", default empty, variable substitution available.
* **configsource.consul.port** port of consul, e.g. "8500", default value is "8500", variable substitution available.
* **configsource.consul.validity** how long to cache values (in seconds), default is 30s
* **configsource.consul.prefix** key prefix to search value in consul, variable substitution available
* **configsource.consul.list-all** whether getProperties() should query consul for all kv pairs, default is false
* **configsource.consul.token** token that will be used to retrieve key/values from consul. Default is empty and retrieval is done without token.

Note: these config values cannot be set in Quarkus application.properties, you need to pass them as JVM arguments like this `-Dconfigsource.consul.host=consul.mycompany.com`
> Note: these config values cannot be set in Quarkus application.properties, you need to pass them as JVM arguments like this `-Dconfigsource.consul.host=consul.mycompany.com` or through environment variables.
> Note: if both host and hosts are empty, this configsource is disabled!
## Ordinal
Config sources have priorities called ordinal. This config source has ordinal 550, but can be overriden with setting 'config_ordinal' in this source (including prefix if defined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Configuration {
.build();

private StringSubstitutor substitutor = new StringSubstitutor(s -> getConfigValue(s, ""));
private String consulHost = substitutor.replace(getConfigValue("configsource.consul.host", "localhost"));
private String consulHost = substitutor.replace(getConfigValue("configsource.consul.host", ""));
private String consulHostList = substitutor.replace(getConfigValue("configsource.consul.hosts", ""));
private int consulPort = Integer.valueOf(substitutor.replace(getConfigValue("configsource.consul.port", "8500")));
private long validity = Long.valueOf(getConfigValue("configsource.consul.validity", "30")) * 1000L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class ConsulConfigSource implements ConfigSource {

Configuration config = new Configuration();
ExpiringMap<String, String> cache = new ExpiringMap<>(config.getValidity());

boolean isDisabled = config.getConsulHost().isEmpty() && config.getConsulHostList().isEmpty();
ConsulClientWrapper client = new ConsulClientWrapper(config.getConsulHost(), config.getConsulHostList(), config.getConsulPort(), config.getToken());

@Override
public Map<String, String> getProperties() {
// only query for values if explicitly enabled
if (config.listAll()) {
if (!isDisabled && config.listAll()) {
List<Entry<String, String>> values = client.getKeyValuePairs(config.getPrefix());
values.forEach(v -> cache.put(v.getKey(), v.getValue()));
}
Expand All @@ -37,6 +37,9 @@ public Map<String, String> getProperties() {

@Override
public String getValue(String propertyName) {
if (isDisabled) {
return null;
}
String value = cache.getOrCompute(propertyName,
p -> client.getValue(config.getPrefix() + propertyName),
p -> logger.debug("consul getKV failed for key {}", p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.jupiter.api.Test;

public class ConfigurationTest {

@Test
public void testGetValidity() throws Exception {
Configuration config = new Configuration();
Expand Down Expand Up @@ -48,7 +48,7 @@ public void testGetPrefix_withSubstitution() throws Exception {
@Test
public void testGetConsulHost() throws Exception {
Configuration config = new Configuration();
assertEquals("localhost", config.getConsulHost());
assertEquals("", config.getConsulHost());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public void init(ClientAndServer client) {
clientServer.when(request().withPath("/v1/status/leader")).respond(response().withBody("localhost"));
clientServer.when(request().withPath("/v1/status/peers")).respond(response().withBody("[\"localhost:8300\"]"));
clientServer.when(request().withPath("/v1/kv/test")).respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"test\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
clientServer.when(request().withPath("/v1/kv/testFromToken").withQueryStringParameter("token", "tokenValue")).respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"testFromToken\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
clientServer.when(request().withPath("/v1/kv/testFromToken").withQueryStringParameter("token", "tokenValue"))
.respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"testFromToken\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
clientServer.when(request().withPath("/v1/kv/myapp")).respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"test\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
clientWrapper = new ConsulClientWrapper("localhost", null, clientServer.getLocalPort(), null);
}
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testGetValue_force_reconnect() {
clientServer.when(request().withPath("/v1/kv/test")).respond(response().withStatusCode(503));
assertThrows(OperationException.class, () -> clientWrapper.getValue("test"));
clientServer.clear(request().withPath("/v1/kv/test"));
clientServer.when(request().withPath("/v1/kv/test")).respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"test\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
clientServer.when(request().withPath("/v1/kv/test")).respond(response().withBody("[{\"LockIndex\":0,\"Key\":\"test\",\"Flags\":0,\"Value\":\"aGVsbG8=\",\"CreateIndex\":1,\"ModifyIndex\":2}]"));
String value = clientWrapper.getValue("test");
assertEquals("hello", value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -21,6 +21,7 @@ class ConsulConfigSourceTest {

@BeforeEach
public void init() {
System.setProperty("configsource.consul.host", "localhost");
configSource = new ConsulConfigSource();
configSource.config = new Configuration();
configSource.client = mock(ConsulClientWrapper.class);
Expand Down Expand Up @@ -51,6 +52,7 @@ void testGetProperties_from_consul() {
@Test
void testGetProperties_with_null() {
when(configSource.client.getValue(anyString())).thenReturn(null);
configSource.getValue("test");
assertEquals(0, configSource.getProperties().size());
}

Expand Down

0 comments on commit 8eb72b6

Please sign in to comment.