diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java index 4b09389e..47d9f322 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java @@ -20,9 +20,17 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; import java.util.List; -import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -39,6 +47,7 @@ import org.springframework.core.env.PropertySource; * * @author l burgazzoli * @author Haytham Mohamed + * @author wind57 */ public abstract class SecretsPropertySourceLocator implements PropertySourceLocator { @@ -57,14 +66,13 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca List sources = this.properties.determineSources(); CompositePropertySource composite = new CompositePropertySource("composite-secrets"); - if (this.properties.isEnableApi()) { - sources.forEach( - s -> composite.addFirstPropertySource(getKubernetesPropertySourceForSingleSecret(env, s))); - } - // read for secrets mount putPathConfig(composite); + if (this.properties.isEnableApi()) { + sources.forEach(s -> composite.addPropertySource(getKubernetesPropertySourceForSingleSecret(env, s))); + } + return composite; } return null; @@ -81,31 +89,72 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca SecretsConfigProperties.NormalizedSource normalizedSource, String configurationTarget); protected void putPathConfig(CompositePropertySource composite) { - this.properties.getPaths().stream().map(Paths::get).filter(Files::exists).forEach(p -> putAll(p, composite)); + + this.properties.getPaths().stream().map(Paths::get).filter(Files::exists).flatMap(x -> { + try { + return Files.walk(x); + } + catch (IOException e) { + LOG.warn("Error walking properties files", e); + return null; + } + }).filter(Objects::nonNull).filter(Files::isRegularFile).collect(new MapPropertySourceCollector()) + .forEach(composite::addPropertySource); } - protected void putAll(Path path, CompositePropertySource composite) { - try { + /** + * @author wind57 + */ + private static class MapPropertySourceCollector + implements Collector, List> { - Files.walk(path).filter(Files::isRegularFile).forEach(p -> readFile(p, composite)); + @Override + public Supplier> supplier() { + return ArrayList::new; } - catch (IOException e) { - LOG.warn("Error walking properties files", e); - } - } - protected void readFile(Path path, CompositePropertySource composite) { - try { - Map result = new HashMap<>(); - result.put(path.getFileName().toString(), new String(Files.readAllBytes(path)).trim()); - if (!result.isEmpty()) { - composite.addFirstPropertySource( - new MapPropertySource(path.getFileName().toString().toLowerCase(), result)); + @Override + public BiConsumer, Path> accumulator() { + return (list, filePath) -> { + MapPropertySource source = property(filePath); + if (source != null) { + list.add(source); + } + }; + } + + @Override + public BinaryOperator> combiner() { + return (left, right) -> { + left.addAll(right); + return left; + }; + } + + @Override + public Function, List> finisher() { + return Function.identity(); + } + + @Override + public Set characteristics() { + return EnumSet.of(Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH); + } + + private MapPropertySource property(Path filePath) { + + String fileName = filePath.getFileName().toString(); + + try { + String content = new String(Files.readAllBytes(filePath)).trim(); + return new MapPropertySource(fileName.toLowerCase(), Collections.singletonMap(fileName, content)); + } + catch (IOException e) { + LOG.warn("Error reading properties file", e); + return null; } } - catch (IOException e) { - LOG.warn("Error reading properties file", e); - } + } }