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

jianghaiting pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new b02d52ca8c8 [feat][broker] Prevent auto-creation of topics using 
legacy cluster-based naming scheme (#23620)
b02d52ca8c8 is described below

commit b02d52ca8c8afd62681c0e243d16d8958abb5380
Author: zjxxzjwang <[email protected]>
AuthorDate: Thu Jan 2 15:16:24 2025 +0800

    [feat][broker] Prevent auto-creation of topics using legacy cluster-based 
naming scheme (#23620)
    
    Co-authored-by: zjxxzjwang <[email protected]>
---
 conf/broker.conf                                      |  4 ++++
 conf/standalone.conf                                  |  4 ++++
 .../apache/pulsar/broker/ServiceConfiguration.java    |  5 +++++
 .../apache/pulsar/broker/service/BrokerService.java   |  7 +++++++
 .../service/BrokerServiceAutoTopicCreationTest.java   | 19 +++++++++++++++++++
 5 files changed, 39 insertions(+)

diff --git a/conf/broker.conf b/conf/broker.conf
index af335c14153..f68306ec7b4 100644
--- a/conf/broker.conf
+++ b/conf/broker.conf
@@ -198,6 +198,10 @@ allowAutoTopicCreation=true
 # The type of topic that is allowed to be automatically 
created.(partitioned/non-partitioned)
 allowAutoTopicCreationType=non-partitioned
 
+# If 'allowAutoTopicCreation' is true and the name of the topic contains 
'cluster',
+# the topic cannot be automatically created.
+allowAutoTopicCreationWithLegacyNamingScheme=true
+
 # Enable subscription auto creation if new consumer connected (disable auto 
creation with value false)
 allowAutoSubscriptionCreation=true
 
diff --git a/conf/standalone.conf b/conf/standalone.conf
index 90cf3b57ff9..2036556da43 100644
--- a/conf/standalone.conf
+++ b/conf/standalone.conf
@@ -1181,6 +1181,10 @@ allowAutoTopicCreation=true
 # The type of topic that is allowed to be automatically 
created.(partitioned/non-partitioned)
 allowAutoTopicCreationType=non-partitioned
 
+# If 'allowAutoTopicCreation' is true and the name of the topic contains 
'cluster',
+# the topic cannot be automatically created.
+allowAutoTopicCreationWithLegacyNamingScheme=true
+
 # Enable subscription auto creation if new consumer connected (disable auto 
creation with value false)
 allowAutoSubscriptionCreation=true
 
diff --git 
a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
 
b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
index 0f7ae00713d..0b6f0e9418c 100644
--- 
a/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
+++ 
b/pulsar-broker-common/src/main/java/org/apache/pulsar/broker/ServiceConfiguration.java
@@ -2154,6 +2154,11 @@ public class ServiceConfiguration implements 
PulsarConfiguration {
             doc = "The type of topic that is allowed to be automatically 
created.(partitioned/non-partitioned)"
     )
     private TopicType allowAutoTopicCreationType = TopicType.NON_PARTITIONED;
+    @FieldContext(category = CATEGORY_SERVER, dynamic = true,
+            doc = "If 'allowAutoTopicCreation' is true and the name of the 
topic contains 'cluster',"
+                    + "the topic cannot be automatically created."
+    )
+    private boolean allowAutoTopicCreationWithLegacyNamingScheme = true;
     @FieldContext(
         category = CATEGORY_STORAGE_ML,
         dynamic = true,
diff --git 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
index c79d839097e..b8102488f49 100644
--- 
a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
+++ 
b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
@@ -3536,6 +3536,13 @@ public class BrokerService implements Closeable {
             return CompletableFuture.completedFuture(true);
         }
 
+        //If 'allowAutoTopicCreation' is true, and the name of the topic 
contains 'cluster',
+        //the topic cannot be automatically created.
+        if 
(!pulsar.getConfiguration().isAllowAutoTopicCreationWithLegacyNamingScheme()
+                && StringUtils.isNotBlank(topicName.getCluster())) {
+            return CompletableFuture.completedFuture(false);
+        }
+
         final boolean allowed;
         AutoTopicCreationOverride autoTopicCreationOverride = 
getAutoTopicCreationOverride(topicName, policies);
         if (autoTopicCreationOverride != null) {
diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceAutoTopicCreationTest.java
 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceAutoTopicCreationTest.java
index 3e735ee4c85..59eb8ab74c8 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceAutoTopicCreationTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceAutoTopicCreationTest.java
@@ -587,4 +587,23 @@ public class BrokerServiceAutoTopicCreationTest extends 
BrokerTestBase{
 
     }
 
+    @Test
+    public void testAutoPartitionedTopicNameWithClusterName() throws Exception 
{
+        pulsar.getConfiguration().setAllowAutoTopicCreation(true);
+        
pulsar.getConfiguration().setAllowAutoTopicCreationType(TopicType.PARTITIONED);
+        pulsar.getConfiguration().setDefaultNumPartitions(3);
+        
+        final String topicString = "persistent://prop/ns-abc/testTopic/1";
+        // When allowAutoTopicCreationWithLegacyNamingScheme as the default 
value is false,
+        // four-paragraph topic cannot be created.
+        
pulsar.getConfiguration().setAllowAutoTopicCreationWithLegacyNamingScheme(false);
+        Assert.assertThrows(PulsarClientException.NotFoundException.class,
+                () -> pulsarClient.newProducer().topic(topicString).create());
+
+        
pulsar.getConfiguration().setAllowAutoTopicCreationWithLegacyNamingScheme(true);
+        Producer producer = 
pulsarClient.newProducer().topic(topicString).create();
+        Assert.assertEquals(producer.getTopic(), topicString);
+        producer.close();
+    }
+
 }

Reply via email to