michaeljmarshall commented on code in PR #19390:
URL: https://github.com/apache/pulsar/pull/19390#discussion_r1099225283
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -971,6 +973,7 @@ protected void handleConnect(CommandConnect connect) {
authRole =
getBrokerService().getAuthenticationService().getAnonymousUserRole()
Review Comment:
Why not just add the following before the call to get the anonymous role? I
think this would correctly handle the issue of a proxy authenticating as the
anonymous role.
```java
if (connect.hasOriginalPrincipal() || connect.hasOriginalAuthData()) {
throw new AuthenticationException("Proxy cannot authenticate as the
anonymous role.");
}
```
##########
pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAnonymousRoleTest.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.proxy.server;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.spy;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import java.util.Base64;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import javax.crypto.SecretKey;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.AuthenticationService;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.api.AuthenticationFactory;
+import org.apache.pulsar.client.api.ClientBuilder;
+import org.apache.pulsar.client.api.ProducerConsumerBase;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
+import org.apache.pulsar.common.policies.data.AuthAction;
+import org.apache.pulsar.common.policies.data.ClusterData;
+import org.apache.pulsar.common.policies.data.TenantInfoImpl;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Slf4j
+@Test(groups = "proxy")
+public class ProxyAnonymousRoleTest extends ProducerConsumerBase {
+ private final String ANONYMOUS_ROLE = "proxy-connector";
+ private final String CLIENT_ROLE = "client";
+ private final String ADMIN_ROLE = "admin";
+ private final SecretKey SECRET_KEY =
AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+ private final String ADMIN_TOKEN =
Jwts.builder().setSubject(ADMIN_ROLE).signWith(SECRET_KEY).compact();
+ private final String CLIENT_TOKEN =
Jwts.builder().setSubject(CLIENT_ROLE).signWith(SECRET_KEY).compact();
+
+ private ProxyService proxyService;
+ private final ProxyConfiguration proxyConfig = new ProxyConfiguration();
+
+ @BeforeMethod
+ @Override
+ protected void setup() throws Exception {
+ // enable auth&auth and use JWT at broker
+ conf.setAuthenticationEnabled(true);
+ conf.setAuthorizationEnabled(true);
+ conf.getProperties().setProperty("tokenSecretKey", "data:;base64," +
Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+
+ Set<String> superUserRoles = new HashSet<>();
+ superUserRoles.add(ANONYMOUS_ROLE);
+ superUserRoles.add(ADMIN_ROLE);
+ conf.setSuperUserRoles(superUserRoles);
+ conf.setAnonymousUserRole(ANONYMOUS_ROLE);
+
+ Set<String> providers = new HashSet<>();
+ providers.add(AuthenticationProviderToken.class.getName());
+ conf.setAuthenticationProviders(providers);
+
+ conf.setClusterName("proxy-authorization");
+ conf.setNumExecutorThreadPoolSize(5);
+
+ super.init();
+ // start proxy service
+ proxyConfig.setAuthenticationEnabled(true);
+ proxyConfig.setAuthorizationEnabled(false);
+ proxyConfig.getProperties().setProperty("tokenSecretKey",
"data:;base64," + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+ proxyConfig.setBrokerServiceURL(pulsar.getBrokerServiceUrl());
+ proxyConfig.setServicePort(Optional.of(0));
+ proxyConfig.setBrokerProxyAllowedTargetPorts("*");
+ proxyConfig.setWebServicePort(Optional.of(0));
+ proxyConfig.setAuthenticationProviders(providers);
+
+ proxyService = Mockito.spy(new ProxyService(proxyConfig,
+ new AuthenticationService(
+ PulsarConfigurationLoader.convertFrom(proxyConfig))));
+ }
+
+ @AfterMethod(alwaysRun = true)
+ @Override
+ protected void cleanup() throws Exception {
+ super.internalCleanup();
+ proxyService.close();
+ }
+
+ private void startProxy() throws Exception {
+ proxyService.start();
+ }
+
+ @Test
+ public void testAuthorizationWithProxyAsAnonymousRoleWithOriginalRole()
throws Exception {
Review Comment:
If we do go with my proposed restriction, it might be helpful to use the
`ServerCnxTest` class to validate how the `Connect` command behaves. If you
look through the commit history on that file, you'll see that I've added
several authentication related tests that allow for low level manipulation of
the protocol without much need for mocking.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -971,6 +973,7 @@ protected void handleConnect(CommandConnect connect) {
authRole =
getBrokerService().getAuthenticationService().getAnonymousUserRole()
.orElseThrow(() ->
new AuthenticationException("No anonymous role, and no
authentication provider configured"));
+ checkAndStoreOriginalAuthDataForwardedByProxy(connect);
Review Comment:
My concern with this solution is that it lets a proxy connect as the
anonymous role, and I don't think that will work correctly. Note that we do not
use the `anonymousRole` for the `originalPrincipal`, so it seems to me that the
`anonymousRole` is only meant for direct broker access.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]