Skip to content

Commit

Permalink
[fix][client] Fix the Windows absolute path not recognized in auth pa…
Browse files Browse the repository at this point in the history
…ram string (#18403)
  • Loading branch information
BewareMyPower authored Nov 21, 2022
1 parent 6c9ff8f commit 177b96a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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;
Expand Down Expand Up @@ -79,6 +78,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.MockZooKeeperSession;
Expand All @@ -93,20 +93,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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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;
Expand Down Expand Up @@ -2224,8 +2223,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ public static Map<String, String> 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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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"), "");
}
}

0 comments on commit 177b96a

Please sign in to comment.