Search in sources :

Example 36 with Mode

use of alluxio.security.authorization.Mode in project alluxio by Alluxio.

the class FileSystemAclIntegrationTest method loadFileMetadataMode.

/**
   * Tests the loaded file metadata from UFS having the same mode as that in the UFS.
   */
@Test
public void loadFileMetadataMode() throws Exception {
    // Skip non-local and non-HDFS-2 UFSs.
    Assume.assumeTrue(UnderFileSystemUtils.isLocal(sUfs) || (UnderFileSystemUtils.isHdfs(sUfs) && HadoopClientTestUtils.isHadoop2x()));
    List<Integer> permissionValues = Lists.newArrayList(0111, 0222, 0333, 0444, 0555, 0666, 0777, 0755, 0733, 0644, 0533, 0511);
    for (int value : permissionValues) {
        Path file = new Path("/loadFileMetadataMode" + value);
        sTFS.delete(file, false);
        // Create a file directly in UFS and set the corresponding mode.
        String ufsPath = PathUtils.concatPath(sUfsRoot, file);
        sUfs.create(ufsPath, CreateOptions.defaults().setOwner("testuser").setGroup("testgroup").setMode(new Mode((short) value))).close();
        Assert.assertTrue(sUfs.isFile(PathUtils.concatPath(sUfsRoot, file)));
        // Check the mode is consistent in Alluxio namespace once it's loaded from UFS to Alluxio.
        Assert.assertEquals(new Mode((short) value).toString(), new Mode(sTFS.getFileStatus(file).getPermission().toShort()).toString());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Mode(alluxio.security.authorization.Mode) Test(org.junit.Test)

Example 37 with Mode

use of alluxio.security.authorization.Mode in project alluxio by Alluxio.

the class UnderFileSystemUtils method prepareFilePath.

/**
   * Creates parent directories for path with correct permissions if required.
   *
   * @param alluxioPath Alluxio path
   * @param ufsPath path in the under file system
   * @param fs file system master client
   * @param ufs the under file system
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   * @throws IOException if folder creation fails
   */
public static void prepareFilePath(AlluxioURI alluxioPath, String ufsPath, FileSystem fs, UnderFileSystem ufs) throws AlluxioException, IOException {
    AlluxioURI dstPath = new AlluxioURI(ufsPath);
    String parentPath = dstPath.getParent().getPath();
    // creates the parent folder if it does not exist
    if (!ufs.isDirectory(parentPath)) {
        // Create ancestor directories from top to the bottom. We cannot use recursive create parents
        // here because the permission for the ancestors can be different.
        Stack<Pair<String, MkdirsOptions>> ufsDirsToMakeWithOptions = new Stack<>();
        AlluxioURI curAlluxioPath = alluxioPath.getParent();
        AlluxioURI curUfsPath = dstPath.getParent();
        // Stop at Alluxio mount point because the mapped directory in UFS may not exist.
        while (curUfsPath != null && !ufs.isDirectory(curUfsPath.toString()) && curAlluxioPath != null) {
            URIStatus curDirStatus = fs.getStatus(curAlluxioPath);
            if (curDirStatus.isMountPoint()) {
                throw new IOException(ExceptionMessage.UFS_PATH_DOES_NOT_EXIST.getMessage(curUfsPath));
            }
            ufsDirsToMakeWithOptions.push(new Pair<>(curUfsPath.toString(), MkdirsOptions.defaults().setCreateParent(false).setOwner(curDirStatus.getOwner()).setGroup(curDirStatus.getGroup()).setMode(new Mode((short) curDirStatus.getMode()))));
            curAlluxioPath = curAlluxioPath.getParent();
            curUfsPath = curUfsPath.getParent();
        }
        while (!ufsDirsToMakeWithOptions.empty()) {
            Pair<String, MkdirsOptions> ufsDirAndPerm = ufsDirsToMakeWithOptions.pop();
            // and assume the directory is already prepared, regardless of permission matching.
            if (!ufs.mkdirs(ufsDirAndPerm.getFirst(), ufsDirAndPerm.getSecond()) && !ufs.isDirectory(ufsDirAndPerm.getFirst())) {
                throw new IOException("Failed to create dir: " + ufsDirAndPerm.getFirst());
            }
        }
    }
}
Also used : MkdirsOptions(alluxio.underfs.options.MkdirsOptions) Mode(alluxio.security.authorization.Mode) IOException(java.io.IOException) AlluxioURI(alluxio.AlluxioURI) Stack(java.util.Stack) Pair(alluxio.collections.Pair)

Example 38 with Mode

use of alluxio.security.authorization.Mode in project alluxio by Alluxio.

the class CompleteUfsFileOptionsTest method toThrift.

/**
   * Tests conversion to thrift representation.
   */
@Test
public void toThrift() throws IOException {
    Random random = new Random();
    String owner = CommonUtils.randomAlphaNumString(10);
    String group = CommonUtils.randomAlphaNumString(10);
    Mode mode = new Mode((short) random.nextInt());
    CompleteUfsFileOptions options = CompleteUfsFileOptions.defaults();
    options.setOwner(owner);
    options.setGroup(group);
    options.setMode(mode);
    CompleteUfsFileTOptions thriftOptions = options.toThrift();
    Assert.assertEquals(owner, thriftOptions.getOwner());
    Assert.assertEquals(group, thriftOptions.getGroup());
    Assert.assertEquals(mode.toShort(), thriftOptions.getMode());
}
Also used : CompleteUfsFileTOptions(alluxio.thrift.CompleteUfsFileTOptions) Random(java.util.Random) Mode(alluxio.security.authorization.Mode) Test(org.junit.Test)

Example 39 with Mode

use of alluxio.security.authorization.Mode in project alluxio by Alluxio.

the class ChmodCommand method chmod.

/**
   * Changes the permissions of directory or file with the path specified in args.
   *
   * @param path The {@link AlluxioURI} path as the input of the command
   * @param modeStr The new permission to be updated to the file or directory
   * @param recursive Whether change the permission recursively
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void chmod(AlluxioURI path, String modeStr, boolean recursive) throws AlluxioException, IOException {
    Mode mode = mParser.parse(modeStr);
    SetAttributeOptions options = SetAttributeOptions.defaults().setMode(mode).setRecursive(recursive);
    mFileSystem.setAttribute(path, options);
    System.out.println("Changed permission of " + path + " to " + Integer.toOctalString(mode.toShort()));
}
Also used : SetAttributeOptions(alluxio.client.file.options.SetAttributeOptions) Mode(alluxio.security.authorization.Mode)

Example 40 with Mode

use of alluxio.security.authorization.Mode in project alluxio by Alluxio.

the class PersistCommandTest method persistWithAncestorPermission.

@Test
public void persistWithAncestorPermission() throws Exception {
    String ufsRoot = PathUtils.concatPath(Configuration.get(PropertyKey.UNDERFS_ADDRESS));
    UnderFileSystem ufs = UnderFileSystem.Factory.get(ufsRoot);
    // Skip non-local and non-HDFS UFSs.
    Assume.assumeTrue(UnderFileSystemUtils.isLocal(ufs) || UnderFileSystemUtils.isHdfs(ufs));
    AlluxioURI testFile = new AlluxioURI("/grand/parent/file");
    AlluxioURI grandParent = new AlluxioURI("/grand");
    Mode grandParentMode = new Mode((short) 0777);
    FileSystemTestUtils.createByteFile(mFileSystem, testFile, WriteType.MUST_CACHE, 10);
    URIStatus status = mFileSystem.getStatus(testFile);
    Assert.assertFalse(status.isPersisted());
    mFileSystem.setAttribute(grandParent, SetAttributeOptions.defaults().setMode(grandParentMode));
    int ret = mFsShell.run("persist", testFile.toString());
    Assert.assertEquals(0, ret);
    checkFilePersisted(testFile, 10);
    // Check the permission of the created file and ancestor dir are in-sync between Alluxio and UFS
    short fileMode = (short) status.getMode();
    short parentMode = (short) mFileSystem.getStatus(testFile.getParent()).getMode();
    Assert.assertEquals(fileMode, ufs.getMode(PathUtils.concatPath(ufsRoot, testFile)));
    Assert.assertEquals(parentMode, ufs.getMode(PathUtils.concatPath(ufsRoot, testFile.getParent())));
    Assert.assertEquals(grandParentMode, new Mode(ufs.getMode(PathUtils.concatPath(ufsRoot, grandParent))));
}
Also used : Mode(alluxio.security.authorization.Mode) UnderFileSystem(alluxio.underfs.UnderFileSystem) URIStatus(alluxio.client.file.URIStatus) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test) AlluxioShellUtilsTest(alluxio.shell.AlluxioShellUtilsTest) AbstractAlluxioShellTest(alluxio.shell.AbstractAlluxioShellTest)

Aggregations

Mode (alluxio.security.authorization.Mode)78 Test (org.junit.Test)47 AlluxioURI (alluxio.AlluxioURI)43 BaseIntegrationTest (alluxio.testutils.BaseIntegrationTest)15 UnderFileSystem (alluxio.underfs.UnderFileSystem)14 Random (java.util.Random)14 IOException (java.io.IOException)11 UfsMode (alluxio.underfs.UfsMode)9 URIStatus (alluxio.client.file.URIStatus)8 FileInfo (alluxio.wire.FileInfo)8 ArrayList (java.util.ArrayList)8 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)7 SetAttributePOptions (alluxio.grpc.SetAttributePOptions)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 WriteType (alluxio.client.WriteType)5 AlluxioException (alluxio.exception.AlluxioException)5 LockedInodePath (alluxio.master.file.meta.LockedInodePath)5 AclEntry (alluxio.security.authorization.AclEntry)5 AuthenticatedClientUserResource (alluxio.AuthenticatedClientUserResource)4 AuthenticatedUserRule (alluxio.AuthenticatedUserRule)4