You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2025/08/19 04:29:42 UTC

(pulsar) branch branch-4.0 updated: [fix][ws] Allow websocket principals to specify originalPrincipal without proxy role (#24642)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-4.0 by this push:
     new f138eb05cb9 [fix][ws] Allow websocket principals to specify originalPrincipal without proxy role (#24642)
f138eb05cb9 is described below

commit f138eb05cb9dc25479e3b950ca112d5d7456a767
Author: Penghui Li <pe...@apache.org>
AuthorDate: Mon Aug 18 21:27:21 2025 -0700

    [fix][ws] Allow websocket principals to specify originalPrincipal without proxy role (#24642)
    
    ## Summary
    - Fixes authorization issue where websocket connections using admin token authentication were incorrectly rejected when specifying an originalPrincipal
    - Adds support for websocket principals to use originalPrincipal without requiring proxy role authentication
    - Includes comprehensive test coverage for the new behavior
    
    ## Changes
    - Modified `AuthorizationService.java` to add websocket principal check in authorization logic
    - Updated `ProxyRoleAuthTest.java` to make `ADMIN_TOKEN` accessible for test inheritance
    - Added `NonProxyRoleTest.java` to verify websocket proxy authentication without proxy role
    
    (cherry picked from commit 34f86576758715f881d755747a74929c189df5ce)
---
 .../broker/authorization/AuthorizationService.java |  3 +-
 .../pulsar/websocket/proxy/NonProxyRoleTest.java   | 39 ++++++++++++++++++++++
 .../pulsar/websocket/proxy/ProxyRoleAuthTest.java  |  2 +-
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/AuthorizationService.java b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/AuthorizationService.java
index f02fb386f48..2964db30a55 100644
--- a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/AuthorizationService.java
+++ b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/AuthorizationService.java
@@ -489,7 +489,8 @@ public class AuthorizationService {
                 errorMsg = "originalPrincipal cannot be a proxy role.";
             }
         } else if (StringUtils.isNotBlank(originalPrincipal)
-                && !(allowNonProxyPrincipalsToBeEqual && originalPrincipal.equals(authenticatedPrincipal))) {
+                && !(allowNonProxyPrincipalsToBeEqual && originalPrincipal.equals(authenticatedPrincipal))
+                && !isWebsocketPrinciple(originalPrincipal)) {
             errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
         }
         if (errorMsg != null) {
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/NonProxyRoleTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/NonProxyRoleTest.java
new file mode 100644
index 00000000000..6c18057e964
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/NonProxyRoleTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.websocket.proxy;
+
+import org.apache.pulsar.websocket.service.WebSocketProxyConfiguration;
+import org.testng.annotations.Test;
+
+/**
+ * Tests WebSocket proxy authentication when the websocket server connects to the broker
+ * using admin token authentication instead of proxy role authentication.
+ */
+@Test(groups = "websocket")
+public class NonProxyRoleTest extends ProxyRoleAuthTest {
+
+    @Override
+    protected WebSocketProxyConfiguration getProxyConfig() {
+        WebSocketProxyConfiguration configuration = super.getProxyConfig();
+        // Configure proxy's internal client to use ADMIN_TOKEN when connecting to broker
+        configuration.setBrokerClientAuthenticationPlugin("org.apache.pulsar.client.impl.auth.AuthenticationToken");
+        configuration.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        return configuration;
+    }
+}
\ No newline at end of file
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyRoleAuthTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyRoleAuthTest.java
index 595991c40aa..0eb90b9fe36 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyRoleAuthTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/websocket/proxy/ProxyRoleAuthTest.java
@@ -77,7 +77,7 @@ public class ProxyRoleAuthTest extends ProducerConsumerBase {
     private static final String PROXY_TOKEN = AuthTokenUtils.createToken(SECRET_KEY, "websocket_proxy",
             Optional.empty());
     private static final String CLIENT_TOKEN = AuthTokenUtils.createToken(SECRET_KEY, "client", Optional.empty());
-    private static final String ADMIN_TOKEN = AuthTokenUtils.createToken(SECRET_KEY, "admin", Optional.empty());
+    protected static final String ADMIN_TOKEN = AuthTokenUtils.createToken(SECRET_KEY, "admin", Optional.empty());
     private static final String UNAUTHORIZED_TOKEN = AuthTokenUtils.createToken(SECRET_KEY, "unauthorized_user",
             Optional.empty());