Skip to content

[SPARK-30797][SQL] Set tradition user/group/other permission to ACL entries when setting up ACLs in truncate table #27548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package org.apache.spark.sql.execution.command

import java.net.{URI, URISyntaxException}

import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer
import scala.util.Try
import scala.util.control.NonFatal

import org.apache.hadoop.fs.{FileContext, FsConstants, Path}
import org.apache.hadoop.fs.permission.{AclEntry, FsPermission}
import org.apache.hadoop.fs.permission.{AclEntry, AclEntryScope, AclEntryType, FsAction, FsPermission}

import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
import org.apache.spark.sql.catalyst.TableIdentifier
Expand Down Expand Up @@ -538,12 +539,27 @@ case class TruncateTableCommand(
}
}
optAcls.foreach { acls =>
val aclEntries = acls.asScala.filter(_.getName != null).asJava

// If the path doesn't have default ACLs, `setAcl` API will throw an error
// as it expects user/group/other permissions must be in ACL entries.
// So we need to add tradition user/group/other permission
// in the form of ACL.
optPermission.map { permission =>
aclEntries.add(newAclEntry(AclEntryScope.ACCESS,
AclEntryType.USER, permission.getUserAction()))
aclEntries.add(newAclEntry(AclEntryScope.ACCESS,
AclEntryType.GROUP, permission.getGroupAction()))
aclEntries.add(newAclEntry(AclEntryScope.ACCESS,
AclEntryType.OTHER, permission.getOtherAction()))
}

try {
fs.setAcl(path, acls)
fs.setAcl(path, aclEntries)
} catch {
case NonFatal(e) =>
throw new SecurityException(
s"Failed to set original ACL $acls back to " +
s"Failed to set original ACL $aclEntries back to " +
s"the created path: $path. Exception: ${e.getMessage}")
}
}
Expand Down Expand Up @@ -574,6 +590,16 @@ case class TruncateTableCommand(
}
Seq.empty[Row]
}

private def newAclEntry(
scope: AclEntryScope,
aclType: AclEntryType,
permission: FsAction): AclEntry = {
new AclEntry.Builder()
.setScope(scope)
.setType(aclType)
.setPermission(permission).build()
}
}

abstract class DescribeCommandBase extends RunnableCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
// Set ACL to table path.
val customAcl = new java.util.ArrayList[AclEntry]()
customAcl.add(new AclEntry.Builder()
.setName("test")
.setType(AclEntryType.USER)
.setScope(AclEntryScope.ACCESS)
.setPermission(FsAction.READ).build())
Expand All @@ -2061,8 +2062,26 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
if (ignore) {
assert(aclEntries.size() == 0)
} else {
assert(aclEntries.size() == 1)
assert(aclEntries.size() == 4)
assert(aclEntries.get(0) == customAcl.get(0))

// Setting ACLs will also set user/group/other permissions
// as ACL entries.
val user = new AclEntry.Builder()
.setType(AclEntryType.USER)
.setScope(AclEntryScope.ACCESS)
.setPermission(FsAction.ALL).build()
val group = new AclEntry.Builder()
.setType(AclEntryType.GROUP)
.setScope(AclEntryScope.ACCESS)
.setPermission(FsAction.ALL).build()
val other = new AclEntry.Builder()
.setType(AclEntryType.OTHER)
.setScope(AclEntryScope.ACCESS)
.setPermission(FsAction.ALL).build()
assert(aclEntries.get(1) == user)
assert(aclEntries.get(2) == group)
assert(aclEntries.get(3) == other)
}
}
}
Expand Down