parshimers commented on code in PR #6896:
URL: https://github.com/apache/texera/pull/6896#discussion_r3983651358


##########
access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 org.apache.texera.service.resource
+
+import com.typesafe.scalalogging.LazyLogging
+import io.dropwizard.auth.Auth
+import jakarta.annotation.security.RolesAllowed
+import jakarta.ws.rs._
+import jakarta.ws.rs.core.MediaType
+import org.apache.texera.auth.JwtAuth.{jwtClaims, jwtToken}
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.auth.util.ComputingUnitAccess
+import org.apache.texera.common.config.{EnvironmentalVariable, 
KubernetesConfig}
+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,
+  MountRequestValidation,
+  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.
+  *
+  * Read access to the data is not decided here. file-service's S3 proxy 
authorizes every
+  * read, so a mount of a repository the user cannot read reads nothing.
+  *
+  * Not routed at the gateway — these endpoints are reached in-cluster, by the 
engine.

Review Comment:
   let's say i write a UDF that makes a network call. that's allowed, right? so 
i don't really understand this comment. it shouldn't really make a difference 
from a security standpoint, one way or another



##########
access-control-service/src/main/scala/org/apache/texera/service/util/MounterClient.scala:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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 org.apache.texera.service.util
+
+import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+
+import java.net.{HttpURLConnection, URI}
+import java.nio.charset.StandardCharsets
+import java.nio.file.{Files, Paths}
+import scala.util.Using
+
+/**
+  * HTTP client for the per-node `texera-mounter`.
+  *
+  * The mounter is privileged and listens on a hostPort, so it admits exactly 
one caller: it
+  * requires a service-account token minted for its own audience and checks it 
with the
+  * Kubernetes TokenReview API (see `authenticate_caller` in 
`bin/mounter/mounter.py`). That
+  * caller is this service, which is why the client lives here.
+  */
+class MounterClient(tokenPath: String = MounterDefaults.ProjectedTokenPath) {
+
+  import MounterClient._
+
+  private val mapper: ObjectMapper = new 
ObjectMapper().registerModule(DefaultScalaModule)
+
+  private val connectTimeoutMs = 10000
+  private val readTimeoutMs = 35000
+
+  private def baseUrl(nodeIp: String, port: Int): String = 
s"http://$nodeIp:$port";
+
+  // Read per call, not cached: the kubelet rewrites the projected token in 
place.
+  private def mounterToken(): String =
+    try Files.readString(Paths.get(tokenPath)).trim
+    catch {
+      case e: Exception =>
+        throw new IllegalStateException(
+          s"cannot read the mounter service-account token at $tokenPath; 
without it this " +
+            s"service cannot authenticate to the node mounter: ${e.getMessage}"
+        )
+    }
+
+  def mount(
+      nodeIp: String,
+      port: Int,
+      cuid: String,
+      repositoryName: String,
+      commitHash: String,
+      jwt: String,
+      fileServiceBase: String
+  ): String = {
+    MountRequestValidation.validate(cuid, repositoryName, commitHash)
+
+    val body = mapper.createObjectNode()
+    body.put("cuid", cuid)
+    body.put("repositoryName", repositoryName)
+    body.put("commitHash", commitHash)
+    body.put("jwt", jwt)
+    body.put("fileServiceBase", fileServiceBase)
+
+    val response = send("POST", s"${baseUrl(nodeIp, port)}/mount", 
Some(body.toString))
+    Option(response.get("mountPath")).map(_.asText()).getOrElse("")

Review Comment:
   why do we want to return an empty string here in the else? doesn't the 
response mount path being empty mean something went wrong?



##########
access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 org.apache.texera.service.resource
+
+import com.typesafe.scalalogging.LazyLogging
+import io.dropwizard.auth.Auth
+import jakarta.annotation.security.RolesAllowed
+import jakarta.ws.rs._
+import jakarta.ws.rs.core.MediaType
+import org.apache.texera.auth.JwtAuth.{jwtClaims, jwtToken}
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.auth.util.ComputingUnitAccess
+import org.apache.texera.common.config.{EnvironmentalVariable, 
KubernetesConfig}
+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,
+  MountRequestValidation,
+  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.
+  *
+  * Read access to the data is not decided here. file-service's S3 proxy 
authorizes every
+  * read, so a mount of a repository the user cannot read reads nothing.
+  *
+  * Not routed at the gateway — these endpoints are reached in-cluster, by the 
engine.
+  */
+@Path("/mounts")
+@RolesAllowed(Array("REGULAR", "ADMIN"))
+@Produces(Array(MediaType.APPLICATION_JSON))
+class ComputingUnitMountResource(
+    mounterEnabled: Boolean,
+    mounterPort: Option[Int],

Review Comment:
   why is the port and url part of this and not pulled in through the chart 
somehow?



##########
access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 org.apache.texera.service.resource
+
+import com.typesafe.scalalogging.LazyLogging
+import io.dropwizard.auth.Auth
+import jakarta.annotation.security.RolesAllowed
+import jakarta.ws.rs._
+import jakarta.ws.rs.core.MediaType
+import org.apache.texera.auth.JwtAuth.{jwtClaims, jwtToken}
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.auth.util.ComputingUnitAccess
+import org.apache.texera.common.config.{EnvironmentalVariable, 
KubernetesConfig}
+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,
+  MountRequestValidation,
+  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.
+  *
+  * Read access to the data is not decided here. file-service's S3 proxy 
authorizes every
+  * read, so a mount of a repository the user cannot read reads nothing.
+  *
+  * Not routed at the gateway — these endpoints are reached in-cluster, by the 
engine.
+  */
+@Path("/mounts")
+@RolesAllowed(Array("REGULAR", "ADMIN"))
+@Produces(Array(MediaType.APPLICATION_JSON))
+class ComputingUnitMountResource(
+    mounterEnabled: Boolean,
+    mounterPort: Option[Int],
+    fileServiceBaseUrl: Option[String],
+    nodeLocator: ComputingUnitNodeLocator,
+    mounter: MounterClient
+) extends LazyLogging {
+
+  // No-arg constructor for Jersey reflection. Tests use the param-ful form.
+  def this() =
+    this(
+      KubernetesConfig.mounterEnabled,
+      EnvironmentalVariable.get(MounterPortVariable).map(_.trim.toInt),
+      EnvironmentalVariable.get(FileServiceUrlVariable),
+      ComputingUnitNodeLocator,
+      MounterClient
+    )
+
+  @POST
+  @Path("/{cuid}")
+  @Consumes(Array(MediaType.APPLICATION_JSON))
+  def mount(
+      @PathParam("cuid") cuid: Int,
+      request: MountRequest,
+      @Auth user: SessionUser
+  ): MountInfo = {
+    val (port, fileService) = requireMountConfiguration()
+    try MountRequestValidation.validate(cuid.toString, request.repositoryName, 
request.commitHash)
+    catch { case e: IllegalArgumentException => throw new 
BadRequestException(e.getMessage) }
+    requireComputingUnitAccess(cuid, user)
+    requireRepositoryReadAccess(request.repositoryName, request.commitHash, 
user.getUid)
+    val nodeIp = requireNodeIp(cuid)
+
+    // A token minted here, after the access check: GeeseFS keeps presenting 
it for the life
+    // of the mount, so it must be one this service vouched for.
+    val mountPath =
+      try {
+        mounter.mount(
+          nodeIp,
+          port,
+          cuid.toString,
+          request.repositoryName,
+          request.commitHash,
+          jwtToken(jwtClaims(user.getUser)),

Review Comment:
   this is the same token the user uses to login, right? why not use a separate 
token that is only really used for these purposes, specifically?



##########
access-control-service/src/main/scala/org/apache/texera/service/resource/ComputingUnitMountResource.scala:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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 org.apache.texera.service.resource
+
+import com.typesafe.scalalogging.LazyLogging
+import io.dropwizard.auth.Auth
+import jakarta.annotation.security.RolesAllowed
+import jakarta.ws.rs._
+import jakarta.ws.rs.core.MediaType
+import org.apache.texera.auth.JwtAuth.{jwtClaims, jwtToken}
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.auth.util.ComputingUnitAccess
+import org.apache.texera.common.config.{EnvironmentalVariable, 
KubernetesConfig}
+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,
+  MountRequestValidation,
+  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.
+  *
+  * Read access to the data is not decided here. file-service's S3 proxy 
authorizes every
+  * read, so a mount of a repository the user cannot read reads nothing.
+  *
+  * Not routed at the gateway — these endpoints are reached in-cluster, by the 
engine.
+  */
+@Path("/mounts")
+@RolesAllowed(Array("REGULAR", "ADMIN"))
+@Produces(Array(MediaType.APPLICATION_JSON))
+class ComputingUnitMountResource(
+    mounterEnabled: Boolean,
+    mounterPort: Option[Int],
+    fileServiceBaseUrl: Option[String],
+    nodeLocator: ComputingUnitNodeLocator,
+    mounter: MounterClient
+) extends LazyLogging {
+
+  // No-arg constructor for Jersey reflection. Tests use the param-ful form.
+  def this() =
+    this(
+      KubernetesConfig.mounterEnabled,
+      EnvironmentalVariable.get(MounterPortVariable).map(_.trim.toInt),
+      EnvironmentalVariable.get(FileServiceUrlVariable),
+      ComputingUnitNodeLocator,
+      MounterClient
+    )
+
+  @POST
+  @Path("/{cuid}")
+  @Consumes(Array(MediaType.APPLICATION_JSON))
+  def mount(
+      @PathParam("cuid") cuid: Int,
+      request: MountRequest,
+      @Auth user: SessionUser
+  ): MountInfo = {
+    val (port, fileService) = requireMountConfiguration()
+    try MountRequestValidation.validate(cuid.toString, request.repositoryName, 
request.commitHash)
+    catch { case e: IllegalArgumentException => throw new 
BadRequestException(e.getMessage) }
+    requireComputingUnitAccess(cuid, user)
+    requireRepositoryReadAccess(request.repositoryName, request.commitHash, 
user.getUid)
+    val nodeIp = requireNodeIp(cuid)
+
+    // A token minted here, after the access check: GeeseFS keeps presenting 
it for the life
+    // of the mount, so it must be one this service vouched for.
+    val mountPath =
+      try {
+        mounter.mount(
+          nodeIp,
+          port,
+          cuid.toString,
+          request.repositoryName,
+          request.commitHash,
+          jwtToken(jwtClaims(user.getUser)),
+          fileService
+        )
+      } catch {
+        case e: IllegalArgumentException =>
+          throw new BadRequestException(e.getMessage)
+        case e: MounterClient.MounterRequestException =>
+          logger.warn(s"node mounter at $nodeIp refused a mount for computing 
unit $cuid", e)
+          throw new BadRequestException(e.getMessage)
+      }
+
+    logger.info(
+      s"user ${user.getUid} mounted 
${request.repositoryName}:${request.commitHash} " +
+        s"onto computing unit $cuid at $mountPath"
+    )
+    MountInfo(request.repositoryName, request.commitHash, mountPath)

Review Comment:
   should there be some limit on how many mounts a CU can have? 



##########
access-control-service/src/main/scala/org/apache/texera/service/util/MounterClient.scala:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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 org.apache.texera.service.util
+
+import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+
+import java.net.{HttpURLConnection, URI}
+import java.nio.charset.StandardCharsets
+import java.nio.file.{Files, Paths}
+import scala.util.Using
+
+/**
+  * HTTP client for the per-node `texera-mounter`.
+  *
+  * The mounter is privileged and listens on a hostPort, so it admits exactly 
one caller: it
+  * requires a service-account token minted for its own audience and checks it 
with the
+  * Kubernetes TokenReview API (see `authenticate_caller` in 
`bin/mounter/mounter.py`). That
+  * caller is this service, which is why the client lives here.
+  */
+class MounterClient(tokenPath: String = MounterDefaults.ProjectedTokenPath) {
+
+  import MounterClient._
+
+  private val mapper: ObjectMapper = new 
ObjectMapper().registerModule(DefaultScalaModule)
+
+  private val connectTimeoutMs = 10000
+  private val readTimeoutMs = 35000
+
+  private def baseUrl(nodeIp: String, port: Int): String = 
s"http://$nodeIp:$port";
+
+  // Read per call, not cached: the kubelet rewrites the projected token in 
place.
+  private def mounterToken(): String =
+    try Files.readString(Paths.get(tokenPath)).trim
+    catch {
+      case e: Exception =>
+        throw new IllegalStateException(
+          s"cannot read the mounter service-account token at $tokenPath; 
without it this " +
+            s"service cannot authenticate to the node mounter: ${e.getMessage}"
+        )
+    }
+
+  def mount(
+      nodeIp: String,
+      port: Int,
+      cuid: String,
+      repositoryName: String,
+      commitHash: String,
+      jwt: String,
+      fileServiceBase: String
+  ): String = {
+    MountRequestValidation.validate(cuid, repositoryName, commitHash)
+
+    val body = mapper.createObjectNode()
+    body.put("cuid", cuid)
+    body.put("repositoryName", repositoryName)
+    body.put("commitHash", commitHash)
+    body.put("jwt", jwt)
+    body.put("fileServiceBase", fileServiceBase)
+
+    val response = send("POST", s"${baseUrl(nodeIp, port)}/mount", 
Some(body.toString))
+    Option(response.get("mountPath")).map(_.asText()).getOrElse("")
+  }
+
+  private def send(method: String, url: String, body: Option[String]): 
JsonNode = {
+    val connection = 
URI.create(url).toURL.openConnection().asInstanceOf[HttpURLConnection]
+    connection.setRequestMethod(method)
+    connection.setRequestProperty("Authorization", s"Bearer ${mounterToken()}")
+    connection.setConnectTimeout(connectTimeoutMs)
+    connection.setReadTimeout(readTimeoutMs)
+    body.foreach { _ =>
+      connection.setRequestProperty("Content-Type", "application/json")
+      connection.setDoOutput(true)
+    }
+    try {
+      body.foreach(payload =>
+        
Using(connection.getOutputStream)(_.write(payload.getBytes(StandardCharsets.UTF_8)))
+      )
+      val code = connection.getResponseCode
+      val stream =
+        if (code >= 200 && code < 300) connection.getInputStream else 
connection.getErrorStream
+      val responseBody = Option(stream)
+        .map(s => new String(s.readAllBytes(), StandardCharsets.UTF_8))
+        .getOrElse("")
+      if (code < 200 || code >= 300) {
+        throw new MounterRequestException(code, s"mounter $method failed: HTTP 
$code $responseBody")
+      }
+      if (responseBody.isEmpty) mapper.createObjectNode() else 
mapper.readTree(responseBody)
+    } finally {
+      connection.disconnect()
+    }
+  }
+}
+
+object MounterClient extends MounterClient(MounterDefaults.ProjectedTokenPath) 
{

Review Comment:
   idk not a scala expert but from a java standpoint this is weird. like why 
extend a class you're defining in the same file?



##########
build.sbt:
##########


Review Comment:
   what's this action freaking out about here?



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