/** * Get the current user's group list from Unix by running the command 'groups' NOTE. For non-existing user it will return EMPTY list * * @param user user name * @return the groups set that the <code>user</code> belongs to * * @throws IOException if encounter any error when running the command */ private Set<String> getUnixGroups(final String user) throws IOException { String result; try { result = shellCommandRunner.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.debug("Unable to get groups for user " + user + ". ShellUtils command failed with exit code " + e.getExitCode()); return new HashSet<>(); } Set<String> groups = new HashSet<>(); for (String group : result.split(shellCommandRunner.getTokenSeparatorRegex())) { groups.add(group); } return groups; }
@Test public void testCanGetGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS); Set<String> groups = groupsMapping.getGroups(TEST_USER_1); assertThat(groups, containsInAnyOrder(TEST_TWO_GROUPS.split(GROUP_SEPARATOR_REGEX))); } }
@Test public void testWillCacheGroups() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, is(secondGroups)); } }
@Test public void testWillExpireCache() throws Exception { try (SimulatedTime t = new SimulatedTime()) { groupsMapping.prepare(topoConf); when(mockShell.execCommand(ShellUtils.getGroupsForUserCommand(TEST_USER_1))).thenReturn(TEST_TWO_GROUPS, TEST_NO_GROUPS); Set<String> firstGroups = groupsMapping.getGroups(TEST_USER_1); Time.advanceTimeSecs(CACHE_EXPIRATION_SECS * 2); Set<String> secondGroups = groupsMapping.getGroups(TEST_USER_1); assertThat(firstGroups, not(secondGroups)); assertThat(secondGroups, contains(TEST_NO_GROUPS)); } }
/** * Get the current user's group list from Unix by running the command 'groups' * NOTE. For non-existing user it will return EMPTY list * @param user user name * @return the groups set that the <code>user</code> belongs to * @throws IOException if encounter any error when running the command */ private static Set<String> getUnixGroups(final String user) throws IOException { String result; try { result = ShellUtils.execCommand(ShellUtils.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.debug("unable to get groups for user " + user + ".ShellUtils command failed with exit code "+ e.getExitCode()); return new HashSet<>(); } StringTokenizer tokenizer = new StringTokenizer(result, ShellUtils.TOKEN_SEPARATOR_REGEX); Set<String> groups = new HashSet<>(); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }