dengziming commented on a change in pull request #9769:
URL: https://github.com/apache/kafka/pull/9769#discussion_r566550612



##########
File path: core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
##########
@@ -1879,6 +1879,109 @@ class KafkaApisTest {
     
assertTrue(response.topicsByError(Errors.UNKNOWN_TOPIC_OR_PARTITION).isEmpty)
   }
 
+  @Test
+  def testUnauthorizedTopicMetadataRequest(): Unit = {
+
+    // 1. Set up broker information
+    val plaintextListener = 
ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT)
+    val broker = new UpdateMetadataBroker()
+      .setId(0)
+      .setRack("rack")
+      .setEndpoints(Seq(
+        new UpdateMetadataEndpoint()
+          .setHost("broker0")
+          .setPort(9092)
+          .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id)
+          .setListener(plaintextListener.value)
+      ).asJava)
+
+    // 2. Set up authorizer
+    val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer])
+    val unauthorizedTopic = "unauthorized-topic"
+    val authorizedTopic = "authorized-topic"
+
+    val expectedActions = Seq(
+      new Action(AclOperation.DESCRIBE, new 
ResourcePattern(ResourceType.TOPIC, unauthorizedTopic, PatternType.LITERAL), 1, 
true, true),
+      new Action(AclOperation.DESCRIBE, new 
ResourcePattern(ResourceType.TOPIC, authorizedTopic, PatternType.LITERAL), 1, 
true, true)
+    )
+
+    val expectedAuthorizeResult = Seq(AuthorizationResult.DENIED, 
AuthorizationResult.ALLOWED).asJava
+
+    EasyMock.expect(authorizer.authorize(anyObject[RequestContext], 
EasyMock.eq(expectedActions.asJava)))
+      .andReturn(expectedAuthorizeResult)
+      .times(2)
+
+    // 3. Set up MetadataCache
+    val authorizedTopicId = Uuid.randomUuid();
+    val unauthorizedTopicId = Uuid.randomUuid();
+
+    val topicIds = new util.HashMap[String, Uuid]()
+    topicIds.put(authorizedTopic, authorizedTopicId)
+    topicIds.put(unauthorizedTopic, unauthorizedTopicId)
+
+    def createDummyPartitionStates(topic: String) = {
+      new UpdateMetadataPartitionState()
+        .setTopicName(topic)
+        .setPartitionIndex(0)
+        .setControllerEpoch(1)
+        .setLeader(0)
+        .setLeaderEpoch(1)
+        .setReplicas(Collections.singletonList(0))
+        .setZkVersion(0)
+        .setIsr(Collections.singletonList(0))
+    }
+
+    // Send UpdateMetadataReq to update MetadataCache
+    val partitionStates = Seq(unauthorizedTopic, 
authorizedTopic).map(createDummyPartitionStates)
+
+    val updateMetadataRequest = new 
UpdateMetadataRequest.Builder(ApiKeys.UPDATE_METADATA.latestVersion, 0,
+      0, 0, partitionStates.asJava, Seq(broker).asJava, topicIds).build()
+    metadataCache.updateMetadata(correlationId = 0, updateMetadataRequest)
+
+    // 4. Send TopicMetadataReq using topicId
+    val capturedMetadataByTopicIdResp = expectNoThrottling()
+    EasyMock.replay(clientRequestQuotaManager, requestChannel, authorizer)
+
+    val metadataReqByTopicId = new 
MetadataRequest.Builder(util.Arrays.asList(authorizedTopicId, 
unauthorizedTopicId)).build()
+    createKafkaApis(authorizer = 
Some(authorizer)).handleTopicMetadataRequest(buildRequest(metadataReqByTopicId, 
plaintextListener))
+    val metadataByTopicIdResp = readResponse(metadataReqByTopicId, 
capturedMetadataByTopicIdResp).asInstanceOf[MetadataResponse]
+
+    val metadataByTopicId = 
metadataByTopicIdResp.data().topics().asScala.groupBy(_.topicId()).view.mapValues(_.head).toMap

Review comment:
       Thank you, 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.

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


Reply via email to