java.nio.file.attribute.UserPrincipalLookupService Java Examples

The following examples show how to use java.nio.file.attribute.UserPrincipalLookupService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserLookupServiceTest.java    From jimfs with Apache License 2.0 7 votes vote down vote up
@Test
public void testUserLookupService() throws IOException {
  UserPrincipalLookupService service = new UserLookupService(true);
  UserPrincipal bob1 = service.lookupPrincipalByName("bob");
  UserPrincipal bob2 = service.lookupPrincipalByName("bob");
  UserPrincipal alice = service.lookupPrincipalByName("alice");

  assertThat(bob1).isEqualTo(bob2);
  assertThat(bob1).isNotEqualTo(alice);

  GroupPrincipal group1 = service.lookupPrincipalByGroupName("group");
  GroupPrincipal group2 = service.lookupPrincipalByGroupName("group");
  GroupPrincipal foo = service.lookupPrincipalByGroupName("foo");

  assertThat(group1).isEqualTo(group2);
  assertThat(group1).isNotEqualTo(foo);
}
 
Example #2
Source File: SaltActionChainGeneratorService.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Make sure the {@literal actionchains} subdir exists.
 * @return the {@link Path} of the {@literal} actionchains directory
 */
private Path createActionChainsDir() {
    Path targetDir = getTargetDir();
    if (!Files.exists(targetDir)) {
        try {
            Files.createDirectories(targetDir);
            if (!skipSetOwner) {
                FileSystem fileSystem = FileSystems.getDefault();
                UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
                UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");
                Files.setOwner(targetDir, tomcatUser);
            }
        }
        catch (IOException e) {
            LOG.error("Could not create action chain directory " + targetDir, e);
            throw new RuntimeException(e);
        }
    }
    return targetDir;
}
 
Example #3
Source File: CommandDelegatorMethods.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** This method changes the ownership of folder/file to the user provided
 * @param destinationLocation : the location of the path to be owned
 * @param user : the user which needs to own the folder/file
 */
private void changeOwnership(String destinationLocation, String user){
	
	Path path = Paths.get(destinationLocation);
    FileOwnerAttributeView view = Files.getFileAttributeView(path,
        FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault()
        .getUserPrincipalLookupService();
    UserPrincipal userPrincipal;
	try {
		userPrincipal = lookupService.lookupPrincipalByName(user);
		Files.setOwner(path, userPrincipal);
	} catch (IOException e) {
		LOGGER.error("Error while changing ownership of destination location[" + destinationLocation +"]" + "to user[" + user + "]" , e);
	}
}
 
Example #4
Source File: UnixPath.java    From vespa with Apache License 2.0 5 votes vote down vote up
public UnixPath setOwner(String owner) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    UserPrincipal principal = uncheck(
            () -> service.lookupPrincipalByName(owner),
            "While looking up user %s", owner);
    uncheck(() -> Files.setOwner(path, principal));
    return this;
}
 
Example #5
Source File: UnixFileAttributeManager.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException
{
    try {
        if (_isCacheEnabled) {
            return _nameToUserPrincipal.get(userName);
        }
        UserPrincipalLookupService service =
                FileSystems.getDefault().getUserPrincipalLookupService();
        return service.lookupPrincipalByName(userName);
    } catch (IOException | UnsupportedOperationException e) {
        return null;
    }
}
 
Example #6
Source File: UnixFileAttributeManager.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException
{
    try {
        if (_isCacheEnabled) {
            return _nameToGroupPrincipal.get(groupName);
        }
        UserPrincipalLookupService service =
                FileSystems.getDefault().getUserPrincipalLookupService();
        return service.lookupPrincipalByGroupName(groupName);
    } catch (IOException | UnsupportedOperationException e) {
        return null;
    }
}
 
Example #7
Source File: PosixFileAttributeManager.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private UserPrincipal getUserPrincipalFrom(String userName) throws IOException
{
    try {
        UserPrincipal principal = _nameToUserPrincipal.get(userName);
        if (principal == null) {
            UserPrincipalLookupService service =
                    FileSystems.getDefault().getUserPrincipalLookupService();
            principal = service.lookupPrincipalByName(userName);
            _nameToUserPrincipal.put(userName, principal);
        }
        return principal;
    } catch (UnsupportedOperationException e) {
        throw new IOException(e);
    }
}
 
Example #8
Source File: PosixFileAttributeManager.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException
{
    try {
        GroupPrincipal principal = _nameToGroupPrincipal.get(groupName);
        if (principal == null) {
            UserPrincipalLookupService service =
                    FileSystems.getDefault().getUserPrincipalLookupService();
            principal = service.lookupPrincipalByGroupName(groupName);
            _nameToGroupPrincipal.put(groupName, principal);
        }
        return principal;
    } catch (UnsupportedOperationException e) {
        throw new IOException(e);
    }
}
 
Example #9
Source File: HadoopFileOwnerAttributeView.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public UserPrincipal getOwner() throws IOException {
  UserPrincipalLookupService ls = this.path.getFileSystem()
      .getUserPrincipalLookupService();
  FileStatus fileStatus = path.getFileSystem().getHDFS()
      .getFileStatus(path.getRawResolvedPath());
  return ls.lookupPrincipalByName(fileStatus.getOwner());
}
 
Example #10
Source File: TestUserPrincipalLookupService.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Test UserPrincipalLookupService support.
 * 
 * @throws IOException
 */
@Test
public void testGetPosixViewSetOwner() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipalLookupService lus = rootPath.getFileSystem()
      .getUserPrincipalLookupService();
  assertNotNull(lus);
}
 
