-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20172][Core] Add file permission check when listing files in FsHistoryProvider #17495
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
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
10d254b
Add file permission check in listing files
jerryshao 63142cc
Also add logics to handle other exceptions
jerryshao 1d1440b
Address the comments
jerryshao 9447011
Further address the comments
jerryshao 4d3838a
Address the comments to change the UT
jerryshao ab5117b
Using test UGI to mimic current user and groups
jerryshao b36fa75
Address the comments
jerryshao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
core/src/test/scala/org/apache/spark/deploy/SparkHadoopUtilSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * 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.spark.deploy | ||
|
|
||
| import java.security.PrivilegedExceptionAction | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| import org.apache.hadoop.fs.FileStatus | ||
| import org.apache.hadoop.fs.permission.{FsAction, FsPermission} | ||
| import org.apache.hadoop.security.UserGroupInformation | ||
| import org.scalatest.Matchers | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
|
|
||
| class SparkHadoopUtilSuite extends SparkFunSuite with Matchers { | ||
| test("check file permission") { | ||
| import FsAction._ | ||
| val user = UserGroupInformation.getCurrentUser.getShortUserName | ||
| val groups = UserGroupInformation.getCurrentUser.getGroupNames | ||
| require(!groups.isEmpty) | ||
|
|
||
| val testUser = user + "-" + Random.nextInt(100) | ||
|
||
| val testGroups = groups.map { g => g + "-" + Random.nextInt(100) } | ||
| val testUgi = UserGroupInformation.createUserForTesting(testUser, testGroups) | ||
|
|
||
| testUgi.doAs(new PrivilegedExceptionAction[Void] { | ||
| override def run(): Void = { | ||
| val sparkHadoopUtil = new SparkHadoopUtil | ||
|
|
||
| // If file is owned by user and user has access permission | ||
| var status = fileStatus(testUser, testGroups.head, READ_WRITE, READ_WRITE, NONE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) | ||
|
|
||
| // If file is owned by user but user has no access permission | ||
| status = fileStatus(testUser, testGroups.head, NONE, READ_WRITE, NONE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) | ||
|
|
||
| var otherUser = "test" | ||
| var otherGroup = "test" | ||
| while (otherUser == testUser || testGroups.contains(otherGroup)) { | ||
| otherUser = s"test-${Random.nextInt(100)}" | ||
| otherGroup = s"test-${Random.nextInt(100)}" | ||
| } | ||
|
|
||
| // If file is owned by user's group and user's group has access permission | ||
| status = fileStatus(otherUser, testGroups.head, NONE, READ_WRITE, NONE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) | ||
|
|
||
| // If file is owned by user's group but user's group has no access permission | ||
| status = fileStatus(otherUser, testGroups.head, READ_WRITE, NONE, NONE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) | ||
|
|
||
| // If file is owned by other user and this user has access permission | ||
| status = fileStatus(otherUser, otherGroup, READ_WRITE, READ_WRITE, READ_WRITE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) | ||
|
|
||
| // If file is owned by other user but this user has no access permission | ||
| status = fileStatus(otherUser, otherGroup, READ_WRITE, READ_WRITE, NONE) | ||
| sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) | ||
| sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) | ||
|
|
||
| null | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private def fileStatus( | ||
| owner: String, | ||
| group: String, | ||
| userAction: FsAction, | ||
| groupAction: FsAction, | ||
| otherAction: FsAction): FileStatus = { | ||
| new FileStatus(0L, | ||
| false, | ||
| 0, | ||
| 0L, | ||
| 0L, | ||
| 0L, | ||
| new FsPermission(userAction, groupAction, otherAction), | ||
| owner, | ||
| group, | ||
| null) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
getCurrentUser, I think it would be safer to usecreateUserForTesting+ugi.doAs. That lets you define the user name and the groups and thus avoids possible clashes with the OS configuration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that would be better, thanks for the suggestion.