Added support for multiple database secrets

Original pull request: gh-587
Closes gh-459
This commit is contained in:
Quintin Beukes
2021-09-08 11:45:44 +02:00
committed by Mark Paluch
parent 9dba371fba
commit 2ddb9d1e66
5 changed files with 76 additions and 7 deletions

View File

@@ -16,9 +16,8 @@
package org.springframework.cloud.vault.config.databases;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.vault.config.PropertyNameTransformer;
@@ -27,9 +26,15 @@ import org.springframework.cloud.vault.config.SecretBackendMetadataFactory;
import org.springframework.cloud.vault.config.VaultSecretBackendDescriptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.util.Assert;
import org.springframework.vault.core.util.PropertyTransformer;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
/**
* Bootstrap configuration providing support for the Database secret backends such as
* Database, Apache Cassandra, Couchbase and MongoDB.
@@ -38,13 +43,32 @@ import org.springframework.vault.core.util.PropertyTransformer;
* @author Per Abich
* @author Sebastien Nahelou
* @author Francis Hitchens
* @author Quintin Beukes
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ VaultMySqlProperties.class, VaultPostgreSqlProperties.class,
VaultCassandraProperties.class, VaultCouchbaseProperties.class, VaultMongoProperties.class,
VaultElasticsearchProperties.class, VaultDatabaseProperties.class })
VaultElasticsearchProperties.class, VaultMultipleDatabaseProperties.class, VaultDatabaseProperties.class })
@Order(Ordered.LOWEST_PRECEDENCE - 15)
public class VaultConfigDatabaseBootstrapConfiguration {
@Autowired
private ConfigurableBeanFactory beanFactory;
@Autowired
private VaultMultipleDatabaseProperties multipleDatabaseProperties;
@PostConstruct
public void registerBeans() {
multipleDatabaseProperties.getDatabases().forEach(d -> {
String beanName = String.format("vaultMultipleDatabaseProperties_%s",
multipleDatabaseProperties.getDatabases().indexOf(d));
if (!beanFactory.containsBean(beanName)) {
beanFactory.registerSingleton(beanName, d);
}
});
}
@Bean
@ConditionalOnMissingBean
public DatabaseSecretBackendMetadataFactory databaseSecretBackendMetadataFactory() {

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2016-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.vault.config.databases;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
/**
* Configuration properties for multiple database secrets using the Database backend. This
* is configured with the spring.cloud.vault.databases list.
*
* @author Quintin Beukes
* @since 3.0.3
*/
@ConfigurationProperties("spring.cloud.vault")
public class VaultMultipleDatabaseProperties {
private List<VaultDatabaseProperties> databases = new ArrayList<>();
public List<VaultDatabaseProperties> getDatabases() {
return databases;
}
public void setDatabases(List<VaultDatabaseProperties> databases) {
this.databases = databases;
}
}

View File

@@ -24,7 +24,8 @@ import org.springframework.lang.Nullable;
* Configuration properties for Vault using the MySQL integration.
*
* @author Mark Paluch
* @deprecated since 2.0. Use {@link VaultDatabaseProperties}.
* @deprecated since 2.0. Use {@link VaultDatabaseProperties} or
* {@link VaultMultipleDatabaseProperties}.
*/
@ConfigurationProperties("spring.cloud.vault.mysql")
@Deprecated

View File

@@ -23,7 +23,8 @@ import org.springframework.lang.Nullable;
* Configuration properties for Vault using the PostgreSQL integration.
*
* @author Mark Paluch
* @deprecated since 2.0. Use {@link VaultDatabaseProperties}.
* @deprecated since 2.0. Use {@link VaultDatabaseProperties} or
* {@link VaultMultipleDatabaseProperties}.
*/
@ConfigurationProperties("spring.cloud.vault.postgresql")
@Deprecated

View File

@@ -119,7 +119,6 @@ public abstract class VaultPropertySourceLocatorSupport implements PropertySourc
List<PropertySource<?>> propertySources = new ArrayList<>(doCreateKeyValuePropertySources(environment));
for (SecretBackendMetadata backendAccessor : sorted) {
PropertySource<?> vaultPropertySource = createVaultPropertySource(backendAccessor);
propertySources.add(vaultPropertySource);
}