Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,23 @@
package org.springframework.vault.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation that specifies how to map property names received from Vault to Spring environment.
*/
@Target({})
@Retention(RetentionPolicy.RUNTIME)
public @interface PropertyMapping {

/**
* Property name received from Vault.
*/
String from();

/**
* Property name to be set to Spring environment.
*/
String to();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package org.springframework.vault.annotation;

import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

/**
* Annotation providing a convenient and declarative mechanism for adding a
* {@link VaultPropertySource} to Spring's {@link org.springframework.core.env.Environment
Expand Down Expand Up @@ -113,6 +113,11 @@
*/
Renewal renewal() default Renewal.OFF;

/**
* Specify property names mapping from Vault to Spring environment.
*/
PropertyMapping[] propertyMappings() default {};

enum Renewal {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
*/
package org.springframework.vault.annotation;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.jspecify.annotations.Nullable;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
Expand All @@ -45,6 +38,12 @@
import org.springframework.vault.core.util.PropertyTransformer;
import org.springframework.vault.core.util.PropertyTransformers;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
* Registrar to register {@link org.springframework.vault.core.env.VaultPropertySource}s
* based on {@link VaultPropertySource}.
Expand Down Expand Up @@ -121,6 +120,7 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanD
String propertyNamePrefix = propertySource.getString("propertyNamePrefix");
Renewal renewal = propertySource.getEnum("renewal");
boolean ignoreSecretNotFound = propertySource.getBoolean("ignoreSecretNotFound");
PropertyMapping[] propertyMappings = propertySource.getAnnotationArray("propertyMappings", PropertyMapping.class);

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

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

if (propertyMappings.length != 0) {
propertyTransformer = propertyTransformer.andThen(PropertyTransformers.propertyMappingBased(propertyMappings));
}

for (String propertyPath : paths) {

if (!StringUtils.hasText(propertyPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package org.springframework.vault.core.util;

import org.springframework.util.Assert;
import org.springframework.vault.annotation.PropertyMapping;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.util.Assert;

/**
* Implementations of {@link PropertyTransformer} that provide various useful property
* transformation operations, prefixing, etc.
Expand Down Expand Up @@ -53,6 +56,14 @@ public static PropertyTransformer propertyNamePrefix(String propertyNamePrefix)
return KeyPrefixPropertyTransformer.forPrefix(propertyNamePrefix);
}

/**
* @param mappings the rules to remap property names.
* @return {@link PropertyTransformer} to map property names as specified in mappings.
*/
public static PropertyTransformer propertyMappingBased(PropertyMapping[] mappings) {
return PropertyMappingBasedPropertyTransformer.forPropertyMappings(mappings);
}

/**
* {@link PropertyTransformer} that passes the given properties through without
* returning changed properties.
Expand Down Expand Up @@ -159,4 +170,46 @@ public Map<String, Object> transformProperties(Map<String, ? extends Object> inp

}

/**
* {@link PropertyTransformer} based on {@link org.springframework.vault.annotation.PropertyMapping} annotaion rules.
*/
public static class PropertyMappingBasedPropertyTransformer implements PropertyTransformer {

private final Map<String, String> mappingRules;

private PropertyMappingBasedPropertyTransformer(PropertyMapping[] mappings) {

Map<String, String> mappingRules = new HashMap<>();

for (PropertyMapping propertyMapping : mappings) {
mappingRules.put(propertyMapping.from(), propertyMapping.to());
}

this.mappingRules = Collections.unmodifiableMap(mappingRules);
}

/**
* Create a new {@link PropertyMappingBasedPropertyTransformer} that maps property names as specified in mappings.
* @param mappings the property mapping rules.
* If key does not exist in input properties, property name will not be remapped
* @return a new {@link PropertyMappingBasedPropertyTransformer}.
*/
public static PropertyTransformer forPropertyMappings(PropertyMapping[] mappings) {
return new PropertyMappingBasedPropertyTransformer(mappings);
}

@Override
public Map<String, Object> transformProperties(Map<String, ? extends Object> input) {

Map<String, Object> target = new LinkedHashMap<>(input.size(), 1);

for (Entry<String, ? extends Object> entry : input.entrySet()) {
String newKey = this.mappingRules.getOrDefault(entry.getKey(), entry.getKey());
target.put(newKey, entry.getValue());
}

return target;
}

}
}