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

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

Introduction

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

Prototype

public void setOwner(Path p, String username, String groupname) throws IOException 

Source Link

Document

Set owner of a path (i.e.

Usage

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

License:Apache License

private void createTestDirForChgrp(Path basePath, FileSystem fs) throws Exception {
    String testUser = getTestUser();
    String testGroup = getTestGroup();
    fs.mkdirs(basePath);/* ww  w  .  j  a  v a2s . 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.setOwner(new Path(basePath, "10"), testUser, testGroup);
    fs.setOwner(new Path(basePath + "/10/dir1"), testUser, testGroup);
    fs.setOwner(new Path(basePath + "/10/dir1/file1"), testUser, testGroup);
    fs.setOwner(new Path(basePath + "/10/dir2"), testUser, testGroup);
    fs.setOwner(new Path(basePath, "11"), testUser, testGroup);
    fs.setOwner(new Path(basePath + "/11/dir3"), testUser, testGroup);
    fs.setOwner(new Path(basePath, "12"), testUser, testGroup);
}

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

License:Apache License

private void _testAuthorizationService(boolean useDefaultGroup) throws Exception {
    init(useDefaultGroup, true);/*from  www .  j  av a2 s  .c  o m*/
    Reader reader = IOUtils.getResourceAsReader("wf-ext-schema-valid.xml", -1);
    Writer writer = new FileWriter(new File(getTestCaseDir(), "workflow.xml"));
    IOUtils.copyCharStream(reader, writer);

    final DagEngine engine = new DagEngine(getTestUser());
    Configuration jobConf = new XConfiguration();
    jobConf.set(OozieClient.APP_PATH, getTestCaseFileUri("workflow.xml"));
    jobConf.set(OozieClient.USER_NAME, getTestUser());
    if (useDefaultGroup) {
        jobConf.set(OozieClient.GROUP_NAME, getTestGroup());
    } else {
        jobConf.set(OozieClient.GROUP_NAME, getTestGroup() + ",foo");
    }

    jobConf.set(OozieClient.LOG_TOKEN, "t");

    jobConf.set("external-status", "ok");
    jobConf.set("signal-value", "based_on_action_status");

    final String jobId = engine.submitJob(jobConf, true);

    HadoopAccessorService has = Services.get().get(HadoopAccessorService.class);
    URI uri = getFileSystem().getUri();
    Configuration fsConf = has.createJobConf(uri.getAuthority());
    FileSystem fileSystem = has.createFileSystem(getTestUser(), uri, fsConf);

    Path path = new Path(fileSystem.getWorkingDirectory(), UUID.randomUUID().toString());
    Path fsTestDir = fileSystem.makeQualified(path);
    System.out.println(XLog.format("Setting FS testcase work dir[{0}]", fsTestDir));
    fileSystem.delete(fsTestDir, true);
    if (!fileSystem.mkdirs(path)) {
        throw new IOException(XLog.format("Could not create FS testcase dir [{0}]", fsTestDir));
    }

    String appPath = fsTestDir.toString() + "/app";

    Path jobXmlPath = new Path(appPath, "workflow.xml");
    fileSystem.create(jobXmlPath).close();
    fileSystem.setOwner(jobXmlPath, getTestUser(), getTestGroup());

    FsPermission permissions = new FsPermission(FsAction.READ_WRITE, FsAction.READ, FsAction.NONE);
    fileSystem.setPermission(jobXmlPath, permissions);

    AuthorizationService as = services.get(AuthorizationService.class);
    assertNotNull(as);
    as.authorizeForGroup(getTestUser(), getTestGroup());
    assertNotNull(as.getDefaultGroup(getTestUser()));

    as.authorizeForApp(getTestUser2(), getTestGroup(), appPath, jobConf);

    try {
        as.authorizeForApp(getTestUser3(), getTestGroup(), appPath, jobConf);
        fail();
    } catch (AuthorizationException ex) {
    }

    as.authorizeForJob(getTestUser(), jobId, false);
    as.authorizeForJob(getTestUser(), jobId, true);
    if (!useDefaultGroup) {
        as.authorizeForJob("foo", jobId, true);
    }
    try {
        as.authorizeForJob("bar", jobId, true);
        fail();
    } catch (AuthorizationException ex) {
    }
}

From source file:org.apache.sentry.hdfs.TestSentryINodeAttributesProvider.java

License:Apache License

@Test
public void testProvider() throws Exception {
    admin.doAs(new PrivilegedExceptionAction<Void>() {
        @Override//ww w.  j  a v a  2 s.co m
        public Void run() throws Exception {
            String sysUser = UserGroupInformation.getCurrentUser().getShortUserName();
            FileSystem fs = FileSystem.get(miniDFS.getConfiguration(0));

            List<AclEntry> baseAclList = new ArrayList<AclEntry>();
            AclEntry.Builder builder = new AclEntry.Builder();
            baseAclList.add(builder.setType(AclEntryType.USER).setScope(AclEntryScope.ACCESS).build());
            baseAclList.add(builder.setType(AclEntryType.GROUP).setScope(AclEntryScope.ACCESS).build());
            baseAclList.add(builder.setType(AclEntryType.OTHER).setScope(AclEntryScope.ACCESS).build());
            Path path1 = new Path("/user/authz/obj/xxx");
            fs.mkdirs(path1);
            fs.setAcl(path1, baseAclList);

            fs.mkdirs(new Path("/user/authz/xxx"));
            fs.mkdirs(new Path("/user/xxx"));

            // root
            Path path = new Path("/");
            Assert.assertEquals(sysUser, fs.getFileStatus(path).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), fs.getFileStatus(path).getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            // dir before prefixes
            path = new Path("/user");
            Assert.assertEquals(sysUser, fs.getFileStatus(path).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), fs.getFileStatus(path).getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            // prefix dir
            path = new Path("/user/authz");
            Assert.assertEquals(sysUser, fs.getFileStatus(path).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), fs.getFileStatus(path).getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            // dir inside of prefix, no obj
            path = new Path("/user/authz/xxx");
            FileStatus status = fs.getFileStatus(path);
            Assert.assertEquals(sysUser, status.getOwner());
            Assert.assertEquals("supergroup", status.getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), status.getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            // dir inside of prefix, obj
            path = new Path("/user/authz/obj");
            Assert.assertEquals("hive", fs.getFileStatus(path).getOwner());
            Assert.assertEquals("hive", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0771), fs.getFileStatus(path).getPermission());
            Assert.assertFalse(fs.getAclStatus(path).getEntries().isEmpty());

            List<AclEntry> acls = new ArrayList<AclEntry>();
            acls.add(new AclEntry.Builder().setName(sysUser).setType(AclEntryType.USER)
                    .setScope(AclEntryScope.ACCESS).setPermission(FsAction.ALL).build());
            acls.add(new AclEntry.Builder().setName("supergroup").setType(AclEntryType.GROUP)
                    .setScope(AclEntryScope.ACCESS).setPermission(FsAction.READ_EXECUTE).build());
            acls.add(new AclEntry.Builder().setName("user-authz").setType(AclEntryType.USER)
                    .setScope(AclEntryScope.ACCESS).setPermission(FsAction.ALL).build());
            Assert.assertEquals(new LinkedHashSet<AclEntry>(acls),
                    new LinkedHashSet<AclEntry>(fs.getAclStatus(path).getEntries()));

            // dir inside of prefix, inside of obj
            path = new Path("/user/authz/obj/xxx");
            Assert.assertEquals("hive", fs.getFileStatus(path).getOwner());
            Assert.assertEquals("hive", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0771), fs.getFileStatus(path).getPermission());
            Assert.assertFalse(fs.getAclStatus(path).getEntries().isEmpty());

            Path path2 = new Path("/user/authz/obj/path2");
            fs.mkdirs(path2);
            fs.setAcl(path2, baseAclList);

            // dir outside of prefix
            path = new Path("/user/xxx");
            Assert.assertEquals(sysUser, fs.getFileStatus(path).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(path).getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), fs.getFileStatus(path).getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            //stale and dir inside of prefix, obj
            System.setProperty("test.stale", "true");
            path = new Path("/user/authz/xxx");
            status = fs.getFileStatus(path);
            Assert.assertEquals(sysUser, status.getOwner());
            Assert.assertEquals("supergroup", status.getGroup());
            Assert.assertEquals(new FsPermission((short) 0755), status.getPermission());
            Assert.assertTrue(fs.getAclStatus(path).getEntries().isEmpty());

            // setPermission sets the permission for dir outside of prefix.
            // setUser/setGroup sets the user/group for dir outside of prefix.
            Path pathOutside = new Path("/user/xxx");

            fs.setPermission(pathOutside, new FsPermission((short) 0000));
            Assert.assertEquals(new FsPermission((short) 0000), fs.getFileStatus(pathOutside).getPermission());
            fs.setOwner(pathOutside, sysUser, "supergroup");
            Assert.assertEquals(sysUser, fs.getFileStatus(pathOutside).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(pathOutside).getGroup());

            // removeAcl removes the ACL entries for dir outside of prefix.
            List<AclEntry> aclsOutside = new ArrayList<AclEntry>(baseAclList);
            List<AclEntry> acl = new ArrayList<AclEntry>();
            acl.add(new AclEntry.Builder().setName("supergroup").setType(AclEntryType.GROUP)
                    .setScope(AclEntryScope.ACCESS).setPermission(FsAction.READ_EXECUTE).build());
            aclsOutside.addAll(acl);
            fs.setAcl(pathOutside, aclsOutside);
            fs.removeAclEntries(pathOutside, acl);
            Assert.assertFalse(fs.getAclStatus(pathOutside).getEntries().containsAll(acl));

            // setPermission sets the permission for dir inside of prefix but not a hive obj.
            // setUser/setGroup sets the user/group for dir inside of prefix but not a hive obj.
            Path pathInside = new Path("/user/authz/xxx");

            fs.setPermission(pathInside, new FsPermission((short) 0000));
            Assert.assertEquals(new FsPermission((short) 0000), fs.getFileStatus(pathInside).getPermission());
            fs.setOwner(pathInside, sysUser, "supergroup");
            Assert.assertEquals(sysUser, fs.getFileStatus(pathInside).getOwner());
            Assert.assertEquals("supergroup", fs.getFileStatus(pathInside).getGroup());

            // removeAcl is a no op for dir inside of prefix.
            Assert.assertTrue(fs.getAclStatus(pathInside).getEntries().isEmpty());
            fs.removeAclEntries(pathInside, acl);
            Assert.assertTrue(fs.getAclStatus(pathInside).getEntries().isEmpty());

            // setPermission/setUser/setGroup is a no op for dir inside of prefix, and is a hive obj.
            Path pathInsideAndHive = new Path("/user/authz/obj");

            fs.setPermission(pathInsideAndHive, new FsPermission((short) 0000));
            Assert.assertEquals(new FsPermission((short) 0771),
                    fs.getFileStatus(pathInsideAndHive).getPermission());
            fs.setOwner(pathInsideAndHive, sysUser, "supergroup");
            Assert.assertEquals("hive", fs.getFileStatus(pathInsideAndHive).getOwner());
            Assert.assertEquals("hive", fs.getFileStatus(pathInsideAndHive).getGroup());

            return null;
        }
    });
}

