Skip to content

Commit

Permalink
Use standard java regex rather than commons-text string-substitutor (#…
Browse files Browse the repository at this point in the history
…416)

Use standard java regex rather than commons-text string-substitutor
  • Loading branch information
carterkozak authored Oct 25, 2022
1 parent d0a9749 commit a2ca1eb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-416.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: break
break:
description: Use standard java regex rather than commons-text string-substitutor.
Note that `DecryptingVariableSubstitutor` no longer extends commons-lang `StringSubstitutor`.
links:
- https://github.com/palantir/encrypted-config-value/pull/416
1 change: 0 additions & 1 deletion encrypted-config-value-module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies {
api 'com.google.guava:guava'
api 'com.fasterxml.jackson.core:jackson-databind'
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
api 'org.apache.commons:commons-text'

implementation 'com.fasterxml.jackson.core:jackson-core'
implementation 'com.palantir.safe-logging:preconditions'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@

import com.palantir.config.crypto.jackson.Substitutor;
import com.palantir.config.crypto.util.StringSubstitutionException;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookupFactory;
import java.util.regex.Pattern;

public final class DecryptingVariableSubstitutor extends StringSubstitutor implements Substitutor {
public final class DecryptingVariableSubstitutor implements Substitutor {

public DecryptingVariableSubstitutor() {
super(StringLookupFactory.INSTANCE.functionStringLookup(encryptedValue -> {
if (!EncryptedValue.isEncryptedValue(encryptedValue)) {
return null;
}
private static final Pattern PATTERN = Pattern.compile("\\$\\{(enc:.*?)}");

try {
return KeyFileUtils.decryptUsingDefaultKeys(EncryptedValue.fromString(encryptedValue));
} catch (RuntimeException e) {
throw new StringSubstitutionException(e, encryptedValue);
}
}));
public DecryptingVariableSubstitutor() {}

@Override
public String replace(String source) {
if (source != null && source.contains("${")) {
return PATTERN.matcher(source).replaceAll(matchResult -> {
String encryptedValue = matchResult.group(1);
try {
return KeyFileUtils.decryptUsingDefaultKeys(EncryptedValue.fromString(encryptedValue));
} catch (RuntimeException e) {
throw new StringSubstitutionException(e, encryptedValue);
}
});
}
return source;
}
}
4 changes: 2 additions & 2 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ javax.ws.rs:javax.ws.rs-api:2.0.1 (8 constraints: ed9109e8)
javax.xml.bind:jaxb-api:2.3.1 (1 constraints: 290e1140)
joda-time:joda-time:2.10.9 (2 constraints: b8238780)
net.sourceforge.argparse4j:argparse4j:0.8.1 (1 constraints: 430d3a1f)
org.apache.commons:commons-lang3:3.12.0 (3 constraints: d02c96a3)
org.apache.commons:commons-text:1.10.0 (2 constraints: e8155ec0)
org.apache.commons:commons-lang3:3.11 (3 constraints: 712c254a)
org.apache.commons:commons-text:1.9 (1 constraints: b5102f9b)
org.checkerframework:checker-qual:3.12.0 (1 constraints: 480a3bbf)
org.eclipse.jetty:jetty-continuation:9.4.35.v20201120 (2 constraints: 6421a041)
org.eclipse.jetty:jetty-http:9.4.35.v20201120 (3 constraints: f130624d)
Expand Down
1 change: 0 additions & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ com.palantir.safe-logging:* = 3.1.0
io.dropwizard:* = 1.0.0
io.dropwizard:dropwizard-validation = 1.3.29
javax.ws.rs:javax.ws.rs-api = 2.0.1
org.apache.commons:commons-text = 1.10.0
org.assertj:assertj-core = 3.23.1
org.immutables:* = 2.8.8
org.junit.jupiter:* = 5.9.1
Expand Down

0 comments on commit a2ca1eb

Please sign in to comment.