|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.deploy |
| 19 | + |
| 20 | +import java.security.PrivilegedExceptionAction |
| 21 | + |
| 22 | +import scala.util.Random |
| 23 | + |
| 24 | +import org.apache.hadoop.fs.FileStatus |
| 25 | +import org.apache.hadoop.fs.permission.{FsAction, FsPermission} |
| 26 | +import org.apache.hadoop.security.UserGroupInformation |
| 27 | +import org.scalatest.Matchers |
| 28 | + |
| 29 | +import org.apache.spark.SparkFunSuite |
| 30 | + |
| 31 | +class SparkHadoopUtilSuite extends SparkFunSuite with Matchers { |
| 32 | + test("check file permission") { |
| 33 | + import FsAction._ |
| 34 | + val testUser = s"user-${Random.nextInt(100)}" |
| 35 | + val testGroups = Array(s"group-${Random.nextInt(100)}") |
| 36 | + val testUgi = UserGroupInformation.createUserForTesting(testUser, testGroups) |
| 37 | + |
| 38 | + testUgi.doAs(new PrivilegedExceptionAction[Void] { |
| 39 | + override def run(): Void = { |
| 40 | + val sparkHadoopUtil = new SparkHadoopUtil |
| 41 | + |
| 42 | + // If file is owned by user and user has access permission |
| 43 | + var status = fileStatus(testUser, testGroups.head, READ_WRITE, READ_WRITE, NONE) |
| 44 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) |
| 45 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) |
| 46 | + |
| 47 | + // If file is owned by user but user has no access permission |
| 48 | + status = fileStatus(testUser, testGroups.head, NONE, READ_WRITE, NONE) |
| 49 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) |
| 50 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) |
| 51 | + |
| 52 | + val otherUser = s"test-${Random.nextInt(100)}" |
| 53 | + val otherGroup = s"test-${Random.nextInt(100)}" |
| 54 | + |
| 55 | + // If file is owned by user's group and user's group has access permission |
| 56 | + status = fileStatus(otherUser, testGroups.head, NONE, READ_WRITE, NONE) |
| 57 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) |
| 58 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) |
| 59 | + |
| 60 | + // If file is owned by user's group but user's group has no access permission |
| 61 | + status = fileStatus(otherUser, testGroups.head, READ_WRITE, NONE, NONE) |
| 62 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) |
| 63 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) |
| 64 | + |
| 65 | + // If file is owned by other user and this user has access permission |
| 66 | + status = fileStatus(otherUser, otherGroup, READ_WRITE, READ_WRITE, READ_WRITE) |
| 67 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(true) |
| 68 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(true) |
| 69 | + |
| 70 | + // If file is owned by other user but this user has no access permission |
| 71 | + status = fileStatus(otherUser, otherGroup, READ_WRITE, READ_WRITE, NONE) |
| 72 | + sparkHadoopUtil.checkAccessPermission(status, READ) should be(false) |
| 73 | + sparkHadoopUtil.checkAccessPermission(status, WRITE) should be(false) |
| 74 | + |
| 75 | + null |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + private def fileStatus( |
| 81 | + owner: String, |
| 82 | + group: String, |
| 83 | + userAction: FsAction, |
| 84 | + groupAction: FsAction, |
| 85 | + otherAction: FsAction): FileStatus = { |
| 86 | + new FileStatus(0L, |
| 87 | + false, |
| 88 | + 0, |
| 89 | + 0L, |
| 90 | + 0L, |
| 91 | + 0L, |
| 92 | + new FsPermission(userAction, groupAction, otherAction), |
| 93 | + owner, |
| 94 | + group, |
| 95 | + null) |
| 96 | + } |
| 97 | +} |
0 commit comments