You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2020/01/10 12:51:40 UTC
svn commit: r1872592 - in /felix/trunk/configadmin-plugins/interpolation/src:
main/java/org/apache/felix/configadmin/plugin/interpolation/
test/java/org/apache/felix/configadmin/plugin/interpolation/
Author: cziegeler
Date: Fri Jan 10 12:51:40 2020
New Revision: 1872592
URL: http://svn.apache.org/viewvc?rev=1872592&view=rev
Log:
FELIX-6164 : Support escaping to for interpolation syntax
Added:
felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java (with props)
felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java (with props)
Modified:
felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java
felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java
Modified: felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java?rev=1872592&r1=1872591&r2=1872592&view=diff
==============================================================================
--- felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java (original)
+++ felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java Fri Jan 10 12:51:40 2020
@@ -19,14 +19,10 @@ package org.apache.felix.configadmin.plu
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
-import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
-import java.util.function.Function;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -36,15 +32,16 @@ import org.osgi.util.converter.Converter
import org.slf4j.Logger;
class InterpolationConfigurationPlugin implements ConfigurationPlugin {
- private static final String PREFIX = "$[";
- private static final String SUFFIX = "]";
- private static final String ENV_PREFIX = PREFIX + "env:";
- private static final Pattern ENV_PATTERN = createPattern(ENV_PREFIX);
- private static final String PROP_PREFIX = PREFIX + "prop:";
- private static final Pattern PROP_PATTERN = createPattern(PROP_PREFIX);
- private static final String SECRET_PREFIX = PREFIX + "secret:";
- private static final Pattern SECRET_PATTERN = createPattern(SECRET_PREFIX);
+ private static final String TYPE_ENV = "env";
+
+ private static final String TYPE_PROP = "prop";
+
+ private static final String TYPE_SECRET = "secret";
+
+ private static final String DIRECTIVE_TYPE = "type";
+
+ private static final String DIRECTIVE_DEFAULT = "default";
private static final Map<String, Class<?>> TYPE_MAP = new HashMap<>();
static {
@@ -86,10 +83,6 @@ class InterpolationConfigurationPlugin i
TYPE_MAP.put("char[]", char[].class);
}
- private static Pattern createPattern(String prefix) {
- return Pattern.compile("\\Q" + prefix + "\\E.+?\\Q" + SUFFIX + "\\E");
- }
-
private final BundleContext context;
private final File directory;
@@ -114,7 +107,7 @@ class InterpolationConfigurationPlugin i
String key = keys.nextElement();
Object val = properties.get(key);
if (val instanceof String) {
- Object newVal = replace(key, pid, (String) val);
+ Object newVal = getNewValue(key, (String) val, pid);
if (newVal != null && !newVal.equals(val)) {
properties.put(key, newVal);
getLog().info("Replaced value of configuration property '{}' for PID {}", key, pid);
@@ -123,7 +116,7 @@ class InterpolationConfigurationPlugin i
String[] array = (String[]) val;
String[] newArray = null;
for (int i = 0; i < array.length; i++) {
- Object newVal = replace(key, pid, array[i]);
+ Object newVal = getNewValue(key, array[i], pid);
if (newVal != null && !newVal.equals(array[i])) {
if (newArray == null) {
newArray = new String[array.length];
@@ -140,113 +133,78 @@ class InterpolationConfigurationPlugin i
}
}
- private Object replace(final String key, Object pid, String sv) {
- int idx = sv.indexOf(PREFIX);
- if (idx != -1) {
- String varStart = sv.substring(idx);
- Object newVal = null;
- if (varStart.startsWith(SECRET_PREFIX)) {
- newVal = replaceVariablesFromFile(key, sv, pid);
- } else if (varStart.startsWith(ENV_PREFIX)) {
- newVal = replaceVariablesFromEnvironment(sv);
- } else if (varStart.startsWith(PROP_PREFIX)) {
- newVal = replaceVariablesFromProperties(sv);
- }
-
- return newVal;
+ private Object getNewValue(final String key, final String value, final Object pid) {
+ final Object result = replace(key, value, pid);
+ if (value.equals(result)) {
+ return null;
}
- return null;
+ return result;
+ }
+
+ Object replace(final String key, final String value, final Object pid) {
+ final Object result = Interpolator.replace(value, (type, name, dir) -> {
+ String v = null;
+ if (TYPE_ENV.equals(type)) {
+ v = getVariableFromEnvironment(name);
+
+ } else if (TYPE_PROP.equals(type)) {
+ v = getVariableFromProperty(name);
+
+ } else if (TYPE_SECRET.equals(type)) {
+ v = getVariableFromFile(key, name, pid);
+ }
+ if (v == null) {
+ v = dir.get(DIRECTIVE_DEFAULT);
+ }
+ if (v != null && dir.containsKey(DIRECTIVE_TYPE)) {
+ return convertType(dir.get(DIRECTIVE_TYPE), v);
+ }
+ return v;
+ });
+ return result;
}
- Object replaceVariablesFromEnvironment(final String value) {
- return replaceVariables(ENV_PREFIX, ENV_PATTERN, value, n -> System.getenv(n));
+ String getVariableFromEnvironment(final String name) {
+ return System.getenv(name);
}
- Object replaceVariablesFromProperties(final String value) {
- return replaceVariables(PROP_PREFIX, PROP_PATTERN, value, n -> context.getProperty(n));
+ String getVariableFromProperty(final String name) {
+ return context.getProperty(name);
}
- Object replaceVariablesFromFile(final String key, final String value, final Object pid) {
+ String getVariableFromFile(final String key, final String name, final Object pid) {
if (directory == null) {
getLog().warn("Cannot replace property value {} for PID {}. No directory configured via framework property " +
Activator.DIR_PROPERTY, key, pid);
return null;
}
- return replaceVariables(SECRET_PREFIX, SECRET_PATTERN, value, n -> {
- if (n.contains("..")) {
- getLog().error("Illegal secret location: " + n + " Going up in the directory structure is not allowed");
- return null;
- }
-
- File file = new File(directory, n);
- if (!file.isFile()) {
- getLog().warn("Cannot replace variable. Configured path is not a regular file: " + file);
- return null;
- }
- byte[] bytes;
- try {
- bytes = Files.readAllBytes(file.toPath());
- } catch (IOException e) {
- getLog().error("Problem replacing configuration property '{}' for PID {} from file {}",
- key, pid, file, e);
-
- return null;
- }
- return new String(bytes).trim();
- });
- }
-
- Object replaceVariables(final String prefix, final Pattern pattern,
- final String value,
- final Function<String, String> valueSource) {
- final Matcher m = pattern.matcher(value);
- final StringBuffer sb = new StringBuffer();
- String type = null;
- while (m.find()) {
- final String var = m.group();
-
- final int len = var.length();
- final int idx = var.indexOf(';');
-
- final Map<String, String> directives;
- final int endIdx;
- if (idx >= 0) {
- endIdx = idx;
- directives = parseDirectives(var.substring(idx, len - SUFFIX.length()));
- } else {
- endIdx = len - SUFFIX.length();
- directives = Collections.emptyMap();
- }
+ if (name.contains("..")) {
+ getLog().error("Illegal secret location: " + name + " Going up in the directory structure is not allowed");
+ return null;
+ }
- final String varName = var.substring(prefix.length(), endIdx);
- String replacement = valueSource.apply(varName);
- if (replacement != null) {
- m.appendReplacement(sb, Matcher.quoteReplacement(replacement));
- } else {
- String defVal = directives.get("default");
- if (defVal != null) {
- m.appendReplacement(sb, Matcher.quoteReplacement(defVal));
- }
- }
- type = directives.get("type");
+ File file = new File(directory, name);
+ if (!file.isFile()) {
+ getLog().warn("Cannot replace variable. Configured path is not a regular file: " + file);
+ return null;
}
- m.appendTail(sb);
- return convertType(type, sb.toString());
- }
+ if (!file.getAbsolutePath().startsWith(directory.getAbsolutePath())) {
+ getLog().error("Illegal secret location: " + name + " Going out the directory structure is not allowed");
+ return null;
+ }
- private Map<String, String> parseDirectives(String dirString) {
- Map<String, String> dirs = new HashMap<>();
+ byte[] bytes;
+ try {
+ bytes = Files.readAllBytes(file.toPath());
+ } catch (IOException e) {
+ getLog().error("Problem replacing configuration property '{}' for PID {} from file {}",
+ key, pid, file, e);
- for (String dir : dirString.split(";")) {
- String[] kv = dir.split("=");
- if (kv.length == 2) {
- dirs.put(kv[0], kv[1]);
- }
+ return null;
}
-
- return dirs;
+ return new String(bytes).trim();
}
private Object convertType(String type, String s) {
Added: felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java?rev=1872592&view=auto
==============================================================================
--- felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java (added)
+++ felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java Fri Jan 10 12:51:40 2020
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.felix.configadmin.plugin.interpolation;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Replace place holders in a string
+ */
+public class Interpolator {
+
+ public static final char END = ']';
+
+ public static final String START = "$[";
+
+ public static final char ESCAPE = '\\';
+
+ /**
+ * The value for the replacement is returned by this provider
+ */
+ @FunctionalInterface
+ public static interface Provider {
+
+ Object provide(String type, String name, Map<String, String> directives);
+ }
+
+ /**
+ * Replace all place holders
+ *
+ * @param value Value with place holders
+ * @param provider Provider for providing the values
+ * @return Replaced object (or original value)
+ */
+ public static Object replace(final String value, final Provider provider) {
+ String result = value;
+ int start = -1;
+ while (start < result.length()) {
+ start = result.indexOf(START, start);
+ if (start == -1) {
+ // no placeholder found -> end
+ start = result.length();
+ continue;
+ }
+
+ boolean replace = true;
+ if (start > 0 && result.charAt(start - 1) == ESCAPE) {
+ if (start == 1 || result.charAt(start - 2) != ESCAPE) {
+ replace = false;
+ }
+ }
+
+ if (!replace) {
+ // placeholder is escaped -> remove placeholder and continue
+ result = result.substring(0, start - 1).concat(result.substring(start));
+ start = start + START.length();
+ continue;
+ }
+
+ int count = 1;
+ int index = start + START.length();
+ while (index < result.length() && count > 0) {
+ if (result.charAt(index) == '[' && result.charAt(index - 1) == '$') {
+ count++;
+ } else if (result.charAt(index) == END) {
+ count--;
+ }
+ index++;
+ }
+
+ if (count > 0) {
+ // no matching end found -> end
+ start = result.length();
+ continue;
+ }
+
+ final String key = result.substring(start + START.length(), index - 1);
+ final int sep = key.indexOf(':');
+ if (sep == -1) {
+ // invalid key
+ start = index;
+ continue;
+ }
+
+ final String type = key.substring(0, sep);
+ final String postfix = key.substring(sep + 1);
+
+ final int dirPos = postfix.indexOf(';');
+ final Map<String, String> directives;
+ final String name;
+ if (dirPos == -1) {
+ name = postfix;
+ directives = Collections.emptyMap();
+ } else {
+ name = postfix.substring(0, dirPos);
+ directives = new HashMap<>();
+
+ for (String dir : postfix.substring(dirPos + 1).split(";")) {
+ String[] kv = dir.split("=");
+ if (kv.length == 2) {
+ directives.put(kv[0], kv[1]);
+ }
+ }
+ }
+
+ // recursive replacement
+ final Object newName = replace(name, provider);
+
+ Object replacement = provider.provide(type, newName.toString(), directives);
+ if (replacement == null) {
+ // no replacement found -> leave as is and continue
+ start = index;
+ } else {
+ if (!(replacement instanceof String)) {
+ if (start == 0 && index == result.length()) {
+ return replacement;
+ }
+ }
+ // replace and continue with replacement
+ result = result.substring(0, start).concat(replacement.toString()).concat(result.substring(index));
+ }
+ }
+ return result;
+ }
+}
\ No newline at end of file
Propchange: felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: felix/trunk/configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/Interpolator.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Modified: felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java?rev=1872592&r1=1872591&r2=1872592&view=diff
==============================================================================
--- felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java (original)
+++ felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java Fri Jan 10 12:51:40 2020
@@ -117,9 +117,9 @@ public class InterpolationConfigurationP
InterpolationConfigurationPlugin plugin = new InterpolationConfigurationPlugin(null,
new File(rf).getParent());
- assertEquals("xxla la layy", plugin.replaceVariablesFromFile("akey", "xx$[secret:testfile.txt]yy", "apid"));
+ assertEquals("xxla la layy", plugin.replace("akey", "xx$[secret:testfile.txt]yy", "apid"));
String doesNotReplace = "xx$[" + rf + "]yy";
- assertEquals(doesNotReplace, plugin.replaceVariablesFromFile("akey", doesNotReplace, "apid"));
+ assertEquals(doesNotReplace, plugin.replace("akey", doesNotReplace, "apid"));
}
@Test
@@ -128,7 +128,7 @@ public class InterpolationConfigurationP
InterpolationConfigurationPlugin plugin = new InterpolationConfigurationPlugin(null,
new File(rf).getParent());
- assertEquals("foo", plugin.replaceVariablesFromFile("akey", "foo", "apid"));
+ assertEquals("foo", plugin.replace("akey", "foo", "apid"));
}
@Test
@@ -172,4 +172,28 @@ public class InterpolationConfigurationP
assertEquals("hello there", array[1]);
assertEquals("3", array[2]);
}
+
+ @Test
+ public void testMultiplePlaceholders() throws Exception {
+ BundleContext bc = Mockito.mock(BundleContext.class);
+ Mockito.when(bc.getProperty("foo.bar")).thenReturn("hello there");
+ String rf = getClass().getResource("/testfile.txt").getFile();
+ InterpolationConfigurationPlugin plugin = new InterpolationConfigurationPlugin(bc, new File(rf).getParent());
+
+ assertEquals("xxhello thereyyhello therezz",
+ plugin.replace("akey", "xx$[prop:foo.bar]yy$[prop:foo.bar]zz", "apid"));
+
+ assertEquals("xxla la layyhello therezz",
+ plugin.replace("akey", "xx$[secret:testfile.txt]yy$[prop:foo.bar]zz", "apid"));
+ }
+
+ @Test
+ public void testNestedPlaceholders() throws Exception {
+ BundleContext bc = Mockito.mock(BundleContext.class);
+ Mockito.when(bc.getProperty("foo.bar")).thenReturn("hello there");
+ Mockito.when(bc.getProperty("key")).thenReturn("foo.bar");
+ InterpolationConfigurationPlugin plugin = new InterpolationConfigurationPlugin(bc, null);
+
+ assertEquals("hello there", plugin.replace("akey", "$[prop:$[prop:key]]", "apid"));
+ }
}
Added: felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java?rev=1872592&view=auto
==============================================================================
--- felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java (added)
+++ felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java Fri Jan 10 12:51:40 2020
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.felix.configadmin.plugin.interpolation;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.apache.felix.configadmin.plugin.interpolation.Interpolator.Provider;
+import org.junit.Test;
+
+public class InterpolatorTest {
+
+ @Test
+ public void testNoValue() {
+ assertEquals("$[foo:hello]", Interpolator.replace("$[foo:hello]", (type, name, dir) -> null));
+ }
+
+ @Test
+ public void testValue() {
+ assertEquals("hello world", Interpolator.replace("$[foo:hello]", (type, name, dir) -> {
+ if ("foo".equals(type) && "hello".equals(name)) {
+ return "hello world";
+ }
+ return null;
+ }));
+ }
+
+ @Test
+ public void testValueAndConstantText() {
+ assertEquals("beforehello worldafter", Interpolator.replace("before$[foo:hello]after", (type, name, dir) -> {
+ if ("foo".equals(type) && "hello".equals(name)) {
+ return "hello world";
+ }
+ return null;
+ }));
+ }
+
+ @Test
+ public void testRecursion() {
+ assertEquals("beforehello worldafter",
+ Interpolator.replace("before$[foo:$[foo:inner]]after", (type, name, dir) -> {
+ if ("foo".equals(type) && "hello".equals(name)) {
+ return "hello world";
+ } else if ("foo".equals(type) && "inner".equals(name)) {
+ return "hello";
+ }
+ return null;
+ }));
+ }
+
+ @Test
+ public void testEscaping() {
+ final Provider p = new Provider() {
+
+ @Override
+ public Object provide(String type, String name, Map<String, String> directives) {
+ return "value";
+ }
+ };
+ assertEquals("$[no:replacement]", Interpolator.replace("\\$[no:replacement]", p));
+ }
+}
Propchange: felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: felix/trunk/configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url