This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-7337-7a79db153e9aeaafd6120805838f56e05c702ae8
in repository https://gitbox.apache.org/repos/asf/texera.git

commit f46b2a71d4bc7d96cc2987a34e608717f239f39d
Author: Kunwoo (Chris) <[email protected]>
AuthorDate: Wed Aug 5 16:45:51 2026 -0400

    feat(computing-unit-managing-service): let admins terminate any computing 
unit (#7337)
    
    <!--
    Thanks for sending a pull request (PR)! Here are some tips for you:
    1. If this is your first time, please read our contributor guidelines:
    [Contributing to
    Texera](https://github.com/apache/texera/blob/main/CONTRIBUTING.md)
      2. Ensure you have added or run the appropriate tests for your PR
      3. If the PR is work in progress, mark it a draft on GitHub.
      4. Please write your PR title to summarize what this PR proposes, we
        are following Conventional Commits style for PR titles as well.
      5. Be sure to keep the PR description updated to reflect all changes.
    -->
    
    ### What changes were proposed in this PR?
    <!--
    Please clarify what changes you are proposing. The purpose of this
    section
    is to outline the changes. Here are some tips for you:
      1. If you propose a new API, clarify the use case for a new API.
      2. If you fix a bug, you can clarify why it is a bug.
      3. If it is a refactoring, clarify what has been changed.
      3. It would be helpful to include a before-and-after comparison using
         screenshots or GIFs.
      4. Please consider writing useful notes for better and faster reviews.
    -->
    
    Admins can now terminate any computing unit, not just their own.
    
    `terminateComputingUnit` gated on ownership alone. This PR relaxes that
    guard so an ADMIN passes it.
    
    ### Any related issues, documentation, discussions?
    <!--
    Please use this section to link other resources if not mentioned
    already.
    1. If this PR fixes an issue, please include `Fixes #1234`, `Resolves
    #1234`
    or `Closes #1234`. If it is only related, simply mention the issue
    number.
      2. If there is design documentation, please add the link.
      3. If there is a discussion in the mailing list, please add the link.
    -->
    
    Closes #6478.
    
    ### How was this PR tested?
    <!--
    If tests were added, say they were added here. Or simply mention that if
    the PR
    is tested with existing test cases. Make sure to include/update test
    cases that
    check the changes thoroughly including negative and positive cases if
    possible.
    If it was tested in a way different from regular unit tests, please
    clarify how
    you tested step by step, ideally copy and paste-able, so that other
    reviewers can
    test and check, and descendants can verify in the future. If tests were
    not added,
    please describe why they were not added and/or why it was difficult to
    add.
    -->
    
    Added tests in `ComputingUnitManagingResourceSpec`
    
    Run: `sbt "ComputingUnitManagingService/testOnly
    *ComputingUnitManagingResourceSpec"`
    
    ### Was this PR authored or co-authored using generative AI tooling?
    <!--
    If generative AI tooling has been used in the process of authoring this
    PR,
    please include the phrase: 'Generated-by: ' followed by the name of the
    tool
    and its version. If no, write 'No'.
    Please refer to the [ASF Generative Tooling
    Guidance](https://www.apache.org/legal/generative-tooling.html) for
    details.
    -->
    Generated-by: Claude Code (Opus 4.8)
---
 .../resource/AdminComputingUnitResource.scala      |  4 --
 .../resource/ComputingUnitManagingResource.scala   |  9 ++-
 .../ComputingUnitManagingResourceSpec.scala        | 70 +++++++++++++++++++++-
 3 files changed, 74 insertions(+), 9 deletions(-)

diff --git 
a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/AdminComputingUnitResource.scala
 
b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/AdminComputingUnitResource.scala
index 45d137a4b4..28c95999b2 100644
--- 
a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/AdminComputingUnitResource.scala
+++ 
b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/AdminComputingUnitResource.scala
@@ -55,10 +55,6 @@ class AdminComputingUnitResource {
 
   /**
     * List every non-terminated computing unit across all users (ADMIN-only).
-    *
-    * TODO: rows report WRITE, but the mutating endpoints on 
[[ComputingUnitManagingResource]]
-    * still gate on ownership with no ADMIN bypass, so an admin acting on a 
unit it does not own is
-    * rejected. Add that bypass; until then a client must not present these 
rows as writable.
     */
   @GET
   @Path("/list")
diff --git 
a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitManagingResource.scala
 
b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitManagingResource.scala
index df67fb3bfe..778e5ed02a 100644
--- 
a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitManagingResource.scala
+++ 
b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitManagingResource.scala
@@ -42,7 +42,11 @@ import org.apache.texera.common.config.{
 }
 import org.apache.texera.dao.SqlServer
 import org.apache.texera.dao.SqlServer.withTransaction
-import org.apache.texera.dao.jooq.generated.enums.{PrivilegeEnum, 
WorkflowComputingUnitTypeEnum}
+import org.apache.texera.dao.jooq.generated.enums.{
+  PrivilegeEnum,
+  UserRoleEnum,
+  WorkflowComputingUnitTypeEnum
+}
 import org.apache.texera.dao.jooq.generated.tables.daos.{
   ComputingUnitUserAccessDao,
   UserDao,
@@ -599,7 +603,8 @@ class ComputingUnitManagingResource {
       @PathParam("cuid") cuid: Integer,
       @Auth user: SessionUser
   ): Response = {
-    if (!userOwnComputingUnit(context, cuid, user.getUid)) {
+    // ADMINs may terminate any unit; everyone else must own it.
+    if (!user.isRoleOf(UserRoleEnum.ADMIN) && !userOwnComputingUnit(context, 
cuid, user.getUid)) {
       return Response
         .status(Response.Status.BAD_REQUEST)
         .entity(s"User has no access to the computing unit")
diff --git 
a/computing-unit-managing-service/src/test/scala/org/apache/texera/service/resource/ComputingUnitManagingResourceSpec.scala
 
b/computing-unit-managing-service/src/test/scala/org/apache/texera/service/resource/ComputingUnitManagingResourceSpec.scala
index 54e495473d..872b5e23b6 100644
--- 
a/computing-unit-managing-service/src/test/scala/org/apache/texera/service/resource/ComputingUnitManagingResourceSpec.scala
+++ 
b/computing-unit-managing-service/src/test/scala/org/apache/texera/service/resource/ComputingUnitManagingResourceSpec.scala
@@ -19,6 +19,7 @@
 
 package org.apache.texera.service.resource
 
+import jakarta.ws.rs.NotFoundException
 import org.apache.texera.auth.SessionUser
 import org.apache.texera.dao.MockTexeraDB
 import org.apache.texera.dao.jooq.generated.enums.{
@@ -53,19 +54,51 @@ class ComputingUnitManagingResourceSpec
     new SessionUser(u)
   }
 
-  private def localUnit(cuid: Int, name: String): WorkflowComputingUnit = {
+  // Fixtures for the terminate tests. The units acted on are owned by 
"victim" (uid 901), not by
+  // `user` (uid 800), so they never show up in listComputingUnits(user)'s 
exact-set assertion.
+  private def makeUser(id: Int, name: String, role: UserRoleEnum): User = {
+    val u = new User()
+    u.setUid(id)
+    u.setName(name)
+    u.setEmail(s"[email protected]")
+    u.setRole(role)
+    u.setPassword("password")
+    u
+  }
+  private lazy val adminUser: SessionUser =
+    new SessionUser(makeUser(900, "admin", UserRoleEnum.ADMIN))
+  private lazy val strangerUser: SessionUser =
+    new SessionUser(makeUser(902, "stranger", UserRoleEnum.REGULAR))
+
+  private def localUnit(cuid: Int, name: String): WorkflowComputingUnit =
+    localUnitOwnedBy(cuid, uid, name)
+
+  private def localUnitOwnedBy(cuid: Int, ownerUid: Int, name: String): 
WorkflowComputingUnit = {
     val unit = new WorkflowComputingUnit()
     unit.setCuid(cuid)
-    unit.setUid(uid)
+    unit.setUid(ownerUid)
     unit.setName(name)
     unit.setType(WorkflowComputingUnitTypeEnum.local)
     unit
   }
 
+  private def insertLocalUnit(cuid: Int, ownerUid: Int, name: String): Unit =
+    new WorkflowComputingUnitDao(getDSLContext.configuration())
+      .insert(localUnitOwnedBy(cuid, ownerUid, name))
+
+  private def isTerminated(cuid: Int): Boolean =
+    new WorkflowComputingUnitDao(getDSLContext.configuration())
+      .fetchOneByCuid(cuid)
+      .getTerminateTime != null
+
   override protected def beforeAll(): Unit = {
     super.beforeAll()
     initializeDBAndReplaceDSLContext()
-    new UserDao(getDSLContext.configuration()).insert(user.getUser)
+    val userDao = new UserDao(getDSLContext.configuration())
+    userDao.insert(user.getUser)
+    userDao.insert(adminUser.getUser)
+    userDao.insert(makeUser(901, "victim", UserRoleEnum.REGULAR))
+    userDao.insert(strangerUser.getUser)
     val unitDao = new WorkflowComputingUnitDao(getDSLContext.configuration())
     unitDao.insert(localUnit(800, "cu-a"))
     unitDao.insert(localUnit(801, "cu-b"))
@@ -101,4 +134,35 @@ class ComputingUnitManagingResourceSpec
     all(result.map(_.accessPrivilege)) shouldBe PrivilegeEnum.WRITE
     all(result.map(_.status)) shouldBe "Running"
   }
+
+  "terminateComputingUnit" should "let an admin terminate a unit it does not 
own" in {
+    insertLocalUnit(cuid = 910, ownerUid = 901, name = "victim-cu")
+
+    val response = resource.terminateComputingUnit(910, adminUser)
+
+    response.getStatus shouldBe 200
+    isTerminated(910) shouldBe true
+  }
+
+  it should "reject a non-admin acting on a unit it does not own with 400 and 
not terminate it" in {
+    insertLocalUnit(cuid = 911, ownerUid = 901, name = "victim-cu-2")
+
+    val response = resource.terminateComputingUnit(911, strangerUser)
+
+    response.getStatus shouldBe 400
+    isTerminated(911) shouldBe false
+  }
+
+  it should "let an owner terminate its own unit" in {
+    insertLocalUnit(cuid = 912, ownerUid = 902, name = "stranger-own-cu")
+
+    val response = resource.terminateComputingUnit(912, strangerUser)
+
+    response.getStatus shouldBe 200
+    isTerminated(912) shouldBe true
+  }
+
+  it should "return 404 when an admin terminates a nonexistent unit" in {
+    a[NotFoundException] should be thrownBy 
resource.terminateComputingUnit(99999, adminUser)
+  }
 }

Reply via email to