From 401555dc167bc9ab319e2b35839073360f0e061a Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Wed, 27 Jan 2021 19:25:50 +0100 Subject: [PATCH] feat(mpconfig): Make DirConfigSource ignore files with certains extensions Ignore *.properties, *.yaml, *.yml, *.xml, *.json files, as those are likely to contain more complex structures used for other config sources. Remember: the file name compiles to the property name (up to three letter endings silently cut off) and might contain whatever. If you put a file with any of these extensions in the directory on purpose, it should not be used with this config source. - Relates to #5006 - Based on request from @pdudits in #5007 --- .../config/source/DirConfigSource.java | 8 +++----- .../config/source/DirConfigSourceTest.java | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java index a16c21c6fc9..d4426d30aea 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/DirConfigSource.java @@ -56,11 +56,7 @@ import java.nio.file.WatchService; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; @@ -210,6 +206,7 @@ public final void run() { } private static final Logger logger = Logger.getLogger(DirConfigSource.class.getName()); + static final String[] ignoredExtensions = {".xml", ".yaml", ".yml", ".json", ".properties"}; private Path directory; private final ConcurrentHashMap properties = new ConcurrentHashMap<>(); @@ -404,6 +401,7 @@ public final static boolean isAptFile(Path path, BasicFileAttributes atts) throw return path != null && Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path) && !path.getFileName().toString().startsWith(".") && + Arrays.stream(ignoredExtensions).noneMatch(ext -> path.getFileName().toString().endsWith(ext)) && atts.size() < 512*1024; } diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java index ff341e401a6..2724c470180 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/test/java/fish/payara/nucleus/microprofile/config/source/DirConfigSourceTest.java @@ -71,7 +71,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DirConfigSourceTest { @@ -149,13 +150,12 @@ public void testIsAptDir() throws IOException { @Test public void testIsAptFile() throws IOException { - Map examples = new HashMap<>(); examples.put(subpath( "aptdir", "aptfile"), TRUE); examples.put(subpath( "aptdir", ".unaptfile"), FALSE); - - assertEquals(FALSE, DirConfigSource.isAptFile(null, null)); - assertEquals(FALSE, DirConfigSource.isAptFile(subpath( "aptdir", "aptnotexisting"), null)); + + assertFalse(DirConfigSource.isAptFile(null, null)); + assertFalse(DirConfigSource.isAptFile(subpath( "aptdir", "aptnotexisting"), null)); for (Map.Entry ex : examples.entrySet()) { BasicFileAttributes atts = writeFile(ex.getKey(), "test"); assertEquals(ex.getValue(), DirConfigSource.isAptFile(ex.getKey(), atts)); @@ -163,11 +163,17 @@ public void testIsAptFile() throws IOException { Path file100k = subpath("aptdir", "100k-file"); BasicFileAttributes atts100k = writeRandFile(file100k, 100*1024); - assertEquals(TRUE, DirConfigSource.isAptFile(file100k, atts100k)); + assertTrue(DirConfigSource.isAptFile(file100k, atts100k)); Path file600k = subpath("aptdir", "600k-file"); BasicFileAttributes atts600k = writeRandFile(file600k, 600*1024); - assertEquals(FALSE, DirConfigSource.isAptFile(file600k, atts600k)); + assertFalse(DirConfigSource.isAptFile(file600k, atts600k)); + + for (String ext : DirConfigSource.ignoredExtensions) { + Path file = subpath("aptdir", "ignorefile"+ext); + BasicFileAttributes attsFile = writeFile(file, "test"); + assertFalse(DirConfigSource.isAptFile(file, attsFile)); + } } @Test