Use unique bean names for VaultPropertySource registration.

We now make sure to register VaultPropertySourceRegistrar once and to use unique bean names for vaultPropertySource beans.

Resolves: gh-421.
This commit is contained in:
Mark Paluch
2019-05-01 21:04:22 +02:00
parent 320fe6404e
commit 69808a6ea9
2 changed files with 30 additions and 9 deletions

View File

@@ -97,11 +97,13 @@ class VaultPropertySourceRegistrar implements ImportBeanDefinitionRegistrar,
Assert.notNull(annotationMetadata, "AnnotationMetadata must not be null!");
Assert.notNull(registry, "BeanDefinitionRegistry must not be null!");
registry.registerBeanDefinition("VaultPropertySourceRegistrar",
BeanDefinitionBuilder //
.rootBeanDefinition(VaultPropertySourceRegistrar.class) //
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE) //
.getBeanDefinition());
if (!registry.isBeanNameInUse("VaultPropertySourceRegistrar")) {
registry.registerBeanDefinition("VaultPropertySourceRegistrar",
BeanDefinitionBuilder //
.rootBeanDefinition(VaultPropertySourceRegistrar.class) //
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE) //
.getBeanDefinition());
}
Set<AnnotationAttributes> propertySources = attributesForRepeatable(
annotationMetadata, VaultPropertySources.class.getName(),
@@ -135,10 +137,17 @@ class VaultPropertySourceRegistrar implements ImportBeanDefinitionRegistrar,
AbstractBeanDefinition beanDefinition = createBeanDefinition(ref,
renewal, propertyTransformer, propertyPath);
registry.registerBeanDefinition("vaultPropertySource#" + counter,
beanDefinition);
do {
String beanName = "vaultPropertySource#" + counter;
counter++;
if (!registry.isBeanNameInUse(beanName)) {
registry.registerBeanDefinition(beanName, beanDefinition);
break;
}
counter++;
}
while (true);
}
}
}

View File

@@ -24,6 +24,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -42,10 +43,18 @@ import static org.assertj.core.api.Assertions.assertThat;
@ContextConfiguration
public class VaultPropertySourceIntegrationTests {
@VaultPropertySource({ "secret/myapp", "secret/myapp/profile" })
@Import({ Partial1.class, Partial2.class })
static class Config extends VaultIntegrationTestConfiguration {
}
@VaultPropertySource({ "secret/myapp", "secret/myapp/profile" })
static class Partial1 {
}
@VaultPropertySource("secret/generic")
static class Partial2 {
}
@Autowired
Environment env;
@Autowired
@@ -63,6 +72,8 @@ public class VaultPropertySourceIntegrationTests {
vaultOperations.write("secret/myapp",
Collections.singletonMap("myapp", "myvalue"));
vaultOperations.write("secret/generic",
Collections.singletonMap("generic", "generic-value"));
vaultOperations.write("secret/myapp/profile",
Collections.singletonMap("myprofile", "myprofilevalue"));
}
@@ -72,6 +83,7 @@ public class VaultPropertySourceIntegrationTests {
assertThat(env.getProperty("myapp")).isEqualTo("myvalue");
assertThat(env.getProperty("myprofile")).isEqualTo("myprofilevalue");
assertThat(env.getProperty("generic")).isEqualTo("generic-value");
}
@Test