Skip to content

Commit

Permalink
Fix decryption of values including replacement syntax (#424)
Browse files Browse the repository at this point in the history
Fix decryption of values including replacement syntax
  • Loading branch information
carterkozak authored Nov 2, 2022
1 parent dd1019c commit 17f4582
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-424.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix decryption of values including replacement syntax
links:
- https://github.com/palantir/encrypted-config-value/pull/424
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.palantir.config.crypto.jackson.Substitutor;
import com.palantir.config.crypto.util.StringSubstitutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class DecryptingVariableSubstitutor implements Substitutor {
Expand All @@ -32,7 +33,8 @@ public String replace(String source) {
return PATTERN.matcher(source).replaceAll(matchResult -> {
String encryptedValue = matchResult.group(1);
try {
return KeyFileUtils.decryptUsingDefaultKeys(EncryptedValue.fromString(encryptedValue));
return Matcher.quoteReplacement(
KeyFileUtils.decryptUsingDefaultKeys(EncryptedValue.fromString(encryptedValue)));
} catch (RuntimeException e) {
throw new StringSubstitutionException(e, encryptedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public final void variableIsDecrypted() throws Exception {
assertThat(substitutor.replace("${" + encrypt("abc") + "}")).isEqualTo("abc");
}

@Test
public final void variableIsDecryptedWithRegex() throws Exception {
assertThat(substitutor.replace("${" + encrypt("$5") + "}")).isEqualTo("$5");
}

private String encrypt(String value) {
return ALGORITHM.newEncrypter().encrypt(KEY_PAIR.encryptionKey(), value).toString();
}
Expand Down

0 comments on commit 17f4582

Please sign in to comment.