Example usage for org.apache.hadoop.fs FileSystem setPermission

List of usage examples for org.apache.hadoop.fs FileSystem setPermission

Introduction

In this page you can find the example usage for org.apache.hadoop.fs FileSystem setPermission.

Prototype

public void setPermission(Path p, FsPermission permission) throws IOException 

Source Link

Document

Set permission of a path.

Usage

From source file:org.apache.oozie.action.hadoop.TestFsActionExecutor.java

License:Apache License

public void testChmod() throws Exception {
    FsActionExecutor ae = new FsActionExecutor();
    FileSystem fs = getFileSystem();

    Path path = new Path(getFsTestCaseDir(), "dir");
    Path child = new Path(path, "child");
    Path grandchild = new Path(child, "grandchild");
    fs.mkdirs(grandchild);/*from ww w  .  j a v a  2  s  .c o m*/
    fs.setPermission(path, FsPermission.valueOf("-rwx------"));
    fs.setPermission(child, FsPermission.valueOf("-rwxr-----"));
    fs.setPermission(grandchild, FsPermission.valueOf("-rwx---r--"));
    assertEquals("rwx------", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr-----", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());

    Context context = createContext("<fs/>");

    ae.chmod(context, path, "-rwx-----x", false, false);
    assertEquals("rwx-----x", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr-----", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());

    ae.chmod(context, path, "-rwxr----x", true, false);
    assertEquals("rwxr----x", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr----x", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());
}

From source file:org.apache.oozie.action.hadoop.TestFsActionExecutor.java

License:Apache License

private void createTestDirForChmod(Path basePath, FileSystem fs) throws Exception {
    fs.mkdirs(basePath);/*from   w ww. j ava  2  s.  com*/
    fs.mkdirs(new Path(basePath, "10"));
    fs.mkdirs(new Path(basePath + "/10/dir1"));
    fs.createNewFile(new Path(basePath + "/10/dir1/file1"));
    fs.mkdirs(new Path(basePath + "/10/dir2"));
    fs.mkdirs(new Path(basePath, "11"));
    fs.mkdirs(new Path(basePath + "/11/dir3"));
    fs.mkdirs(new Path(basePath, "12"));
    fs.setPermission(new Path(basePath, "10"), FsPermission.valueOf("-rwxrwxrwx"));
    fs.setPermission(new Path(basePath + "/10/dir1"), FsPermission.valueOf("-rwxrwxrwx"));
    fs.setPermission(new Path(basePath + "/10/dir2"), FsPermission.valueOf("-rwxrwxrwx"));
    fs.setPermission(new Path(basePath + "/10/dir1/file1"), FsPermission.valueOf("-rw-rw-rw-"));
    fs.setPermission(new Path(basePath, "11"), FsPermission.valueOf("-rwxrwxrwx"));
    fs.setPermission(new Path(basePath + "/11/dir3"), FsPermission.valueOf("-rwxrwxrwx"));
    fs.setPermission(new Path(basePath, "12"), FsPermission.valueOf("-rwxrwxrwx"));
}

From source file:org.apache.oozie.action.hadoop.TestFsActionExecutor.java

License:Apache License

public void testChmodRecursive() throws Exception {
    FsActionExecutor ae = new FsActionExecutor();
    FileSystem fs = getFileSystem();

    Path path = new Path(getFsTestCaseDir(), "dir");
    Path child = new Path(path, "child");
    Path grandchild = new Path(child, "grandchild");
    fs.mkdirs(grandchild);//from   w  w w  .  j a  v  a  2s. c om
    fs.setPermission(path, FsPermission.valueOf("-rwx------"));
    fs.setPermission(child, FsPermission.valueOf("-rwxr-----"));
    fs.setPermission(grandchild, FsPermission.valueOf("-rwx---r--"));
    assertEquals("rwx------", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr-----", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());

    Context context = createContext("<fs/>");

    ae.chmod(context, path, "-rwx-----x", false, false);
    assertEquals("rwx-----x", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr-----", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());

    ae.chmod(context, path, "-rwx-----x", false, true);
    assertEquals("rwx-----x", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr-----", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwx---r--", fs.getFileStatus(grandchild).getPermission().toString());

    ae.chmod(context, path, "-rwxr----x", true, true);
    assertEquals("rwxr----x", fs.getFileStatus(path).getPermission().toString());
    assertEquals("rwxr----x", fs.getFileStatus(child).getPermission().toString());
    assertEquals("rwxr----x", fs.getFileStatus(grandchild).getPermission().toString());
}

From source file:org.apache.oozie.action.hadoop.TestFsActionExecutor.java

License:Apache License

public void testRetryOnAccessControlError() throws Exception {
    FsActionExecutor ae = new FsActionExecutor();
    FileSystem fs = getFileSystem();

    Path path = new Path(getFsTestCaseDir(), "dir");
    fs.mkdirs(path);//from ww w  . ja v a 2 s .c o m
    fs.setPermission(path, FsPermission.valueOf("-rwx------"));
    // do FS operation using different user to cause permission error
    Context context = createContextWithUser("<fs/>", getTestUser2());
    try {
        ae.chmod(context, path, "-r--------", false, false);
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof ActionExecutorException);
        assertEquals("FS014", ((ActionExecutorException) e).getErrorCode());
        assertEquals(ActionExecutorException.ErrorType.ERROR, ((ActionExecutorException) e).getErrorType());
    }
}

From source file:org.apache.oozie.action.hadoop.TestGitActionExecutor.java

License:Apache License

@SuppressWarnings("unchecked")
public void testWhenParametersFilledThenConfigurationFieldsPopulated() throws Exception {
    final GitActionExecutor ae = new GitActionExecutor();
    assertTrue("Can not find GitMain class in launcher classes",
            ae.getLauncherClasses().contains(GitMain.class));

    final Path testKey = new Path(getAppPath().toString() + "/test_key");
    createTestFile(testKey);/*from  w  w w  .  ja v  a  2 s. c  o  m*/
    final FileSystem fs = getFileSystem();
    fs.setPermission(testKey, FsPermission.valueOf("-r--------"));

    final String repoUrl = "https://github.com/apache/oozie";
    final String keyUrl = testKey.toString();
    final String destDir = "repoDir";
    final String branch = "myBranch";
    final Element actionXml = XmlUtils.parseXml("<git>" + "<resource-manager>" + getJobTrackerUri()
            + "</resource-manager>" + "<name-node>" + getNameNodeUri() + "</name-node>" + "<git-uri>" + repoUrl
            + "</git-uri>" + "<branch>" + branch + "</branch>" + "<key-path>" + keyUrl + "</key-path>"
            + "<destination-uri>" + destDir + "</destination-uri>" + "</git>");

    final XConfiguration protoConf = new XConfiguration();
    protoConf.set(WorkflowAppService.HADOOP_USER, getTestUser());

    final WorkflowJobBean wf = createBaseWorkflow(protoConf, GitActionExecutor.GIT_ACTION_TYPE + "-action");
    final WorkflowActionBean action = (WorkflowActionBean) wf.getActions().get(0);
    action.setType(ae.getType());

    final Context context = new Context(wf, action);
    final Configuration conf = ae.createBaseHadoopConf(context, actionXml);
    ae.setupActionConf(conf, context, actionXml, getFsTestCaseDir());

    assertEquals("git uri must be set", repoUrl, conf.get(GitActionExecutor.GIT_URI));
    assertEquals("key path must be set", keyUrl, conf.get(GitActionExecutor.KEY_PATH));
    assertEquals("branch must be set", branch, conf.get(GitActionExecutor.GIT_BRANCH));
    assertEquals("destination uri must be set", destDir, conf.get(GitActionExecutor.DESTINATION_URI));
}

From source file:org.apache.oozie.action.hadoop.TestGitActionExecutor.java

License:Apache License

public void testAccessKeyPermissionsInsecure() throws Exception {
    final GitActionExecutor ae = new GitActionExecutor();

    final Path testKey = new Path(getAppPath().toString() + "/test_key");
    createTestFile(testKey);/* w ww  . j a  v a2  s .c om*/
    final FileSystem fs = getFileSystem();
    fs.setPermission(testKey, FsPermission.valueOf("-r-----rw-"));

    final String repoUrl = "https://github.com/apache/oozie";
    final String keyUrl = testKey.toString();
    final String destDir = "repoDir";
    final String branch = "myBranch";
    final Element actionXml = XmlUtils.parseXml("<git>" + "<resource-manager>" + getJobTrackerUri()
            + "</resource-manager>" + "<name-node>" + getNameNodeUri() + "</name-node>" + "<git-uri>" + repoUrl
            + "</git-uri>" + "<branch>" + branch + "</branch>" + "<key-path>" + keyUrl + "</key-path>"
            + "<destination-uri>" + destDir + "</destination-uri>" + "</git>");

    final XConfiguration protoConf = new XConfiguration();
    protoConf.set(WorkflowAppService.HADOOP_USER, getTestUser());

    final WorkflowJobBean wf = createBaseWorkflow(protoConf, GitActionExecutor.GIT_ACTION_TYPE + "-action");
    final WorkflowActionBean action = (WorkflowActionBean) wf.getActions().get(0);
    action.setType(ae.getType());

    final Context context = new Context(wf, action);
    final Configuration conf = ae.createBaseHadoopConf(context, actionXml);
    try {
        // we expect this to throw an ActionExecutorException:
        ae.setupActionConf(conf, context, actionXml, getFsTestCaseDir());
        Assert.fail("Expected ActionExecutorException");
    } catch (final ActionExecutorException e) {
        if (!e.getMessage().contains("insecure")) {
            Assert.fail("Unexpected exception message: " + e.getMessage());
        }
    }
}

From source file:org.apache.oozie.action.hadoop.TestGitActionExecutor.java

License:Apache License

public void testAccessKeyACLsSecure() throws Exception {
    final GitActionExecutor ae = new GitActionExecutor();

    final Path testKey = new Path(getAppPath().toString() + "/test_key");
    createTestFile(testKey);//from   ww w  . j a va2  s . co  m
    // set file permissions to be secure -- allowing only the owner to read
    final FileSystem fs = getFileSystem();
    fs.setPermission(testKey, FsPermission.valueOf("-r--------"));
    fs.setAcl(testKey, AclEntry.parseAclSpec("user::rwx,user:foo:rw-,group::r--,other::---", true));

    final String repoUrl = "https://github.com/apache/oozie";
    final String keyUrl = testKey.toString();
    final String destDir = "repoDir";
    final String branch = "myBranch";
    final Element actionXml = XmlUtils.parseXml("<git>" + "<resource-manager>" + getJobTrackerUri()
            + "</resource-manager>" + "<name-node>" + getNameNodeUri() + "</name-node>" + "<git-uri>" + repoUrl
            + "</git-uri>" + "<branch>" + branch + "</branch>" + "<key-path>" + keyUrl + "</key-path>"
            + "<destination-uri>" + destDir + "</destination-uri>" + "</git>");

    final XConfiguration protoConf = new XConfiguration();
    protoConf.set(WorkflowAppService.HADOOP_USER, getTestUser());

    final WorkflowJobBean wf = createBaseWorkflow(protoConf, GitActionExecutor.GIT_ACTION_TYPE + "-action");
    final WorkflowActionBean action = (WorkflowActionBean) wf.getActions().get(0);
    action.setType(ae.getType());

    final Context context = new Context(wf, action);
    final Configuration conf = ae.createBaseHadoopConf(context, actionXml);
    try {
        ae.setupActionConf(conf, context, actionXml, getFsTestCaseDir());
    } catch (final ActionExecutorException e) {
        fail("Unexpected exception, could not check ACLs: " + e.getMessage());
    }
}

From source file:org.apache.oozie.action.hadoop.TestGitActionExecutor.java

License:Apache License

public void testAccessKeyACLsInsecure() throws Exception {
    final GitActionExecutor ae = new GitActionExecutor();

    final Path testKey = new Path(getAppPath().toString() + "/test_key");
    createTestFile(testKey);/*w w w  .  j  a  v  a2 s.  co m*/
    // set file permissions to be secure -- allowing only the owner to read
    final FileSystem fs = getFileSystem();
    fs.setPermission(testKey, FsPermission.valueOf("-r--------"));
    fs.setAcl(testKey, AclEntry.parseAclSpec("user::rwx,user:foo:rw-,group::r--,other::r--", true));

    final String repoUrl = "https://github.com/apache/oozie";
    final String keyUrl = testKey.toString();
    final String destDir = "repoDir";
    final String branch = "myBranch";
    final Element actionXml = XmlUtils.parseXml("<git>" + "<resource-manager>" + getJobTrackerUri()
            + "</resource-manager>" + "<name-node>" + getNameNodeUri() + "</name-node>" + "<git-uri>" + repoUrl
            + "</git-uri>" + "<branch>" + branch + "</branch>" + "<key-path>" + keyUrl + "</key-path>"
            + "<destination-uri>" + destDir + "</destination-uri>" + "</git>");

    final XConfiguration protoConf = new XConfiguration();
    protoConf.set(WorkflowAppService.HADOOP_USER, getTestUser());

    final WorkflowJobBean wf = createBaseWorkflow(protoConf, GitActionExecutor.GIT_ACTION_TYPE + "-action");
    final WorkflowActionBean action = (WorkflowActionBean) wf.getActions().get(0);
    action.setType(ae.getType());

    final Context context = new Context(wf, action);

    try {
        ae.createBaseHadoopConf(context, actionXml);
    } catch (final Exception e) {
        fail("Unexpected exception, could not create Hadoop configuration with insecure setup: "
                + e.getMessage());
    }
}

From source file:org.apache.oozie.command.wf.TestActionStartXCommand.java

License:Apache License

private WorkflowActionBean createWorkflowActionForFS(String wfId, WorkflowAction.Status status)
        throws Exception {
    WorkflowActionBean action = new WorkflowActionBean();
    String actionname = "testAction";
    action.setName(actionname);/*from   w  ww  .j  av a2 s.c o m*/
    action.setCred("null");
    action.setId(Services.get().get(UUIDService.class).generateChildId(wfId, actionname));
    action.setJobId(wfId);
    action.setType("fs");
    action.setTransition("transition");
    action.setStatus(status);
    action.setStartTime(new Date());
    action.setEndTime(new Date());
    action.setLastCheckTime(new Date());
    action.setPending();
    action.setExecutionPath("a");
    action.setRetries(1);
    action.setUserRetryCount(0);
    action.setUserRetryMax(1);

    Path path = new Path(getFsTestCaseDir(), "test");
    FileSystem fs = getFileSystem();
    fs.mkdirs(path);
    fs.setPermission(path, FsPermission.valueOf("-rwx------"));

    String actionXml = "<fs>" + "<chmod path='" + path.toString()
            + "' permissions='-r--------' dir-files='false' />" + "</fs>";
    action.setConf(actionXml);
    return action;
}

From source file:org.apache.oozie.service.ShareLibService.java

License:Apache License

/**
 * Recursive change permissions.//from  w  w  w  .ja  va 2  s  .c o m
 *
 * @param fs the FileSystem
 * @param path the Path
 * @param perm is permission
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void recursiveChangePermissions(FileSystem fs, Path path, FsPermission fsPerm) throws IOException {
    fs.setPermission(path, fsPerm);
    FileStatus[] filesStatus = fs.listStatus(path);
    for (int i = 0; i < filesStatus.length; i++) {
        Path p = filesStatus[i].getPath();
        if (filesStatus[i].isDir()) {
            recursiveChangePermissions(fs, p, fsPerm);
        } else {
            fs.setPermission(p, fsPerm);
        }
    }
}