[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2021-01-14 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r557461217



##
File path: core/src/main/scala/kafka/server/RequestHandlerUtils.scala
##
@@ -0,0 +1,203 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.server
+
+import kafka.cluster.Partition
+import kafka.coordinator.group.GroupCoordinator
+import kafka.coordinator.transaction.TransactionCoordinator
+
+import java.lang.{Byte => JByte}
+import java.util.Collections
+import kafka.network.RequestChannel
+import kafka.security.authorizer.AclEntry
+import kafka.server.QuotaFactory.QuotaManagers
+import kafka.utils.Logging
+import org.apache.kafka.common.acl.AclOperation
+import org.apache.kafka.common.errors.ClusterAuthorizationException
+import org.apache.kafka.common.internals.Topic
+import org.apache.kafka.common.network.Send
+import org.apache.kafka.common.requests.{AbstractRequest, AbstractResponse, 
RequestContext}
+import org.apache.kafka.common.resource.Resource.CLUSTER_NAME
+import org.apache.kafka.common.resource.ResourceType.CLUSTER
+import org.apache.kafka.common.resource.{PatternType, Resource, 
ResourcePattern, ResourceType}
+import org.apache.kafka.common.utils.{Time, Utils}
+import org.apache.kafka.server.authorizer.{Action, AuthorizationResult, 
Authorizer}
+
+import scala.jdk.CollectionConverters._
+
+/**
+ * Helper methods for request handlers
+ */
+object RequestHandlerUtils {
+  def onLeadershipChange(groupCoordinator: GroupCoordinator,
+ txnCoordinator: TransactionCoordinator,
+ updatedLeaders: Iterable[Partition],
+ updatedFollowers: Iterable[Partition]): Unit = {
+// for each new leader or follower, call coordinator to handle consumer 
group migration.
+// this callback is invoked under the replica state change lock to ensure 
proper order of
+// leadership changes
+updatedLeaders.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onElection(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onElection(partition.partitionId, 
partition.getLeaderEpoch)
+}
+
+updatedFollowers.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onResignation(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onResignation(partition.partitionId, 
Some(partition.getLeaderEpoch))
+}
+  }
+}
+
+class AuthHelper(val requestChannel: RequestChannel,
+ val authorizer: Option[Authorizer]) {
+  def authorize(requestContext: RequestContext,
+operation: AclOperation,
+resourceType: ResourceType,
+resourceName: String,
+logIfAllowed: Boolean = true,
+logIfDenied: Boolean = true,
+refCount: Int = 1): Boolean = {
+authorizer.forall { authZ =>
+  val resource = new ResourcePattern(resourceType, resourceName, 
PatternType.LITERAL)
+  val actions = Collections.singletonList(new Action(operation, resource, 
refCount, logIfAllowed, logIfDenied))
+  authZ.authorize(requestContext, actions).get(0) == 
AuthorizationResult.ALLOWED
+}
+  }
+
+  def authorizeClusterOperation(request: RequestChannel.Request, operation: 
AclOperation): Unit = {
+if (!authorize(request.context, operation, CLUSTER, CLUSTER_NAME))
+  throw new ClusterAuthorizationException(s"Request $request is not 
authorized.")
+  }
+
+  def authorizedOperations(request: RequestChannel.Request, resource: 
Resource): Int = {
+val supportedOps = 
AclEntry.supportedOperations(resource.resourceType).toList
+val authorizedOps = authorizer match {
+  case Some(authZ) =>
+val resourcePattern = new ResourcePattern(resource.resourceType, 
resource.name, PatternType.LITERAL)
+val actions = supportedOps.map { op => new Action(op, resourcePattern, 
1, false, false) }
+authZ.authorize(request.context, actions.asJava).asScala
+  .zip(supportedOps)
+   

[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2021-01-14 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r557461217



##
File path: core/src/main/scala/kafka/server/RequestHandlerUtils.scala
##
@@ -0,0 +1,203 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.server
+
+import kafka.cluster.Partition
+import kafka.coordinator.group.GroupCoordinator
+import kafka.coordinator.transaction.TransactionCoordinator
+
+import java.lang.{Byte => JByte}
+import java.util.Collections
+import kafka.network.RequestChannel
+import kafka.security.authorizer.AclEntry
+import kafka.server.QuotaFactory.QuotaManagers
+import kafka.utils.Logging
+import org.apache.kafka.common.acl.AclOperation
+import org.apache.kafka.common.errors.ClusterAuthorizationException
+import org.apache.kafka.common.internals.Topic
+import org.apache.kafka.common.network.Send
+import org.apache.kafka.common.requests.{AbstractRequest, AbstractResponse, 
RequestContext}
+import org.apache.kafka.common.resource.Resource.CLUSTER_NAME
+import org.apache.kafka.common.resource.ResourceType.CLUSTER
+import org.apache.kafka.common.resource.{PatternType, Resource, 
ResourcePattern, ResourceType}
+import org.apache.kafka.common.utils.{Time, Utils}
+import org.apache.kafka.server.authorizer.{Action, AuthorizationResult, 
Authorizer}
+
+import scala.jdk.CollectionConverters._
+
+/**
+ * Helper methods for request handlers
+ */
+object RequestHandlerUtils {
+  def onLeadershipChange(groupCoordinator: GroupCoordinator,
+ txnCoordinator: TransactionCoordinator,
+ updatedLeaders: Iterable[Partition],
+ updatedFollowers: Iterable[Partition]): Unit = {
+// for each new leader or follower, call coordinator to handle consumer 
group migration.
+// this callback is invoked under the replica state change lock to ensure 
proper order of
+// leadership changes
+updatedLeaders.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onElection(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onElection(partition.partitionId, 
partition.getLeaderEpoch)
+}
+
+updatedFollowers.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onResignation(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onResignation(partition.partitionId, 
Some(partition.getLeaderEpoch))
+}
+  }
+}
+
+class AuthHelper(val requestChannel: RequestChannel,
+ val authorizer: Option[Authorizer]) {
+  def authorize(requestContext: RequestContext,
+operation: AclOperation,
+resourceType: ResourceType,
+resourceName: String,
+logIfAllowed: Boolean = true,
+logIfDenied: Boolean = true,
+refCount: Int = 1): Boolean = {
+authorizer.forall { authZ =>
+  val resource = new ResourcePattern(resourceType, resourceName, 
PatternType.LITERAL)
+  val actions = Collections.singletonList(new Action(operation, resource, 
refCount, logIfAllowed, logIfDenied))
+  authZ.authorize(requestContext, actions).get(0) == 
AuthorizationResult.ALLOWED
+}
+  }
+
+  def authorizeClusterOperation(request: RequestChannel.Request, operation: 
AclOperation): Unit = {
+if (!authorize(request.context, operation, CLUSTER, CLUSTER_NAME))
+  throw new ClusterAuthorizationException(s"Request $request is not 
authorized.")
+  }
+
+  def authorizedOperations(request: RequestChannel.Request, resource: 
Resource): Int = {
+val supportedOps = 
AclEntry.supportedOperations(resource.resourceType).toList
+val authorizedOps = authorizer match {
+  case Some(authZ) =>
+val resourcePattern = new ResourcePattern(resource.resourceType, 
resource.name, PatternType.LITERAL)
+val actions = supportedOps.map { op => new Action(op, resourcePattern, 
1, false, false) }
+authZ.authorize(request.context, actions.asJava).asScala
+  .zip(supportedOps)
+   

[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2021-01-14 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r557460726



##
File path: core/src/main/scala/kafka/server/RequestHandlerUtils.scala
##
@@ -0,0 +1,203 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kafka.server
+
+import kafka.cluster.Partition
+import kafka.coordinator.group.GroupCoordinator
+import kafka.coordinator.transaction.TransactionCoordinator
+
+import java.lang.{Byte => JByte}
+import java.util.Collections
+import kafka.network.RequestChannel
+import kafka.security.authorizer.AclEntry
+import kafka.server.QuotaFactory.QuotaManagers
+import kafka.utils.Logging
+import org.apache.kafka.common.acl.AclOperation
+import org.apache.kafka.common.errors.ClusterAuthorizationException
+import org.apache.kafka.common.internals.Topic
+import org.apache.kafka.common.network.Send
+import org.apache.kafka.common.requests.{AbstractRequest, AbstractResponse, 
RequestContext}
+import org.apache.kafka.common.resource.Resource.CLUSTER_NAME
+import org.apache.kafka.common.resource.ResourceType.CLUSTER
+import org.apache.kafka.common.resource.{PatternType, Resource, 
ResourcePattern, ResourceType}
+import org.apache.kafka.common.utils.{Time, Utils}
+import org.apache.kafka.server.authorizer.{Action, AuthorizationResult, 
Authorizer}
+
+import scala.jdk.CollectionConverters._
+
+/**
+ * Helper methods for request handlers
+ */
+object RequestHandlerUtils {
+  def onLeadershipChange(groupCoordinator: GroupCoordinator,
+ txnCoordinator: TransactionCoordinator,
+ updatedLeaders: Iterable[Partition],
+ updatedFollowers: Iterable[Partition]): Unit = {
+// for each new leader or follower, call coordinator to handle consumer 
group migration.
+// this callback is invoked under the replica state change lock to ensure 
proper order of
+// leadership changes
+updatedLeaders.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onElection(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onElection(partition.partitionId, 
partition.getLeaderEpoch)
+}
+
+updatedFollowers.foreach { partition =>
+  if (partition.topic == Topic.GROUP_METADATA_TOPIC_NAME)
+groupCoordinator.onResignation(partition.partitionId)
+  else if (partition.topic == Topic.TRANSACTION_STATE_TOPIC_NAME)
+txnCoordinator.onResignation(partition.partitionId, 
Some(partition.getLeaderEpoch))
+}
+  }
+}
+
+class AuthHelper(val requestChannel: RequestChannel,

Review comment:
   I would move this to its own file. It seems unrelated to the other 
things in this class.





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




[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2021-01-13 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r556818362



##
File path: core/src/main/scala/kafka/network/RequestChannel.scala
##
@@ -412,8 +412,8 @@ class RequestChannel(val queueSize: Int,
   }
 
   def sendResponse(request: RequestChannel.Request,
-   responseOpt: Option[AbstractResponse],
-   onComplete: Option[Send => Unit]): Unit = {
+   responseOpt: Option[AbstractResponse],
+   onComplete: Option[Send => Unit]): Unit = {

Review comment:
   Is this intentional?

##
File path: core/src/main/scala/kafka/server/ApisUtils.scala
##
@@ -109,7 +106,7 @@ trait ApisUtils extends Logging {
   // Throttle the channel if the request quota is enabled but has been 
violated. Regardless of throttling, send the
   // response immediately.
   def sendResponseMaybeThrottle(request: RequestChannel.Request,
-createResponse: Int => AbstractResponse): Unit 
= {
+createResponse: Int => 
AbstractResponse): Unit = {

Review comment:
   Is this intentional?





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




[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2020-12-09 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r539429096



##
File path: core/src/main/scala/kafka/server/KafkaRequestHandler.scala
##
@@ -34,6 +34,10 @@ trait ApiRequestHandler {
   def handle(request: RequestChannel.Request): Unit
 }
 
+trait BaseApis extends ApiRequestHandler {

Review comment:
   Self types like this are an anti pattern, in my opinion. They were used 
years ago as a DI replacement (the cake pattern), but people have moved on 
since they tend to be hard to understand.





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




[GitHub] [kafka] ijuma commented on a change in pull request #9715: Upstream ApisUtils from kip-500

2020-12-09 Thread GitBox


ijuma commented on a change in pull request #9715:
URL: https://github.com/apache/kafka/pull/9715#discussion_r539390329



##
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##
@@ -98,6 +97,7 @@ import scala.annotation.nowarn
  * Logic to handle the various Kafka requests
  */
 class KafkaApis(val requestChannel: RequestChannel,
+val apisUtils: ApisUtils,

Review comment:
   Is this an improvement? I am not sure. A couple of things:
   1. The `Utils` suffix looks odd both in a trait as well as a class that is 
instantiated.
   2. Using delegation allows for better separation of concerns than 
inheritance.





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