Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;
import org.springframework.vault.core.util.PropertyTransformer;

/**
* Annotation providing a convenient and declarative mechanism for adding a
Expand Down Expand Up @@ -113,6 +114,12 @@
*/
Renewal renewal() default Renewal.OFF;

/**
* Specify additional property transformer classes
* {@link org.springframework.vault.core.util.PropertyTransformer}.
*/
Class<? extends PropertyTransformer>[] propertyTransformers() default {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing another flavor of property transformation creates ambiguity and takes away the simplicity of use. We have property-prefixing in place and we don't want to give up on simplicity.

The huge benefit of the current design approach is that all property transformations are visible from just looking at the annotation. Any other transformers require navigation and indirection.

Instead, I suggest exploring a design towards annotation-based property mapping. Admittedly, @VaultProperties(mapping = {@PropertyMapping(from="password", to="spring.datasource.password")} reads rather clunky, but that is still a direction worth exploring. Maybe you want to explore textblocks or other syntactical variants so that we come to a design that is simple and allows for addressing your use-case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mp911de i liked the approach with @PropertyMapping as annotation parameter, also i came up with idea of specitying an array of strings, where each 2 items is pair of from-to property names. But i like the variant with @PropertyMapping more

@VaultProperties(mapping = { "url", "spring.datasource.url", "user", "spring.datasource.username", "pwd", "spring.datasource.password" })

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mp911de implemented approach with @PropertyMapping


enum Renewal {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.core.env.PropertySource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.vault.annotation.VaultPropertySource.Renewal;
import org.springframework.vault.core.lease.domain.RequestedSecret;
Expand Down Expand Up @@ -121,6 +122,7 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
String propertyNamePrefix = propertySource.getString("propertyNamePrefix");
Renewal renewal = propertySource.getEnum("renewal");
boolean ignoreSecretNotFound = propertySource.getBoolean("ignoreSecretNotFound");
Class<?>[] additionalPropertyTransformers = propertySource.getClassArray("propertyTransformers");

Assert.isTrue(paths.length > 0, "At least one @VaultPropertySource(value) location is required");

Expand All @@ -129,6 +131,10 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
PropertyTransformer propertyTransformer = StringUtils.hasText(propertyNamePrefix)
? PropertyTransformers.propertyNamePrefix(propertyNamePrefix) : PropertyTransformers.noop();

if (additionalPropertyTransformers.length != 0) {
propertyTransformer = appendPropertyTransformers(propertyTransformer, additionalPropertyTransformers);
}

for (String propertyPath : paths) {

if (!StringUtils.hasText(propertyPath)) {
Expand All @@ -154,6 +160,22 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
}
}

private PropertyTransformer appendPropertyTransformers(PropertyTransformer propertyTransformer,
Class<?>[] additionalPropertyTransformers) {
for (Class<?> propertyTransformerClass : additionalPropertyTransformers) {
propertyTransformer = propertyTransformer.andThen(createPropertyTransformer(propertyTransformerClass));
}
return propertyTransformer;
}

private PropertyTransformer createPropertyTransformer(Class<?> cls) {
try {
return (PropertyTransformer) ReflectionUtils.accessibleConstructor(cls).newInstance();
} catch (Exception e) {
throw new IllegalStateException("Could not create property transformer " + cls.getName(), e);
}
}

private String potentiallyResolveRequiredPlaceholders(String expression) {
return this.environment != null ? this.environment.resolveRequiredPlaceholders(expression) : expression;
}
Expand Down