[CLI] check the number of arguments for esusers commands
Adds a check to all of the esusers commands to ensure the correct number of arguments are found. If extra arguments are found, they are printed out with an error message and the tool exits. Closes elastic/elasticsearch#817 Original commit: elastic/x-pack-elasticsearch@cd3e786267
This commit is contained in:
parent
b8f75a2bae
commit
03520e0aa7
|
@ -87,6 +87,9 @@ public class ESUsersTool extends CliTool {
|
|||
public static Command parse(Terminal terminal, CommandLine cli) {
|
||||
if (cli.getArgs().length == 0) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "username is missing");
|
||||
} else if (cli.getArgs().length != 1) {
|
||||
String[] extra = Arrays.copyOfRange(cli.getArgs(), 1, cli.getArgs().length);
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "extra arguments " + Arrays.toString(extra) + " were provided. please ensure all special characters are escaped");
|
||||
}
|
||||
|
||||
String username = cli.getArgs()[0];
|
||||
|
@ -178,6 +181,9 @@ public class ESUsersTool extends CliTool {
|
|||
public static Command parse(Terminal terminal, CommandLine cli) {
|
||||
if (cli.getArgs().length == 0) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "username is missing");
|
||||
} else if (cli.getArgs().length != 1) {
|
||||
String[] extra = Arrays.copyOfRange(cli.getArgs(), 1, cli.getArgs().length);
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "extra arguments " + Arrays.toString(extra) + " were provided. userdel only supports deleting one user at a time");
|
||||
}
|
||||
|
||||
String username = cli.getArgs()[0];
|
||||
|
@ -244,6 +250,9 @@ public class ESUsersTool extends CliTool {
|
|||
public static Command parse(Terminal terminal, CommandLine cli) {
|
||||
if (cli.getArgs().length == 0) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "username is missing");
|
||||
} else if (cli.getArgs().length != 1) {
|
||||
String[] extra = Arrays.copyOfRange(cli.getArgs(), 1, cli.getArgs().length);
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "extra arguments " + Arrays.toString(extra) + " were provided");
|
||||
}
|
||||
|
||||
String username = cli.getArgs()[0];
|
||||
|
@ -310,6 +319,9 @@ public class ESUsersTool extends CliTool {
|
|||
public static Command parse(Terminal terminal, CommandLine cli) {
|
||||
if (cli.getArgs().length == 0) {
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "username is missing");
|
||||
} else if (cli.getArgs().length != 1) {
|
||||
String[] extra = Arrays.copyOfRange(cli.getArgs(), 1, cli.getArgs().length);
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "extra arguments " + Arrays.toString(extra) + " were provided. please ensure all special characters are escaped");
|
||||
}
|
||||
|
||||
String username = cli.getArgs()[0];
|
||||
|
@ -398,7 +410,13 @@ public class ESUsersTool extends CliTool {
|
|||
private static final CliToolConfig.Cmd CMD = cmd(NAME, Useradd.class).build();
|
||||
|
||||
public static Command parse(Terminal terminal, CommandLine cli) {
|
||||
String username = cli.getArgs().length > 0 ? cli.getArgs()[0] : null;
|
||||
String username = null;
|
||||
if (cli.getArgs().length == 1) {
|
||||
username = cli.getArgs()[0];
|
||||
} else if (cli.getArgs().length > 1) {
|
||||
String[] extra = Arrays.copyOfRange(cli.getArgs(), 1, cli.getArgs().length);
|
||||
return exitCmd(ExitStatus.USAGE, terminal, "extra arguments " + Arrays.toString(extra) + " were provided. list can be used without a user or with a single user");
|
||||
}
|
||||
return new ListUsersAndRoles(terminal, username);
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.junit.Test;
|
|||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFileAttributeView;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
@ -45,6 +44,15 @@ public class ESUsersToolTests extends CliToolTestCase {
|
|||
assertThat(cmd.roles, arrayContaining("r1", "r2", "r3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseraddExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("useradd", args("username -p changeme -r r1,r2,r3 r4 r6"));
|
||||
assertThat(command, instanceOf(CliTool.Command.Exit.class));
|
||||
CliTool.Command.Exit exit = (CliTool.Command.Exit) command;
|
||||
assertThat(exit.status(), is(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseradd_Parse_InvalidUsername() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
|
@ -223,6 +231,15 @@ public class ESUsersToolTests extends CliToolTestCase {
|
|||
assertThat(exit.status(), equalTo(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserdel_Parse_ExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("userdel", args("user1 user2"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Command.Exit.class));
|
||||
ESUsersTool.Command.Exit exit = (ESUsersTool.Command.Exit) command;
|
||||
assertThat(exit.status(), equalTo(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserdel_Cmd() throws Exception {
|
||||
Path userFile = writeFile("user1:hash2");
|
||||
|
@ -318,6 +335,15 @@ public class ESUsersToolTests extends CliToolTestCase {
|
|||
assertThat(cmd.status(), is(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswd_Parse_ExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("passwd", args("user1 user2 -p changeme"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Command.Exit.class));
|
||||
ESUsersTool.Command.Exit cmd = (ESUsersTool.Command.Exit) command;
|
||||
assertThat(cmd.status(), is(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswd_Parse_MissingPassword() throws Exception {
|
||||
final AtomicReference<Boolean> secretRequested = new AtomicReference<>(false);
|
||||
|
@ -399,6 +425,15 @@ public class ESUsersToolTests extends CliToolTestCase {
|
|||
assertThat(rolesCommand.removeRoles, arrayContaining("test4", "test5", "test6"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoles_Parse_ExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("roles", args("someuser -a test1,test2,test3 foo -r test4,test5,test6 bar"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Command.Exit.class));
|
||||
ESUsersTool.Command.Exit cmd = (ESUsersTool.Command.Exit) command;
|
||||
assertThat(cmd.status(), is(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoles_Cmd_validatingRoleNames() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
|
@ -586,6 +621,15 @@ public class ESUsersToolTests extends CliToolTestCase {
|
|||
assertThat(listUsersAndRolesCommand.username, is("someuser"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListUsersAndRoles_Cmd_parsingExtraArgs() throws Exception {
|
||||
ESUsersTool tool = new ESUsersTool();
|
||||
CliTool.Command command = tool.parse("list", args("someuser two"));
|
||||
assertThat(command, instanceOf(ESUsersTool.Command.Exit.class));
|
||||
ESUsersTool.Command.Exit cmd = (ESUsersTool.Command.Exit) command;
|
||||
assertThat(cmd.status(), is(CliTool.ExitStatus.USAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListUsersAndRoles_Cmd_listAllUsers() throws Exception {
|
||||
Path usersRoleFile = writeFile("admin: admin\nuser: user\nfoo:user\nbar:user\n");
|
||||
|
|
Loading…
Reference in New Issue