Added "no user found" output to esusers tool

for the `list` command and when no users are defined

Original commit: elastic/x-pack-elasticsearch@3ca7b9a62b
This commit is contained in:
uboness 2014-12-08 13:41:12 +01:00
parent cf71f3f05e
commit 87a2a2afc6
2 changed files with 28 additions and 0 deletions

View File

@ -404,18 +404,26 @@ public class ESUsersTool extends CliTool {
}
} else {
boolean unknownRolesFound = false;
boolean usersExist = false;
for (Map.Entry<String, String[]> entry : userRoles.entrySet()) {
String[] roles = entry.getValue();
Set<String> unknownRoles = Sets.difference(Sets.newHashSet(roles), knownRoles.keySet());
String[] markedRoles = markUnknownRoles(roles, unknownRoles);
terminal.println("%-15s: %s", entry.getKey(), Joiner.on(",").join(markedRoles));
unknownRolesFound = unknownRolesFound || !unknownRoles.isEmpty();
usersExist = true;
}
// list users without roles
Set<String> usersWithoutRoles = Sets.newHashSet(users);
usersWithoutRoles.removeAll(userRoles.keySet());
for (String user : usersWithoutRoles) {
terminal.println("%-15s: -", user);
usersExist = true;
}
if (!usersExist) {
terminal.println("No users found");
return ExitStatus.OK;
}
if (unknownRolesFound) {

View File

@ -631,6 +631,26 @@ public class ESUsersToolTests extends CliToolTestCase {
assertThat(catchTerminalOutput.getTerminalOutput(), hasItem(not(containsString("user"))));
}
@Test
public void testListUsersAndRoles_Cmd_NoUsers() throws Exception {
File usersFile = writeFile("");
File usersRoleFile = writeFile("");
Settings settings = ImmutableSettings.builder()
.put("shield.authc.realms.esusers.type", "esusers")
.put("shield.authc.realms.esusers.files.users", usersFile)
.put("shield.authc.realms.esusers.files.users_roles", usersRoleFile)
.build();
CaptureOutputTerminal terminal = new CaptureOutputTerminal();
ESUsersTool.ListUsersAndRoles cmd = new ESUsersTool.ListUsersAndRoles(terminal, null);
CliTool.ExitStatus status = execute(cmd, settings);
assertThat(status, is(CliTool.ExitStatus.OK));
List<String> output = terminal.getTerminalOutput();
assertThat(output, hasSize(1));
assertThat(output.get(0), equalTo("No users found\n"));
}
@Test
public void testListUsersAndRoles_Cmd_listSingleUserNotFound() throws Exception {
File usersRoleFile = writeFile("admin: admin\nuser: user\nfoo:user\nbar:user\n");