From source file:org.opencloudengine.garuda.backend.hdfs.HdfsServiceImpl.java

License:Open Source License

private void _setOwner(String path, String owner, String group) throws Exception {
    if (StringUtils.isEmpty(owner)) {
        owner = config.getProperty("system.hdfs.super.user");
    }/*from   w w w. ja v  a2  s  . co  m*/
    if (StringUtils.isEmpty(group)) {
        group = owner;
    }
    FileSystem fs = fileSystemFactory.getFileSystem();
    Path fsPath = new Path(path);
    if (!fs.exists(fsPath)) {
        this.notFoundException(fsPath.toString());
    }
    fs.setOwner(fsPath, StringUtils.isEmpty(owner) ? null : owner, StringUtils.isEmpty(group) ? null : group);
    fs.close();
}

From source file:org.wso2.carbon.hdfs.mgt.HDFSAdmin.java

License:Open Source License

public void setGroup(String fsPath, String group) throws HDFSServerManagementException {
    FileSystem hdfsFS = null;
    try {/*from   www. j av  a2  s .c  o m*/
        hdfsFS = hdfsAdminHelperInstance.getFSforUser();
        if (hdfsFS != null) {
            hdfsFS.setOwner(new Path(fsPath), null, group);
        } // TO DO: validate the group / role
    } catch (IOException e) {
        String msg = "Error occurred while trying to mount file system.";
        handleException(msg, e);
    }

}

From source file:org.wso2.carbon.hdfs.mgt.HDFSAdmin.java

License:Open Source License

public void setOwner(String fsPath, String owner) throws HDFSServerManagementException {
    FileSystem hdfsFS = null;
    try {//from   ww w .ja  v  a2s  .c  o  m
        hdfsFS = hdfsAdminHelperInstance.getFSforUser();
        if (hdfsFS != null) {
            hdfsFS.setOwner(new Path(fsPath), owner, null);
        } // TO DO: validate
          // the group / role

    } catch (IOException e) {
        String msg = "Error occurred while trying to mount file system.";
        handleException(msg, e);
    }
}