You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by xy...@apache.org on 2022/08/05 19:02:34 UTC
[pulsar] branch branch-2.8 updated (6b3e46f20e7 -> c038898608a)
This is an automated email from the ASF dual-hosted git repository.
xyz pushed a change to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git
from 6b3e46f20e7 Fix testProducerSemaphoreInvalidMessage by removing usages of mockStatic
new c8c1c0954f3 [improve][authentication] Adapt basic authentication configuration with prefix (#16935)
new c038898608a Fix AuthenticationProviderBasicTest
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../AuthenticationProviderBasic.java | 41 ++++++++++++++--------
.../AuthenticationProviderBasicTest.java | 18 ++++++++--
.../java/org/apache/pulsar/client/api/url/URL.java | 11 ++++++
3 files changed, 53 insertions(+), 17 deletions(-)
[pulsar] 01/02: [improve][authentication] Adapt basic authentication configuration with prefix (#16935)
Posted by xy...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xyz pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit c8c1c0954f391932cb24db3afc071a46f65e707d
Author: Zixuan Liu <no...@gmail.com>
AuthorDate: Thu Aug 4 15:21:30 2022 +0800
[improve][authentication] Adapt basic authentication configuration with prefix (#16935)
Signed-off-by: Zixuan Liu <no...@gmail.com>
(cherry picked from commit cd95594f086dd6474962e21b24cd5c27fca3933f)
---
.../AuthenticationProviderBasic.java | 41 ++++++++++++++--------
.../AuthenticationProviderBasicTest.java | 18 ++++++++--
2 files changed, 42 insertions(+), 17 deletions(-)
diff --git a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasic.java b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasic.java
index 40ff8635e03..3c62640cde7 100644
--- a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasic.java
+++ b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasic.java
@@ -20,11 +20,12 @@
package org.apache.pulsar.broker.authentication;
import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.StringReader;
-import java.nio.charset.StandardCharsets;
+import java.io.InputStreamReader;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
@@ -34,9 +35,11 @@ import javax.naming.AuthenticationException;
import lombok.Cleanup;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.Md5Crypt;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.authentication.metrics.AuthenticationMetrics;
+import org.apache.pulsar.client.api.url.URL;
public class AuthenticationProviderBasic implements AuthenticationProvider {
private static final String HTTP_HEADER_NAME = "Authorization";
@@ -49,6 +52,20 @@ public class AuthenticationProviderBasic implements AuthenticationProvider {
// noop
}
+ public static byte[] readData(String data)
+ throws IOException, URISyntaxException, InstantiationException, IllegalAccessException {
+ if (data.startsWith("data:") || data.startsWith("file:")) {
+ return IOUtils.toByteArray(URL.createURL(data));
+ } else if (Files.exists(Paths.get(data))) {
+ return Files.readAllBytes(Paths.get(data));
+ } else if (org.apache.commons.codec.binary.Base64.isBase64(data)) {
+ return Base64.getDecoder().decode(data);
+ } else {
+ String msg = "Not supported config";
+ throw new IllegalArgumentException(msg);
+ }
+ }
+
@Override
public void initialize(ServiceConfiguration config) throws IOException {
String data = config.getProperties().getProperty(CONF_PULSAR_PROPERTY_KEY);
@@ -60,17 +77,11 @@ public class AuthenticationProviderBasic implements AuthenticationProvider {
}
@Cleanup BufferedReader reader = null;
- if (org.apache.commons.codec.binary.Base64.isBase64(data)) {
- reader = new BufferedReader(new StringReader(new String(Base64.getDecoder().decode(data),
- StandardCharsets.UTF_8)));
- } else {
- File confFile = new File(data);
- if (!confFile.exists()) {
- throw new IOException("The password auth conf file does not exist");
- } else if (!confFile.isFile()) {
- throw new IOException("The path is not a file");
- }
- reader = new BufferedReader(new FileReader(confFile));
+ try {
+ byte[] bytes = readData(data);
+ reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
}
users = new HashMap<>();
diff --git a/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
index 812e99a0abf..69df6663bd1 100644
--- a/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
+++ b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.pulsar.broker.authentication;
+import static org.testng.Assert.assertEquals;
import com.google.common.io.Resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@@ -25,13 +26,12 @@ import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Base64;
import java.util.Properties;
+import javax.naming.AuthenticationException;
import lombok.Cleanup;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.common.api.AuthData;
import org.testng.annotations.Test;
-import javax.naming.AuthenticationException;
-
public class AuthenticationProviderBasicTest {
private final String basicAuthConf = Resources.getResource("authentication/basic/.htpasswd").getPath();
private final String basicAuthConfBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(
@@ -88,4 +88,18 @@ public class AuthenticationProviderBasicTest {
provider.initialize(serviceConfiguration);
testAuthenticate(provider);
}
+
+ @Test
+ public void testReadData() throws Exception {
+ byte[] data = Files.readAllBytes(Path.of(basicAuthConf));
+ String base64Data = Base64.getEncoder().encodeToString(data);
+
+ // base64 format
+ assertEquals(AuthenticationProviderBasic.readData("data:;base64," + base64Data), data);
+ assertEquals(AuthenticationProviderBasic.readData(base64Data), data);
+
+ // file format
+ assertEquals(AuthenticationProviderBasic.readData("file://" + basicAuthConf), data);
+ assertEquals(AuthenticationProviderBasic.readData(basicAuthConf), data);
+ }
}
[pulsar] 02/02: Fix AuthenticationProviderBasicTest
Posted by xy...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xyz pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit c038898608ac901e81093b38c2dc82cf5051265b
Author: Yunze Xu <xy...@163.com>
AuthorDate: Sat Aug 6 03:00:55 2022 +0800
Fix AuthenticationProviderBasicTest
---
.../authentication/AuthenticationProviderBasicTest.java | 2 +-
.../src/main/java/org/apache/pulsar/client/api/url/URL.java | 11 +++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
index 69df6663bd1..e5a7f20c978 100644
--- a/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
+++ b/pulsar-broker-common/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderBasicTest.java
@@ -91,7 +91,7 @@ public class AuthenticationProviderBasicTest {
@Test
public void testReadData() throws Exception {
- byte[] data = Files.readAllBytes(Path.of(basicAuthConf));
+ byte[] data = Files.readAllBytes(FileSystems.getDefault().getPath(basicAuthConf));
String base64Data = Base64.getEncoder().encodeToString(data);
// base64 format
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/client/api/url/URL.java b/pulsar-common/src/main/java/org/apache/pulsar/client/api/url/URL.java
index b2037377d2f..3286900ecb7 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/client/api/url/URL.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/client/api/url/URL.java
@@ -42,6 +42,17 @@ public class URL {
}
}
+ /**
+ * Creates java.net.URL with data protocol support.
+ *
+ * @param spec the input URL as String
+ * @return java.net.URL instance
+ */
+ public static final java.net.URL createURL(String spec)
+ throws MalformedURLException, URISyntaxException, InstantiationException, IllegalAccessException {
+ return new URL(spec).url;
+ }
+
public URLConnection openConnection() throws IOException {
return this.url.openConnection();
}