You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@texera.apache.org by "aicam (via GitHub)" <gi...@apache.org> on 2025/09/25 14:15:24 UTC
[PR] Access control Service added as a new microservice [texera]
aicam opened a new pull request, #3767:
URL: https://github.com/apache/texera/pull/3767
## Overview
This PR is fixing the first part of [#3634](https://github.com/apache/texera/issues/3634). The full [PR](https://github.com/apache/texera/pull/3598) had so many changes so we broke it down into two PRs. This PR is the first one that introduce Access Control Service only without its usage in any part of the system.
## Changes
Currently, the folder named `access-control-service` is added with its dependencies to `auth` folder. Currently, it has test cases to make sure its functionality is working correctly.
<img width="1442" height="643" alt="Screenshot from 2025-09-25 07-14-57" src="https://github.com/user-attachments/assets/4698f34a-580c-4f35-a016-969ee87c3ea2" />
| Component/Flow | File | Description |
| :--- | :--- | :--- |
| **New `AccessControl` Service** | `core/access-control-service/...` | A new Dropwizard-based microservice responsible for authorizing user requests to computing units. This includes its build configuration, application setup (`AccessControlService.scala`), configuration model (`AccessControlServiceConfiguration.scala`), authorization logic (`AccessChecker.scala`, `Authorizer.scala`), REST endpoint (`AccessControlResource.scala`), and unit tests (`AccessControlResourceSpec.scala`). |
| **Database Access Logic** | `core/auth/src/main/scala/edu/uci/ics/texera/auth/util/ComputingUnitAccess.scala` | Implements the logic to query the PostgreSQL database and determine a user's access privilege (`READ`, `WRITE`, `NONE`) for a given Computing Unit. |
| | `core/auth/src/main/scala/edu/uci/ics/texera/auth/util/HeaderField.scala` | Defines constants for the custom HTTP headers (`x-user-cu-access`, `x-user-id`, etc.) that are injected by the Access Control Service. |
--
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: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(access-control-service): AccessControlService added as a new micro service [texera]
Posted by "aicam (via GitHub)" <gi...@apache.org>.
aicam merged PR #3767:
URL: https://github.com/apache/texera/pull/3767
--
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: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Access control Service added as a new microservice [texera]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
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: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(access-control-service): Access control Service added as a new microservice [texera]
Posted by "bobbai00 (via GitHub)" <gi...@apache.org>.
bobbai00 commented on code in PR #3767:
URL: https://github.com/apache/texera/pull/3767#discussion_r2380396887
##########
core/access-control-service/src/main/scala/edu/uci/ics/texera/service/resource/AccessControlResource.scala:
##########
@@ -0,0 +1,126 @@
+// 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.resource
+
+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._
+import jakarta.ws.rs.{GET, POST, Path, Produces}
+
+import java.util.Optional
+import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
+import scala.util.matching.Regex
+
+@Produces(Array(MediaType.APPLICATION_JSON))
+@Path("/auth")
+class AccessControlResource extends LazyLogging {
+
+ @GET
+ @Path("/{path:.*}")
+ def authorizeGet(
+ @Context uriInfo: UriInfo,
+ @Context headers: HttpHeaders
+ ): Response = {
+ AccessControlResource.authorize(uriInfo, headers)
+ }
+
+ @POST
+ @Path("/{path:.*}")
+ def authorizePost(
+ @Context uriInfo: UriInfo,
+ @Context headers: HttpHeaders
+ ): Response = {
+ AccessControlResource.authorize(uriInfo, headers)
+ }
+}
+
+object AccessControlResource extends LazyLogging {
+
+ // 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 = {
Review Comment:
add brief comments explain the purpose of this function
##########
core/auth/src/main/scala/edu/uci/ics/texera/auth/util/ComputingUnitAccess.scala:
##########
@@ -0,0 +1,57 @@
+// 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.auth.util
+
+import edu.uci.ics.texera.dao.SqlServer
+import edu.uci.ics.texera.dao.jooq.generated.enums.PrivilegeEnum
+import edu.uci.ics.texera.dao.jooq.generated.tables.daos.{ComputingUnitUserAccessDao, WorkflowComputingUnitDao}
+import ComputingUnitAccess._
+import org.jooq.DSLContext
+
+import scala.jdk.CollectionConverters._
+
+
+object ComputingUnitAccess {
+ private lazy val context: DSLContext = SqlServer
+ .getInstance()
+ .createDSLContext()
+
+ def getComputingUnitAccess(cuid: Integer, uid: Integer): PrivilegeEnum = {
+ val workflowComputingUnitDao = new WorkflowComputingUnitDao(context.configuration())
+ val unit = workflowComputingUnitDao.fetchOneByCuid(cuid)
+
+ if (unit.getUid.equals(uid)) {
+ return PrivilegeEnum.WRITE // owner has write access
+ }
+
+ val computingUnitUserAccessDao = new ComputingUnitUserAccessDao(context.configuration())
+ val accessOpt = computingUnitUserAccessDao
+ .fetchByUid(uid)
+ .asScala
+ .find(_.getCuid.equals(cuid))
+
+ accessOpt match {
+ case Some(access) => access.getPrivilege
+ case None => PrivilegeEnum.NONE
+ }
+ }
+}
+
+class ComputingUnitAccess {
Review Comment:
Remove this
##########
core/access-control-service/src/main/scala/edu/uci/ics/texera/service/resource/AccessControlResource.scala:
##########
@@ -0,0 +1,126 @@
+// 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.resource
+
+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._
+import jakarta.ws.rs.{GET, POST, Path, Produces}
+
+import java.util.Optional
+import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
+import scala.util.matching.Regex
+
+@Produces(Array(MediaType.APPLICATION_JSON))
+@Path("/auth")
+class AccessControlResource extends LazyLogging {
+
+ @GET
+ @Path("/{path:.*}")
+ def authorizeGet(
+ @Context uriInfo: UriInfo,
+ @Context headers: HttpHeaders
+ ): Response = {
+ AccessControlResource.authorize(uriInfo, headers)
+ }
+
+ @POST
+ @Path("/{path:.*}")
+ def authorizePost(
+ @Context uriInfo: UriInfo,
+ @Context headers: HttpHeaders
+ ): Response = {
+ AccessControlResource.authorize(uriInfo, headers)
+ }
+}
+
+object AccessControlResource extends LazyLogging {
Review Comment:
Move the object to the top, before the 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.
To unsubscribe, e-mail: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(access-control-service): Access control Service added as a new microservice [texera]
Posted by "bobbai00 (via GitHub)" <gi...@apache.org>.
bobbai00 commented on code in PR #3767:
URL: https://github.com/apache/texera/pull/3767#discussion_r2379608577
##########
core/auth/src/main/scala/edu/uci/ics/texera/auth/util/ComputingUnitAccess.scala:
##########
@@ -0,0 +1,56 @@
+// 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.auth.util
+
+import edu.uci.ics.texera.dao.SqlServer
+import edu.uci.ics.texera.dao.jooq.generated.enums.PrivilegeEnum
+import edu.uci.ics.texera.dao.jooq.generated.tables.daos.{ComputingUnitUserAccessDao, WorkflowComputingUnitDao}
+import ComputingUnitAccess._
+import org.jooq.DSLContext
+
+import scala.jdk.CollectionConverters._
+
+
+object ComputingUnitAccess {
+ private lazy val context: DSLContext = SqlServer
+ .getInstance()
+ .createDSLContext()
+}
+
+class ComputingUnitAccess {
Review Comment:
move these methods to the object `ComputingUnitAccess` above to make them static. You can mimic object `DatasetAccessResource`
##########
core/access-control-service/src/main/scala/edu/uci/ics/texera/service/resource/AccessControlResource.scala:
##########
@@ -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.
+
+package edu.uci.ics.texera.service.resource
+
+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 edu.uci.ics.texera.service.access.AccessChecker
+import jakarta.ws.rs.{GET, POST, Path, Produces}
+import jakarta.ws.rs.core.{Context, HttpHeaders, MediaType, Response, UriInfo}
+
+import java.util.Optional
+import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
+
+@Produces(Array(MediaType.APPLICATION_JSON))
+@Path("/auth")
+class AccessControlResource extends LazyLogging {
Review Comment:
Please move the definitions in `AccessChecker.scala`, including constants and functions to here, as `object AccessControlResource`. So that those methods become static. Currently I saw you wrap the `AccessChecker` as a class, but such class carries no internal state, so it is better to simplify it as sets of static functions. Also it makes code easier to read after merging two files into one.
##########
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()
Review Comment:
Please change `computingUnitAccess`'s methods into static methods, so that you don't need to create a dummy instance for it
--
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: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(access-control-service): Access control Service added as a new microservice [texera]
Posted by "aicam (via GitHub)" <gi...@apache.org>.
aicam commented on code in PR #3767:
URL: https://github.com/apache/texera/pull/3767#discussion_r2380252399
##########
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:
Then cuAccess is still defined as `var`, we can't assign them at the same time to prevent val
--
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: dev-unsubscribe@texera.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org