-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-12394: Return TOPIC_AUTHORIZATION_FAILED in delete topic response if no describe permission
#10223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KAFKA-12394: Return TOPIC_AUTHORIZATION_FAILED in delete topic response if no describe permission
#10223
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1874,7 +1874,7 @@ class KafkaApis(val requestChannel: RequestChannel, | |
| if (topic.name() != null && topic.topicId() != Uuid.ZERO_UUID) | ||
| throw new InvalidRequestException("Topic name and topic ID can not both be specified.") | ||
| val name = if (topic.topicId() == Uuid.ZERO_UUID) topic.name() | ||
| else zkSupport.controller.controllerContext.topicNames.getOrElse(topic.topicId(), null) | ||
| else zkSupport.controller.controllerContext.topicName(topic.topicId).orNull | ||
| results.add(new DeletableTopicResult() | ||
| .setName(name) | ||
| .setTopicId(topic.topicId())) | ||
|
|
@@ -1884,20 +1884,24 @@ class KafkaApis(val requestChannel: RequestChannel, | |
| val authorizedDeleteTopics = authHelper.filterByAuthorized(request.context, DELETE, TOPIC, | ||
| results.asScala.filter(result => result.name() != null))(_.name) | ||
| results.forEach { topic => | ||
| val unresolvedTopicId = !(topic.topicId() == Uuid.ZERO_UUID) && topic.name() == null | ||
| if (!config.usesTopicId && topicIdsFromRequest.contains(topic.topicId)) { | ||
| topic.setErrorCode(Errors.UNSUPPORTED_VERSION.code) | ||
| topic.setErrorMessage("Topic IDs are not supported on the server.") | ||
| } else if (unresolvedTopicId) | ||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_ID.code) | ||
| else if (topicIdsFromRequest.contains(topic.topicId) && !authorizedDescribeTopics(topic.name)) | ||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_ID.code) | ||
| else if (!authorizedDeleteTopics.contains(topic.name)) | ||
| topic.setErrorCode(Errors.TOPIC_AUTHORIZATION_FAILED.code) | ||
| else if (!metadataCache.contains(topic.name)) | ||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code) | ||
| else | ||
| toDelete += topic.name | ||
| val unresolvedTopicId = topic.topicId() != Uuid.ZERO_UUID && topic.name() == null | ||
| if (!config.usesTopicId && topicIdsFromRequest.contains(topic.topicId)) { | ||
| topic.setErrorCode(Errors.UNSUPPORTED_VERSION.code) | ||
| topic.setErrorMessage("Topic IDs are not supported on the server.") | ||
| } else if (unresolvedTopicId) { | ||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_ID.code) | ||
| } else if (topicIdsFromRequest.contains(topic.topicId) && !authorizedDescribeTopics(topic.name)) { | ||
|
hachikuji marked this conversation as resolved.
Outdated
|
||
| // Because the client does not have Describe permission, the name should | ||
| // not be returned in the response. | ||
| topic.setName(null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed that the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. I agree using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have one dependence here which makes this a little more work than I wanted to do here: https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/server/KafkaApis.scala#L1909. I filed a separate JIRA so that we don't forget about it: https://issues.apache.org/jira/browse/KAFKA-12395. |
||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_ID.code) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prepared to discuss this error code on #10184 (comment) :) return
|
||
| } else if (!authorizedDeleteTopics.contains(topic.name)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to #10184 (comment), should it handle the case
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess there are really two sub-cases. Here's how they are currently handled: This seems defensible to me. The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That makes sense to me. We have to make sure this rule is applied to #10184 :) |
||
| topic.setErrorCode(Errors.TOPIC_AUTHORIZATION_FAILED.code) | ||
| } else if (!metadataCache.contains(topic.name)) { | ||
| topic.setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code) | ||
| } else { | ||
| toDelete += topic.name | ||
| } | ||
| } | ||
| // If no authorized topics return immediately | ||
| if (toDelete.isEmpty) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import java.util.{Collections, Optional, Properties, Random} | |
|
|
||
| import kafka.api.{ApiVersion, KAFKA_0_10_2_IV0, KAFKA_2_2_IV1, LeaderAndIsr} | ||
| import kafka.cluster.{Broker, Partition} | ||
| import kafka.controller.KafkaController | ||
| import kafka.controller.{ControllerContext, KafkaController} | ||
| import kafka.coordinator.group.GroupCoordinatorConcurrencyTest.{JoinGroupCallback, SyncGroupCallback} | ||
| import kafka.coordinator.group._ | ||
| import kafka.coordinator.transaction.{InitProducerIdResult, TransactionCoordinator} | ||
|
|
@@ -75,6 +75,8 @@ import org.easymock.EasyMock._ | |
| import org.easymock.{Capture, EasyMock, IAnswer} | ||
| import org.junit.jupiter.api.Assertions._ | ||
| import org.junit.jupiter.api.{AfterEach, Test} | ||
| import org.junit.jupiter.params.ParameterizedTest | ||
| import org.junit.jupiter.params.provider.ValueSource | ||
| import org.mockito.{ArgumentMatchers, Mockito} | ||
|
|
||
| import scala.annotation.nowarn | ||
|
|
@@ -3479,6 +3481,161 @@ class KafkaApisTest { | |
| assertEquals(List(mkTopicData(topic = "foo", Seq(1, 2))), fooState.topics.asScala.toList) | ||
| } | ||
|
|
||
| @Test | ||
| def testDeleteTopicsByIdAuthorization(): Unit = { | ||
| val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer]) | ||
| val controllerContext: ControllerContext = EasyMock.mock(classOf[ControllerContext]) | ||
|
|
||
| EasyMock.expect(clientControllerQuotaManager.newQuotaFor( | ||
| EasyMock.anyObject(classOf[RequestChannel.Request]), | ||
| EasyMock.anyShort() | ||
| )).andReturn(UnboundedControllerMutationQuota) | ||
| EasyMock.expect(controller.isActive).andReturn(true) | ||
| EasyMock.expect(controller.controllerContext).andStubReturn(controllerContext) | ||
|
|
||
| // Try to delete three topics: | ||
| // 1. One without describe permission | ||
| // 2. One without delete permission | ||
| // 3. One which is authorized, but doesn't exist | ||
|
|
||
| expectTopicAuthorization(authorizer, AclOperation.DESCRIBE, Map( | ||
| "foo" -> AuthorizationResult.DENIED, | ||
| "bar" -> AuthorizationResult.ALLOWED | ||
| )) | ||
|
|
||
| expectTopicAuthorization(authorizer, AclOperation.DELETE, Map( | ||
| "foo" -> AuthorizationResult.DENIED, | ||
| "bar" -> AuthorizationResult.DENIED | ||
| )) | ||
|
|
||
| val topicIdsMap = Map( | ||
| Uuid.randomUuid() -> Some("foo"), | ||
| Uuid.randomUuid() -> Some("bar"), | ||
| Uuid.randomUuid() -> None | ||
| ) | ||
|
|
||
| topicIdsMap.foreach { case (topicId, topicNameOpt) => | ||
| EasyMock.expect(controllerContext.topicName(topicId)).andReturn(topicNameOpt) | ||
| } | ||
|
|
||
| val topicDatas = topicIdsMap.keys.map { topicId => | ||
| new DeleteTopicsRequestData.DeleteTopicState().setTopicId(topicId) | ||
| }.toList | ||
| val deleteRequest = new DeleteTopicsRequest.Builder(new DeleteTopicsRequestData() | ||
| .setTopics(topicDatas.asJava)) | ||
| .build(ApiKeys.DELETE_TOPICS.latestVersion) | ||
|
|
||
| val request = buildRequest(deleteRequest) | ||
| val capturedResponse = expectNoThrottling(request) | ||
|
|
||
| EasyMock.replay(replicaManager, clientRequestQuotaManager, clientControllerQuotaManager, | ||
| requestChannel, txnCoordinator, controller, controllerContext, authorizer) | ||
| createKafkaApis(authorizer = Some(authorizer)).handleDeleteTopicsRequest(request) | ||
|
|
||
| val deleteResponse = capturedResponse.getValue.asInstanceOf[DeleteTopicsResponse] | ||
|
|
||
| topicIdsMap.foreach { case (topicId, nameOpt) => | ||
| val response = deleteResponse.data.responses.asScala.find(_.topicId == topicId).get | ||
| nameOpt match { | ||
| case Some("foo") => | ||
| assertNull(response.name) | ||
| assertEquals(Errors.UNKNOWN_TOPIC_ID, Errors.forCode(response.errorCode)) | ||
| case Some("bar") => | ||
| assertEquals("bar", response.name) | ||
| assertEquals(Errors.TOPIC_AUTHORIZATION_FAILED, Errors.forCode(response.errorCode)) | ||
| case None => | ||
| assertNull(response.name) | ||
| assertEquals(Errors.UNKNOWN_TOPIC_ID, Errors.forCode(response.errorCode)) | ||
| case _ => | ||
| fail("Unexpected topic id/name mapping") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(booleans = Array(true, false)) | ||
| def testDeleteTopicsByNameAuthorization(usePrimitiveTopicNameArray: Boolean): Unit = { | ||
| val authorizer: Authorizer = EasyMock.niceMock(classOf[Authorizer]) | ||
|
|
||
| EasyMock.expect(clientControllerQuotaManager.newQuotaFor( | ||
| EasyMock.anyObject(classOf[RequestChannel.Request]), | ||
| EasyMock.anyShort() | ||
| )).andReturn(UnboundedControllerMutationQuota) | ||
| EasyMock.expect(controller.isActive).andReturn(true) | ||
|
|
||
| // Try to delete three topics: | ||
| // 1. One without describe permission | ||
| // 2. One without delete permission | ||
| // 3. One which is authorized, but doesn't exist | ||
|
|
||
| expectTopicAuthorization(authorizer, AclOperation.DESCRIBE, Map( | ||
| "foo" -> AuthorizationResult.DENIED, | ||
| "bar" -> AuthorizationResult.ALLOWED, | ||
| "baz" -> AuthorizationResult.ALLOWED | ||
| )) | ||
|
|
||
| expectTopicAuthorization(authorizer, AclOperation.DELETE, Map( | ||
| "foo" -> AuthorizationResult.DENIED, | ||
| "bar" -> AuthorizationResult.DENIED, | ||
| "baz" -> AuthorizationResult.ALLOWED | ||
| )) | ||
|
|
||
| val deleteRequest = if (usePrimitiveTopicNameArray) { | ||
| new DeleteTopicsRequest.Builder(new DeleteTopicsRequestData() | ||
| .setTopicNames(List("foo", "bar", "baz").asJava)) | ||
| .build(5.toShort) | ||
| } else { | ||
| val topicDatas = List( | ||
| new DeleteTopicsRequestData.DeleteTopicState().setName("foo"), | ||
| new DeleteTopicsRequestData.DeleteTopicState().setName("bar"), | ||
| new DeleteTopicsRequestData.DeleteTopicState().setName("baz") | ||
| ) | ||
| new DeleteTopicsRequest.Builder(new DeleteTopicsRequestData() | ||
| .setTopics(topicDatas.asJava)) | ||
| .build(ApiKeys.DELETE_TOPICS.latestVersion) | ||
| } | ||
|
|
||
| val request = buildRequest(deleteRequest) | ||
| val capturedResponse = expectNoThrottling(request) | ||
|
|
||
| EasyMock.replay(replicaManager, clientRequestQuotaManager, clientControllerQuotaManager, | ||
| requestChannel, txnCoordinator, controller, authorizer) | ||
| createKafkaApis(authorizer = Some(authorizer)).handleDeleteTopicsRequest(request) | ||
|
|
||
| val deleteResponse = capturedResponse.getValue.asInstanceOf[DeleteTopicsResponse] | ||
|
|
||
| def lookupErrorCode(topic: String): Option[Errors] = { | ||
| Option(deleteResponse.data.responses().find(topic)) | ||
| .map(result => Errors.forCode(result.errorCode)) | ||
| } | ||
|
|
||
| assertEquals(Some(Errors.TOPIC_AUTHORIZATION_FAILED), lookupErrorCode("foo")) | ||
| assertEquals(Some(Errors.TOPIC_AUTHORIZATION_FAILED), lookupErrorCode("bar")) | ||
| assertEquals(Some(Errors.UNKNOWN_TOPIC_OR_PARTITION), lookupErrorCode("baz")) | ||
| } | ||
|
|
||
| def expectTopicAuthorization( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we simplify this method using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at doing this, but the need to preserve order in the result makes it just as cumbersome. The capture also provides some type safety and avoids the need for casting from the argument list. |
||
| authorizer: Authorizer, | ||
| aclOperation: AclOperation, | ||
| topicResults: Map[String, AuthorizationResult] | ||
| ): Unit = { | ||
| val expectedActions = topicResults.keys.map { topic => | ||
| val pattern = new ResourcePattern(ResourceType.TOPIC, topic, PatternType.LITERAL) | ||
| topic -> new Action(aclOperation, pattern, 1, true, true) | ||
| }.toMap | ||
|
|
||
| val actionsCapture: Capture[util.List[Action]] = EasyMock.newCapture() | ||
| EasyMock.expect(authorizer.authorize(anyObject[RequestContext], EasyMock.capture(actionsCapture))) | ||
| .andAnswer(() => { | ||
| actionsCapture.getValue.asScala.map { action => | ||
| val topic = action.resourcePattern.name | ||
| assertEquals(expectedActions(topic), action) | ||
| topicResults(topic) | ||
| }.asJava | ||
| }) | ||
| .once() | ||
| } | ||
|
|
||
| private def createMockRequest(): RequestChannel.Request = { | ||
| val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request]) | ||
| val requestHeader: RequestHeader = EasyMock.createNiceMock(classOf[RequestHeader]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.