From bff3e94c01bc7afbed46fce892a2ab8d14130205 Mon Sep 17 00:00:00 2001 From: Eric Olson Date: Sat, 27 Feb 2021 11:23:26 -0600 Subject: [PATCH] NIFI-7172 Trim trailing whitespace from NiFi properties This closes #4854 Signed-off-by: David Handermann --- .../nifi/properties/NiFiPropertiesLoader.java | 8 +++++++ .../StandardNiFiPropertiesGroovyTest.groovy | 19 +++++++++++++++ .../conf/nifi_with_whitespace.properties | 24 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/resources/conf/nifi_with_whitespace.properties diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/NiFiPropertiesLoader.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/NiFiPropertiesLoader.java index a0feca664c..7b01ec3a55 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/NiFiPropertiesLoader.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/NiFiPropertiesLoader.java @@ -24,7 +24,9 @@ import java.io.InputStream; import java.security.NoSuchAlgorithmException; import java.security.Security; import java.util.Properties; +import java.util.Set; import javax.crypto.Cipher; +import org.apache.commons.lang3.StringUtils; import org.apache.nifi.security.kms.CryptoUtils; import org.apache.nifi.util.NiFiProperties; import org.bouncycastle.jce.provider.BouncyCastleProvider; @@ -174,6 +176,12 @@ public class NiFiPropertiesLoader { rawProperties.load(inStream); logger.info("Loaded {} properties from {}", rawProperties.size(), file.getAbsolutePath()); + Set keys = rawProperties.stringPropertyNames(); + for (final String key : keys) { + String prop = rawProperties.getProperty(key); + rawProperties.setProperty(key, StringUtils.stripEnd(prop, null)); + } + ProtectedNiFiProperties protectedNiFiProperties = new ProtectedNiFiProperties(rawProperties); return protectedNiFiProperties; } catch (final Exception ex) { diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/groovy/org/apache/nifi/properties/StandardNiFiPropertiesGroovyTest.groovy b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/groovy/org/apache/nifi/properties/StandardNiFiPropertiesGroovyTest.groovy index c02dc018d9..b73ac42e28 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/groovy/org/apache/nifi/properties/StandardNiFiPropertiesGroovyTest.groovy +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/groovy/org/apache/nifi/properties/StandardNiFiPropertiesGroovyTest.groovy @@ -1007,4 +1007,23 @@ class StandardNiFiPropertiesGroovyTest extends GroovyTestCase { // Assert assert webMaxContentSize == "" } + + + @Test + void testShouldStripWhitespace() throws Exception { + // Arrange + File unprotectedFile = new File("src/test/resources/conf/nifi_with_whitespace.properties") + NiFiPropertiesLoader niFiPropertiesLoader = new NiFiPropertiesLoader() + + // Act + NiFiProperties niFiProperties = niFiPropertiesLoader.load(unprotectedFile.path) + + // Assert + assert niFiProperties.getProperty("nifi.whitespace.propWithNoSpace") == "foo" + assert niFiProperties.getProperty("nifi.whitespace.propWithLeadingSpace") == "foo" + assert niFiProperties.getProperty("nifi.whitespace.propWithTrailingSpace") == "foo" + assert niFiProperties.getProperty("nifi.whitespace.propWithLeadingAndTrailingSpace") == "foo" + assert niFiProperties.getProperty("nifi.whitespace.propWithTrailingTab") == "foo" + assert niFiProperties.getProperty("nifi.whitespace.propWithMultipleLines") == "foobarbaz" + } } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/resources/conf/nifi_with_whitespace.properties b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/resources/conf/nifi_with_whitespace.properties new file mode 100644 index 0000000000..420752eb0b --- /dev/null +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/resources/conf/nifi_with_whitespace.properties @@ -0,0 +1,24 @@ +# 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. + +# properties with whitespace +nifi.whitespace.propWithNoSpace=foo +nifi.whitespace.propWithLeadingSpace= foo +nifi.whitespace.propWithTrailingSpace=foo +nifi.whitespace.propWithLeadingAndTrailingSpace= foo +nifi.whitespace.propWithTrailingTab=foo\t +nifi.whitespace.propWithMultipleLines=foo\ + bar\ + baz