Skip to content

Commit

Permalink
Use Apache commons-text StringSubstitutor (#335)
Browse files Browse the repository at this point in the history
Use Apache commons-text StringSubstitutor
  • Loading branch information
schlosna authored Mar 3, 2022
1 parent b0cbdaa commit 3dc7f2b
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 49 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ allprojects {
}

subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'org.inferred.processors'
apply plugin: 'maven-publish'
apply plugin: 'com.palantir.baseline-class-uniqueness'
apply plugin: 'com.palantir.baseline-exact-dependencies'

repositories {
mavenCentral()
}


tasks.check.dependsOn(checkImplicitDependencies)
tasks.check.dependsOn(checkUnusedDependencies)
tasks.check.dependsOn(javadoc)
}

Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-335.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: break
break:
description: Use Apache commons-text StringSubstitutor
links:
- https://github.com/palantir/encrypted-config-value/pull/335
11 changes: 11 additions & 0 deletions encrypted-config-value-bundle-dropwizard1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ dependencies {
api 'com.fasterxml.jackson.core:jackson-databind'
api 'io.dropwizard:dropwizard-core'

implementation 'io.dropwizard:dropwizard-configuration'
implementation 'javax.validation:validation-api'
implementation 'net.sourceforge.argparse4j:argparse4j'
implementation project(':encrypted-config-value')

testImplementation 'com.fasterxml.jackson.core:jackson-annotations'
testImplementation 'com.google.code.findbugs:jsr305'
testImplementation 'com.google.errorprone:error_prone_annotations'
testImplementation 'io.dropwizard:dropwizard-jackson'
testImplementation 'io.dropwizard:dropwizard-jersey'
testImplementation 'io.dropwizard:dropwizard-testing'
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.hamcrest:hamcrest-all'
testImplementation 'org.mockito:mockito-core'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.ImmutableMap;
import com.palantir.config.crypto.util.StringSubstitutionException;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -47,21 +46,21 @@
public final class JsonNodeStringReplacerTest {

@Mock
private StrSubstitutor strSubstitutor;
private Substitutor substitutor;
@Mock
private Throwable cause;

private JsonNodeStringReplacer jsonNodeStringReplacer;

@Before
public void before() {
jsonNodeStringReplacer = new JsonNodeStringReplacer(strSubstitutor);
jsonNodeStringReplacer = new JsonNodeStringReplacer(substitutor);
}

@Test
public void visitTextDelegatesToStrSubstitutor() {
TextNode textNode = new TextNode("abc");
when(strSubstitutor.replace("abc")).thenReturn("def");
when(substitutor.replace("abc")).thenReturn("def");
assertThat(jsonNodeStringReplacer.visitText(textNode)).isEqualTo(new TextNode("def"));
}

Expand All @@ -71,7 +70,7 @@ public void visitArrayDispatchesToChildren() {
arrayNode.add("abc");
arrayNode.add(1);

when(strSubstitutor.replace("abc")).thenReturn("def");
when(substitutor.replace("abc")).thenReturn("def");

ArrayNode expected = new ArrayNode(JsonNodeFactory.instance);
expected.add("def");
Expand All @@ -90,7 +89,7 @@ public void visitArrayExtendsStringSubstitutionException() {
ImmutableMap.<String, JsonNode>of("key", new TextNode("abc"))));
arrayNode.add(4);

doThrow(new StringSubstitutionException(cause, "abc")).when(strSubstitutor).replace("abc");
doThrow(new StringSubstitutionException(cause, "abc")).when(substitutor).replace("abc");

try {
jsonNodeStringReplacer.visitArray(arrayNode);
Expand All @@ -109,7 +108,7 @@ public void visitObjectDispatchesToChildren() {
"key1", new TextNode("abc"),
"key2", new IntNode(1)));

when(strSubstitutor.replace("abc")).thenReturn("def");
when(substitutor.replace("abc")).thenReturn("def");

ObjectNode expected = new ObjectNode(
JsonNodeFactory.instance,
Expand All @@ -136,7 +135,7 @@ public void visitObjectExtendsStringSubstitutionException() {
"key1", new IntNode(1),
"key2", arrayNode));

doThrow(new StringSubstitutionException(cause, "abc")).when(strSubstitutor).replace("abc");
doThrow(new StringSubstitutionException(cause, "abc")).when(substitutor).replace("abc");

try {
jsonNodeStringReplacer.visitObject(objectNode);
Expand Down
12 changes: 9 additions & 3 deletions encrypted-config-value-module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ 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-lang3'
api 'org.apache.commons:commons-text'

testImplementation 'org.hamcrest:hamcrest-all'
implementation 'com.fasterxml.jackson.core:jackson-core'
implementation 'com.palantir.safe-logging:preconditions'
implementation 'com.palantir.safe-logging:safe-logging'

testImplementation 'com.fasterxml.jackson.core:jackson-annotations'
testImplementation 'com.google.code.findbugs:jsr305'
testImplementation 'com.google.errorprone:error_prone_annotations'
testImplementation 'junit:junit'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.hamcrest:hamcrest-all'
}

tasks.check.dependsOn(javadoc)
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package com.palantir.config.crypto;

import com.palantir.config.crypto.jackson.Substitutor;
import com.palantir.config.crypto.util.StringSubstitutionException;
import org.apache.commons.lang3.text.StrLookup;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookupFactory;

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

public DecryptingVariableSubstitutor() {
super(new DecryptingStringLookup());
}

private static final class DecryptingStringLookup extends StrLookup<String> {
@Override
public String lookup(String encryptedValue) {
super(StringLookupFactory.INSTANCE.functionStringLookup(encryptedValue -> {
if (!EncryptedValue.isEncryptedValue(encryptedValue)) {
return null;
}
Expand All @@ -38,6 +34,6 @@ public String lookup(String encryptedValue) {
} catch (RuntimeException e) {
throw new StringSubstitutionException(e, encryptedValue);
}
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@

package com.palantir.config.crypto.jackson;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.palantir.config.crypto.DecryptingVariableSubstitutor;
import java.io.File;
import java.io.IOException;

public final class EncryptedConfigMapperUtils {
private static final JsonNodeStringReplacer JSON_NODE_STRING_REPLACER =
private static final JsonNodeVisitor<JsonNode> JSON_NODE_STRING_REPLACER =
new JsonNodeStringReplacer(new DecryptingVariableSubstitutor());

private EncryptedConfigMapperUtils() {}

public static <T> T getConfig(File configFile, Class<T> clazz, ObjectMapper mapper)
throws JsonParseException, JsonMappingException, IOException {
JsonNode configNode = mapper.readValue(configFile, JsonNode.class);
JsonNode substitutedNode = JsonNodeVisitors.dispatch(configNode, JSON_NODE_STRING_REPLACER);
return mapper.treeToValue(substitutedNode, clazz);
public static <T> T getConfig(File configFile, Class<T> clazz, ObjectMapper mapper) throws IOException {
return substitute(clazz, mapper, mapper.readValue(configFile, JsonNode.class));
}

public static <T> T getConfig(String configFileContent, Class<T> clazz, ObjectMapper mapper) throws IOException {
return substitute(clazz, mapper, mapper.readValue(configFileContent, JsonNode.class));
}

public static <T> T getConfig(String configFileContent, Class<T> clazz, ObjectMapper mapper)
throws JsonParseException, JsonMappingException, IOException {
JsonNode configNode = mapper.readTree(configFileContent);
private static <T> T substitute(Class<T> clazz, ObjectMapper mapper, JsonNode configNode)
throws JsonProcessingException {
JsonNode substitutedNode = JsonNodeVisitors.dispatch(configNode, JSON_NODE_STRING_REPLACER);
return mapper.treeToValue(substitutedNode, clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
import com.palantir.config.crypto.util.StringSubstitutionException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang3.text.StrSubstitutor;

public final class JsonNodeStringReplacer implements JsonNodeVisitor<JsonNode> {

private final StrSubstitutor substitutor;
private final Substitutor substitutor;

public JsonNodeStringReplacer(StrSubstitutor substitutor) {
public JsonNodeStringReplacer(Substitutor substitutor) {
this.substitutor = substitutor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.POJONode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;

public final class JsonNodeVisitors {
private JsonNodeVisitors() {
Expand Down Expand Up @@ -51,7 +53,9 @@ public static <T> T dispatch(JsonNode node, JsonNodeVisitor<T> visitor) {
case STRING:
return visitor.visitText((TextNode) node);
default:
throw new IllegalArgumentException("Unexpected node type " + node.getNodeType());
throw new SafeIllegalArgumentException(
"Unexpected node type",
SafeArg.of("nodeType", node.getNodeType()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.config.crypto.jackson;

@FunctionalInterface
public interface Substitutor {
String replace(String source);
}
4 changes: 4 additions & 0 deletions encrypted-config-value/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ dependencies {
api 'com.google.guava:guava'
api 'com.fasterxml.jackson.core:jackson-databind'

implementation 'com.fasterxml.jackson.core:jackson-annotations'
implementation 'com.fasterxml.jackson.core:jackson-core'
implementation 'com.google.code.findbugs:jsr305'

testImplementation 'org.hamcrest:hamcrest-all'
testImplementation 'junit:junit'
testImplementation 'org.mockito:mockito-core'
Expand Down
22 changes: 12 additions & 10 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ ch.qos.logback:logback-access:1.2.3 (1 constraints: b41148e2)
ch.qos.logback:logback-classic:1.2.3 (2 constraints: 8c1d4e13)
ch.qos.logback:logback-core:1.2.3 (3 constraints: 1a28577f)
com.fasterxml:classmate:1.3.1 (1 constraints: a40e4f58)
com.fasterxml.jackson.core:jackson-annotations:2.9.10 (6 constraints: d074c7b5)
com.fasterxml.jackson.core:jackson-core:2.9.10 (11 constraints: e9ef8948)
com.fasterxml.jackson.core:jackson-annotations:2.9.10 (7 constraints: da791c6b)
com.fasterxml.jackson.core:jackson-core:2.9.10 (12 constraints: f3f494b9)
com.fasterxml.jackson.core:jackson-databind:2.9.10.8 (10 constraints: fdcaac30)
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.10 (2 constraints: 4f1667f6)
com.fasterxml.jackson.datatype:jackson-datatype-guava:2.9.10 (1 constraints: b60e755e)
Expand All @@ -16,17 +16,19 @@ com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.10 (1 constraints: 5
com.fasterxml.jackson.module:jackson-module-afterburner:2.9.10 (1 constraints: b60e755e)
com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.9.10 (1 constraints: 7b17793d)
com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.10 (1 constraints: b60e755e)
com.google.code.findbugs:jsr305:3.0.2 (3 constraints: 701c32e5)
com.google.errorprone:error_prone_annotations:2.11.0 (2 constraints: 520fd196)
com.google.code.findbugs:jsr305:3.0.2 (5 constraints: a23deaf4)
com.google.errorprone:error_prone_annotations:2.11.0 (4 constraints: 8e3086cd)
com.google.guava:failureaccess:1.0.1 (1 constraints: 140ae1b4)
com.google.guava:guava:31.1-jre (5 constraints: 5d4bdfa8)
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava (1 constraints: bd17c918)
com.google.j2objc:j2objc-annotations:1.3 (1 constraints: b809eda0)
com.helger:profiler:1.1.1 (1 constraints: e21053b8)
io.dropwizard:dropwizard-configuration:1.3.29 (1 constraints: 790daa2c)
com.palantir.safe-logging:preconditions:1.21.0 (1 constraints: 3605303b)
com.palantir.safe-logging:safe-logging:1.21.0 (2 constraints: 6516c50b)
io.dropwizard:dropwizard-configuration:1.3.29 (2 constraints: 7b12f725)
io.dropwizard:dropwizard-core:1.3.29 (2 constraints: d0133268)
io.dropwizard:dropwizard-jackson:1.3.29 (5 constraints: 9d4aa8cf)
io.dropwizard:dropwizard-jersey:1.3.29 (1 constraints: 790daa2c)
io.dropwizard:dropwizard-jackson:1.3.29 (6 constraints: 9f4f8b2b)
io.dropwizard:dropwizard-jersey:1.3.29 (2 constraints: 7b12f725)
io.dropwizard:dropwizard-jetty:1.3.29 (2 constraints: 651f7ec2)
io.dropwizard:dropwizard-lifecycle:1.3.29 (2 constraints: 3f1cd3c1)
io.dropwizard:dropwizard-logging:1.3.29 (4 constraints: c53bbb5c)
Expand Down Expand Up @@ -54,8 +56,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: 2823b128)
net.sourceforge.argparse4j:argparse4j:0.8.1 (1 constraints: 430d3a1f)
org.apache.commons:commons-lang3:3.12.0 (4 constraints: a831310c)
org.apache.commons:commons-text:1.9 (1 constraints: b5102f9b)
org.apache.commons:commons-lang3:3.11 (3 constraints: 712c254a)
org.apache.commons:commons-text:1.9 (2 constraints: 6215ca99)
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 Expand Up @@ -92,7 +94,7 @@ org.jboss.logging:jboss-logging:3.3.0.Final (1 constraints: bd10c4b6)
org.slf4j:jcl-over-slf4j:1.7.36 (1 constraints: b30e965e)
org.slf4j:jul-to-slf4j:1.7.36 (1 constraints: b30e965e)
org.slf4j:log4j-over-slf4j:1.7.36 (1 constraints: b30e965e)
org.slf4j:slf4j-api:1.7.36 (10 constraints: 708c7a54)
org.slf4j:slf4j-api:1.7.36 (12 constraints: 10ae31f5)
org.yaml:snakeyaml:1.23 (1 constraints: 6e17f627)

[Test dependencies]
Expand Down
3 changes: 2 additions & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ com.fasterxml.jackson.*:* = 2.6.1
com.google.code.findbugs:jsr305 = 3.0.2
com.google.errorprone:error_prone_annotations = 2.9.0
com.google.guava:guava = 31.1-jre
com.palantir.safe-logging:* = 1.21.0
io.dropwizard:* = 1.0.0
io.dropwizard:dropwizard-validation = 1.3.29
javax.ws.rs:javax.ws.rs-api = 2.0.1
junit:junit = 4.13.2
org.apache.commons:commons-lang3 = 3.12.0
org.apache.commons:commons-text = 1.9
org.assertj:assertj-core = 3.22.0
org.hamcrest:* = 1.3
org.immutables:* = 2.8.8
Expand Down

0 comments on commit 3dc7f2b

Please sign in to comment.