codelipenghui commented on a change in pull request #13355:
URL: https://github.com/apache/pulsar/pull/13355#discussion_r801645253



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
##########
@@ -1245,9 +1247,41 @@ protected void handleProducer(final CommandProducer 
cmdProducer) {
 
                     schemaVersionFuture.thenAccept(schemaVersion -> {
                         
topic.checkIfTransactionBufferRecoverCompletely(isTxnEnabled).thenAccept(future 
-> {
-                            buildProducerAndAddTopic(topic, producerId, 
producerName, requestId, isEncrypted,
+                            CompletableFuture<Subscription> initSubFuture = 
new CompletableFuture<>();
+                            if (!Strings.isNullOrEmpty(initialSubscriptionName)
+                                    && 
!topic.getSubscriptions().containsKey(initialSubscriptionName)
+                                    && topic.isPersistent()) {
+                                initSubFuture = isTopicOperationAllowed(
+                                        topicName, TopicOperation.SUBSCRIBE
+                                ).thenCompose(canSubscribe -> {
+                                    if (!canSubscribe) {
+                                        return FutureUtil.failedFuture(
+                                                new 
BrokerServiceException.ProducerInitSubAuthorizationException(
+                                                        "Client is not 
authorized to subscribe."));
+                                    }
+                                    return 
topic.createSubscription(initialSubscriptionName, InitialPosition.Earliest,
+                                            false);
+                                });
+                            } else {
+                                initSubFuture.complete(null);
+                            }

Review comment:
       can we advance this check? If the producer doesn't have permission to 
create the init sub, we should also avoid creating topic, otherwise, the 
producer got an exception but the topic has been created, only the init sub 
failed

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java
##########
@@ -1245,9 +1247,41 @@ protected void handleProducer(final CommandProducer 
cmdProducer) {
 
                     schemaVersionFuture.thenAccept(schemaVersion -> {
                         
topic.checkIfTransactionBufferRecoverCompletely(isTxnEnabled).thenAccept(future 
-> {
-                            buildProducerAndAddTopic(topic, producerId, 
producerName, requestId, isEncrypted,
+                            CompletableFuture<Subscription> initSubFuture = 
new CompletableFuture<>();
+                            if (!Strings.isNullOrEmpty(initialSubscriptionName)
+                                    && 
!topic.getSubscriptions().containsKey(initialSubscriptionName)
+                                    && topic.isPersistent()) {
+                                initSubFuture = isTopicOperationAllowed(
+                                        topicName, TopicOperation.SUBSCRIBE
+                                ).thenCompose(canSubscribe -> {
+                                    if (!canSubscribe) {
+                                        return FutureUtil.failedFuture(
+                                                new 
BrokerServiceException.ProducerInitSubAuthorizationException(
+                                                        "Client is not 
authorized to subscribe."));
+                                    }
+                                    return 
topic.createSubscription(initialSubscriptionName, InitialPosition.Earliest,
+                                            false);
+                                });
+                            } else {
+                                initSubFuture.complete(null);
+                            }

Review comment:
       Maybe we can change as followings:
   
   ```java
   CompletableFuture<Boolean> isAuthorizedFuture;
   if (!Strings.isNullOrEmpty(initialSubscriptionName)
                                       && 
!topic.getSubscriptions().containsKey(initialSubscriptionName)
                                       && topic.isPersistent()) {
           isAuthorizedFuture = isTopicOperationAllowed(
               topicName, TopicOperation.PRODUCE
           ).combine(isTopicOperationAllowed(
                                           topicName, TopicOperation.SUBSCRIBE
                                   ));
   } else {
           isAuthorizedFuture =  isTopicOperationAllowed(
               topicName, TopicOperation.PRODUCE
           );
   }
   ```




-- 
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]


Reply via email to