Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-7388 equal sign in property value for password #5630

Merged
13 changes: 5 additions & 8 deletions core/src/main/scala/kafka/utils/CommandLineUtils.scala
Expand Up @@ -5,7 +5,7 @@
* 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
Expand Down Expand Up @@ -58,21 +58,18 @@ object CommandLineUtils extends Logging {

/**
* Parse key-value pairs in the form key=value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you add a comment to say that value may contain equals?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* value may contain equals sign
*/
def parseKeyValueArgs(args: Iterable[String], acceptMissingValue: Boolean = true): Properties = {
val splits = args.map(_ split "=").filterNot(_.length == 0)
val splits = args.map(_.split("=", 2)).filterNot(_.length == 0)

val props = new Properties
for (a <- splits) {
if (a.length == 1) {
if (a.length == 1 || (a.length == 2 && a(1).isEmpty())) {
if (acceptMissingValue) props.put(a(0), "")
else throw new IllegalArgumentException(s"Missing value for key ${a(0)}")
}
else if (a.length == 2) props.put(a(0), a(1))
else {
System.err.println("Invalid command line properties: " + args.mkString(" "))
Exit.exit(1)
}
else props.put(a(0), a(1))
}
props
}
Expand Down
23 changes: 17 additions & 6 deletions core/src/test/scala/unit/kafka/KafkaConfigTest.scala
Expand Up @@ -58,12 +58,6 @@ class KafkaTest {
assertEquals(util.Arrays.asList("compact","delete"), config4.logCleanupPolicy)
}

@Test(expected = classOf[FatalExitError])
def testGetKafkaConfigFromArgsWrongSetValue(): Unit = {
val propertiesFile = prepareDefaultConfig()
KafkaConfig.fromProps(Kafka.getPropsFromArgs(Array(propertiesFile, "--override", "a=b=c")))
}

@Test(expected = classOf[FatalExitError])
def testGetKafkaConfigFromArgsNonArgsAtTheEnd(): Unit = {
val propertiesFile = prepareDefaultConfig()
Expand Down Expand Up @@ -97,6 +91,23 @@ class KafkaTest {
assertEquals("truststore_password", config.getPassword(KafkaConfig.SslTruststorePasswordProp).value)
}

@Test
def testKafkaSslPasswordsWithSymbols(): Unit = {
val password = "=!#-+!?*/\"\'^%$=\\.,@:;="
val propertiesFile = prepareDefaultConfig()
val config = KafkaConfig.fromProps(Kafka.getPropsFromArgs(Array(propertiesFile,
"--override", "ssl.keystore.password=" + password,
"--override", "ssl.key.password=" + password,
"--override", "ssl.truststore.password=" + password)))
assertEquals(Password.HIDDEN, config.getPassword(KafkaConfig.SslKeyPasswordProp).toString)
assertEquals(Password.HIDDEN, config.getPassword(KafkaConfig.SslKeystorePasswordProp).toString)
assertEquals(Password.HIDDEN, config.getPassword(KafkaConfig.SslTruststorePasswordProp).toString)

assertEquals(password, config.getPassword(KafkaConfig.SslKeystorePasswordProp).value)
assertEquals(password, config.getPassword(KafkaConfig.SslKeyPasswordProp).value)
assertEquals(password, config.getPassword(KafkaConfig.SslTruststorePasswordProp).value)
}

def prepareDefaultConfig(): String = {
prepareConfig(Array("broker.id=1", "zookeeper.connect=somewhere"))
}
Expand Down
35 changes: 28 additions & 7 deletions core/src/test/scala/unit/kafka/utils/CommandLineUtilsTest.scala
Expand Up @@ -26,30 +26,51 @@ class CommandLineUtilsTest {
@Test(expected = classOf[java.lang.IllegalArgumentException])
def testParseEmptyArg() {
val argArray = Array("my.empty.property=")
CommandLineUtils.parseKeyValueArgs(argArray, false)

CommandLineUtils.parseKeyValueArgs(argArray, acceptMissingValue = false)
}

@Test(expected = classOf[java.lang.IllegalArgumentException])
def testParseEmptyArgWithNoDelimiter() {
val argArray = Array("my.empty.property")

CommandLineUtils.parseKeyValueArgs(argArray, acceptMissingValue = false)
}

@Test
def testParseEmptyArgAsValid() {
val argArray = Array("my.empty.property=")
val argArray = Array("my.empty.property=", "my.empty.property1")
val props = CommandLineUtils.parseKeyValueArgs(argArray)
assertEquals("Value of a key with missing value should be an empty string",props.getProperty("my.empty.property"),"")

assertEquals("Value of a key with missing value should be an empty string", props.getProperty("my.empty.property"), "")
assertEquals("Value of a key with missing value with no delimiter should be an empty string", props.getProperty("my.empty.property1"), "")
}

@Test
def testParseSingleArg() {
val argArray = Array("my.property=value")
val props = CommandLineUtils.parseKeyValueArgs(argArray)
assertEquals("Value of a single property should be 'value' ",props.getProperty("my.property"),"value")

assertEquals("Value of a single property should be 'value' ", props.getProperty("my.property"), "value")
}

@Test
def testParseArgs() {
val argArray = Array("first.property=first","second.property=second")
val props = CommandLineUtils.parseKeyValueArgs(argArray, false)
assertEquals("Value of first property should be 'first'",props.getProperty("first.property"),"first")
assertEquals("Value of second property should be 'second'",props.getProperty("second.property"),"second")
val props = CommandLineUtils.parseKeyValueArgs(argArray)

assertEquals("Value of first property should be 'first'", props.getProperty("first.property"), "first")
assertEquals("Value of second property should be 'second'", props.getProperty("second.property"), "second")
}

@Test
def testParseArgsWithMultipleDelimiters() {
val argArray = Array("first.property==first", "second.property=second=", "third.property=thi=rd")
val props = CommandLineUtils.parseKeyValueArgs(argArray)

assertEquals("Value of first property should be '=first'", props.getProperty("first.property"), "=first")
assertEquals("Value of second property should be 'second='", props.getProperty("second.property"), "second=")
assertEquals("Value of second property should be 'thi=rd'", props.getProperty("third.property"), "thi=rd")
}

}