Skip to content

Commit

Permalink
JASYPT_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgleissner committed May 27, 2020
1 parent 19dfac2 commit 34a27b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ override its methods, and specify the fully qualified name of your subclass in a
| Environment variable | System property name | Default value | Description |
|----------------------|-----------------------|----------------|--------------|
| `JASYPT_PASSWORD` | `jasypt.password` | none | Password used for encrypting property values |
| `JASYPT_KEY` | `jasypt.key` | none | Synonym for `JASYPT_PASSWORD` |
| `JASYPT_ALGORITHM` | `jasypt.algorithm` | `PBEWithHMACSHA512AndAES_256` | [Encryption algorithm](http://www.jasypt.org/cli.html#Listing_algorithms) |
| `JASYPT_ITERATIONS` | `jasypt.iterations` | 1000 | Jasypt key obtention iterations |
| `JASYPT_PROPERTIES` | `jasypt.properties` | `classpath:application.properties,config/application.properties` | Comma-separated property filenames, see below. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Supplier;
import java.util.regex.Pattern;

import static java.lang.Integer.parseInt;
Expand All @@ -30,6 +31,7 @@
@Slf4j
public class JasyptConfigSource implements ConfigSource {
public static final String JASYPT_PASSWORD = "jasypt.password";
public static final String JASYPT_KEY = "jasypt.key";
public static final String JASYPT_ALGORITHM = "jasypt.algorithm";
public static final String JASYPT_ITERATIONS = "jasypt.iterations";
public static final String JASYPT_PROPERTIES = "jasypt.properties";
Expand Down Expand Up @@ -62,10 +64,14 @@ public int getOrdinal() {
return 275;
}

protected String property(String propertyName, String defaultValue) {
protected String property(String propertyName, Supplier<String> defaultValue) {
String envVarName = envVarName(propertyName);
return Optional.ofNullable(System.getenv(envVarName))
.orElseGet(() -> Optional.ofNullable(System.getProperty(propertyName)).orElse(defaultValue));
.orElseGet(() -> Optional.ofNullable(System.getProperty(propertyName)).orElse(defaultValue.get()));
}

protected String property(String propertyName, String defaultValue) {
return property(propertyName, () -> defaultValue);
}

protected String envVarName(String propertyName) {
Expand All @@ -78,7 +84,7 @@ protected StringEncryptor getEncryptor() {

protected StringEncryptor createStringEncryptor() {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(property(JASYPT_PASSWORD, getDefaultPassword()));
encryptor.setPassword(property(JASYPT_PASSWORD, () -> property(JASYPT_KEY, getDefaultPassword())));
encryptor.setAlgorithm(property(JASYPT_ALGORITHM, getDefaultAlgorithm()));
encryptor.setKeyObtentionIterations(parseInt(property(JASYPT_ITERATIONS, Integer.toString(getDefaultIterations()))));
encryptor.setIvGenerator(new RandomIvGenerator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static com.github.chrisgleissner.config.microprofile.jasypt.JasyptConfigSource.JASYPT_KEY;
import static com.github.chrisgleissner.config.microprofile.jasypt.JasyptConfigSource.JASYPT_PASSWORD;
import static com.github.chrisgleissner.config.microprofile.jasypt.JasyptConfigSource.JASYPT_PROPERTIES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class JasyptConfigSourceTest {
private static final String PWD = "pwd";

private JasyptConfigSource createJasyptConfigSource() {
System.setProperty(JASYPT_PASSWORD, "pwd");
System.setProperty(JASYPT_PASSWORD, PWD);
System.setProperty(JASYPT_PROPERTIES, "src/test/resources/application.properties");
return new JasyptConfigSource();
}
Expand All @@ -35,6 +37,19 @@ void getValue() {
assertThat(jcs.getValue("b")).isEqualTo("2");
}

@Test
void jasyptKeyProperty() {
System.clearProperty(JASYPT_PASSWORD);
System.setProperty(JASYPT_KEY, PWD);
try {
JasyptConfigSource jcs = createJasyptConfigSource();
assertThat(jcs.getValue("a")).isEqualTo("1");
assertThat(jcs.getValue("b")).isEqualTo("2");
} finally {
System.clearProperty(JASYPT_KEY);
}
}

@Test
void getProperties() {
JasyptConfigSource jcs = createJasyptConfigSource();
Expand Down

0 comments on commit 34a27b9

Please sign in to comment.