Example #11
Source File: SExprFileOwner.java    From skUtilities with GNU General Public License v3.0 5 votes vote down vote up
public void change(Event e, Object[] delta, Changer.ChangeMode mode) {
  if (mode == Changer.ChangeMode.SET) {
    Path pth = Paths.get(skUtilities.getDefaultPath(path.getSingle(e)));
    try {
      UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
      Files.setOwner(pth, lookupService.lookupPrincipalByName((String) delta[0]));
    } catch (Exception x) {
      skUtilities.prSysE("File: '" + pth + "' doesn't exist, or is not readable!", getClass().getSimpleName(), x);
    }
  }
}
 
Example #12
Source File: UnixPath.java    From vespa with Apache License 2.0 5 votes vote down vote up
public UnixPath setGroup(String group) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    GroupPrincipal principal = uncheck(
            () -> service.lookupPrincipalByGroupName(group),
            "while looking up group %s", group);
    uncheck(() -> Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(principal));
    return this;
}
 
Example #13
Source File: UserLookupServiceTest.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceNotSupportingGroups() throws IOException {
  UserPrincipalLookupService service = new UserLookupService(false);

  try {
    service.lookupPrincipalByGroupName("group");
    fail();
  } catch (UserPrincipalNotFoundException expected) {
    assertThat(expected.getName()).isEqualTo("group");
  }
}
 
Example #14
Source File: FileUtils.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Define file posix attribute view on a path/file.
 *
 * @param path Target path
 * @param filePermissions Permissions to apply
 * @param fileOwner File owner
 * @param fileGroup File group
 * @throws IOException If IO error during definition of file attribute view
 */
public static void defineFilePosixAttributeView(final Path path,
        final Set<PosixFilePermission> filePermissions,
        final String fileOwner,
        final String fileGroup) throws IOException {
    final PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    if (view != null) {
        final UserPrincipalLookupService lookupService = FileSystems.getDefault()
                .getUserPrincipalLookupService();
        if (fileOwner != null) {
            final UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(fileOwner);
            if (userPrincipal != null) {
                // If not sudoers member, it will throw Operation not permitted
                // Only processes with an effective user ID equal to the user ID
                // of the file or with appropriate privileges may change the ownership of a file.
                // If _POSIX_CHOWN_RESTRICTED is in effect for path
                view.setOwner(userPrincipal);
            }
        }
        if (fileGroup != null) {
            final GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(fileGroup);
            if (groupPrincipal != null) {
                // The current user id should be members of this group,
                // if not will raise Operation not permitted
                view.setGroup(groupPrincipal);
            }
        }
        if (filePermissions != null) {
            view.setPermissions(filePermissions);
        }
    }
}
 
Example #15
Source File: SaltServerActionService.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private void setFileOwner(Path path) throws IOException {
    FileSystem fileSystem = FileSystems.getDefault();
    UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
    UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");

    Files.setOwner(path, tomcatUser);
}
 
Example #16
Source File: SCCRefreshLock.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * tries to obtain the lock or throws an exception
 */
public static void tryGetLock() {
    try {
        f = new File(REFRESH_FILE_LOCKED_PATH);
        synchronized (f) {
            // create the lock file
            channel = new RandomAccessFile(f, "rw").getChannel();
            // create it with correct user
            FileSystem fileSystem = FileSystems.getDefault();
            UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
            UserPrincipal rootUser = service.lookupPrincipalByName("root");
            UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");
            if (Files.getOwner(f.toPath(), LinkOption.NOFOLLOW_LINKS).equals(rootUser)) {
                Files.setOwner(f.toPath(), tomcatUser);
            }
            lock = channel.tryLock();
            if (lock == null) {
                // File is lock by other application
                channel.close();
                log.warn("SCC refresh is already running.");
                throw new OverlappingFileLockException();
            }
            log.info("Got the Lock for scc refresh");
            // Add on exit handler to release lock when application shutdown
            OnExitHandler onExitHandler = new OnExitHandler();
            Runtime.getRuntime().addShutdownHook(onExitHandler);
        }
    }
    catch (IOException e) {
        throw new RuntimeException("Could not start process.", e);
    }
}
 
Example #17
Source File: MTGFileSystem.java    From MtgDesktopCompanion with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return null;
}
 
Example #18
Source File: BundleFileSystem.java    From incubator-taverna-language with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	throw new UnsupportedOperationException();
}
 
Example #19
Source File: GitFileSystem.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() throws UnsupportedOperationException {
  throw new UnsupportedOperationException();
}
 
Example #20
Source File: GitFileSystem.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() throws UnsupportedOperationException {
  throw new UnsupportedOperationException();
}
 
Example #21
Source File: EncryptedFileSystem.java    From encfs4j with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return subFileSystem.getUserPrincipalLookupService();
}
 
Example #22
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
Example #23
Source File: FaultyFileSystem.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
Example #24
Source File: FaultyFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}
 
Example #25
Source File: EncryptedFileSystem.java    From encfs4j with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return subFileSystem.getUserPrincipalLookupService();
}
 
Example #26
Source File: HadoopFileSystem.java    From jsr203-hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return userPrincipalLookupService;
}
 
Example #27
Source File: BuckFileSystem.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
  return getDefaultFileSystem().getUserPrincipalLookupService();
}
 
Example #28
Source File: JimfsFileSystem.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
  fileStore.state().checkOpen();
  return userLookupService;
}
 
Example #29
Source File: ReadOnlyFileSystem.java    From sftpserver with Apache License 2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
	return fs.getUserPrincipalLookupService();
}
 
Example #30
Source File: FaultyFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
    // assume that unwrapped objects aren't exposed
    return delegate.getUserPrincipalLookupService();
}