Java Code Examples for java.nio.file.attribute.UserPrincipalLookupService#lookupPrincipalByGroupName()

The following examples show how to use java.nio.file.attribute.UserPrincipalLookupService#lookupPrincipalByGroupName() . 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: 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 3
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 4
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 5
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 6
Source File: SaltService.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Map<Boolean, String> storeMinionScapFiles(
        MinionServer minion, String uploadDir, Long actionId) {
    String actionPath = ScapFileManager
            .getActionPath(minion.getOrg().getId(),
                    minion.getId(), actionId);
    Path mountPoint = Paths.get(com.redhat.rhn.common.conf.Config.get()
            .getString(ConfigDefaults.MOUNT_POINT));
    try {
        // create dirs
        Path actionDir = Files.createDirectories(mountPoint.resolve(actionPath));

        UserPrincipalLookupService lookupService = FileSystems.getDefault()
                .getUserPrincipalLookupService();
        GroupPrincipal susemanagerGroup = lookupService
                .lookupPrincipalByGroupName("susemanager");
        GroupPrincipal wwwGroup = lookupService
                .lookupPrincipalByGroupName("www");
        // systems/<orgId>/<serverId>/actions/<actionId>
        changeGroupAndPerms(actionDir, susemanagerGroup);
        // systems/<orgId>/<serverId>/actions
        actionDir = actionDir.getParent();
        while (!actionDir.equals(mountPoint)) {
            changeGroupAndPerms(actionDir, wwwGroup);
            actionDir = actionDir.getParent();
        }

    }
    catch (IOException e) {
        LOG.error("Error creating dir " + mountPoint.resolve(actionPath), e);
    }

    RunnerCall<Map<Boolean, String>> call = MgrUtilRunner.moveMinionUploadedFiles(
            minion.getMinionId(),
            uploadDir,
            com.redhat.rhn.common.conf.Config.get()
                    .getString(ConfigDefaults.MOUNT_POINT),
            actionPath);
    Optional<Map<Boolean, String>> result = callSync(call,
            err -> err.fold(
                    e -> {
                        LOG.error("Function [" + e.getFunctionName() +
                                " not available for runner call " +
                                "[mgrutil.move_minion_uploaded_files].");
                        return Optional.of(Collections.singletonMap(false,
                                "Function [" + e.getFunctionName()));
                    },
                    e -> {
                        LOG.error("Module [" + e.getModuleName() +
                                "] not supported for runner call " +
                                "[mgrutil.move_minion_uploaded_files].");
                        return Optional.of(Collections.singletonMap(false,
                                "Module [" + e.getModuleName() + "] not supported"));
                    },
                    e -> {
                        LOG.error("Error parsing json response from " +
                                "runner call [mgrutil.move_minion_uploaded_files]: " +
                                e.getJson());
                        return Optional.of(Collections.singletonMap(false,
                                "Error parsing json response: " + e.getJson()));
                    },
                    e -> {
                        LOG.error("Generic Salt error for runner call " +
                                "[mgrutil.move_minion_uploaded_files]: " +
                                e.getMessage());
                        return Optional.of(Collections.singletonMap(false,
                                "Generic Salt error: " + e.getMessage()));
                    }
            )
    );
    return result.orElseGet(() ->
            Collections.singletonMap(false, "Error moving scap result files." +
                    " Please check the logs.")
    );
}
 
Example 7
Source File: LocalLocation.java    From twill with Apache License 2.0 4 votes vote down vote up
@Override
public void setGroup(String group) throws IOException {
  UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
  GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
  Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class).setGroup(groupPrincipal);
}