Use SecretsPropertySource for Kubernetes Secrets (#1234)
Co-authored-by: Nils Breunese <nbreunese@bol.com>
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Path, List<MapPropertySource>, List<MapPropertySource>> {
|
||||
private static class SecretsPropertySourceCollector
|
||||
implements Collector<Path, List<SecretsPropertySource>, List<SecretsPropertySource>> {
|
||||
|
||||
@Override
|
||||
public Supplier<List<MapPropertySource>> supplier() {
|
||||
public Supplier<List<SecretsPropertySource>> supplier() {
|
||||
return ArrayList::new;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiConsumer<List<MapPropertySource>, Path> accumulator() {
|
||||
public BiConsumer<List<SecretsPropertySource>, 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<List<MapPropertySource>> combiner() {
|
||||
public BinaryOperator<List<SecretsPropertySource>> combiner() {
|
||||
return (left, right) -> {
|
||||
left.addAll(right);
|
||||
return left;
|
||||
@@ -154,7 +154,7 @@ public abstract class SecretsPropertySourceLocator implements PropertySourceLoca
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<List<MapPropertySource>, List<MapPropertySource>> finisher() {
|
||||
public Function<List<SecretsPropertySource>, List<SecretsPropertySource>> 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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user