You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/11 04:48:37 UTC

[GitHub] [pulsar] nodece opened a new pull request, #16016: [fix][broker] Fix token expiration

nodece opened a new pull request, #16016:
URL: https://github.com/apache/pulsar/pull/16016

   Signed-off-by: Zixuan Liu <no...@gmail.com>
   
   ### Motivation
   
   When token expiration, the broker requests the client to refresh the token, then the broker performs `org.apache.pulsar.broker.service.ServerCnx#doAuthentication` when the broker receives the auth response, which uses `org.apache.pulsar.broker.authentication.AuthenticationState#authenticate` to authentication, but the `org.apache.pulsar.broker.authentication.AuthenticationProviderToken.TokenAuthenticationState#authenticate` doesn't do anything, this cause a loop to refresh the token.
   
   Right now the token is only validated in the TokenAuthenticationState constructor, we need to add a check to the authentication method.
   
   ### Modifications
   
   - Add check the token expiration
   
   ### Documentation
   
   - [x] `doc-not-needed` 
   Fix
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#issuecomment-1153663315

   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#issuecomment-1153413254

   @codelipenghui @RobertIndie Thanks for your review, could you review this PR again?


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] codelipenghui commented on a diff in pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
codelipenghui commented on code in PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#discussion_r895179250


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);
+            } catch (Exception e) {
+                assertTrue(new Date().compareTo(expiredTime) > 0
+                        && e instanceof PulsarClientException.TimeoutException);

Review Comment:
   This will confuse users by getting a TimeoutException after the token gets expires.



##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);

Review Comment:
   Can we use Awaitibility instead?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on a diff in pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#discussion_r896641140


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);
+            } catch (Exception e) {
+                assertTrue(new Date().compareTo(expiredTime) > 0
+                        && e instanceof PulsarClientException.TimeoutException);
+                break;
+            }

Review Comment:
   @RobertIndie Done.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#issuecomment-1154993985

   @codelipenghui The authenticate logic has been update.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on a diff in pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#discussion_r895194682


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);

Review Comment:
   This is just to control the rate.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] RobertIndie commented on a diff in pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
RobertIndie commented on code in PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#discussion_r895263350


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);
+            } catch (Exception e) {
+                assertTrue(new Date().compareTo(expiredTime) > 0
+                        && e instanceof PulsarClientException.TimeoutException);
+                break;
+            }

Review Comment:
   Seems that we can use Awaitibility to refactor this bunch of codes.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#issuecomment-1152882769

   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] nodece commented on a diff in pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #16016:
URL: https://github.com/apache/pulsar/pull/16016#discussion_r895194590


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/TokenExpirationProduceConsumerTest.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.api;
+
+import static org.testng.Assert.assertTrue;
+import com.google.common.collect.Sets;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.authentication.AuthenticationProviderToken;
+import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminBuilder;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.common.naming.NamespaceName;
+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.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import javax.crypto.SecretKey;
+import java.util.Base64;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = "broker-api")
+@Slf4j
+public class TokenExpirationProduceConsumerTest extends TlsProducerConsumerBase {
+    private final String tenant ="my-tenant";
+    private final NamespaceName namespaceName = NamespaceName.get("my-tenant","my-ns");
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        // TLS configuration for Broker
+        internalSetUpForBroker();
+
+        // Start Broker
+        super.init();
+
+        admin = getAdmin(ADMIN_TOKEN);
+        admin.clusters().createCluster(configClusterName,
+                ClusterData.builder()
+                        .serviceUrl(brokerUrl.toString())
+                        .serviceUrlTls(brokerUrlTls.toString())
+                        .brokerServiceUrl(pulsar.getBrokerServiceUrl())
+                        .brokerServiceUrlTls(pulsar.getBrokerServiceUrlTls())
+                        .build());
+        admin.tenants().createTenant(tenant,
+                new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(configClusterName)));
+        admin.namespaces().createNamespace(namespaceName.toString());
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);
+    public static final String ADMIN_TOKEN = Jwts.builder().setSubject("admin").signWith(SECRET_KEY).compact();
+
+    public String getExpireToken(String role, Date date) {
+        return Jwts.builder().setSubject(role).signWith(SECRET_KEY)
+                .setExpiration(date).compact();
+    }
+
+    protected void internalSetUpForBroker() {
+        conf.setBrokerServicePortTls(Optional.of(0));
+        conf.setWebServicePortTls(Optional.of(0));
+        conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
+        conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
+        conf.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
+        conf.setClusterName(configClusterName);
+        conf.setAuthenticationRefreshCheckSeconds(1);
+        conf.setTlsRequireTrustedClientCertOnConnect(false);
+        conf.setTlsAllowInsecureConnection(false);
+        conf.setAuthenticationEnabled(true);
+        conf.setTransactionCoordinatorEnabled(true);
+        conf.setSuperUserRoles(Sets.newHashSet("admin"));
+        conf.setAuthenticationProviders(Sets.newHashSet(AuthenticationProviderToken.class.getName()));
+        conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
+        conf.setBrokerClientAuthenticationParameters("token:" + ADMIN_TOKEN);
+        conf.getProperties().setProperty("tokenSecretKey", "data:;base64,"
+                + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
+    }
+
+    private PulsarClient getClient(String token) throws Exception {
+        ClientBuilder clientBuilder = PulsarClient.builder()
+                .serviceUrl(pulsar.getBrokerServiceUrlTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .enableTls(true)
+                .allowTlsInsecureConnection(false)
+                .enableTlsHostnameVerification(true)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .operationTimeout(1000, TimeUnit.MILLISECONDS);
+        return clientBuilder.build();
+    }
+
+    private PulsarAdmin getAdmin(String token) throws Exception {
+        PulsarAdminBuilder clientBuilder = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddressTls())
+                .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
+                .allowTlsInsecureConnection(false)
+                .authentication(AuthenticationToken.class.getName(),"token:" +token)
+                .enableTlsHostnameVerification(true);
+        return clientBuilder.build();
+    }
+
+    @Test(timeOut = 60 * 1000)
+    public void testTokenExpirationProduceConsumer() throws Exception {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.SECOND, 20);
+        String role = "test";
+        String token = getExpireToken(role, calendar.getTime());
+        Date expiredTime = calendar.getTime();
+
+        Set<AuthAction> permissions = new HashSet<>();
+        permissions.add(AuthAction.consume);
+        permissions.add(AuthAction.produce);
+        admin.namespaces().grantPermissionOnNamespace(namespaceName.toString(), role, permissions);
+
+        @Cleanup
+        PulsarClient pulsarClient = getClient(token);
+        String topic = namespaceName + "/test-token";
+
+        @Cleanup final Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName("test-token")
+                .subscribe();
+        @Cleanup final Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .create();
+        while (true) {
+            try {
+                producer.send("heart beat".getBytes());
+                Message<byte[]> message = consumer.receive();
+                consumer.acknowledge(message);
+                TimeUnit.SECONDS.sleep(3);
+            } catch (Exception e) {
+                assertTrue(new Date().compareTo(expiredTime) > 0
+                        && e instanceof PulsarClientException.TimeoutException);

Review Comment:
   @codelipenghui  Thanks for your review.
   
   > Looks the issue is introduced by https://github.com/apache/pulsar/pull/14044?
   
   Yes.
   
   > This will confuse users by getting a TimeoutException after the token gets expires.
   
   The Producer can not send message, it will throw a send timeout exception.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] codelipenghui merged pull request #16016: [fix][broker] Fix token expiration

Posted by GitBox <gi...@apache.org>.
codelipenghui merged PR #16016:
URL: https://github.com/apache/pulsar/pull/16016


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org