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-9480-4ff82fadc33126bf6475ec71fb03308ff8ecff85 in repository https://gitbox.apache.org/repos/asf/texera.git
commit 82cea123eb0f287ccfa1c3719c7500d7a6d2c1a3 Author: Xinyuan Lin <[email protected]> AuthorDate: Tue Nov 17 06:23:08 2026 +0000 refactor: narrow resource access queries (#9480) ### What changes were proposed in this PR? Select only the fields consumed by sharing, repository, and owner lookups. **+189/−139 lines** across fourteen Scala files. - Workflow, dataset, and model sharing read the owner and grantee fields they use. The owner's email comes from one join, and all three grants find a registered grantee through `OwnerAccess.registeredUid`, which computing-unit sharing now uses too. Access listings still populate the same response fields. - Dataset and model resources read only their owner's email: `ResourceAccess.ownerEmailOf` replaces `owner`, and `getOwnerEmail` replaces `getOwner` at all twelve call sites. - The S3 proxy and computing-unit mount share a lookup for dataset and model IDs. Both reject ambiguous repository names. - File resolution reads the resource ID, repository name, and version hash. It still checks read access before looking up a version. ### Any related issues, documentation, discussions? Closes #9478. ### How was this PR tested? All 175 existing tests passed across eight suites on JDK 17 with freshly generated jOOQ classes. An additional 21 local PostgreSQL probe cases matched on main and the patch. Across 241 captured SELECTs, 119 read fewer columns. The predicates, parameters, and order matched, with equality replacing single-value `IN` where the old DAO generated it. Checks covered placeholder accounts, exact-case email lookup, owner and grant precedence, ambiguous repository names, and duplicate version names. Scalafix on all five affected modules, scoped Scalafmt, and `git diff --check` passed. After review, the grantee lookup checks the placeholder flag in SQL, owner emails come from a join, and dataset and model resources read only the owner's email. New cases pin an owner without an email: the owner lookup gives null, and the owner-email endpoint returns null rather than an empty string. They passed before and after the owner change. All 214 cases in eleven suites passed on the final head, where a 22-case local probe of `ownerEmail`, `grant`, `accessList`, and `revoke` also matched main, covering an owner without an email, placeholder, unknown, and wrong-case grantees, and the owner's own access row. Scalafmt and Scalafix passed for `Auth`, `Resource`, `FileService`, `AccessControlService`, `ComputingUnitManagingService`, and `WorkflowExecutionService`. ```text sbt "DAO/jooqGenerate" "WorkflowExecutionService/testOnly *WorkflowAccessResourceSpec" "Auth/testOnly *OwnerAccessSpec *WorkflowAccessSpec" "Resource/testOnly *ResourceAccessSpec" "FileService/testOnly *DatasetAccessResourceSpec *ModelAccessResourceSpec *S3ProxyServletSpec" "AccessControlService/testOnly *ComputingUnitMountResourceSpec" "ComputingUnitManagingService/testOnly *ComputingUnitAccessResourceSpec *ComputingUnitAccessSharingDisabledSpec" "WorkflowCore/testOnly *FileResolverSpec" ``` ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-6), Claude Code (Claude Opus 5.5) --- .../resource/ComputingUnitMountResource.scala | 20 ++-- .../user/workflow/WorkflowAccessResource.scala | 42 ++++++--- .../org/apache/texera/auth/util/OwnerAccess.scala | 16 +++- .../texera/service/resource/ResourceAccess.scala | 102 ++++++++++++--------- .../texera/amber/core/storage/FileResolver.scala | 36 ++++---- .../resource/ComputingUnitAccessResource.scala | 11 +-- .../service/resource/DatasetAccessResource.scala | 5 +- .../texera/service/resource/DatasetResource.scala | 12 +-- .../service/resource/ModelAccessResource.scala | 5 +- .../texera/service/resource/ModelResource.scala | 12 +-- .../texera/service/util/S3ProxyServlet.scala | 24 ++--- .../service/resource/AccessResourceBehaviors.scala | 31 ++++++- .../resource/DatasetAccessResourceSpec.scala | 6 +- .../service/resource/ModelAccessResourceSpec.scala | 6 +- 14 files changed, 189 insertions(+), 139 deletions(-) diff --git a/access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala b/access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala index b521bdf821..4ed8c61dfe 100644 --- a/access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala +++ b/access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala @@ -32,7 +32,6 @@ import org.apache.texera.dao.SqlServer import org.apache.texera.dao.SqlServer.withTransaction import org.apache.texera.dao.jooq.generated.Tables.{DATASET_VERSION, MODEL_VERSION} import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{DatasetDao, ModelDao} import org.apache.texera.service.resource.ComputingUnitMountResource._ import org.apache.texera.service.util.{ ComputingUnitNodeLocator, @@ -40,8 +39,6 @@ import org.apache.texera.service.util.{ MounterClient } -import scala.jdk.CollectionConverters._ - /** * The mount authority: this service decides whether a user may act on a computing unit, so * it is where a mount request is authorized before being forwarded to that unit's node. @@ -164,23 +161,20 @@ class ComputingUnitMountResource( uid: Integer ): Unit = withTransaction(SqlServer.getInstance().createDSLContext()) { ctx => - val datasets = new DatasetDao(ctx.configuration()).fetchByRepositoryName(repositoryName) - val models = new ModelDao(ctx.configuration()).fetchByRepositoryName(repositoryName) - - val readable = (datasets.asScala.toList, models.asScala.toList) match { - case (dataset :: Nil, Nil) => - ResourceAccess.userHasReadAccess(ctx, ResourceTables.Dataset, dataset.getDid, uid) && + val readable = ResourceAccess.repositoryIds(ctx, repositoryName) match { + case (did :: Nil, Nil) => + ResourceAccess.userHasReadAccess(ctx, ResourceTables.Dataset, did, uid) && ctx.fetchExists( DATASET_VERSION, DATASET_VERSION.DID - .eq(dataset.getDid) + .eq(did) .and(DATASET_VERSION.VERSION_HASH.eq(commitHash)) ) - case (Nil, model :: Nil) => - ResourceAccess.userHasReadAccess(ctx, ResourceTables.Model, model.getMid, uid) && + case (Nil, mid :: Nil) => + ResourceAccess.userHasReadAccess(ctx, ResourceTables.Model, mid, uid) && ctx.fetchExists( MODEL_VERSION, - MODEL_VERSION.MID.eq(model.getMid).and(MODEL_VERSION.VERSION_HASH.eq(commitHash)) + MODEL_VERSION.MID.eq(mid).and(MODEL_VERSION.VERSION_HASH.eq(commitHash)) ) case _ => false } diff --git a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala index ce10a3b380..b6925ec7fd 100644 --- a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala +++ b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala @@ -26,11 +26,7 @@ import org.apache.texera.auth.util.WorkflowAccess.{getPrivilege, hasWriteAccess, import org.apache.texera.dao.SqlServer import org.apache.texera.dao.jooq.generated.Tables._ import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{ - UserDao, - WorkflowOfUserDao, - WorkflowUserAccessDao -} +import org.apache.texera.dao.jooq.generated.tables.daos.WorkflowUserAccessDao import org.apache.texera.dao.jooq.generated.tables.pojos.WorkflowUserAccess import org.apache.texera.web.model.common.AccessEntry import org.apache.texera.web.resource.dashboard.user.workflow.WorkflowAccessResource.context @@ -52,10 +48,16 @@ object WorkflowAccessResource { @RolesAllowed(Array("REGULAR", "ADMIN")) @Path("/access/workflow") class WorkflowAccessResource() { - final private val userDao = new UserDao(context.configuration()) - final private val workflowOfUserDao = new WorkflowOfUserDao(context.configuration) final private val workflowUserAccessDao = new WorkflowUserAccessDao(context.configuration) + private def ownerUid(wid: Integer): Integer = + context + .select(WORKFLOW_OF_USER.UID) + .from(WORKFLOW_OF_USER) + .where(WORKFLOW_OF_USER.WID.eq(wid)) + .fetch(WORKFLOW_OF_USER.UID) + .get(0) + /** * This method returns the owner of a workflow * @@ -65,7 +67,14 @@ class WorkflowAccessResource() { @GET @Path("/owner/{wid}") def getOwner(@PathParam("wid") wid: Integer): String = { - userDao.fetchOneByUid(workflowOfUserDao.fetchByWid(wid).get(0).getUid).getEmail + context + .select(USER.EMAIL) + .from(WORKFLOW_OF_USER) + .join(USER) + .on(USER.UID.eq(WORKFLOW_OF_USER.UID)) + .where(WORKFLOW_OF_USER.WID.eq(wid)) + .fetch(USER.EMAIL) + .get(0) } /** @@ -91,7 +100,7 @@ class WorkflowAccessResource() { .where( WORKFLOW_USER_ACCESS.WID .eq(wid) - .and(WORKFLOW_USER_ACCESS.UID.notEqual(workflowOfUserDao.fetchByWid(wid).get(0).getUid)) + .and(WORKFLOW_USER_ACCESS.UID.notEqual(ownerUid(wid))) ) .fetchInto(classOf[AccessEntry]) } @@ -126,11 +135,9 @@ class WorkflowAccessResource() { throw new ForbiddenException(s"You do not have permission to modify workflow $wid") } - val targetUser = userDao.fetchOneByEmail(email) - if (targetUser == null || targetUser.getIsPlaceholder) { - throw new BadRequestException(s"No registered user with email $email") - } - val userUid = targetUser.getUid + val userUid = OwnerAccess + .registeredUid(context, email) + .getOrElse(throw new BadRequestException(s"No registered user with email $email")) if (isOwner(wid, userUid)) { // amber is on javax JAX-RS, so it raises its own ForbiddenException with the shared message. throw new ForbiddenException(OwnerAccess.ownerChangeRefused) @@ -165,7 +172,12 @@ class WorkflowAccessResource() { @Auth user: SessionUser ): Unit = { try { - val targetUserUid = userDao.fetchOneByEmail(email).getUid + val targetUserUid = context + .select(USER.UID) + .from(USER) + .where(USER.EMAIL.eq(email)) + .fetchOne() + .value1() // Prevent owner from revoking their own access if (isOwner(wid, targetUserUid)) { diff --git a/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala b/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala index 45b37dbf61..22292eafa8 100644 --- a/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala +++ b/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala @@ -18,9 +18,11 @@ package org.apache.texera.auth.util import jakarta.ws.rs.ForbiddenException +import org.apache.texera.dao.jooq.generated.Tables.USER import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum +import org.jooq.DSLContext -/** The rule every shared resource applies to its owner. */ +/** The rules every shared resource applies to its owner and to the users it is shared with. */ object OwnerAccess { /** The owner always has WRITE; anyone else has what they were granted, or NONE. @@ -34,4 +36,16 @@ object OwnerAccess { /** Refuses any change to the owner's own access. */ def requireNotOwner(isOwner: Boolean): Unit = if (isOwner) throw new ForbiddenException(ownerChangeRefused) + + /** The uid of the registered account with this email, or None when the email is unknown or + * belongs to a placeholder; neither can be granted access. + */ + def registeredUid(ctx: DSLContext, email: String): Option[Integer] = + Option( + ctx + .select(USER.UID) + .from(USER) + .where(USER.EMAIL.eq(email).and(USER.IS_PLACEHOLDER.isFalse)) + .fetchOne(USER.UID) + ) } diff --git a/common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala b/common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala index edf5364caf..55cf4a55a8 100644 --- a/common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala +++ b/common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala @@ -22,11 +22,9 @@ package org.apache.texera.service.resource import jakarta.ws.rs.{BadRequestException, ForbiddenException} import jakarta.ws.rs.core.Response import org.apache.texera.auth.util.OwnerAccess -import org.apache.texera.dao.jooq.generated.Tables.USER +import org.apache.texera.dao.jooq.generated.Tables.{DATASET, MODEL, USER} import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.UserDao -import org.apache.texera.dao.jooq.generated.tables.pojos.User -import org.jooq.{DSLContext, EnumType, Record} +import org.jooq.{DSLContext, EnumType, Record, Record1} import scala.jdk.CollectionConverters._ @@ -41,6 +39,24 @@ object ResourceAccess { /** One shared grant, as returned by the access-list endpoints. */ case class AccessEntry(email: String, name: String, privilege: EnumType) {} + /** All matching IDs in each resource table, so mounts can reject ambiguous legacy names. */ + private[service] def repositoryIds( + ctx: DSLContext, + repositoryName: String + ): (List[Integer], List[Integer]) = { + val datasetIds = ctx + .select(DATASET.DID) + .from(DATASET) + .where(DATASET.REPOSITORY_NAME.eq(repositoryName)) + .fetch(DATASET.DID) + val modelIds = ctx + .select(MODEL.MID) + .from(MODEL) + .where(MODEL.REPOSITORY_NAME.eq(repositoryName)) + .fetch(MODEL.MID) + (datasetIds.asScala.toList, modelIds.asScala.toList) + } + def isPublic[R <: Record, A <: Record]( ctx: DSLContext, resource: ResourceTables[R, A], @@ -55,12 +71,11 @@ object ResourceAccess { ).flatMap(record => Option(record.value1())) .exists(_.booleanValue()) - def userOwns[R <: Record, A <: Record]( + private def ownerUid[R <: Record, A <: Record]( ctx: DSLContext, resource: ResourceTables[R, A], - id: Integer, - uid: Integer - ): Boolean = + id: Integer + ): Option[Integer] = Option( ctx .select(resource.ownerUidField) @@ -68,7 +83,14 @@ object ResourceAccess { .where(resource.idField.eq(id)) .fetchOne() ).flatMap(record => Option(record.value1())) - .contains(uid) + + def userOwns[R <: Record, A <: Record]( + ctx: DSLContext, + resource: ResourceTables[R, A], + id: Integer, + uid: Integer + ): Boolean = + ownerUid(ctx, resource, id).contains(uid) /** The owner always has WRITE, whatever their own access row says. */ def privilegeOf[R <: Record, A <: Record]( @@ -106,23 +128,29 @@ object ResourceAccess { ): Boolean = isPublic(ctx, resource, id) || privilegeOf(ctx, resource, id, uid) != PrivilegeEnum.NONE - /** The owning user, or null when the resource does not exist. */ - def owner[R <: Record, A <: Record]( + /** The owner's email row, or None when the resource does not exist; the email may be null. */ + private def ownerEmailRow[R <: Record, A <: Record]( ctx: DSLContext, resource: ResourceTables[R, A], id: Integer - ): User = { - val userDao = new UserDao(ctx.configuration()) + ): Option[Record1[String]] = Option( ctx - .select(resource.ownerUidField) + .select(USER.EMAIL) .from(resource.table) + .join(USER) + .on(USER.UID.eq(resource.ownerUidField)) .where(resource.idField.eq(id)) .fetchOne() - ).flatMap(record => Option(record.value1())) - .map(ownerUid => userDao.fetchOneByUid(ownerUid)) - .orNull - } + ) + + /** The owner's email, or null when the resource does not exist. Does not check access. */ + def ownerEmailOf[R <: Record, A <: Record]( + ctx: DSLContext, + resource: ResourceTables[R, A], + id: Integer + ): String = + ownerEmailRow(ctx, resource, id).map(_.value1()).orNull /** * The owner's email. @@ -136,7 +164,7 @@ object ResourceAccess { requesterUid: Integer ): String = { requireReadAccess(ctx, resource, id, requesterUid) - Option(owner(ctx, resource, id)).map(_.getEmail).getOrElse("") + ownerEmailRow(ctx, resource, id).map(_.value1()).getOrElse("") } /** @@ -153,12 +181,6 @@ object ResourceAccess { requesterUid: Integer ): java.util.List[AccessEntry] = { requireReadAccess(ctx, resource, id, requesterUid) - val ownerUid = ctx - .select(resource.ownerUidField) - .from(resource.table) - .where(resource.idField.eq(id)) - .fetchOne() - .value1() ctx .select(USER.EMAIL, USER.NAME, resource.privilegeField) @@ -168,7 +190,7 @@ object ResourceAccess { .where( resource.accessIdField .eq(id) - .and(resource.accessUidField.notEqual(ownerUid)) + .and(resource.accessUidField.notEqual(ownerUid(ctx, resource, id).orNull)) ) .fetchInto(classOf[AccessEntry]) } @@ -195,7 +217,7 @@ object ResourceAccess { ): List[D] = { // (id, entry) pairs so the public pass can skip ids already granted, without re-querying val granted: List[(Integer, D)] = ctx - .select() + .select((resource.table.fields().toSeq ++ Seq(resource.privilegeField, USER.EMAIL)): _*) .from( resource.table .leftJoin(resource.accessTable) @@ -209,23 +231,22 @@ object ResourceAccess { .toList .flatMap { record => val entity = record.into(resource.table).into(pojoClass) - val isOwner = record.into(resource.table).get(resource.ownerUidField) == uid + val isOwner = record.get(resource.ownerUidField) == uid val privilege = OwnerAccess.privilege( isOwner, - record.into(resource.accessTable).get(resource.privilegeField) + record.get(resource.privilegeField) ) - fromGrant(entity, record.into(USER).getEmail, privilege, isOwner) + fromGrant(entity, record.get(USER.EMAIL), privilege, isOwner) .map(entry => (idOf(entity), entry)) } - val grantedIds = granted.map(_._1).toSet - val public = if (!includePublic) Nil - else + else { + val grantedIds = granted.map(_._1).toSet ctx - .select() + .select((resource.table.fields().toSeq :+ USER.EMAIL): _*) .from( resource.table .leftJoin(USER) @@ -238,8 +259,9 @@ object ResourceAccess { .flatMap { record => val entity = record.into(resource.table).into(pojoClass) if (grantedIds.contains(idOf(entity))) None - else fromPublic(entity, record.into(USER).getEmail) + else fromPublic(entity, record.get(USER.EMAIL)) } + } granted.map(_._2) ++ public } @@ -256,11 +278,9 @@ object ResourceAccess { requesterUid: Integer ): Response = { requireWriteAccess(ctx, resource, id, requesterUid) - val grantee = new UserDao(ctx.configuration()).fetchOneByEmail(email) - if (grantee == null || grantee.getIsPlaceholder) { - throw new BadRequestException(s"No registered user with email $email") - } - val granteeUid = grantee.getUid + val granteeUid = OwnerAccess + .registeredUid(ctx, email) + .getOrElse(throw new BadRequestException(s"No registered user with email $email")) OwnerAccess.requireNotOwner(userOwns(ctx, resource, id, granteeUid)) val granted = PrivilegeEnum.valueOf(privilege) @@ -291,7 +311,7 @@ object ResourceAccess { requesterUid: Integer ): Response = { requireWriteAccess(ctx, resource, id, requesterUid) - val granteeUid = new UserDao(ctx.configuration()).fetchOneByEmail(email).getUid + val granteeUid = ctx.select(USER.UID).from(USER).where(USER.EMAIL.eq(email)).fetchOne().value1() OwnerAccess.requireNotOwner(userOwns(ctx, resource, id, granteeUid)) ctx diff --git a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/FileResolver.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/FileResolver.scala index 724be9ab21..20b452ab6f 100644 --- a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/FileResolver.scala +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/storage/FileResolver.scala @@ -27,12 +27,6 @@ import org.apache.texera.dao.jooq.generated.tables.DatasetVersion.DATASET_VERSIO import org.apache.texera.dao.jooq.generated.tables.Model.MODEL import org.apache.texera.dao.jooq.generated.tables.ModelVersion.MODEL_VERSION import org.apache.texera.dao.jooq.generated.tables.User.USER -import org.apache.texera.dao.jooq.generated.tables.pojos.{ - Dataset, - DatasetVersion, - Model, - ModelVersion -} import org.apache.texera.service.resource.{ResourceAccess, ResourceTables} import org.jooq.{DSLContext, Record} @@ -226,28 +220,29 @@ object FileResolver { .createDSLContext() ) { ctx => val dataset = ctx - .select(DATASET.fields: _*) + .select(DATASET.DID, DATASET.REPOSITORY_NAME) .from(DATASET) .leftJoin(USER) .on(USER.UID.eq(DATASET.OWNER_UID)) .where(USER.EMAIL.eq(ownerEmail)) .and(DATASET.NAME.eq(datasetName)) - .fetchOneInto(classOf[Dataset]) + .fetchOne() - if (dataset == null || !readableBy(ctx, ResourceTables.Dataset, dataset.getDid, reader)) { + if (dataset == null || !readableBy(ctx, ResourceTables.Dataset, dataset.value1(), reader)) { throw new FileNotFoundException(s"Dataset file $fileName not found.") } val datasetVersion = ctx - .selectFrom(DATASET_VERSION) - .where(DATASET_VERSION.DID.eq(dataset.getDid)) + .select(DATASET_VERSION.VERSION_HASH) + .from(DATASET_VERSION) + .where(DATASET_VERSION.DID.eq(dataset.value1())) .and(DATASET_VERSION.NAME.eq(versionName)) - .fetchOneInto(classOf[DatasetVersion]) + .fetchOne() if (datasetVersion == null) { throw new FileNotFoundException(s"Dataset file $fileName not found.") } - (dataset.getRepositoryName, datasetVersion.getVersionHash) + (dataset.value2(), datasetVersion.value1()) } /** @@ -271,28 +266,29 @@ object FileResolver { .createDSLContext() ) { ctx => val model = ctx - .select(MODEL.fields: _*) + .select(MODEL.MID, MODEL.REPOSITORY_NAME) .from(MODEL) .leftJoin(USER) .on(USER.UID.eq(MODEL.OWNER_UID)) .where(USER.EMAIL.eq(ownerEmail)) .and(MODEL.NAME.eq(modelName)) - .fetchOneInto(classOf[Model]) + .fetchOne() - if (model == null || !readableBy(ctx, ResourceTables.Model, model.getMid, reader)) { + if (model == null || !readableBy(ctx, ResourceTables.Model, model.value1(), reader)) { throw new FileNotFoundException(s"Model file $fileName not found.") } val modelVersion = ctx - .selectFrom(MODEL_VERSION) - .where(MODEL_VERSION.MID.eq(model.getMid)) + .select(MODEL_VERSION.VERSION_HASH) + .from(MODEL_VERSION) + .where(MODEL_VERSION.MID.eq(model.value1())) .and(MODEL_VERSION.NAME.eq(versionName)) - .fetchOneInto(classOf[ModelVersion]) + .fetchOne() if (modelVersion == null) { throw new FileNotFoundException(s"Model file $fileName not found.") } - (model.getRepositoryName, modelVersion.getVersionHash) + (model.value2(), modelVersion.value1()) } /** diff --git a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitAccessResource.scala b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitAccessResource.scala index 2faf81265d..f8665ca1bb 100644 --- a/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitAccessResource.scala +++ b/computing-unit-managing-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitAccessResource.scala @@ -119,13 +119,10 @@ class ComputingUnitAccessResource { * account matches — the service registers no ExceptionMapper for IllegalArgumentException, * so that would otherwise surface as an opaque HTTP 500. Shared by grant/revoke. */ - private def resolveUidByEmail(email: String): Integer = { - val user = userDao.fetchOneByEmail(email) - if (user == null || user.getIsPlaceholder) { - throw new BadRequestException("User with the given email does not exist") - } - user.getUid - } + private def resolveUidByEmail(email: String): Integer = + OwnerAccess + .registeredUid(context, email) + .getOrElse(throw new BadRequestException("User with the given email does not exist")) @GET @Produces(Array(MediaType.APPLICATION_JSON)) diff --git a/file-service/src/main/scala/org/apache/texera/service/resource/DatasetAccessResource.scala b/file-service/src/main/scala/org/apache/texera/service/resource/DatasetAccessResource.scala index ad26338968..cb8bc74804 100644 --- a/file-service/src/main/scala/org/apache/texera/service/resource/DatasetAccessResource.scala +++ b/file-service/src/main/scala/org/apache/texera/service/resource/DatasetAccessResource.scala @@ -27,7 +27,6 @@ import org.apache.texera.auth.SessionUser import org.apache.texera.dao.SqlServer import org.apache.texera.dao.SqlServer.withTransaction import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.pojos.User import org.apache.texera.service.resource.DatasetAccessResource.context import org.apache.texera.service.resource.ResourceTables.{Dataset => DATASET_RESOURCE} import org.jooq.DSLContext @@ -59,8 +58,8 @@ object DatasetAccessResource { uid: Integer ): PrivilegeEnum = ResourceAccess.privilegeOf(ctx, DATASET_RESOURCE, did, uid) - def getOwner(ctx: DSLContext, did: Integer): User = - ResourceAccess.owner(ctx, DATASET_RESOURCE, did) + def getOwnerEmail(ctx: DSLContext, did: Integer): String = + ResourceAccess.ownerEmailOf(ctx, DATASET_RESOURCE, did) } @Produces(Array(MediaType.APPLICATION_JSON)) diff --git a/file-service/src/main/scala/org/apache/texera/service/resource/DatasetResource.scala b/file-service/src/main/scala/org/apache/texera/service/resource/DatasetResource.scala index 5c2f8dfacf..6223f9810d 100644 --- a/file-service/src/main/scala/org/apache/texera/service/resource/DatasetResource.scala +++ b/file-service/src/main/scala/org/apache/texera/service/resource/DatasetResource.scala @@ -313,7 +313,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { DashboardDataset( targetDataset, - getOwner(ctx, did).getEmail, + getOwnerEmail(ctx, did), userAccessPrivilege, isOwner, withLakeFSErrorHandling(s"retrieving the size of dataset '${targetDataset.getName}'") { @@ -507,7 +507,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { LakeFSFileNode .fromLakeFSRepositoryCommittedObjects( resourceType, - Map((getOwner(ctx, did).getEmail, datasetName, newVersionName) -> fileNodes) + Map((getOwnerEmail(ctx, did), datasetName, newVersionName) -> fileNodes) ) ) } @@ -968,7 +968,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { ResourceUploadService .versionRootFileNodes( resourceType, - getOwner(ctx, did).getEmail, + getOwnerEmail(ctx, did), dataset.getName, latestVersion.getName, dataset.getRepositoryName, @@ -1167,7 +1167,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { val document = CoverImageUtils.openCoverOrBadRequest( resourceType, - getOwner(ctx, did).getEmail, + getOwnerEmail(ctx, did), dataset.getName, normalized ) @@ -1201,7 +1201,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { ) val document = CoverImageUtils - .openCover(resourceType, getOwner(ctx, did).getEmail, dataset.getName, coverImage) + .openCover(resourceType, getOwnerEmail(ctx, did), dataset.getName, coverImage) .getOrElse(throw new NotFoundException("No cover image")) Response @@ -1231,7 +1231,7 @@ class DatasetResource extends LazyLogging with ResourceUploadSupport { Response.ok(Map("url" -> null)).build() case Some(coverImage) => val url = CoverImageUtils - .openCover(resourceType, getOwner(ctx, did).getEmail, dataset.getName, coverImage) + .openCover(resourceType, getOwnerEmail(ctx, did), dataset.getName, coverImage) .map(CoverImageUtils.presignedUrl(_, coverImage)) Response.ok(Map("url" -> url.orNull)).build() } diff --git a/file-service/src/main/scala/org/apache/texera/service/resource/ModelAccessResource.scala b/file-service/src/main/scala/org/apache/texera/service/resource/ModelAccessResource.scala index 390afbc0bf..10d9f6a81f 100644 --- a/file-service/src/main/scala/org/apache/texera/service/resource/ModelAccessResource.scala +++ b/file-service/src/main/scala/org/apache/texera/service/resource/ModelAccessResource.scala @@ -27,7 +27,6 @@ import org.apache.texera.auth.SessionUser import org.apache.texera.dao.SqlServer import org.apache.texera.dao.SqlServer.withTransaction import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.pojos.User import org.apache.texera.service.resource.ModelAccessResource.context import org.apache.texera.service.resource.ResourceTables.{Model => MODEL_RESOURCE} import org.jooq.DSLContext @@ -59,8 +58,8 @@ object ModelAccessResource { uid: Integer ): PrivilegeEnum = ResourceAccess.privilegeOf(ctx, MODEL_RESOURCE, mid, uid) - def getOwner(ctx: DSLContext, mid: Integer): User = - ResourceAccess.owner(ctx, MODEL_RESOURCE, mid) + def getOwnerEmail(ctx: DSLContext, mid: Integer): String = + ResourceAccess.ownerEmailOf(ctx, MODEL_RESOURCE, mid) } @Produces(Array(MediaType.APPLICATION_JSON)) diff --git a/file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala b/file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala index 66d24f3bfd..9be7c6196e 100644 --- a/file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala +++ b/file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala @@ -210,7 +210,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { DashboardModel( targetModel, - getOwner(ctx, mid).getEmail, + getOwnerEmail(ctx, mid), userAccessPrivilege, isOwner, withLakeFSErrorHandling(s"retrieving the size of model '${targetModel.getName}'") { @@ -804,7 +804,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { LakeFSFileNode .fromLakeFSRepositoryCommittedObjects( ResourceType.Model, - Map((getOwner(ctx, mid).getEmail, modelName, newVersionName) -> fileNodes) + Map((getOwnerEmail(ctx, mid), modelName, newVersionName) -> fileNodes) ) ) } @@ -1008,7 +1008,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { val document = CoverImageUtils.openCoverOrBadRequest( ResourceType.Model, - getOwner(ctx, mid).getEmail, + getOwnerEmail(ctx, mid), model.getName, normalized ) @@ -1035,7 +1035,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { ) val document = CoverImageUtils - .openCover(ResourceType.Model, getOwner(ctx, mid).getEmail, model.getName, coverImage) + .openCover(ResourceType.Model, getOwnerEmail(ctx, mid), model.getName, coverImage) .getOrElse(throw new NotFoundException("No cover image")) Response @@ -1063,7 +1063,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { case None => Response.ok(Map("url" -> null)).build() case Some(coverImage) => val url = CoverImageUtils - .openCover(ResourceType.Model, getOwner(ctx, mid).getEmail, model.getName, coverImage) + .openCover(ResourceType.Model, getOwnerEmail(ctx, mid), model.getName, coverImage) .map(CoverImageUtils.presignedUrl(_, coverImage)) Response.ok(Map("url" -> url.orNull)).build() } @@ -1124,7 +1124,7 @@ class ModelResource extends LazyLogging with ResourceUploadSupport { ResourceUploadService .versionRootFileNodes( ResourceType.Model, - getOwner(ctx, mid).getEmail, + getOwnerEmail(ctx, mid), model.getName, modelVersion.getName, model.getRepositoryName, diff --git a/file-service/src/main/scala/org/apache/texera/service/util/S3ProxyServlet.scala b/file-service/src/main/scala/org/apache/texera/service/util/S3ProxyServlet.scala index 81685ca507..680d10142c 100644 --- a/file-service/src/main/scala/org/apache/texera/service/util/S3ProxyServlet.scala +++ b/file-service/src/main/scala/org/apache/texera/service/util/S3ProxyServlet.scala @@ -25,8 +25,11 @@ import org.apache.texera.auth.JwtParser import org.apache.texera.common.config.StorageConfig import org.apache.texera.dao.SqlServer import org.apache.texera.dao.SqlServer.withTransaction -import org.apache.texera.dao.jooq.generated.tables.daos.{DatasetDao, ModelDao} -import org.apache.texera.service.resource.{DatasetAccessResource, ModelAccessResource} +import org.apache.texera.service.resource.{ + DatasetAccessResource, + ModelAccessResource, + ResourceAccess +} import software.amazon.awssdk.auth.credentials.AwsBasicCredentials import software.amazon.awssdk.auth.signer.AwsS3V4Signer import software.amazon.awssdk.auth.signer.params.AwsS3V4SignerParams @@ -145,19 +148,12 @@ class S3ProxyServlet extends HttpServlet with LazyLogging { */ private def authorizedToRead(uid: Integer, repositoryName: String): Boolean = withTransaction(SqlServer.getInstance().createDSLContext()) { ctx => - val datasets = new DatasetDao(ctx.configuration()) - .fetchByRepositoryName(repositoryName) - .asScala - .toList - val models = new ModelDao(ctx.configuration()) - .fetchByRepositoryName(repositoryName) - .asScala - .toList + val (datasets, models) = ResourceAccess.repositoryIds(ctx, repositoryName) (datasets, models) match { - case (dataset :: Nil, Nil) => - DatasetAccessResource.userHasReadAccess(ctx, dataset.getDid, uid) - case (Nil, model :: Nil) => - ModelAccessResource.userHasReadAccess(ctx, model.getMid, uid) + case (did :: Nil, Nil) => + DatasetAccessResource.userHasReadAccess(ctx, did, uid) + case (Nil, mid :: Nil) => + ModelAccessResource.userHasReadAccess(ctx, mid, uid) case (Nil, Nil) => false case _ => diff --git a/file-service/src/test/scala/org/apache/texera/service/resource/AccessResourceBehaviors.scala b/file-service/src/test/scala/org/apache/texera/service/resource/AccessResourceBehaviors.scala index b9ac75f5f7..8f79657332 100644 --- a/file-service/src/test/scala/org/apache/texera/service/resource/AccessResourceBehaviors.scala +++ b/file-service/src/test/scala/org/apache/texera/service/resource/AccessResourceBehaviors.scala @@ -68,7 +68,7 @@ abstract class AccessResourceBehaviors[R <: Record, A <: Record](tables: Resourc protected def userPrivilege(ctx: DSLContext, id: Integer, uid: Integer): PrivilegeEnum protected def userCanRead(ctx: DSLContext, id: Integer, uid: Integer): Boolean protected def userCanWrite(ctx: DSLContext, id: Integer, uid: Integer): Boolean - protected def resourceOwner(ctx: DSLContext, id: Integer): User + protected def resourceOwnerEmail(ctx: DSLContext, id: Integer): String // the resource's endpoints protected def grant(id: Integer, email: String, privilege: String, user: SessionUser): Response @@ -101,6 +101,21 @@ abstract class AccessResourceBehaviors[R <: Record, A <: Record](tables: Resourc private lazy val readGranteeSession = new SessionUser(readGranteeUser) private lazy val strangerSession = new SessionUser(strangerUser) + // An owner with no email, and a private resource they own; inserted on first use. + private lazy val emaillessOwner: User = { + val user = new User + user.setName(s"${label}_emailless_owner") + user.setRole(UserRoleEnum.REGULAR) + new UserDao(getDSLContext.configuration()).insert(user) + user + } + private lazy val emaillessOwnedId: Integer = insertResource( + s"emailless-$label", + s"$label whose owner has no email", + isPublic = false, + emaillessOwner.getUid + ) + private def grantDirectly(id: Integer, uid: Integer, privilege: PrivilegeEnum): Unit = getDSLContext .insertInto(tables.accessTable) @@ -232,12 +247,16 @@ abstract class AccessResourceBehaviors[R <: Record, A <: Record](tables: Resourc userCanWrite(getDSLContext, nonExistentId, ownerUser.getUid) shouldBe false } - "getOwner" should "return the owning user" in { - resourceOwner(getDSLContext, privateId).getEmail shouldEqual ownerUser.getEmail + "getOwnerEmail" should "return the owner's email" in { + resourceOwnerEmail(getDSLContext, privateId) shouldEqual ownerUser.getEmail } it should s"return null for a nonexistent $label" in { - resourceOwner(getDSLContext, nonExistentId) shouldBe null + resourceOwnerEmail(getDSLContext, nonExistentId) shouldBe null + } + + it should "return null for an owner without an email" in { + resourceOwnerEmail(getDSLContext, emaillessOwnedId) shouldBe null } // =========================================================================== @@ -422,6 +441,10 @@ abstract class AccessResourceBehaviors[R <: Record, A <: Record](tables: Resourc ownerEmail(publicId, strangerSession) shouldEqual ownerUser.getEmail } + it should "return null, not an empty string, for an owner without an email" in { + ownerEmail(emaillessOwnedId, new SessionUser(emaillessOwner)) shouldBe null + } + it should s"be forbidden for a nonexistent $label" in { assertThrows[ForbiddenException] { ownerEmail(nonExistentId, ownerSession) diff --git a/file-service/src/test/scala/org/apache/texera/service/resource/DatasetAccessResourceSpec.scala b/file-service/src/test/scala/org/apache/texera/service/resource/DatasetAccessResourceSpec.scala index a9323256f1..f3a126f34f 100644 --- a/file-service/src/test/scala/org/apache/texera/service/resource/DatasetAccessResourceSpec.scala +++ b/file-service/src/test/scala/org/apache/texera/service/resource/DatasetAccessResourceSpec.scala @@ -23,7 +23,7 @@ import jakarta.ws.rs.core.Response import org.apache.texera.auth.SessionUser import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum import org.apache.texera.dao.jooq.generated.tables.daos.DatasetDao -import org.apache.texera.dao.jooq.generated.tables.pojos.{Dataset, User} +import org.apache.texera.dao.jooq.generated.tables.pojos.Dataset import org.jooq.DSLContext class DatasetAccessResourceSpec extends AccessResourceBehaviors(ResourceTables.Dataset) { @@ -62,8 +62,8 @@ class DatasetAccessResourceSpec extends AccessResourceBehaviors(ResourceTables.D override protected def userCanWrite(ctx: DSLContext, id: Integer, uid: Integer): Boolean = DatasetAccessResource.userHasWriteAccess(ctx, id, uid) - override protected def resourceOwner(ctx: DSLContext, id: Integer): User = - DatasetAccessResource.getOwner(ctx, id) + override protected def resourceOwnerEmail(ctx: DSLContext, id: Integer): String = + DatasetAccessResource.getOwnerEmail(ctx, id) override protected def grant( id: Integer, diff --git a/file-service/src/test/scala/org/apache/texera/service/resource/ModelAccessResourceSpec.scala b/file-service/src/test/scala/org/apache/texera/service/resource/ModelAccessResourceSpec.scala index 93ae52cfae..bd337ea0e4 100644 --- a/file-service/src/test/scala/org/apache/texera/service/resource/ModelAccessResourceSpec.scala +++ b/file-service/src/test/scala/org/apache/texera/service/resource/ModelAccessResourceSpec.scala @@ -23,7 +23,7 @@ import jakarta.ws.rs.core.Response import org.apache.texera.auth.SessionUser import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum import org.apache.texera.dao.jooq.generated.tables.daos.ModelDao -import org.apache.texera.dao.jooq.generated.tables.pojos.{Model, User} +import org.apache.texera.dao.jooq.generated.tables.pojos.Model import org.jooq.DSLContext class ModelAccessResourceSpec extends AccessResourceBehaviors(ResourceTables.Model) { @@ -63,8 +63,8 @@ class ModelAccessResourceSpec extends AccessResourceBehaviors(ResourceTables.Mod override protected def userCanWrite(ctx: DSLContext, id: Integer, uid: Integer): Boolean = ModelAccessResource.userHasWriteAccess(ctx, id, uid) - override protected def resourceOwner(ctx: DSLContext, id: Integer): User = - ModelAccessResource.getOwner(ctx, id) + override protected def resourceOwnerEmail(ctx: DSLContext, id: Integer): String = + ModelAccessResource.getOwnerEmail(ctx, id) override protected def grant( id: Integer,
