From 7083755a73a69246d0e6b66c11b9afa7b2c26d8f Mon Sep 17 00:00:00 2001 From: Michael Marshall Date: Tue, 14 Feb 2023 13:38:25 -0600 Subject: [PATCH] [fix][client] Fix the Windows absolute path not recognized in auth param string (#18403) (cherry picked from commit 177b96a78acada4888cb92f96c10ebba3eca8db7) --- .../auth/MockedPulsarServiceBaseTest.java | 14 ++++---- .../apache/pulsar/utils/ResourceUtils.java | 31 ++++++++++++++++ .../pulsar/admin/cli/PulsarAdminToolTest.java | 5 ++- .../client/impl/AuthenticationUtil.java | 11 ++++-- .../client/impl/AuthenticationUtilTest.java | 36 +++++++++++++++++++ 5 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 pulsar-broker/src/test/java/org/apache/pulsar/utils/ResourceUtils.java create mode 100644 pulsar-client/src/test/java/org/apache/pulsar/client/impl/AuthenticationUtilTest.java diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java index 52d13a9781627..3b483efc33d5c 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/auth/MockedPulsarServiceBaseTest.java @@ -26,7 +26,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import com.google.common.collect.Sets; -import com.google.common.io.Resources; import com.google.common.util.concurrent.MoreExecutors; import io.netty.channel.EventLoopGroup; import java.lang.reflect.Field; @@ -74,6 +73,7 @@ import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; import org.apache.pulsar.metadata.impl.ZKMetadataStore; import org.apache.pulsar.tests.TestRetrySupport; +import org.apache.pulsar.utils.ResourceUtils; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.MockZooKeeper; import org.apache.zookeeper.data.ACL; @@ -88,20 +88,20 @@ */ public abstract class MockedPulsarServiceBaseTest extends TestRetrySupport { public final static String BROKER_KEYSTORE_FILE_PATH = - Resources.getResource("certificate-authority/jks/broker.keystore.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/broker.keystore.jks"); public final static String BROKER_TRUSTSTORE_FILE_PATH = - Resources.getResource("certificate-authority/jks/broker.truststore.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/broker.truststore.jks"); public final static String BROKER_TRUSTSTORE_NO_PASSWORD_FILE_PATH = - Resources.getResource("certificate-authority/jks/broker.truststore.nopassword.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/broker.truststore.nopassword.jks"); public final static String BROKER_KEYSTORE_PW = "111111"; public final static String BROKER_TRUSTSTORE_PW = "111111"; public final static String CLIENT_KEYSTORE_FILE_PATH = - Resources.getResource("certificate-authority/jks/client.keystore.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/client.keystore.jks"); public final static String CLIENT_TRUSTSTORE_FILE_PATH = - Resources.getResource("certificate-authority/jks/client.truststore.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/client.truststore.jks"); public final static String CLIENT_TRUSTSTORE_NO_PASSWORD_FILE_PATH = - Resources.getResource("certificate-authority/jks/client.truststore.nopassword.jks").getPath(); + ResourceUtils.getAbsolutePath("certificate-authority/jks/client.truststore.nopassword.jks"); public final static String CLIENT_KEYSTORE_PW = "111111"; public final static String CLIENT_TRUSTSTORE_PW = "111111"; diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/utils/ResourceUtils.java b/pulsar-broker/src/test/java/org/apache/pulsar/utils/ResourceUtils.java new file mode 100644 index 0000000000000..d0511e272f488 --- /dev/null +++ b/pulsar-broker/src/test/java/org/apache/pulsar/utils/ResourceUtils.java @@ -0,0 +1,31 @@ +/** + * 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.pulsar.utils; + +import com.google.common.io.Resources; +import java.io.File; + +public class ResourceUtils { + + public static String getAbsolutePath(String resourceName) { + // On Windows, URL#getPath might return a string that starts with a disk name, e.g. "/C:/" + // It's invalid to use this path to open a file, so we need to get the absolute path via File. + return new File(Resources.getResource(resourceName).getPath()).getAbsolutePath(); + } +} diff --git a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java index 76f28bdd857db..0e393c901b489 100644 --- a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java +++ b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java @@ -27,7 +27,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import com.fasterxml.jackson.databind.ObjectMapper; @@ -2100,8 +2099,8 @@ public void testAuthTlsWithJsonParam() throws Exception { conf = conf = ((PulsarAdminImpl)tool.getPulsarAdminSupplier().get()) .getClientConfigData(); atuh = (AuthenticationTls) conf.getAuthentication(); - assertNull(atuh.getCertFilePath()); - assertNull(atuh.getKeyFilePath()); + assertEquals(atuh.getCertFilePath(), certFilePath); + assertEquals(atuh.getKeyFilePath(), keyFilePath); } @Test diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/AuthenticationUtil.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/AuthenticationUtil.java index a3a7db02c87bf..ba63805687248 100644 --- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/AuthenticationUtil.java +++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/AuthenticationUtil.java @@ -43,9 +43,14 @@ public static Map configureFromPulsar1AuthParamString(String aut if (isNotBlank(authParamsString)) { String[] params = authParamsString.split(","); for (String p : params) { - String[] kv = p.split(":"); - if (kv.length == 2) { - authParams.put(kv[0], kv[1]); + // The value could be a file path, which could contain a colon like "C:\\path\\to\\file" on Windows. + int index = p.indexOf(':'); + if (index < 0) { + continue; + } + String key = p.substring(0, index); + if (!key.isEmpty()) { + authParams.put(key, p.substring(index + 1)); } } } diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/AuthenticationUtilTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/AuthenticationUtilTest.java new file mode 100644 index 0000000000000..038ccdb66c758 --- /dev/null +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/AuthenticationUtilTest.java @@ -0,0 +1,36 @@ +/** + * 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.pulsar.client.impl; + +import static org.testng.Assert.assertEquals; +import java.util.Map; +import org.testng.annotations.Test; + +public class AuthenticationUtilTest { + + @Test + public void testConfigureAuthParamString() { + Map params = AuthenticationUtil.configureFromPulsar1AuthParamString( + "key:value,path:C:\\path\\to\\file,null-key:,:null-value,:,key:value-2"); + assertEquals(params.size(), 3); + assertEquals(params.get("key"), "value-2"); + assertEquals(params.get("path"), "C:\\path\\to\\file"); + assertEquals(params.get("null-key"), ""); + } +}