ulisesbocchio on master
Fix Version check in upgrade mo… Fix typo Merge pull request #233 from tk… (compare)
ulisesbocchio on master
Rename jasypt-spring-boot-start… Merge pull request #244 from fe… (compare)
ulisesbocchio on master
Fix Copy&Paste Error und docume… Merge pull request #249 from je… (compare)
for now you should probably stick to:
user: user
defaultPassword: ENC(asdf)
password: ${PASSWORD:${defaultPassword}}
endpoint: https://${user}:${password}@localhost:30000
which adds one more level of indirection but the works because defaultPassword
is a top level property
-Djasypt.encryptor.password=password
should be enough in any JVM. Keep in mind though that spring boot has to run.
@Bean
public PropertySource allPropertiesSource() throws Exception {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:*.properties");
PropertySourceLoader loader = new PropertiesPropertySourceLoader();
Function<Resource, PropertySource> toSource = resource -> {
try {
return loader.load(resource.getFilename(), resource).get(0);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
Function<List<PropertySource>, PropertySource> collector = sources -> {
CompositePropertySource ps = new CompositePropertySource("all properties");
sources.forEach(ps::addPropertySource);
return ps;
};
return Arrays.stream(resources).map(toSource).collect(Collectors.collectingAndThen(Collectors.toList(), collector));
}
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyDetector;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySource;
import com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySources;
/**
@Configuration
@EncryptablePropertySources({ @EncryptablePropertySource("classpath:platform-encrypted.properties") })
public class PlatformEncryptionConfiguration {
@Bean(name = "encryptablePropertyDetector")
public EncryptablePropertyDetector encryptablePropertyDetector() {
return new MyEncryptablePropertyDetector();
}
@Bean(name="encryptablePropertyResolver")
EncryptablePropertyResolver encryptablePropertyResolver() {
return new MyEncryptablePropertyResolver();
}
private class MyEncryptablePropertyDetector implements EncryptablePropertyDetector {
@Override
public boolean isEncrypted(String value) {
if (value != null) {
return value.startsWith("3DES@");
}
return false;
}
@Override
public String unwrapEncryptedValue(String value) {
return value.substring("3DES@".length());
}
}
private class MyEncryptablePropertyResolver implements EncryptablePropertyResolver {
private final PooledPBEStringEncryptor encryptor;
public MyEncryptablePropertyResolver() {
this.encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPasswordCharArray("password".toCharArray());
config.setAlgorithm("3DES");
config.setKeyObtentionIterations("1000");
config.setPoolSize(1);
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
}
@Override
public String resolvePropertyValue(String value) {
if (value != null && value.startsWith("{cipher}")) {
return encryptor.decrypt(value.substring("{cipher}".length()));
}
return value;
}
}
}
in spring.factories -> org.springframework.cloud.bootstrap.BootstrapConfiguration=\
<packg>.PlatformEncryptionConfiguration.class
Hey, guys may I ask for help?
I added com.github.ulisesbocchio:jasypt-spring-boot-starter:1.17
to Spring Boot app. I encrypted MySQL password with test
, and added this JVM arg -Djasypt.encryptor.password=test
.
I start my Spring Boot app, see following logs:
2018-10-12 12:30:05 INFO EnableEncryptablePropertiesBeanFactoryPostProcessor:48 - Post-processing PropertySource instances
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource bootstrap [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource servletConfigInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource servletContextInitParams [org.springframework.core.env.PropertySource$StubPropertySource] to EncryptablePropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource jndiProperties [org.springframework.jndi.JndiPropertySource] to EncryptablePropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource systemProperties [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource systemEnvironment [org.springframework.core.env.SystemEnvironmentPropertySource] to EncryptableMapPropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource random [org.springframework.boot.context.config.RandomValuePropertySource] to EncryptablePropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource springCloudClientHostInfo [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2018-10-12 12:30:05 INFO EncryptablePropertySourceConverter:38 - Converting PropertySource defaultProperties [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
But when I try to resolve my MySQL pwd:
@Value("${spring.datasource.password}")
private String password;
It never decrypts it, it is resolved as ENC(...)
instead of decrypted password.
I'm very close, and I feel like I'm missing something