Allow overriding SecretBackendMetadataFactory beans.

We now allow overriding SecretBackendMetadataFactory by application-provided configuration to customize behavior of integrated secret backends.

Closes gh-118.
This commit is contained in:
Mark Paluch
2017-06-03 15:09:40 +02:00
parent 1c9be8b4ad
commit 97bcf1ebe8
12 changed files with 366 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.vault.config.aws;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.vault.config.PropertyNameTransformer;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
@@ -38,11 +39,16 @@ import org.springframework.vault.core.util.PropertyTransformer;
public class VaultConfigAwsBootstrapConfiguration {
@Bean
public SecretBackendMetadataFactory<VaultAwsProperties> awsSecretBackendMetadataFactory() {
@ConditionalOnMissingBean
public AwsSecretBackendMetadataFactory awsSecretBackendMetadataFactory() {
return new AwsSecretBackendMetadataFactory();
}
static class AwsSecretBackendMetadataFactory
/**
* {@link SecretBackendMetadataFactory} for AWS integration using
* {@link VaultAwsProperties}.
*/
public static class AwsSecretBackendMetadataFactory
implements SecretBackendMetadataFactory<VaultAwsProperties> {
@Override
@@ -66,7 +72,7 @@ public class VaultConfigAwsBootstrapConfiguration {
* @param properties must not be {@literal null}.
* @return the {@link SecretBackendMetadata}
*/
public static SecretBackendMetadata forAws(final VaultAwsProperties properties) {
static SecretBackendMetadata forAws(final VaultAwsProperties properties) {
Assert.notNull(properties, "VaultAwsProperties must not be null");

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2017 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
*
* http://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.aws;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.config.GenericSecretBackendMetadata;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
import org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfiguration.AwsSecretBackendMetadataFactory;
import org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfigurationTests.CustomBootstrapConfiguration;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link VaultConfigAwsBootstrapConfiguration}.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CustomBootstrapConfiguration.class, properties = {
"VaultConfigAwsBootstrapConfigurationTests.custom.config=true",
"spring.cloud.vault.aws.role=foo" })
public class VaultConfigAwsBootstrapConfigurationTests extends IntegrationTestSupport {
@Autowired
AwsSecretBackendMetadataFactory factory;
@Autowired
VaultAwsProperties properties;
@Test
public void shouldApplyCustomConfiguration() {
SecretBackendMetadata metadata = factory.createMetadata(properties);
assertThat(metadata).isInstanceOf(GenericSecretBackendMetadata.class);
assertThat(metadata.getPath()).isEqualTo(properties.getRole());
}
@Configuration
public static class CustomBootstrapConfiguration {
@Bean
@ConditionalOnProperty("VaultConfigAwsBootstrapConfigurationTests.custom.config")
AwsSecretBackendMetadataFactory customFactory() {
return new AwsSecretBackendMetadataFactory() {
@Override
public SecretBackendMetadata createMetadata(
VaultAwsProperties backendDescriptor) {
return GenericSecretBackendMetadata
.create(backendDescriptor.getRole());
}
};
}
}
}

View File

@@ -0,0 +1,4 @@
# Bootstrap Configuration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfigurationTests.CustomBootstrapConfiguration,\
org.springframework.cloud.vault.config.aws.VaultConfigAwsBootstrapConfiguration

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.vault.config.consul;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.vault.config.PropertyNameTransformer;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
@@ -38,11 +39,16 @@ import org.springframework.vault.core.util.PropertyTransformer;
public class VaultConfigConsulBootstrapConfiguration {
@Bean
public SecretBackendMetadataFactory<VaultConsulProperties> consulSecretBackendAccessorFactory() {
@ConditionalOnMissingBean
public ConsulSecretBackendMetadataFactory consulSecretBackendAccessorFactory() {
return new ConsulSecretBackendMetadataFactory();
}
static class ConsulSecretBackendMetadataFactory
/**
* {@link SecretBackendMetadataFactory} for Consul integration using
* {@link VaultConsulProperties}.
*/
public static class ConsulSecretBackendMetadataFactory
implements SecretBackendMetadataFactory<VaultConsulProperties> {
@Override
@@ -64,7 +70,7 @@ public class VaultConfigConsulBootstrapConfiguration {
* @param properties must not be {@literal null}.
* @return the {@link SecretBackendMetadata}
*/
public static SecretBackendMetadata forConsul(
static SecretBackendMetadata forConsul(
final VaultConsulProperties properties) {
Assert.notNull(properties, "VaultConsulProperties must not be null");

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2017 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
*
* http://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.consul;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.config.GenericSecretBackendMetadata;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
import org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfiguration.ConsulSecretBackendMetadataFactory;
import org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfigurationTests.CustomBootstrapConfiguration;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link VaultConfigConsulBootstrapConfigurationTests}.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CustomBootstrapConfiguration.class, properties = {
"VaultConfigConsulBootstrapConfigurationTests.custom.config=true",
"spring.cloud.vault.consul.role=foo", "spring.cloud.vault.consul.enabled=true" })
public class VaultConfigConsulBootstrapConfigurationTests extends IntegrationTestSupport {
@Autowired
ConsulSecretBackendMetadataFactory factory;
@Autowired
VaultConsulProperties properties;
@Test
public void shouldApplyCustomConfiguration() {
SecretBackendMetadata metadata = factory.createMetadata(properties);
assertThat(metadata).isInstanceOf(GenericSecretBackendMetadata.class);
assertThat(metadata.getPath()).isEqualTo(properties.getRole());
}
@Configuration
public static class CustomBootstrapConfiguration {
@Bean
@ConditionalOnProperty("VaultConfigConsulBootstrapConfigurationTests.custom.config")
ConsulSecretBackendMetadataFactory customFactory() {
return new ConsulSecretBackendMetadataFactory() {
@Override
public SecretBackendMetadata createMetadata(
VaultConsulProperties backendDescriptor) {
return GenericSecretBackendMetadata
.create(backendDescriptor.getRole());
}
};
}
}
}

View File

@@ -0,0 +1,4 @@
# Bootstrap Configuration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfigurationTests.CustomBootstrapConfiguration,\
org.springframework.cloud.vault.config.consul.VaultConfigConsulBootstrapConfiguration

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.vault.config.databases;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.vault.config.PropertyNameTransformer;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
@@ -41,11 +42,16 @@ import org.springframework.vault.core.util.PropertyTransformer;
public class VaultConfigDatabaseBootstrapConfiguration {
@Bean
public SecretBackendMetadataFactory<DatabaseSecretProperties> databaseSecretBackendMetadataFactory() {
@ConditionalOnMissingBean
public DatabaseSecretBackendMetadataFactory databaseSecretBackendMetadataFactory() {
return new DatabaseSecretBackendMetadataFactory();
}
static class DatabaseSecretBackendMetadataFactory
/**
* {@link SecretBackendMetadataFactory} for Database integration using
* {@link DatabaseSecretProperties}.
*/
public static class DatabaseSecretBackendMetadataFactory
implements SecretBackendMetadataFactory<DatabaseSecretProperties> {
@Override
@@ -69,7 +75,7 @@ public class VaultConfigDatabaseBootstrapConfiguration {
* @param properties must not be {@literal null}.
* @return the {@link SecretBackendMetadata}
*/
public static SecretBackendMetadata forDatabase(
static SecretBackendMetadata forDatabase(
final DatabaseSecretProperties properties) {
Assert.notNull(properties, "DatabaseSecretProperties must not be null");

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2017 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
*
* http://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.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.config.GenericSecretBackendMetadata;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
import org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration.DatabaseSecretBackendMetadataFactory;
import org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfigurationTests.CustomBootstrapConfiguration;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link VaultConfigDatabaseBootstrapConfiguration}.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CustomBootstrapConfiguration.class, properties = {
"VaultConfigDatabaseBootstrapConfigurationTests.custom.config=true",
"spring.cloud.vault.mysql.role=foo", "spring.cloud.vault.mysql.enabled=true" })
public class VaultConfigDatabaseBootstrapConfigurationTests
extends IntegrationTestSupport {
@Autowired
DatabaseSecretBackendMetadataFactory factory;
@Autowired
VaultMySqlProperties properties;
@Test
public void shouldApplyCustomConfiguration() {
SecretBackendMetadata metadata = factory.createMetadata(properties);
assertThat(metadata).isInstanceOf(GenericSecretBackendMetadata.class);
assertThat(metadata.getPath()).isEqualTo(properties.getRole());
}
@Configuration
public static class CustomBootstrapConfiguration {
@Bean
@ConditionalOnProperty("VaultConfigDatabaseBootstrapConfigurationTests.custom.config")
DatabaseSecretBackendMetadataFactory customFactory() {
return new DatabaseSecretBackendMetadataFactory() {
@Override
public SecretBackendMetadata createMetadata(
DatabaseSecretProperties backendDescriptor) {
return GenericSecretBackendMetadata
.create(backendDescriptor.getRole());
}
};
}
}
}

View File

@@ -0,0 +1,4 @@
# Bootstrap Configuration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfigurationTests.CustomBootstrapConfiguration,\
org.springframework.cloud.vault.config.databases.VaultConfigDatabaseBootstrapConfiguration

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.vault.config.rabbitmq;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.vault.config.PropertyNameTransformer;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
@@ -38,7 +39,8 @@ import org.springframework.vault.core.util.PropertyTransformer;
public class VaultConfigRabbitMqBootstrapConfiguration {
@Bean
public SecretBackendMetadataFactory<VaultRabbitMqProperties> rabbitMqSecureBackendAccessorFactory() {
@ConditionalOnMissingBean
public RabbitMqSecretBackendMetadataFactory rabbitMqSecureBackendAccessorFactory() {
return new RabbitMqSecretBackendMetadataFactory();
}
@@ -47,7 +49,11 @@ public class VaultConfigRabbitMqBootstrapConfiguration {
return new VaultRabbitMqProperties();
}
static class RabbitMqSecretBackendMetadataFactory
/**
* {@link SecretBackendMetadataFactory} for RabbitMq integration using
* {@link VaultRabbitMqProperties}.
*/
public static class RabbitMqSecretBackendMetadataFactory
implements SecretBackendMetadataFactory<VaultRabbitMqProperties> {
@Override
@@ -71,7 +77,7 @@ public class VaultConfigRabbitMqBootstrapConfiguration {
* @param properties must not be {@literal null}.
* @return the {@link SecretBackendMetadata}
*/
public static SecretBackendMetadata forRabbitMq(
static SecretBackendMetadata forRabbitMq(
final VaultRabbitMqProperties properties) {
Assert.notNull(properties, "VaultRabbitMqProperties must not be null");

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2017 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
*
* http://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.rabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.vault.config.GenericSecretBackendMetadata;
import org.springframework.cloud.vault.config.SecretBackendMetadata;
import org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfiguration.RabbitMqSecretBackendMetadataFactory;
import org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfigurationTests.CustomBootstrapConfiguration;
import org.springframework.cloud.vault.util.IntegrationTestSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link VaultConfigRabbitMqBootstrapConfigurationTests}.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CustomBootstrapConfiguration.class, properties = {
"VaultConfigRabbitMqBootstrapConfigurationTests.custom.config=true",
"spring.cloud.vault.rabbitmq.role=foo" })
public class VaultConfigRabbitMqBootstrapConfigurationTests
extends IntegrationTestSupport {
@Autowired
RabbitMqSecretBackendMetadataFactory factory;
@Autowired
VaultRabbitMqProperties properties;
@Test
public void shouldApplyCustomConfiguration() {
SecretBackendMetadata metadata = factory.createMetadata(properties);
assertThat(metadata).isInstanceOf(GenericSecretBackendMetadata.class);
assertThat(metadata.getPath()).isEqualTo(properties.getRole());
}
@Configuration
public static class CustomBootstrapConfiguration {
@Bean
@ConditionalOnProperty("VaultConfigRabbitMqBootstrapConfigurationTests.custom.config")
RabbitMqSecretBackendMetadataFactory customFactory() {
return new RabbitMqSecretBackendMetadataFactory() {
@Override
public SecretBackendMetadata createMetadata(
VaultRabbitMqProperties backendDescriptor) {
return GenericSecretBackendMetadata
.create(backendDescriptor.getRole());
}
};
}
}
}

View File

@@ -0,0 +1,4 @@
# Bootstrap Configuration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfigurationTests.CustomBootstrapConfiguration,\
org.springframework.cloud.vault.config.rabbitmq.VaultConfigRabbitMqBootstrapConfiguration