From 120fea22953c5d4436cd3ea088306ae8db5cfffd Mon Sep 17 00:00:00 2001 From: Nils Breunese Date: Thu, 23 Feb 2023 19:08:45 +0100 Subject: [PATCH] Use SecretsPropertySource for Kubernetes Secrets (#1234) Co-authored-by: Nils Breunese --- ...tesClientSecretsPropertySourceLocator.java | 4 +-- ...RetryableSecretsPropertySourceLocator.java | 3 +- .../config/SecretsPropertySourceLocator.java | 30 ++++++++++--------- .../Fabric8SecretsPropertySourceLocator.java | 4 +-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocator.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocator.java index 0337d799..575d1640 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySourceLocator.java @@ -21,9 +21,9 @@ import io.kubernetes.client.openapi.apis.CoreV1Api; import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.config.NormalizedSource; import org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource; import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.MapPropertySource; import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace; @@ -45,7 +45,7 @@ public class KubernetesClientSecretsPropertySourceLocator extends SecretsPropert } @Override - protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource source) { + protected SecretsPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource source) { String normalizedNamespace = source.namespace().orElse(null); String namespace = getApplicationNamespace(normalizedNamespace, source.target(), kubernetesNamespaceProvider); diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java index a9554206..3cc71e96 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigDataRetryableSecretsPropertySourceLocator.java @@ -20,7 +20,6 @@ import java.util.Collection; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; -import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.retry.support.RetryTemplate; @@ -71,7 +70,7 @@ public class ConfigDataRetryableSecretsPropertySourceLocator extends SecretsProp } @Override - protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, + protected SecretsPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource normalizedSource) { return this.secretsPropertySourceLocator.getPropertySource(environment, normalizedSource); } 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 e247ff10..4f2664c6 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 @@ -41,7 +41,6 @@ import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; -import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; /** @@ -87,7 +86,8 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca putPathConfig(composite); if (this.properties.enableApi()) { - uniqueSources.forEach(s -> composite.addPropertySource(getMapPropertySourceForSingleSecret(env, s))); + uniqueSources + .forEach(s -> composite.addPropertySource(getSecretsPropertySourceForSingleSecret(env, s))); } cache.discardAll(); @@ -101,13 +101,13 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca return PropertySourceLocator.super.locateCollection(environment); } - private MapPropertySource getMapPropertySourceForSingleSecret(ConfigurableEnvironment environment, + private SecretsPropertySource getSecretsPropertySourceForSingleSecret(ConfigurableEnvironment environment, NormalizedSource normalizedSource) { return getPropertySource(environment, normalizedSource); } - protected abstract MapPropertySource getPropertySource(ConfigurableEnvironment environment, + protected abstract SecretsPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource normalizedSource); protected void putPathConfig(CompositePropertySource composite) { @@ -120,25 +120,25 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca LOG.warn("Error walking properties files", e); return null; } - }).filter(Objects::nonNull).filter(Files::isRegularFile).collect(new MapPropertySourceCollector()) + }).filter(Objects::nonNull).filter(Files::isRegularFile).collect(new SecretsPropertySourceCollector()) .forEach(composite::addPropertySource); } /** * @author wind57 */ - private static class MapPropertySourceCollector - implements Collector, List> { + private static class SecretsPropertySourceCollector + implements Collector, List> { @Override - public Supplier> supplier() { + public Supplier> supplier() { return ArrayList::new; } @Override - public BiConsumer, Path> accumulator() { + public BiConsumer, Path> accumulator() { return (list, filePath) -> { - MapPropertySource source = property(filePath); + SecretsPropertySource source = property(filePath); if (source != null) { list.add(source); } @@ -146,7 +146,7 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca } @Override - public BinaryOperator> combiner() { + public BinaryOperator> combiner() { return (left, right) -> { left.addAll(right); return left; @@ -154,7 +154,7 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca } @Override - public Function, List> finisher() { + public Function, List> finisher() { return Function.identity(); } @@ -163,13 +163,15 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca return EnumSet.of(Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH); } - private MapPropertySource property(Path filePath) { + private SecretsPropertySource 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)); + String sourceName = fileName.toLowerCase(); + SourceData sourceData = new SourceData(sourceName, Collections.singletonMap(fileName, content)); + return new SecretsPropertySource(sourceData); } catch (IOException e) { LOG.warn("Error reading properties file", e); diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocator.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocator.java index 56c27f1a..2d098ebf 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8SecretsPropertySourceLocator.java @@ -22,10 +22,10 @@ import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.config.NormalizedSource; import org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties; +import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySource; import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.MapPropertySource; import static org.springframework.cloud.kubernetes.fabric8.Fabric8Utils.getApplicationNamespace; @@ -51,7 +51,7 @@ public class Fabric8SecretsPropertySourceLocator extends SecretsPropertySourceLo } @Override - protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, + protected SecretsPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource normalizedSource) { // NormalizedSource has a namespace, but users can skip it. // In such cases we try to get it elsewhere