diff --git a/core/src/main/scala/kafka/utils/Json.scala b/core/src/main/scala/kafka/utils/Json.scala index 3fafeedaec9..420ece5e712 100644 --- a/core/src/main/scala/kafka/utils/Json.scala +++ b/core/src/main/scala/kafka/utils/Json.scala @@ -35,16 +35,7 @@ object Json { */ def parseFull(input: String): Option[JsonValue] = try Option(mapper.readTree(input)).map(JsonValue(_)) - catch { - case _: JsonProcessingException => - // Before 1.0.1, Json#encode did not escape backslash or any other special characters. SSL principals - // stored in ACLs may contain backslash as an escape char, making the JSON generated in earlier versions invalid. - // Escape backslash and retry to handle these strings which may have been persisted in ZK. - // Note that this does not handle all special characters (e.g. non-escaped double quotes are not supported) - val escapedInput = input.replaceAll("\\\\", "\\\\\\\\") - try Option(mapper.readTree(escapedInput)).map(JsonValue(_)) - catch { case _: JsonProcessingException => None } - } + catch { case _: JsonProcessingException => None } /** * Parse a JSON string into either a generic type T, or a JsonProcessingException in the case of diff --git a/core/src/test/scala/unit/kafka/security/authorizer/AclEntryTest.scala b/core/src/test/scala/unit/kafka/security/authorizer/AclEntryTest.scala index 261b24fec56..80495b03e9b 100644 --- a/core/src/test/scala/unit/kafka/security/authorizer/AclEntryTest.scala +++ b/core/src/test/scala/unit/kafka/security/authorizer/AclEntryTest.scala @@ -23,15 +23,14 @@ import org.apache.kafka.common.acl.AclOperation.READ import org.apache.kafka.common.acl.AclPermissionType.{ALLOW, DENY} import org.apache.kafka.common.security.auth.KafkaPrincipal import org.junit.{Assert, Test} -import org.scalatestplus.junit.JUnitSuite import scala.jdk.CollectionConverters._ -class AclEntryTest extends JUnitSuite { +class AclEntryTest { - val AclJson = "{\"version\": 1, \"acls\": [{\"host\": \"host1\",\"permissionType\": \"Deny\",\"operation\": \"READ\", \"principal\": \"User:alice\" }, " + - "{ \"host\": \"*\" , \"permissionType\": \"Allow\", \"operation\": \"Read\", \"principal\": \"User:bob\" }, " + - "{ \"host\": \"host1\", \"permissionType\": \"Deny\", \"operation\": \"Read\" , \"principal\": \"User:bob\"} ]}" + val AclJson = """{"version": 1, "acls": [{"host": "host1","permissionType": "Deny","operation": "READ", "principal": "User:alice" }, + { "host": "*" , "permissionType": "Allow", "operation": "Read", "principal": "User:bob" }, + { "host": "host1", "permissionType": "Deny", "operation": "Read" , "principal": "User:bob"}]}""" @Test def testAclJsonConversion(): Unit = { @@ -40,10 +39,8 @@ class AclEntryTest extends JUnitSuite { val acl3 = AclEntry(new KafkaPrincipal(KafkaPrincipal.USER_TYPE, "bob"), DENY, "host1", READ) val acls = Set[AclEntry](acl1, acl2, acl3) - val jsonAcls = Json.encodeAsBytes(AclEntry.toJsonCompatibleMap(acls).asJava) - Assert.assertEquals(acls, AclEntry.fromBytes(jsonAcls)) + Assert.assertEquals(acls, AclEntry.fromBytes(Json.encodeAsBytes(AclEntry.toJsonCompatibleMap(acls).asJava))) Assert.assertEquals(acls, AclEntry.fromBytes(AclJson.getBytes(UTF_8))) } - } diff --git a/core/src/test/scala/unit/kafka/utils/JsonTest.scala b/core/src/test/scala/unit/kafka/utils/JsonTest.scala index 4b7f94ae226..8b41b9f4c70 100644 --- a/core/src/test/scala/unit/kafka/utils/JsonTest.scala +++ b/core/src/test/scala/unit/kafka/utils/JsonTest.scala @@ -59,10 +59,6 @@ class JsonTest { val encoded = Json.legacyEncodeAsString(map) val decoded = Json.parseFull(encoded) assertEquals(Json.parseFull("""{"foo1":"bar1\\,bar2", "foo2":"\\bar"}"""), decoded) - - // Test strings with non-escaped backslash and quotes. This is to verify that ACLs - // containing non-escaped chars persisted using 1.0 can be parsed. - assertEquals(decoded, Json.parseFull("""{"foo1":"bar1\,bar2", "foo2":"\bar"}""")) } @Test