Use RelaxedPropertyResolver for default configuration property lookup.

We now use RelaxedPropertyResolver to resolve configuration property defaults because `@Value` defaulting does not support relaxed binding. Previously configuration properties were annotated with of `@Value` specifying a cascade of property names.

Fixes gh-87.
This commit is contained in:
Mark Paluch
2017-03-09 18:35:18 +02:00
parent 754ae1eee7
commit 7bc05c721c
4 changed files with 72 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -15,11 +15,14 @@
*/
package org.springframework.cloud.vault.config;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/**
* Configuration properties for Vault using the generic backend.
@@ -28,7 +31,7 @@ import lombok.Data;
*/
@ConfigurationProperties("spring.cloud.vault.generic")
@Data
public class VaultGenericBackendProperties {
public class VaultGenericBackendProperties implements EnvironmentAware {
/**
* Enable the generic backend.
@@ -56,7 +59,27 @@ public class VaultGenericBackendProperties {
/**
* Application name to be used for the context.
*/
@Value("${spring.cloud.vault.applicationName:${spring.application.name:application}}")
private String applicationName;
private String applicationName = "application";
@Override
public void setEnvironment(Environment environment) {
RelaxedPropertyResolver springCloudVaultPropertyResolver = new RelaxedPropertyResolver(
environment, "spring.cloud.vault.");
String springCloudVaultAppName = springCloudVaultPropertyResolver
.getProperty("application-name");
if (StringUtils.hasText(springCloudVaultAppName)) {
this.applicationName = springCloudVaultAppName;
}
else {
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(
environment, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
if (StringUtils.hasText(springAppName)) {
this.applicationName = springAppName;
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -16,13 +16,16 @@
package org.springframework.cloud.vault.config;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import lombok.Data;
import org.springframework.util.StringUtils;
/**
* @author Spencer Gibb
@@ -30,7 +33,7 @@ import lombok.Data;
*/
@ConfigurationProperties("spring.cloud.vault")
@Data
public class VaultProperties {
public class VaultProperties implements EnvironmentAware {
/**
* Enable Vault config server.
@@ -87,24 +90,35 @@ public class VaultProperties {
/**
* Application name for AppId authentication.
*/
@org.springframework.beans.factory.annotation.Value("${spring.application.name:application}")
private String applicationName;
private String applicationName = "application";
private AuthenticationMethod authentication = AuthenticationMethod.TOKEN;
@Override
public void setEnvironment(Environment environment) {
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(
environment, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
if (StringUtils.hasText(springAppName)) {
this.applicationName = springAppName;
}
}
@Data
public static class AppIdProperties {
/**
* Property value for UserId generation using a Mac-Address.
*
*
* @see org.springframework.vault.authentication.MacAddressUserId
*/
public final static String MAC_ADDRESS = "MAC_ADDRESS";
/**
* Property value for UserId generation using an IP-Address.
*
*
* @see org.springframework.vault.authentication.IpAddressUserId
*/
public final static String IP_ADDRESS = "IP_ADDRESS";

View File

@@ -50,8 +50,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { BootstrapConfiguration.class,
VaultConfigAppIdCustomMechanismTests.TestApplication.class }, properties = {
"spring.cloud.vault.authentication=appid", "use.custom.config=true",
"spring.application.name=VaultConfigAppIdCustomMechanismTests" })
"spring.cloud.vault.authentication=appid", "use.custom.config=true",
"spring.cloud.vault.applicationName=VaultConfigAppIdCustomMechanismTests" })
public class VaultConfigAppIdCustomMechanismTests {
@BeforeClass
@@ -80,9 +80,8 @@ public class VaultConfigAppIdCustomMechanismTests {
String appId = VaultConfigAppIdCustomMechanismTests.class.getSimpleName();
vaultOperations.write(
"secret/" + VaultConfigAppIdCustomMechanismTests.class.getSimpleName(),
Collections.singletonMap("vault.value", "foo"));
vaultOperations.write("secret/" + appId,
Collections.singletonMap("vault.value", appId));
Map<String, String> appIdData = new HashMap<String, String>();
appIdData.put("value", "testpolicy"); // policy
@@ -106,7 +105,7 @@ public class VaultConfigAppIdCustomMechanismTests {
@Test
public void contextLoads() {
assertThat(configValue).isEqualTo("foo");
assertThat(configValue).isEqualTo(getClass().getSimpleName());
}
@SpringBootApplication
@@ -124,12 +123,15 @@ public class VaultConfigAppIdCustomMechanismTests {
@Bean
ClientAuthentication clientAuthentication() {
RestTemplate restTemplate = TestRestTemplateFactory.create(Settings
.createSslConfiguration());
RestTemplate restTemplate = TestRestTemplateFactory
.create(Settings.createSslConfiguration());
return new AppIdAuthentication(AppIdAuthenticationOptions.builder()
.appId("VaultConfigAppIdCustomMechanismTests")
.userIdMechanism(new StaticUserIdMechanism()).build(), restTemplate);
return new AppIdAuthentication(
AppIdAuthenticationOptions.builder()
.appId(VaultConfigAppIdCustomMechanismTests.class
.getSimpleName())
.userIdMechanism(new StaticUserIdMechanism()).build(),
restTemplate);
}
}

View File

@@ -51,7 +51,7 @@ import static org.junit.Assume.assumeTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = VaultConfigCubbyholeAuthenticationTests.TestApplication.class, properties = {
"spring.cloud.vault.authentication=cubbyhole",
"spring.application.name=VaultConfigAppIdTests" })
"spring.cloud.vault.generic.applicationName=VaultConfigCubbyholeAuthenticationTests" })
public class VaultConfigCubbyholeAuthenticationTests {
@BeforeClass
@@ -65,10 +65,10 @@ public class VaultConfigCubbyholeAuthenticationTests {
VaultOperations vaultOperations = vaultRule.prepare().getVaultOperations();
vaultOperations
.write("secret/"
+ VaultConfigCubbyholeAuthenticationTests.class.getSimpleName(),
Collections.singletonMap("vault.value", "foo"));
vaultOperations.write(
"secret/" + VaultConfigCubbyholeAuthenticationTests.class.getSimpleName(),
Collections.singletonMap("vault.value",
VaultConfigCubbyholeAuthenticationTests.class.getSimpleName()));
VaultResponse vaultResponse = vaultOperations
.doWithSession(new RestOperationsCallback<VaultResponse>() {
@@ -98,7 +98,7 @@ public class VaultConfigCubbyholeAuthenticationTests {
@Test
public void contextLoads() {
assertThat(configValue).isEqualTo("foo");
assertThat(configValue).isEqualTo(getClass().getSimpleName());
}
@SpringBootApplication