diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultGenericBackendProperties.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultGenericBackendProperties.java index ecca369c..5c650103 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultGenericBackendProperties.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultGenericBackendProperties.java @@ -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; + } + } + } } diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultProperties.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultProperties.java index e1c083c7..598276c4 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultProperties.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultProperties.java @@ -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"; diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigAppIdCustomMechanismTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigAppIdCustomMechanismTests.java index 38dfa4c1..fbdc3216 100644 --- a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigAppIdCustomMechanismTests.java +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigAppIdCustomMechanismTests.java @@ -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 appIdData = new HashMap(); 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); } } diff --git a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigCubbyholeAuthenticationTests.java b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigCubbyholeAuthenticationTests.java index aa67f467..ab89c416 100644 --- a/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigCubbyholeAuthenticationTests.java +++ b/spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigCubbyholeAuthenticationTests.java @@ -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() { @@ -98,7 +98,7 @@ public class VaultConfigCubbyholeAuthenticationTests { @Test public void contextLoads() { - assertThat(configValue).isEqualTo("foo"); + assertThat(configValue).isEqualTo(getClass().getSimpleName()); } @SpringBootApplication