Skip to content

Commit

Permalink
[SC-5613] Better error message when authorization denied
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Fixed the error message so it shows the permission that is required

## How was this patch tested?

Existing unit tests

Author: Srinath Shankar <srinath@databricks.com>

Closes apache#172 from srinathshankar/aclerrormess.
  • Loading branch information
srinathshankar committed Jan 16, 2017
1 parent 83b9ada commit d058ab5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class CheckPermissions(catalog: PublicCatalog, aclClient: AclClient)
val permissionChecker = new PermissionChecker(aclClient)

def apply(plan: LogicalPlan): Unit = {
if (!getRequestsToCheck(plan).forall(permissionChecker)) {
throw new SecurityException("Principal is not authorized to execute the given query")
getRequestsToCheck(plan).foreach { request =>
if (!permissionChecker(request)) {
throw new SecurityException(toErrorMessageForFailedRequest(request))
}
}
}

Expand Down Expand Up @@ -361,4 +363,13 @@ class CheckPermissions(catalog: PublicCatalog, aclClient: AclClient)
Request(securable, ReadMetadata, toExplainRequests(children.toSeq).toSet)
}
}

private def toErrorMessageForFailedRequest(request: Request): String = {
val actionString = request.action match {
case Own => "own"
case _ => s"have permission ${request.action} on"
}

s"User does not ${actionString} ${request.securable.prettyString}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sealed trait Securable {
val name: String
val key: AnyRef
def typeName: String = getClass.getSimpleName.toUpperCase
def prettyString: String
}

object Securable {
Expand Down Expand Up @@ -159,6 +160,7 @@ object Securable {
case object Catalog extends Securable {
override val key: Option[Nothing] = None
override val name: String = "/CATALOG/`default`"
override def prettyString: String = "CATALOG"
}

case class Database(key: String) extends Securable {
Expand All @@ -167,6 +169,7 @@ case class Database(key: String) extends Securable {
val name = Securable.addBackticks(key)
s"${Catalog.name}/DATABASE/$name"
}
override def prettyString: String = s"database ${Securable.addBackticks(key)}"
}

sealed trait DatabaseSecurable extends Securable {
Expand All @@ -181,6 +184,7 @@ sealed trait DatabaseSecurable extends Securable {

case class Table(key: TableIdentifier) extends DatabaseSecurable {
assert(key != null)
override def prettyString: String = s"table ${key.quotedString}"
}

object Table {
Expand All @@ -192,6 +196,7 @@ sealed trait SecurableFunction extends Securable

case class Function(key: FunctionIdentifier) extends DatabaseSecurable with SecurableFunction {
assert(key != null)
override def prettyString: String = s"function ${key.quotedString}"
}

object Function {
Expand All @@ -203,12 +208,14 @@ case object AnonymousFunction extends SecurableFunction {
override val key: Option[Nothing] = None
override def typeName: String = "ANONYMOUS_FUNCTION"
override val name: String = "/" + typeName
override def prettyString: String = s"anonymous function"
}

case object AnyFile extends Securable {
override val key: Option[Nothing] = None
override def typeName: String = "ANY_FILE"
override val name: String = "/" + typeName
override def prettyString: String = s"any file"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ usr1
-- !query 3 schema
struct<>
-- !query 3 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission SELECT on table `db1`.`vw1`


-- !query 4
Expand Down Expand Up @@ -147,7 +147,7 @@ usr1
-- !query 12 schema
struct<>
-- !query 12 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on database `db1`


-- !query 13
Expand All @@ -157,7 +157,7 @@ usr1
-- !query 13 schema
struct<>
-- !query 13 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission SELECT on table `db1`.`vw1`


-- !query 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ usr1
-- !query 7 schema
struct<>
-- !query 7 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on function `perm`.`fn1`


-- !query 8
Expand All @@ -90,7 +90,7 @@ usr1
-- !query 8 schema
struct<>
-- !query 8 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on function `perm`.`fn1`


-- !query 9
Expand All @@ -100,7 +100,7 @@ usr1
-- !query 9 schema
struct<>
-- !query 9 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on table `perm`.`vw1`


-- !query 10
Expand Down Expand Up @@ -182,7 +182,7 @@ usr1
-- !query 16 schema
struct<>
-- !query 16 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on function `perm`.`fn1`


-- !query 17
Expand All @@ -192,7 +192,7 @@ usr1
-- !query 17 schema
struct<>
-- !query 17 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission SELECT on function `perm`.`fn1`


-- !query 18
Expand All @@ -202,7 +202,7 @@ usr1
-- !query 18 schema
struct<>
-- !query 18 output
java.lang.SecurityException: Principal is not authorized to execute the given query
java.lang.SecurityException: User does not have permission READ_METADATA on function `perm`.`fn1`


-- !query 19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.apache.spark.sql.jdbc.JdbcDialects
*
* To re-generate golden files, run:
* {{{
* SPARK_GENERATE_GOLDEN_FILES=1 build/sbt -Phive -Phive-thriftserver "thriftserver/test-only *ThriftEndToEndAclTestSuite"
* SPARK_GENERATE_GOLDEN_FILES=1 build/sbt -Phive -Phive-thriftserver "hive-thriftserver/test-only *ThriftEndToEndAclTestSuite"
* }}}
*
* The format for input files is simple:
Expand Down

0 comments on commit d058ab5

Please sign in to comment.