From 91b20f3f0afbb7d26e4c556844cba78989737fc7 Mon Sep 17 00:00:00 2001 From: Johannes Beck Date: Mon, 13 Apr 2020 16:04:41 +0200 Subject: [PATCH] Properties from file system should be search with the same rules used for environment variables (#276) Properties from file system should be search with the same rules used for environment variables. --- .../filesystem-config-source.adoc | 6 ++++ .../source/file/FileSystemConfigSource.java | 28 +++++++++++++++++++ .../file/FileSystemConfigSourceTestCase.java | 17 +++++++++++ .../file/configDir/MyService_mp_rest_url | 1 + .../file/configDir/OTHERSERVICE_MP_REST_URL | 1 + 5 files changed, 53 insertions(+) create mode 100644 sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/MyService_mp_rest_url create mode 100644 sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/OTHERSERVICE_MP_REST_URL diff --git a/doc/modules/ROOT/pages/config-sources/filesystem-config-source.adoc b/doc/modules/ROOT/pages/config-sources/filesystem-config-source.adoc index 711ad78d6..aa4539f2a 100644 --- a/doc/modules/ROOT/pages/config-sources/filesystem-config-source.adoc +++ b/doc/modules/ROOT/pages/config-sources/filesystem-config-source.adoc @@ -25,6 +25,12 @@ Nested directories are not supported. This Config Source can be used to read configuration from https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap[Kubernetes ConfigMap]. +The same mapping rules as defined for environment variables are applied, so the FileSystemConfigSource will search for a given property name (e.g. com.ACME.size): + +* Exact match (i.e. com.ACME.size) +* Replace each character that is neither alphanumeric nor _ with _ (i.e. com_ACME_size) +* Replace each character that is neither alphanumeric nor _ with _; then convert the name to upper case (i.e. COM_ACME_SIZE) + === Usage To use the FileSystem Config Source, add the following to your Maven `pom.xml`: diff --git a/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java b/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java index 6f46f1ff8..30bccbe96 100644 --- a/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java +++ b/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java @@ -23,6 +23,7 @@ import java.nio.file.Path; import java.util.Collections; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -59,6 +60,7 @@ public class FileSystemConfigSource extends MapBackedConfigSource { private static final long serialVersionUID = 654034634846856045L; + private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]"); private static final Logger LOG = Logger.getLogger("io.smallrye.config"); FileSystemConfigSource(File dir) { @@ -96,4 +98,30 @@ private static String readContent(Path file) { throw new UncheckedIOException(e); } } + + @Override + public String getValue(String name) { + if (name == null) { + return null; + } + + final Map properties = getProperties(); + + // exact match + String value = properties.get(name); + if (value != null) { + return value; + } + + // replace non-alphanumeric characters by underscores + String sanitizedName = PATTERN.matcher(name).replaceAll("_"); + + value = properties.get(sanitizedName); + if (value != null) { + return value; + } + + // replace non-alphanumeric characters by underscores and convert to uppercase + return properties.get(sanitizedName.toUpperCase()); + } } diff --git a/sources/file-system/src/test/java/io/smallrye/config/source/file/FileSystemConfigSourceTestCase.java b/sources/file-system/src/test/java/io/smallrye/config/source/file/FileSystemConfigSourceTestCase.java index 342aff896..dfcddddaf 100644 --- a/sources/file-system/src/test/java/io/smallrye/config/source/file/FileSystemConfigSourceTestCase.java +++ b/sources/file-system/src/test/java/io/smallrye/config/source/file/FileSystemConfigSourceTestCase.java @@ -17,6 +17,7 @@ package io.smallrye.config.source.file; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.io.File; import java.net.URISyntaxException; @@ -42,4 +43,20 @@ public void testConfigSourceFromDir() throws URISyntaxException { assertEquals("myValue1", configSource.getValue("myKey1")); assertEquals("true", configSource.getValue("myKey2")); } + + @Test + public void testCharacterReplacement() throws URISyntaxException { + URL configDirURL = this.getClass().getResource("configDir"); + File dir = new File(configDirURL.toURI()); + + ConfigSource configSource = new FileSystemConfigSource(dir); + // the non-alphanumeric chars may be replaced by _ + assertEquals("http://localhost:8080/my-service", configSource.getValue("MyService/mp-rest/url")); + // or the file name is uppercased + assertEquals("http://localhost:8080/other-service", configSource.getValue("OtherService/mp-rest/url")); + // but the key is still case sensitive + assertNull(configSource.getValue("myservice/mp-rest/url")); + // you can't rewrite the key, only the file name + assertNull(configSource.getValue("MYSERVICE_MP_REST_URL")); + } } diff --git a/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/MyService_mp_rest_url b/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/MyService_mp_rest_url new file mode 100644 index 000000000..78a0fcbeb --- /dev/null +++ b/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/MyService_mp_rest_url @@ -0,0 +1 @@ +http://localhost:8080/my-service \ No newline at end of file diff --git a/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/OTHERSERVICE_MP_REST_URL b/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/OTHERSERVICE_MP_REST_URL new file mode 100644 index 000000000..df3faa126 --- /dev/null +++ b/sources/file-system/src/test/resources/io/smallrye/config/source/file/configDir/OTHERSERVICE_MP_REST_URL @@ -0,0 +1 @@ +http://localhost:8080/other-service \ No newline at end of file