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)
${NAME:default}
are resolved at the Environment
level
@ulisesbocchio
hey, sorry for kinda late reply. I tried to use EncryptableEnvironment, and it seemed like the original case worked, but now there's another problem:
user: user
password: ${PASSWORD:ENC(asdf)}
endpoint: https://${user}:${password}@localhost:30000
Trying to resolve turns into:
user: user
password: ENC(asdf)
endpoint: https://user:ENC(asdf)@localhost:30000
This makes sense since resolve happens before decrypt. But seems like this is a bug because the EncryptableEnvironment, when it follows the ${...} resolution, it should also be trying to decrypt if it can. Thoughts?
propVal = placeholderResolver.resolvePlaceholder(propVal);
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;
}
}
}