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

feiwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new caf2f2f3a [KYUUBI #3681] Fix rest api response message
caf2f2f3a is described below

commit caf2f2f3a6a588a522f99e9844615cd44e21d598
Author: Tianlin Liao <[email protected]>
AuthorDate: Mon Oct 24 11:17:58 2022 +0800

    [KYUUBI #3681] Fix rest api response message
    
    ### _Why are the changes needed?_
    
    ### _How was this patch tested?_
    - [x] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #3682 from lightning-L/not-found-exception.
    
    Closes #3681
    
    0e4d8ee6c [Tianlin Liao] [KYUUBI #3681] fix rest api response message
    
    Authored-by: Tianlin Liao <[email protected]>
    Signed-off-by: Fei Wang <[email protected]>
---
 .../org/apache/kyuubi/server/KyuubiRestFrontendService.scala  |  7 +++++--
 .../org/apache/kyuubi/server/api/v1/BatchesResource.scala     | 11 +++++++----
 .../apache/kyuubi/server/api/v1/BatchesResourceSuite.scala    |  6 ++++++
 .../apache/kyuubi/server/rest/client/BatchRestApiSuite.scala  |  7 +++++++
 4 files changed, 25 insertions(+), 6 deletions(-)

diff --git 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiRestFrontendService.scala
 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiRestFrontendService.scala
index 3d0ee3d52..56065b357 100644
--- 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiRestFrontendService.scala
+++ 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiRestFrontendService.scala
@@ -21,7 +21,8 @@ import java.util.EnumSet
 import java.util.concurrent.{Future, TimeUnit}
 import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger}
 import javax.servlet.DispatcherType
-import javax.ws.rs.NotAllowedException
+import javax.ws.rs.WebApplicationException
+import javax.ws.rs.core.Response.Status
 
 import com.google.common.annotations.VisibleForTesting
 import org.apache.hadoop.conf.Configuration
@@ -187,7 +188,9 @@ class KyuubiRestFrontendService(override val serverable: 
Serverable)
     try {
       getProxyUser(sessionConf, ipAddress, realUser)
     } catch {
-      case t: Throwable => throw new NotAllowedException(t.getMessage)
+      case t: Throwable => throw new WebApplicationException(
+          t.getMessage,
+          Status.METHOD_NOT_ALLOWED)
     }
   }
 
diff --git 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/BatchesResource.scala
 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/BatchesResource.scala
index ceeee3d7f..b0333b309 100644
--- 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/BatchesResource.scala
+++ 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/BatchesResource.scala
@@ -21,6 +21,7 @@ import java.util.Locale
 import java.util.concurrent.ConcurrentHashMap
 import javax.ws.rs._
 import javax.ws.rs.core.MediaType
+import javax.ws.rs.core.Response.Status
 
 import scala.collection.JavaConverters._
 import scala.util.control.NonFatal
@@ -313,8 +314,9 @@ private[v1] class BatchesResource extends ApiRequestContext 
with Logging {
 
     Option(sessionManager.getBatchSessionImpl(sessionHandle)).map { 
batchSession =>
       if (userName != batchSession.user) {
-        throw new NotAllowedException(
-          s"$userName is not allowed to close the session belong to 
${batchSession.user}")
+        throw new WebApplicationException(
+          s"$userName is not allowed to close the session belong to 
${batchSession.user}",
+          Status.METHOD_NOT_ALLOWED)
       }
       sessionManager.closeSession(batchSession.handle)
       val (success, msg) = batchSession.batchJobSubmissionOp.getKillMessage
@@ -322,8 +324,9 @@ private[v1] class BatchesResource extends ApiRequestContext 
with Logging {
     }.getOrElse {
       Option(sessionManager.getBatchMetadata(batchId)).map { metadata =>
         if (userName != metadata.username) {
-          throw new NotAllowedException(
-            s"$userName is not allowed to close the session belong to 
${metadata.username}")
+          throw new WebApplicationException(
+            s"$userName is not allowed to close the session belong to 
${metadata.username}",
+            Status.METHOD_NOT_ALLOWED)
         } else if 
(OperationState.isTerminal(OperationState.withName(metadata.state)) ||
           metadata.kyuubiInstance == fe.connectionUrl) {
           new CloseBatchResponse(false, s"The batch[$metadata] has been 
terminated.")
diff --git 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/BatchesResourceSuite.scala
 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/BatchesResourceSuite.scala
index 30ed1e8a1..1f59a7dc6 100644
--- 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/BatchesResourceSuite.scala
+++ 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/BatchesResourceSuite.scala
@@ -83,6 +83,8 @@ class BatchesResourceSuite extends KyuubiFunSuite with 
RestFrontendTestHelper wi
       .request(MediaType.APPLICATION_JSON_TYPE)
       .post(Entity.entity(proxyUserRequest, MediaType.APPLICATION_JSON_TYPE))
     assert(405 == proxyUserResponse.getStatus)
+    var errorMessage = "Failed to validate proxy privilege of anonymous for 
root"
+    
assert(proxyUserResponse.readEntity(classOf[String]).contains(errorMessage))
 
     var getBatchResponse = webTarget.path(s"api/v1/batches/${batch.getId()}")
       .request(MediaType.APPLICATION_JSON_TYPE)
@@ -138,6 +140,8 @@ class BatchesResourceSuite extends KyuubiFunSuite with 
RestFrontendTestHelper wi
       .header(AUTHORIZATION_HEADER, s"BASIC $encodeAuthorization")
       .delete()
     assert(405 == deleteBatchResponse.getStatus)
+    errorMessage = s"${batch.getId()} is not allowed to close the session 
belong to anonymous"
+    
assert(deleteBatchResponse.readEntity(classOf[String]).contains(errorMessage))
 
     // invalid batchId
     deleteBatchResponse = webTarget.path(s"api/v1/batches/notValidUUID")
@@ -157,6 +161,8 @@ class BatchesResourceSuite extends KyuubiFunSuite with 
RestFrontendTestHelper wi
       .request(MediaType.APPLICATION_JSON_TYPE)
       .delete()
     assert(405 == deleteBatchResponse.getStatus)
+    errorMessage = "Failed to validate proxy privilege of anonymous for 
invalidProxy"
+    
assert(deleteBatchResponse.readEntity(classOf[String]).contains(errorMessage))
 
     // check close batch session
     deleteBatchResponse = webTarget.path(s"api/v1/batches/${batch.getId()}")
diff --git 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/rest/client/BatchRestApiSuite.scala
 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/rest/client/BatchRestApiSuite.scala
index d144d61be..df6beb22f 100644
--- 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/rest/client/BatchRestApiSuite.scala
+++ 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/rest/client/BatchRestApiSuite.scala
@@ -58,6 +58,13 @@ class BatchRestApiSuite extends RestClientTestHelper with 
BatchTestHelper {
     val closeResp = batchRestApi.deleteBatch(batch.getId(), null)
     assert(closeResp.getMsg.nonEmpty)
 
+    // delete batch - error
+    val e = intercept[KyuubiRestException] {
+      batchRestApi.deleteBatch(batch.getId(), "fake")
+    }
+    assert(e.getCause.toString.contains(
+      s"Failed to validate proxy privilege of ${ldapUser} for fake"))
+
     basicKyuubiRestClient.close()
   }
 

Reply via email to