Skip to content
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-18817: ShareGroupHeartbeat and ShareGroupDescribe API must check topic describe #19083

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions core/src/main/scala/kafka/server/KafkaApis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ class KafkaApis(val requestChannel: RequestChannel,
}

// Clients are not allowed to see topics that are not authorized for Describe.
if (!authorizer.isEmpty) {
if (authorizer.isDefined) {
val topicsToCheck = response.groups.stream()
.flatMap(group => group.members.stream)
.flatMap(member => util.stream.Stream.of(member.assignment, member.targetAssignment))
Expand Down Expand Up @@ -2835,9 +2835,24 @@ class KafkaApis(val requestChannel: RequestChannel,
requestHelper.sendMaybeThrottle(request, shareGroupHeartbeatRequest.getErrorResponse(Errors.GROUP_AUTHORIZATION_FAILED.exception))
CompletableFuture.completedFuture[Unit](())
} else {
if (shareGroupHeartbeatRequest.data.subscribedTopicNames != null &&
!shareGroupHeartbeatRequest.data.subscribedTopicNames.isEmpty) {
// Check the authorization if the subscribed topic names are provided.
// Clients are not allowed to see topics that are not authorized for Describe.
val subscribedTopicSet = shareGroupHeartbeatRequest.data.subscribedTopicNames.asScala.toSet
val authorizedTopics = authHelper.filterByAuthorized(request.context, DESCRIBE, TOPIC,
subscribedTopicSet)(identity)
if (authorizedTopics.size < subscribedTopicSet.size) {
val responseData = new ShareGroupHeartbeatResponseData()
.setErrorCode(Errors.TOPIC_AUTHORIZATION_FAILED.code)
requestHelper.sendMaybeThrottle(request, new ShareGroupHeartbeatResponse(responseData))
return CompletableFuture.completedFuture[Unit](())
}
}

groupCoordinator.shareGroupHeartbeat(
request.context,
shareGroupHeartbeatRequest.data,
shareGroupHeartbeatRequest.data
).handle[Unit] { (response, exception) =>

if (exception != null) {
Expand Down Expand Up @@ -2897,6 +2912,34 @@ class KafkaApis(val requestChannel: RequestChannel,
response.groups.addAll(results)
}

// Clients are not allowed to see topics that are not authorized for Describe.
if (authorizer.isDefined) {
val topicsToCheck = response.groups.stream()
.flatMap(group => group.members.stream)
.flatMap(member => member.assignment.topicPartitions.stream)
.map(topicPartition => topicPartition.topicName)
.collect(Collectors.toSet[String])
.asScala
val authorizedTopics = authHelper.filterByAuthorized(request.context, DESCRIBE, TOPIC,
topicsToCheck)(identity)
val updatedGroups = response.groups.stream().map { group =>
val hasUnauthorizedTopic = group.members.stream()
.flatMap(member => member.assignment.topicPartitions.stream)
.anyMatch(tp => !authorizedTopics.contains(tp.topicName))

if (hasUnauthorizedTopic) {
new ShareGroupDescribeResponseData.DescribedGroup()
.setGroupId(group.groupId)
.setErrorCode(Errors.TOPIC_AUTHORIZATION_FAILED.code)
.setErrorMessage("The group has described topic(s) that the client is not authorized to describe.")
.setMembers(List.empty.asJava)
} else {
group
}
}.collect(Collectors.toList[ShareGroupDescribeResponseData.DescribedGroup])
response.setGroups(updatedGroups)
}

requestHelper.sendMaybeThrottle(request, new ShareGroupDescribeResponse(response))
}
}
Expand Down
141 changes: 136 additions & 5 deletions core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@ class KafkaApisTest extends Logging {
checkInvalidPartition(-1)
checkInvalidPartition(1) // topic has only one partition
}

@Test
def requiredAclsNotPresentWriteTxnMarkersThrowsAuthorizationException(): Unit = {
val topicPartition = new TopicPartition("t", 0)
Expand Down Expand Up @@ -10717,7 +10717,7 @@ class KafkaApisTest extends Logging {
}

@Test
def testShareGroupHeartbeatRequestAuthorizationFailed(): Unit = {
def testShareGroupHeartbeatRequestGroupAuthorizationFailed(): Unit = {
val shareGroupHeartbeatRequest = new ShareGroupHeartbeatRequestData().setGroupId("group")

val requestChannelRequest = buildRequest(new ShareGroupHeartbeatRequest.Builder(shareGroupHeartbeatRequest, true).build())
Expand All @@ -10736,6 +10736,46 @@ class KafkaApisTest extends Logging {
assertEquals(Errors.GROUP_AUTHORIZATION_FAILED.code, response.data.errorCode)
}

@Test
def testShareGroupHeartbeatRequestTopicAuthorizationFailed(): Unit = {
metadataCache = MetadataCache.kRaftMetadataCache(brokerId, () => KRaftVersion.KRAFT_VERSION_0)
val groupId = "group"
val fooTopicName = "foo"
val barTopicName = "bar"
val zarTopicName = "zar"

val shareGroupHeartbeatRequest = new ShareGroupHeartbeatRequestData()
.setGroupId(groupId)
.setSubscribedTopicNames(List(fooTopicName, barTopicName, zarTopicName).asJava)

val requestChannelRequest = buildRequest(new ShareGroupHeartbeatRequest.Builder(shareGroupHeartbeatRequest).build())

val authorizer: Authorizer = mock(classOf[Authorizer])
val acls = Map(
groupId -> AuthorizationResult.ALLOWED,
fooTopicName -> AuthorizationResult.ALLOWED,
barTopicName -> AuthorizationResult.DENIED,
)
when(authorizer.authorize(
any[RequestContext],
any[util.List[Action]]
)).thenAnswer { invocation =>
val actions = invocation.getArgument(1, classOf[util.List[Action]])
actions.asScala.map { action =>
acls.getOrElse(action.resourcePattern.name, AuthorizationResult.DENIED)
}.asJava
}

kafkaApis = createKafkaApis(
overrideProperties = Map(ShareGroupConfig.SHARE_GROUP_ENABLE_CONFIG -> "true"),
authorizer = Some(authorizer),
)
kafkaApis.handle(requestChannelRequest, RequestLocal.noCaching)

val response = verifyNoThrottling[ShareGroupHeartbeatResponse](requestChannelRequest)
assertEquals(Errors.TOPIC_AUTHORIZATION_FAILED.code, response.data.errorCode)
}

@Test
def testShareGroupHeartbeatRequestFutureFailed(): Unit = {
val shareGroupHeartbeatRequest = new ShareGroupHeartbeatRequestData().setGroupId("group")
Expand All @@ -10760,10 +10800,34 @@ class KafkaApisTest extends Logging {

@Test
def testShareGroupDescribeSuccess(): Unit = {
val groupIds = List("share-group-id-0", "share-group-id-1").asJava
val fooTopicName = "foo"
val barTopicName = "bar"

val groupIds = List("share-group-id-0", "share-group-id-1", "share-group_id-2").asJava

val member0 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member0")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(fooTopicName)).asJava))

val member1 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member1")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(fooTopicName),
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(barTopicName)).asJava))

val member2 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member2")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(barTopicName)).asJava))

val describedGroups: util.List[ShareGroupDescribeResponseData.DescribedGroup] = List(
new ShareGroupDescribeResponseData.DescribedGroup().setGroupId(groupIds.get(0)),
new ShareGroupDescribeResponseData.DescribedGroup().setGroupId(groupIds.get(1))
new ShareGroupDescribeResponseData.DescribedGroup().setGroupId(groupIds.get(0)).setMembers(List(member0).asJava),
new ShareGroupDescribeResponseData.DescribedGroup().setGroupId(groupIds.get(1)).setMembers(List(member1).asJava),
new ShareGroupDescribeResponseData.DescribedGroup().setGroupId(groupIds.get(2)).setMembers(List(member2).asJava)
).asJava
getShareGroupDescribeResponse(groupIds, Map(ShareGroupConfig.SHARE_GROUP_ENABLE_CONFIG -> "true")
, true, null, describedGroups)
Expand Down Expand Up @@ -10816,6 +10880,73 @@ class KafkaApisTest extends Logging {
assertEquals(Errors.NONE.code(), response.data.groups.get(1).errorCode())
}

@Test
def testShareGroupDescribeFilterUnauthorizedTopics(): Unit = {
val fooTopicName = "foo"
val barTopicName = "bar"
val errorMessage = "The group has described topic(s) that the client is not authorized to describe."

val groupIds = List("share-group-id-0", "share-group-id-1", "share-group_id-2").asJava

val authorizer: Authorizer = mock(classOf[Authorizer])
val acls = Map(
groupIds.get(0) -> AuthorizationResult.ALLOWED,
groupIds.get(1) -> AuthorizationResult.ALLOWED,
groupIds.get(2) -> AuthorizationResult.ALLOWED,
fooTopicName -> AuthorizationResult.ALLOWED,
barTopicName -> AuthorizationResult.DENIED,
)
when(authorizer.authorize(
any[RequestContext],
any[util.List[Action]]
)).thenAnswer { invocation =>
val actions = invocation.getArgument(1, classOf[util.List[Action]])
actions.asScala.map { action =>
acls.getOrElse(action.resourcePattern.name, AuthorizationResult.DENIED)
}.asJava
}
val member0 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member0")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(fooTopicName)).asJava))

val member1 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member1")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(fooTopicName),
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(barTopicName)).asJava))

val member2 = new ShareGroupDescribeResponseData.Member()
.setMemberId("member2")
.setAssignment(new ShareGroupDescribeResponseData.Assignment()
.setTopicPartitions(List(
new ShareGroupDescribeResponseData.TopicPartitions().setTopicName(barTopicName)).asJava))

val describedGroups: util.List[ShareGroupDescribeResponseData.DescribedGroup] = List(
new ShareGroupDescribeResponseData.DescribedGroup()
.setGroupId(groupIds.get(0))
.setMembers(List(member0).asJava),
new ShareGroupDescribeResponseData.DescribedGroup()
.setGroupId(groupIds.get(1))
.setMembers(List(member1).asJava),
new ShareGroupDescribeResponseData.DescribedGroup()
.setGroupId(groupIds.get(2))
.setMembers(List(member2).asJava)).asJava

val response = getShareGroupDescribeResponse(groupIds, Map(ShareGroupConfig.SHARE_GROUP_ENABLE_CONFIG -> "true")
, false, authorizer, describedGroups)

assertNotNull(response.data)
assertEquals(3, response.data.groups.size)
assertEquals(Errors.NONE.code(), response.data.groups.get(0).errorCode())
assertEquals(Errors.TOPIC_AUTHORIZATION_FAILED.code(), response.data.groups.get(1).errorCode())
assertEquals(Errors.TOPIC_AUTHORIZATION_FAILED.code(), response.data.groups.get(2).errorCode())
assertEquals(errorMessage, response.data.groups.get(1).errorMessage())
assertEquals(errorMessage, response.data.groups.get(2).errorMessage())
}

@Test
def testReadShareGroupStateSuccess(): Unit = {
val topicId = Uuid.randomUuid();
Expand Down