From 8938a094342e91bf25b946cc94884affd7aa5cd3 Mon Sep 17 00:00:00 2001 From: c04x Date: Mon, 5 Feb 2024 16:18:19 +0100 Subject: [PATCH] Fix for multiple env vars in config parameters --- .../plugins/ConfigVariableExpander.java | 12 ++++++++-- .../common/ConfigVariableExpanderTest.java | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/logstash-core/src/main/java/org/logstash/plugins/ConfigVariableExpander.java b/logstash-core/src/main/java/org/logstash/plugins/ConfigVariableExpander.java index 008ddc599d6..9e8bd4d23ba 100644 --- a/logstash-core/src/main/java/org/logstash/plugins/ConfigVariableExpander.java +++ b/logstash-core/src/main/java/org/logstash/plugins/ConfigVariableExpander.java @@ -82,9 +82,17 @@ public Object expand(Object value, boolean keepSecrets) { String variable = (String) value; Matcher m = substitutionPattern.matcher(variable); - if (!m.matches()) { - return variable; + + if (m.matches()) { + return replaceMatchedVariables(keepSecrets, m, variable); + } else if (m.find()) { + return m.replaceAll(matchResult -> replaceMatchedVariables(false, m, variable).toString()); } + + return variable; + } + + private Object replaceMatchedVariables(boolean keepSecrets, Matcher m, String variable) { String variableName = m.group("name"); if (secretStore != null) { diff --git a/logstash-core/src/test/java/org/logstash/common/ConfigVariableExpanderTest.java b/logstash-core/src/test/java/org/logstash/common/ConfigVariableExpanderTest.java index 0b111308f3d..81640033b5a 100644 --- a/logstash-core/src/test/java/org/logstash/common/ConfigVariableExpanderTest.java +++ b/logstash-core/src/test/java/org/logstash/common/ConfigVariableExpanderTest.java @@ -61,6 +61,28 @@ public void testSimpleExpansion() throws Exception { Assert.assertEquals(val, expandedValue); } + @Test + public void testPartialExpansion() throws Exception { + String key = "foo"; + String val = "bar"; + ConfigVariableExpander cve = getFakeCve(Collections.emptyMap(), Collections.singletonMap(key, val)); + + String expandedValue = (String) cve.expand("test_partial_${" + key + "}"); + Assert.assertEquals("test_partial_"+val, expandedValue); + } + + @Test + public void testTwoExpansions() throws Exception { + String key1 = "foo"; + String key2 = "bar"; + String val1 = "foo_val"; + String val2 = "bar_val"; + ConfigVariableExpander cve = getFakeCve(Collections.emptyMap(), Map.of(key1, val1, key2, val2)); + + String expandedValue = (String) cve.expand("${" + key1 + "}${" + key2 + "}"); + Assert.assertEquals(val1+val2, expandedValue); + } + @Test public void testExpansionWithDefaultValue() throws Exception { String key = "foo";