Add configuration for reactive Vault client.
Closes gh-133.
This commit is contained in:
30
pom.xml
30
pom.xml
@@ -1,12 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-build</artifactId>
|
||||
<version>2.0.0.M1</version>
|
||||
<relativePath />
|
||||
<relativePath/>
|
||||
<!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -73,6 +74,31 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Pin versions until next milestone -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-framework-bom</artifactId>
|
||||
<version>5.0.0.BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-bom</artifactId>
|
||||
<version>Bismuth-BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson</groupId>
|
||||
<artifactId>jackson-bom</artifactId>
|
||||
<version>2.9.0.pr4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- HTTP Client Libraries -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
|
||||
@@ -41,6 +41,30 @@
|
||||
<artifactId>spring-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webflux</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.ipc</groupId>
|
||||
<artifactId>reactor-netty</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
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.http.client.reactive.ClientHttpConnector;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.authentication.AuthenticationStepsFactory;
|
||||
import org.springframework.vault.authentication.AuthenticationStepsOperator;
|
||||
import org.springframework.vault.authentication.CachingVaultTokenSupplier;
|
||||
import org.springframework.vault.authentication.VaultTokenSupplier;
|
||||
import org.springframework.vault.client.ReactiveVaultClients;
|
||||
import org.springframework.vault.client.VaultEndpoint;
|
||||
import org.springframework.vault.config.ClientHttpConnectorFactory;
|
||||
import org.springframework.vault.core.ReactiveVaultOperations;
|
||||
import org.springframework.vault.core.ReactiveVaultTemplate;
|
||||
import org.springframework.vault.support.ClientOptions;
|
||||
import org.springframework.vault.support.SslConfiguration;
|
||||
import org.springframework.vault.support.SslConfiguration.KeyStoreConfiguration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for reactive Spring Vault support.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
|
||||
@ConditionalOnClass({ Flux.class, WebClient.class, ReactiveVaultOperations.class })
|
||||
@EnableConfigurationProperties({ VaultProperties.class })
|
||||
@Order(Ordered.LOWEST_PRECEDENCE - 10)
|
||||
public class ReactiveVaultBootstrapConfiguration {
|
||||
|
||||
private final VaultProperties vaultProperties;
|
||||
|
||||
private final VaultEndpoint vaultEndpoint;
|
||||
|
||||
private final ClientHttpConnector clientHttpConnector;
|
||||
|
||||
public ReactiveVaultBootstrapConfiguration(VaultProperties vaultProperties) {
|
||||
|
||||
this.vaultProperties = vaultProperties;
|
||||
this.vaultEndpoint = getVaultEndpoint(vaultProperties);
|
||||
this.clientHttpConnector = createConnector(this.vaultProperties);
|
||||
}
|
||||
|
||||
private static VaultEndpoint getVaultEndpoint(VaultProperties vaultProperties) {
|
||||
|
||||
if (StringUtils.hasText(vaultProperties.getUri())) {
|
||||
return VaultEndpoint.from(URI.create(vaultProperties.getUri()));
|
||||
}
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
vaultEndpoint.setHost(vaultProperties.getHost());
|
||||
vaultEndpoint.setPort(vaultProperties.getPort());
|
||||
vaultEndpoint.setScheme(vaultProperties.getScheme());
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link ClientHttpConnector} configured with {@link ClientOptions} and
|
||||
* {@link SslConfiguration} which are not necessarily applicable for the whole
|
||||
* application.
|
||||
*
|
||||
* @return the {@link ClientHttpConnector}.
|
||||
*/
|
||||
private static ClientHttpConnector createConnector(VaultProperties vaultProperties) {
|
||||
|
||||
ClientOptions clientOptions = new ClientOptions(Duration.ofMillis(vaultProperties
|
||||
.getConnectionTimeout()), Duration.ofMillis(vaultProperties
|
||||
.getReadTimeout()));
|
||||
|
||||
VaultProperties.Ssl ssl = vaultProperties.getSsl();
|
||||
SslConfiguration sslConfiguration;
|
||||
if (ssl != null) {
|
||||
|
||||
KeyStoreConfiguration keyStore = KeyStoreConfiguration.EMPTY;
|
||||
KeyStoreConfiguration trustStore = KeyStoreConfiguration.EMPTY;
|
||||
|
||||
if (ssl.getKeyStore() != null) {
|
||||
keyStore = new KeyStoreConfiguration(ssl.getKeyStore(),
|
||||
ssl.getKeyStorePassword() != null ? ssl.getKeyStorePassword()
|
||||
.toCharArray() : null, null);
|
||||
}
|
||||
|
||||
if (ssl.getTrustStore() != null) {
|
||||
trustStore = new KeyStoreConfiguration(ssl.getTrustStore(),
|
||||
ssl.getTrustStorePassword() != null ? ssl.getTrustStorePassword()
|
||||
.toCharArray() : null, null);
|
||||
}
|
||||
|
||||
sslConfiguration = new SslConfiguration(keyStore, trustStore);
|
||||
}
|
||||
else {
|
||||
sslConfiguration = SslConfiguration.NONE;
|
||||
}
|
||||
|
||||
return ClientHttpConnectorFactory.create(clientOptions, sslConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link ReactiveVaultTemplate}.
|
||||
*
|
||||
* @return
|
||||
* @see #vaultTokenSupplier(AuthenticationStepsFactory)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ReactiveVaultTemplate reactiveVaultTemplate(VaultTokenSupplier tokenSupplier) {
|
||||
return new ReactiveVaultTemplate(vaultEndpoint, clientHttpConnector,
|
||||
tokenSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link VaultTokenSupplier} for reactive Vault session management.
|
||||
* @see AuthenticationStepsFactory
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(VaultTokenSupplier.class)
|
||||
public VaultTokenSupplier vaultTokenSupplier(AuthenticationStepsFactory factory) {
|
||||
|
||||
WebClient webClient = ReactiveVaultClients.createWebClient(this.vaultEndpoint,
|
||||
this.clientHttpConnector);
|
||||
AuthenticationStepsOperator operator = new AuthenticationStepsOperator(
|
||||
factory.getAuthenticationSteps(), webClient);
|
||||
|
||||
return CachingVaultTokenSupplier.of(operator);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
# Bootstrap Configuration
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
org.springframework.cloud.vault.config.VaultBootstrapConfiguration,\
|
||||
org.springframework.cloud.vault.config.VaultBootstrapHealthIndicatorConfiguration
|
||||
org.springframework.cloud.vault.config.ReactiveVaultBootstrapConfiguration,\
|
||||
org.springframework.cloud.vault.config.VaultBootstrapHealthIndicatorConfiguration
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.vault.authentication.AuthenticationSteps;
|
||||
import org.springframework.vault.authentication.AuthenticationStepsFactory;
|
||||
import org.springframework.vault.authentication.VaultTokenSupplier;
|
||||
import org.springframework.vault.core.ReactiveVaultOperations;
|
||||
import org.springframework.vault.support.VaultToken;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveVaultBootstrapConfiguration}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ReactiveVaultBootstrapConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void shouldConfigureTemplate() {
|
||||
|
||||
load(AuthenticationFactoryConfiguration.class);
|
||||
|
||||
assertThat(context.getBean(ReactiveVaultOperations.class)).isNotNull();
|
||||
assertThat(context.getBean(AuthenticationStepsFactory.class)).isNotNull();
|
||||
assertThat(context.getBeanNamesForType(WebClient.class)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConfigureTemplateWithTokenSupplier() {
|
||||
|
||||
load(TokeSupplierConfiguration.class);
|
||||
|
||||
assertThat(context.getBean(ReactiveVaultOperations.class)).isNotNull();
|
||||
assertThat(context.getBeanNamesForType(WebClient.class)).isEmpty();
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
|
||||
ctx.register(config);
|
||||
ctx.register(ReactiveVaultBootstrapConfiguration.class);
|
||||
ctx.refresh();
|
||||
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class AuthenticationFactoryConfiguration {
|
||||
|
||||
@Bean
|
||||
AuthenticationStepsFactory authenticationStepsFactory() {
|
||||
return () -> AuthenticationSteps.just(VaultToken.of("foo"));
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TokeSupplierConfiguration {
|
||||
|
||||
@Bean
|
||||
VaultTokenSupplier vaultTokenSupplier() {
|
||||
return () -> Mono.just(VaultToken.of("foo"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.vault.util.VaultRule;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.vault.core.ReactiveVaultOperations;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Integration test using config infrastructure with token authentication.
|
||||
*
|
||||
* <p>
|
||||
* In case this test should fail because of SSL make sure you run the test within the
|
||||
* spring-cloud-vault-config/spring-cloud-vault-config directory as the keystore is
|
||||
* referenced with {@code ../work/keystore.jks}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = ReactiveVaultOperationsTests.TestApplication.class, properties = {
|
||||
"spring.cloud.vault.host=foo", "spring.cloud.vault.uri=https://localhost:8200", })
|
||||
public class ReactiveVaultOperationsTests {
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
|
||||
VaultRule vaultRule = new VaultRule();
|
||||
vaultRule.before();
|
||||
|
||||
Map<String, Object> object = new HashMap<>();
|
||||
object.put("vault.value", "foo");
|
||||
object.put("nested", Collections.singletonMap("key", "value"));
|
||||
|
||||
vaultRule.prepare().getVaultOperations().write("secret/testVaultApp", object);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
ReactiveVaultOperations reactiveOperations;
|
||||
|
||||
@Test
|
||||
public void shouldAccessVault() {
|
||||
|
||||
StepVerifier.create(reactiveOperations.read("secret/testVaultApp"))
|
||||
.consumeNextWith(actual -> {
|
||||
assertThat(actual.getData()).containsEntry("vault.value", "foo");
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
public static class TestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@
|
||||
<description>Spring Cloud Vault Dependencies</description>
|
||||
|
||||
<properties>
|
||||
<spring-vault.version>2.0.0.M1</spring-vault.version>
|
||||
<spring-vault.version>2.0.0.BUILD-SNAPSHOT</spring-vault.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
Reference in New Issue
Block a user