diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/EncryptablePropertySourceConverter.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/EncryptablePropertySourceConverter.java index 2035c9e..217ef0e 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/EncryptablePropertySourceConverter.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/EncryptablePropertySourceConverter.java @@ -26,73 +26,85 @@ @Slf4j public class EncryptablePropertySourceConverter { - public static void convertPropertySources(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver propertyResolver, EncryptablePropertyFilter propertyFilter, MutablePropertySources propSources) { + private final InterceptionMode interceptionMode; + private final List>> skipPropertySourceClasses; + private final EncryptablePropertyResolver propertyResolver; + private final EncryptablePropertyFilter propertyFilter; + + public EncryptablePropertySourceConverter(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver propertyResolver, EncryptablePropertyFilter propertyFilter) { + this.interceptionMode = interceptionMode; + this.skipPropertySourceClasses = skipPropertySourceClasses; + this.propertyResolver = propertyResolver; + this.propertyFilter = propertyFilter; + } + + public void convertPropertySources(MutablePropertySources propSources) { StreamSupport.stream(propSources.spliterator(), false) .filter(ps -> !(ps instanceof EncryptablePropertySource)) - .map(ps -> makeEncryptable(interceptionMode, skipPropertySourceClasses, propertyResolver, propertyFilter, ps)) + .map(this::makeEncryptable) .collect(toList()) .forEach(ps -> propSources.replace(ps.getName(), ps)); } @SuppressWarnings("unchecked") - public static PropertySource makeEncryptable(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver propertyResolver, EncryptablePropertyFilter propertyFilter, PropertySource propertySource) { + public PropertySource makeEncryptable(PropertySource propertySource) { if (propertySource instanceof EncryptablePropertySource || skipPropertySourceClasses.stream().anyMatch(skipClass -> skipClass.equals(propertySource.getClass()))) { log.info("Skipping PropertySource {} [{}", propertySource.getName(), propertySource.getClass()); return propertySource; } - PropertySource encryptablePropertySource = convertPropertySource(interceptionMode, propertyResolver, propertyFilter, propertySource); + PropertySource encryptablePropertySource = convertPropertySource(propertySource); log.info("Converting PropertySource {} [{}] to {}", propertySource.getName(), propertySource.getClass().getName(), AopUtils.isAopProxy(encryptablePropertySource) ? "AOP Proxy" : encryptablePropertySource.getClass().getSimpleName()); return encryptablePropertySource; } - public static MutablePropertySources proxyPropertySources(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver propertyResolver, EncryptablePropertyFilter propertyFilter, MutablePropertySources propertySources, EnvCopy envCopy) { + public MutablePropertySources proxyPropertySources(MutablePropertySources propertySources, EnvCopy envCopy) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(MutablePropertySources.class); proxyFactory.setProxyTargetClass(true); proxyFactory.addInterface(PropertySources.class); proxyFactory.setTarget(propertySources); - proxyFactory.addAdvice(new EncryptableMutablePropertySourcesInterceptor(interceptionMode, skipPropertySourceClasses, propertyResolver, propertyFilter, envCopy)); + proxyFactory.addAdvice(new EncryptableMutablePropertySourcesInterceptor(this, envCopy)); return (MutablePropertySources) proxyFactory.getProxy(); } - private static PropertySource convertPropertySource(InterceptionMode interceptionMode, EncryptablePropertyResolver propertyResolver, EncryptablePropertyFilter propertyFilter, PropertySource propertySource) { + private PropertySource convertPropertySource(PropertySource propertySource) { return interceptionMode == InterceptionMode.PROXY - ? proxyPropertySource(propertySource, propertyResolver, propertyFilter) : instantiatePropertySource(propertySource, propertyResolver, propertyFilter); + ? proxyPropertySource(propertySource) : instantiatePropertySource(propertySource); } @SuppressWarnings("unchecked") - private static PropertySource proxyPropertySource(PropertySource propertySource, EncryptablePropertyResolver resolver, EncryptablePropertyFilter propertyFilter) { + private PropertySource proxyPropertySource(PropertySource propertySource) { //Silly Chris Beams for making CommandLinePropertySource getProperty and containsProperty methods final. Those methods //can't be proxied with CGLib because of it. So fallback to wrapper for Command Line Arguments only. if (CommandLinePropertySource.class.isAssignableFrom(propertySource.getClass()) // Other PropertySource classes like org.springframework.boot.env.OriginTrackedMapPropertySource // are final classes as well || Modifier.isFinal(propertySource.getClass().getModifiers())) { - return instantiatePropertySource(propertySource, resolver, propertyFilter); + return instantiatePropertySource(propertySource); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetClass(propertySource.getClass()); proxyFactory.setProxyTargetClass(true); proxyFactory.addInterface(EncryptablePropertySource.class); proxyFactory.setTarget(propertySource); - proxyFactory.addAdvice(new EncryptablePropertySourceMethodInterceptor<>(propertySource, resolver, propertyFilter)); + proxyFactory.addAdvice(new EncryptablePropertySourceMethodInterceptor<>(propertySource, propertyResolver, propertyFilter)); return (PropertySource) proxyFactory.getProxy(); } @SuppressWarnings({"unchecked", "rawtypes"}) - private static PropertySource instantiatePropertySource(PropertySource propertySource, EncryptablePropertyResolver resolver, EncryptablePropertyFilter propertyFilter) { + private PropertySource instantiatePropertySource(PropertySource propertySource) { PropertySource encryptablePropertySource; if (needsProxyAnyway(propertySource)) { - encryptablePropertySource = proxyPropertySource(propertySource, resolver, propertyFilter); + encryptablePropertySource = proxyPropertySource(propertySource); } else if (propertySource instanceof SystemEnvironmentPropertySource) { - encryptablePropertySource = (PropertySource) new EncryptableSystemEnvironmentPropertySourceWrapper((SystemEnvironmentPropertySource) propertySource, resolver, propertyFilter); + encryptablePropertySource = (PropertySource) new EncryptableSystemEnvironmentPropertySourceWrapper((SystemEnvironmentPropertySource) propertySource, propertyResolver, propertyFilter); } else if (propertySource instanceof MapPropertySource) { - encryptablePropertySource = (PropertySource) new EncryptableMapPropertySourceWrapper((MapPropertySource) propertySource, resolver, propertyFilter); + encryptablePropertySource = (PropertySource) new EncryptableMapPropertySourceWrapper((MapPropertySource) propertySource, propertyResolver, propertyFilter); } else if (propertySource instanceof EnumerablePropertySource) { - encryptablePropertySource = new EncryptableEnumerablePropertySourceWrapper<>((EnumerablePropertySource) propertySource, resolver, propertyFilter); + encryptablePropertySource = new EncryptableEnumerablePropertySourceWrapper<>((EnumerablePropertySource) propertySource, propertyResolver, propertyFilter); } else { - encryptablePropertySource = new EncryptablePropertySourceWrapper<>(propertySource, resolver, propertyFilter); + encryptablePropertySource = new EncryptablePropertySourceWrapper<>(propertySource, propertyResolver, propertyFilter); } return encryptablePropertySource; } diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/aop/EncryptableMutablePropertySourcesInterceptor.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/aop/EncryptableMutablePropertySourcesInterceptor.java index bbcb61e..c28eade 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/aop/EncryptableMutablePropertySourcesInterceptor.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/aop/EncryptableMutablePropertySourcesInterceptor.java @@ -16,22 +16,16 @@ */ public class EncryptableMutablePropertySourcesInterceptor implements MethodInterceptor { - private final InterceptionMode interceptionMode; - private final List>> skipPropertySourceClasses; - private final EncryptablePropertyResolver resolver; - private final EncryptablePropertyFilter filter; + private final EncryptablePropertySourceConverter propertyConverter; private final EnvCopy envCopy; - public EncryptableMutablePropertySourcesInterceptor(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter, EnvCopy envCopy) { - this.interceptionMode = interceptionMode; - this.skipPropertySourceClasses = skipPropertySourceClasses; - this.resolver = resolver; - this.filter = filter; + public EncryptableMutablePropertySourcesInterceptor(EncryptablePropertySourceConverter propertyConverter, EnvCopy envCopy) { + this.propertyConverter = propertyConverter; this.envCopy = envCopy; } private Object makeEncryptable(Object propertySource) { - return EncryptablePropertySourceConverter.makeEncryptable(interceptionMode, skipPropertySourceClasses, resolver, filter, (PropertySource) propertySource); + return propertyConverter.makeEncryptable((PropertySource) propertySource); } @Override diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/caching/RefreshScopeRefreshedEventListener.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/caching/RefreshScopeRefreshedEventListener.java index e8db2e6..e3e20e9 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/caching/RefreshScopeRefreshedEventListener.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/caching/RefreshScopeRefreshedEventListener.java @@ -1,16 +1,14 @@ package com.ulisesbocchio.jasyptspringboot.caching; import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource; +import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.PropertySources; +import org.springframework.core.env.*; import org.springframework.util.ClassUtils; @Order(Ordered.HIGHEST_PRECEDENCE) @@ -20,9 +18,11 @@ public class RefreshScopeRefreshedEventListener implements ApplicationListener propertySource) { if (propertySource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) propertySource; cps.getPropertySources().forEach(this::refreshPropertySource); - } else if (propertySource instanceof EncryptablePropertySource){ + } else if (propertySource instanceof EncryptablePropertySource) { EncryptablePropertySource eps = (EncryptablePropertySource) propertySource; eps.refresh(); } diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/CachingConfiguration.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/CachingConfiguration.java index b902d25..c6e0d88 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/CachingConfiguration.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/CachingConfiguration.java @@ -1,5 +1,6 @@ package com.ulisesbocchio.jasyptspringboot.configuration; +import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter; import com.ulisesbocchio.jasyptspringboot.caching.RefreshScopeRefreshedEventListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -8,7 +9,7 @@ @Configuration public class CachingConfiguration { @Bean - public RefreshScopeRefreshedEventListener refreshScopeRefreshedEventListener(ConfigurableEnvironment environment) { - return new RefreshScopeRefreshedEventListener(environment); + public RefreshScopeRefreshedEventListener refreshScopeRefreshedEventListener(ConfigurableEnvironment environment, EncryptablePropertySourceConverter converter) { + return new RefreshScopeRefreshedEventListener(environment, converter); } } diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesBeanFactoryPostProcessor.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesBeanFactoryPostProcessor.java index dfab720..d2b3950 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesBeanFactoryPostProcessor.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesBeanFactoryPostProcessor.java @@ -1,8 +1,7 @@ package com.ulisesbocchio.jasyptspringboot.configuration; -import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter; import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver; -import com.ulisesbocchio.jasyptspringboot.InterceptionMode; +import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; @@ -14,12 +13,6 @@ import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; -import java.util.List; - -import static com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter.convertPropertySources; -import static com.ulisesbocchio.jasyptspringboot.configuration.EncryptablePropertyResolverConfiguration.FILTER_BEAN_NAME; -import static com.ulisesbocchio.jasyptspringboot.configuration.EncryptablePropertyResolverConfiguration.RESOLVER_BEAN_NAME; - /** *

{@link BeanFactoryPostProcessor} that wraps all {@link PropertySource} defined in the {@link Environment} * with {@link com.ulisesbocchio.jasyptspringboot.wrapper.EncryptablePropertySourceWrapper} and defines a default {@link @@ -33,23 +26,19 @@ public class EnableEncryptablePropertiesBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { private static final Logger LOG = LoggerFactory.getLogger(EnableEncryptablePropertiesBeanFactoryPostProcessor.class); - private ConfigurableEnvironment environment; - private InterceptionMode interceptionMode; - private final List>> skipPropertySourceClasses; + private final ConfigurableEnvironment environment; + private final EncryptablePropertySourceConverter converter; - public EnableEncryptablePropertiesBeanFactoryPostProcessor(ConfigurableEnvironment environment, InterceptionMode interceptionMode, List>> skipPropertySourceClasses) { + public EnableEncryptablePropertiesBeanFactoryPostProcessor(ConfigurableEnvironment environment, EncryptablePropertySourceConverter converter) { this.environment = environment; - this.interceptionMode = interceptionMode; - this.skipPropertySourceClasses = skipPropertySourceClasses; + this.converter = converter; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { LOG.info("Post-processing PropertySource instances"); - EncryptablePropertyResolver propertyResolver = beanFactory.getBean(RESOLVER_BEAN_NAME, EncryptablePropertyResolver.class); - EncryptablePropertyFilter propertyFilter = beanFactory.getBean(FILTER_BEAN_NAME, EncryptablePropertyFilter.class); MutablePropertySources propSources = environment.getPropertySources(); - convertPropertySources(interceptionMode, skipPropertySourceClasses, propertyResolver, propertyFilter, propSources); + converter.convertPropertySources(propSources); } @Override diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesConfiguration.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesConfiguration.java index a02fa39..e490ad4 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesConfiguration.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesConfiguration.java @@ -1,6 +1,6 @@ package com.ulisesbocchio.jasyptspringboot.configuration; -import com.ulisesbocchio.jasyptspringboot.InterceptionMode; +import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter; import lombok.extern.slf4j.Slf4j; import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.pbe.config.StringPBEConfig; @@ -12,10 +12,6 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - /** *

Configuration class that registers a {@link BeanFactoryPostProcessor} that wraps all {@link PropertySource} defined in the {@link Environment} * with {@link com.ulisesbocchio.jasyptspringboot.wrapper.EncryptablePropertySourceWrapper} and defines a default {@link StringEncryptor} for decrypting properties @@ -62,26 +58,8 @@ @Slf4j public class EnableEncryptablePropertiesConfiguration { - @SuppressWarnings("unchecked") @Bean - public static EnableEncryptablePropertiesBeanFactoryPostProcessor enableEncryptablePropertySourcesPostProcessor(final ConfigurableEnvironment environment) { - final boolean proxyPropertySources = environment.getProperty("jasypt.encryptor.proxy-property-sources", Boolean.TYPE, false); - final List skipPropertySources = (List) environment.getProperty("jasypt.encryptor.skip-property-sources", List.class, Collections.EMPTY_LIST); - final List>> skipPropertySourceClasses = skipPropertySources.stream().map(EnableEncryptablePropertiesConfiguration::getPropertiesClass).collect(Collectors.toList()); - final InterceptionMode interceptionMode = proxyPropertySources ? InterceptionMode.PROXY : InterceptionMode.WRAPPER; - return new EnableEncryptablePropertiesBeanFactoryPostProcessor(environment, interceptionMode, skipPropertySourceClasses); - } - - @SuppressWarnings("unchecked") - private static Class> getPropertiesClass(String className) { - try { - Class clazz = Class.forName(className); - if (PropertySource.class.isAssignableFrom(clazz)) { - return (Class>) clazz; - } - throw new IllegalArgumentException(String.format("Invalid jasypt.encryptor.skip-property-sources: Class %s does not implement %s", className, PropertySource.class.getName())); - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(String.format("Invalid jasypt.encryptor.skip-property-sources: Class %s not found", className), e); - } + public static EnableEncryptablePropertiesBeanFactoryPostProcessor enableEncryptablePropertySourcesPostProcessor(final ConfigurableEnvironment environment, EncryptablePropertySourceConverter converter) { + return new EnableEncryptablePropertiesBeanFactoryPostProcessor(environment, converter); } } diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EncryptablePropertyResolverConfiguration.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EncryptablePropertyResolverConfiguration.java index 587234b..4e182a5 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EncryptablePropertyResolverConfiguration.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/EncryptablePropertyResolverConfiguration.java @@ -1,8 +1,6 @@ package com.ulisesbocchio.jasyptspringboot.configuration; -import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyDetector; -import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter; -import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver; +import com.ulisesbocchio.jasyptspringboot.*; import com.ulisesbocchio.jasyptspringboot.detector.DefaultLazyPropertyDetector; import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor; import com.ulisesbocchio.jasyptspringboot.filter.DefaultLazyPropertyFilter; @@ -16,6 +14,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; /** * @author Ulises Bocchio @@ -35,8 +38,31 @@ public class EncryptablePropertyResolverConfiguration { private static final String ENCRYPTOR_BEAN_NAME = "lazyJasyptStringEncryptor"; private static final String DETECTOR_BEAN_NAME = "lazyEncryptablePropertyDetector"; private static final String CONFIG_SINGLETON = "configPropsSingleton"; - static final String RESOLVER_BEAN_NAME = "lazyEncryptablePropertyResolver"; - static final String FILTER_BEAN_NAME = "lazyEncryptablePropertyFilter"; + public static final String RESOLVER_BEAN_NAME = "lazyEncryptablePropertyResolver"; + public static final String FILTER_BEAN_NAME = "lazyEncryptablePropertyFilter"; + + @SuppressWarnings("unchecked") + @Bean + public static EncryptablePropertySourceConverter encryptablePropertySourceConverter(ConfigurableEnvironment environment, @Qualifier(RESOLVER_BEAN_NAME) EncryptablePropertyResolver propertyResolver, @Qualifier(FILTER_BEAN_NAME) EncryptablePropertyFilter propertyFilter) { + final boolean proxyPropertySources = environment.getProperty("jasypt.encryptor.proxy-property-sources", Boolean.TYPE, false); + final List skipPropertySources = (List) environment.getProperty("jasypt.encryptor.skip-property-sources", List.class, Collections.EMPTY_LIST); + final List>> skipPropertySourceClasses = skipPropertySources.stream().map(EncryptablePropertyResolverConfiguration::getPropertiesClass).collect(Collectors.toList()); + final InterceptionMode interceptionMode = proxyPropertySources ? InterceptionMode.PROXY : InterceptionMode.WRAPPER; + return new EncryptablePropertySourceConverter(interceptionMode, skipPropertySourceClasses, propertyResolver, propertyFilter); + } + + @SuppressWarnings("unchecked") + private static Class> getPropertiesClass(String className) { + try { + Class clazz = Class.forName(className); + if (PropertySource.class.isAssignableFrom(clazz)) { + return (Class>) clazz; + } + throw new IllegalArgumentException(String.format("Invalid jasypt.encryptor.skip-property-sources: Class %s does not implement %s", className, PropertySource.class.getName())); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(String.format("Invalid jasypt.encryptor.skip-property-sources: Class %s not found", className), e); + } + } @Bean public EnvCopy envCopy(final ConfigurableEnvironment environment) { diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/StringEncryptorBuilder.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/StringEncryptorBuilder.java index 5cb2ef5..d128c3b 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/StringEncryptorBuilder.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/configuration/StringEncryptorBuilder.java @@ -45,7 +45,7 @@ private StringEncryptor createAsymmetricDefault() { config.setPrivateKey(get(configProps::getPrivateKeyString, propertyPrefix + ".private-key-string", null)); config.setPrivateKeyLocation(get(configProps::getPrivateKeyLocation, propertyPrefix + ".private-key-location", null)); config.setPrivateKeyFormat(get(configProps::getPrivateKeyFormat, propertyPrefix + ".private-key-format", AsymmetricCryptography.KeyFormat.DER)); - config.setPrivateKey(get(configProps::getPublicKeyString, propertyPrefix + ".public-key-string", null)); + config.setPublicKey(get(configProps::getPublicKeyString, propertyPrefix + ".public-key-string", null)); config.setPublicKeyLocation(get(configProps::getPublicKeyLocation, propertyPrefix + ".public-key-location", null)); config.setPublicKeyFormat(get(configProps::getPublicKeyFormat, propertyPrefix + ".public-key-format", AsymmetricCryptography.KeyFormat.DER)); return new SimpleAsymmetricStringEncryptor(config); diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/EnvironmentInitializer.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/EnvironmentInitializer.java new file mode 100644 index 0000000..4c869f5 --- /dev/null +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/EnvironmentInitializer.java @@ -0,0 +1,50 @@ +package com.ulisesbocchio.jasyptspringboot.environment; + +import com.ulisesbocchio.jasyptspringboot.*; +import com.ulisesbocchio.jasyptspringboot.configuration.EnvCopy; +import com.ulisesbocchio.jasyptspringboot.detector.DefaultLazyPropertyDetector; +import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor; +import com.ulisesbocchio.jasyptspringboot.filter.DefaultLazyPropertyFilter; +import com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver; +import org.jasypt.encryption.StringEncryptor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class EnvironmentInitializer { + private final ConfigurableEnvironment environment; + private final InterceptionMode interceptionMode; + private final List>> skipPropertySourceClasses; + private final EncryptablePropertyResolver resolver; + private final EncryptablePropertyFilter filter; + private final StringEncryptor encryptor; + private final EncryptablePropertyDetector detector; + + public EnvironmentInitializer(ConfigurableEnvironment environment, InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter, StringEncryptor encryptor, EncryptablePropertyDetector detector) { + + this.environment = environment; + this.interceptionMode = interceptionMode; + this.skipPropertySourceClasses = skipPropertySourceClasses; + this.resolver = resolver; + this.filter = filter; + this.encryptor = encryptor; + this.detector = detector; + } + + MutablePropertySources initialize(MutablePropertySources originalPropertySources) { + InterceptionMode actualInterceptionMode = Optional.ofNullable(interceptionMode).orElse(InterceptionMode.WRAPPER); + List>> actualSkipPropertySourceClasses = Optional.ofNullable(skipPropertySourceClasses).orElseGet(Collections::emptyList); + EnvCopy envCopy = new EnvCopy(environment); + EncryptablePropertyFilter actualFilter = Optional.ofNullable(filter).orElseGet(() -> new DefaultLazyPropertyFilter(envCopy.get())); + StringEncryptor actualEncryptor = Optional.ofNullable(encryptor).orElseGet(() -> new DefaultLazyEncryptor(envCopy.get())); + EncryptablePropertyDetector actualDetector = Optional.ofNullable(detector).orElseGet(() -> new DefaultLazyPropertyDetector(envCopy.get())); + EncryptablePropertyResolver actualResolver = Optional.ofNullable(resolver).orElseGet(() -> new DefaultLazyPropertyResolver(actualDetector, actualEncryptor, environment)); + EncryptablePropertySourceConverter converter = new EncryptablePropertySourceConverter(actualInterceptionMode, actualSkipPropertySourceClasses, actualResolver, actualFilter); + converter.convertPropertySources(originalPropertySources); + return converter.proxyPropertySources(originalPropertySources, envCopy); + } +} \ No newline at end of file diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableEnvironment.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableEnvironment.java index 974ee0c..a935e46 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableEnvironment.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableEnvironment.java @@ -4,7 +4,6 @@ import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter; import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver; import com.ulisesbocchio.jasyptspringboot.InterceptionMode; -import com.ulisesbocchio.jasyptspringboot.configuration.EnvCopy; import com.ulisesbocchio.jasyptspringboot.detector.DefaultLazyPropertyDetector; import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor; import com.ulisesbocchio.jasyptspringboot.filter.DefaultLazyPropertyFilter; @@ -16,12 +15,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; -import java.util.Collections; import java.util.List; -import java.util.Optional; - -import static com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter.convertPropertySources; -import static com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter.proxyPropertySources; /** * A custom {@link ConfigurableEnvironment} that is useful for @@ -52,15 +46,8 @@ public StandardEncryptableEnvironment() { */ @Builder public StandardEncryptableEnvironment(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter, StringEncryptor encryptor, EncryptablePropertyDetector detector) { - InterceptionMode actualInterceptionMode = Optional.ofNullable(interceptionMode).orElse(InterceptionMode.WRAPPER); - List>> actualSkipPropertySourceClasses = Optional.ofNullable(skipPropertySourceClasses).orElseGet(Collections::emptyList); - EnvCopy envCopy = new EnvCopy(this); - EncryptablePropertyFilter actualFilter = Optional.ofNullable(filter).orElseGet(() -> new DefaultLazyPropertyFilter(envCopy.get())); - StringEncryptor actualEncryptor = Optional.ofNullable(encryptor).orElseGet(() -> new DefaultLazyEncryptor(envCopy.get())); - EncryptablePropertyDetector actualDetector = Optional.ofNullable(detector).orElseGet(() -> new DefaultLazyPropertyDetector(envCopy.get())); - EncryptablePropertyResolver actualResolver = Optional.ofNullable(resolver).orElseGet(() -> new DefaultLazyPropertyResolver(actualDetector, actualEncryptor, this)); - convertPropertySources(actualInterceptionMode, actualSkipPropertySourceClasses, actualResolver, actualFilter, originalPropertySources); - this.encryptablePropertySources = proxyPropertySources(actualInterceptionMode, actualSkipPropertySourceClasses, actualResolver, actualFilter, originalPropertySources, envCopy); + EnvironmentInitializer initializer = new EnvironmentInitializer(this, interceptionMode, skipPropertySourceClasses, resolver, filter, encryptor, detector); + this.encryptablePropertySources = initializer.initialize(originalPropertySources); } @Override diff --git a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableServletEnvironment.java b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableServletEnvironment.java index 49c8290..0a5fe8b 100644 --- a/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableServletEnvironment.java +++ b/jasypt-spring-boot/src/main/java/com/ulisesbocchio/jasyptspringboot/environment/StandardEncryptableServletEnvironment.java @@ -4,7 +4,6 @@ import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter; import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver; import com.ulisesbocchio.jasyptspringboot.InterceptionMode; -import com.ulisesbocchio.jasyptspringboot.configuration.EnvCopy; import com.ulisesbocchio.jasyptspringboot.detector.DefaultLazyPropertyDetector; import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor; import com.ulisesbocchio.jasyptspringboot.filter.DefaultLazyPropertyFilter; @@ -16,12 +15,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.web.context.support.StandardServletEnvironment; -import java.util.Collections; import java.util.List; -import java.util.Optional; - -import static com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter.convertPropertySources; -import static com.ulisesbocchio.jasyptspringboot.EncryptablePropertySourceConverter.proxyPropertySources; /** * A custom {@link ConfigurableEnvironment} that is useful for @@ -52,15 +46,8 @@ public StandardEncryptableServletEnvironment() { */ @Builder public StandardEncryptableServletEnvironment(InterceptionMode interceptionMode, List>> skipPropertySourceClasses, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter, StringEncryptor encryptor, EncryptablePropertyDetector detector) { - InterceptionMode actualInterceptionMode = Optional.ofNullable(interceptionMode).orElse(InterceptionMode.WRAPPER); - List>> actualSkipPropertySourceClasses = Optional.ofNullable(skipPropertySourceClasses).orElseGet(Collections::emptyList); - EnvCopy envCopy = new EnvCopy(this); - EncryptablePropertyFilter actualFilter = Optional.ofNullable(filter).orElseGet(() -> new DefaultLazyPropertyFilter(envCopy.get())); - StringEncryptor actualEncryptor = Optional.ofNullable(encryptor).orElseGet(() -> new DefaultLazyEncryptor(envCopy.get())); - EncryptablePropertyDetector actualDetector = Optional.ofNullable(detector).orElseGet(() -> new DefaultLazyPropertyDetector(envCopy.get())); - EncryptablePropertyResolver actualResolver = Optional.ofNullable(resolver).orElseGet(() -> new DefaultLazyPropertyResolver(actualDetector, actualEncryptor, this)); - convertPropertySources(actualInterceptionMode, actualSkipPropertySourceClasses, actualResolver, actualFilter, originalPropertySources); - this.encryptablePropertySources = proxyPropertySources(actualInterceptionMode, actualSkipPropertySourceClasses, actualResolver, actualFilter, originalPropertySources, envCopy); + EnvironmentInitializer initializer = new EnvironmentInitializer(this, interceptionMode, skipPropertySourceClasses, resolver, filter, encryptor, detector); + this.encryptablePropertySources = initializer.initialize(originalPropertySources); } @Override