NIFI-7172 Trim trailing whitespace from NiFi properties

This closes #4854

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Eric Olson 2021-02-27 11:23:26 -06:00 committed by exceptionfactory
parent 1cd3fbb4eb
commit bff3e94c01
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
3 changed files with 51 additions and 0 deletions

View File

@ -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<String> 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) {

View File

@ -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"
}
}

View File

@ -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