Copilot commented on code in PR #3767:
URL: https://github.com/apache/texera/pull/3767#discussion_r2379308826


##########
core/access-control-service/src/main/scala/edu/uci/ics/texera/service/access/AccessChecker.scala:
##########
@@ -0,0 +1,104 @@
+// 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 edu.uci.ics.texera.service.access
+
+import com.typesafe.scalalogging.LazyLogging
+import edu.uci.ics.texera.auth.JwtParser.parseToken
+import edu.uci.ics.texera.auth.SessionUser
+import edu.uci.ics.texera.auth.util.{ComputingUnitAccess, HeaderField}
+import edu.uci.ics.texera.dao.jooq.generated.enums.PrivilegeEnum
+import jakarta.ws.rs.core.{HttpHeaders, Response, UriInfo}
+
+import java.util.Optional
+import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
+import scala.util.matching.Regex
+
+class AccessChecker extends LazyLogging {
+
+  private val computingUnitAccess: ComputingUnitAccess = new 
ComputingUnitAccess()
+
+  // Regex for the paths that require authorization
+  private val wsapiWorkflowWebsocket: Regex = 
""".*/wsapi/workflow-websocket.*""".r
+  private val apiExecutionsStats: Regex = 
""".*/api/executions/[0-9]+/stats/[0-9]+.*""".r
+  private val apiExecutionsResultExport: Regex = 
""".*/api/executions/result/export.*""".r
+
+  def authorize(uriInfo: UriInfo, headers: HttpHeaders): Response = {
+    val path = uriInfo.getPath
+    logger.info(s"Authorizing request for path: $path")
+
+    path match {
+      case wsapiWorkflowWebsocket() | apiExecutionsStats() | 
apiExecutionsResultExport() =>
+      checkComputingUnitAccess(uriInfo, headers)
+      case _ =>
+        logger.warn(s"No authorization logic for path: $path. Denying access.")
+        Response.status(Response.Status.FORBIDDEN).build()
+    }
+  }
+
+  private def checkComputingUnitAccess(uriInfo: UriInfo, headers: 
HttpHeaders): Response = {
+    val queryParams: Map[String, String] = uriInfo
+      .getQueryParameters()
+      .asScala
+      .view
+      .mapValues(values => values.asScala.headOption.getOrElse(""))
+      .toMap
+
+    logger.info(s"Request URI: ${uriInfo.getRequestUri} and headers: 
${headers.getRequestHeaders.asScala} and queryParams: $queryParams")
+
+    val token = queryParams.getOrElse(
+      "access-token",
+      headers
+        .getRequestHeader("Authorization")
+        .asScala
+        .headOption
+        .getOrElse("")
+        .replace("Bearer ", "")
+    )
+    val cuid = queryParams.getOrElse("cuid", "")
+    val cuidInt = try {
+      cuid.toInt
+    } catch {
+      case _: NumberFormatException =>
+        return Response.status(Response.Status.FORBIDDEN).build()
+    }
+
+    var cuAccess: PrivilegeEnum = PrivilegeEnum.NONE
+    var userSession: Optional[SessionUser] = Optional.empty()
+    try {
+      userSession = parseToken(token)
+      if (userSession.isEmpty)
+        return Response.status(Response.Status.FORBIDDEN).build()
+
+      val uid = userSession.get().getUid
+      cuAccess = computingUnitAccess.getComputingUnitAccess(cuidInt, uid)
+      if (cuAccess == PrivilegeEnum.NONE)
+        return Response.status(Response.Status.FORBIDDEN).build()
+    } catch {
+      case e: Exception =>
+        return Response.status(Response.Status.FORBIDDEN).build()
+    }
+

Review Comment:
   Using `var` is discouraged in Scala. Consider restructuring the logic to use 
immutable values or pattern matching to avoid mutable variables.
   ```suggestion
       val userSession = try {
         parseToken(token)
       } catch {
         case _: Exception =>
           return Response.status(Response.Status.FORBIDDEN).build()
       }
       if (userSession.isEmpty)
         return Response.status(Response.Status.FORBIDDEN).build()
   
       val uid = userSession.get().getUid
       cuAccess = computingUnitAccess.getComputingUnitAccess(cuidInt, uid)
       if (cuAccess == PrivilegeEnum.NONE)
         return Response.status(Response.Status.FORBIDDEN).build()
   ```



##########
core/access-control-service/src/main/scala/edu/uci/ics/texera/service/access/AccessChecker.scala:
##########
@@ -0,0 +1,104 @@
+// 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 edu.uci.ics.texera.service.access
+
+import com.typesafe.scalalogging.LazyLogging
+import edu.uci.ics.texera.auth.JwtParser.parseToken
+import edu.uci.ics.texera.auth.SessionUser
+import edu.uci.ics.texera.auth.util.{ComputingUnitAccess, HeaderField}
+import edu.uci.ics.texera.dao.jooq.generated.enums.PrivilegeEnum
+import jakarta.ws.rs.core.{HttpHeaders, Response, UriInfo}
+
+import java.util.Optional
+import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
+import scala.util.matching.Regex
+
+class AccessChecker extends LazyLogging {
+
+  private val computingUnitAccess: ComputingUnitAccess = new 
ComputingUnitAccess()
+
+  // Regex for the paths that require authorization
+  private val wsapiWorkflowWebsocket: Regex = 
""".*/wsapi/workflow-websocket.*""".r
+  private val apiExecutionsStats: Regex = 
""".*/api/executions/[0-9]+/stats/[0-9]+.*""".r
+  private val apiExecutionsResultExport: Regex = 
""".*/api/executions/result/export.*""".r
+
+  def authorize(uriInfo: UriInfo, headers: HttpHeaders): Response = {
+    val path = uriInfo.getPath
+    logger.info(s"Authorizing request for path: $path")
+
+    path match {
+      case wsapiWorkflowWebsocket() | apiExecutionsStats() | 
apiExecutionsResultExport() =>
+      checkComputingUnitAccess(uriInfo, headers)

Review Comment:
   Missing indentation for the case body. The method call should be indented 
consistently with Scala formatting conventions.
   ```suggestion
           checkComputingUnitAccess(uriInfo, headers)
   ```



##########
core/access-control-service/src/main/resources/logback.xml:
##########
@@ -0,0 +1,55 @@
+<!--
+ 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.
+-->
+
+<configuration>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">-->

Review Comment:
   Extra comment close marker '-->' should be removed from the opening tag.
   ```suggestion
       <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to