HDFS-13867. RBF: Add validation for max arguments for Router admin ls, clrQuota, setQuota, rm and nameservice commands. Contributed by Ayush Saxena.

This commit is contained in:
Vinayakumar B 2018-09-03 14:28:31 +05:30
parent 873ef8ae81
commit 780df9034f
2 changed files with 76 additions and 4 deletions

View File

@ -146,6 +146,43 @@ public class RouterAdmin extends Configured implements Tool {
return getUsage(null); return getUsage(null);
} }
/**
* Usage: validates the maximum number of arguments for a command.
* @param arg List of of command line parameters.
*/
private void validateMax(String[] arg) {
if (arg[0].equals("-rm")) {
if (arg.length > 2) {
throw new IllegalArgumentException(
"Too many arguments, Max=1 argument allowed");
}
} else if (arg[0].equals("-ls")) {
if (arg.length > 2) {
throw new IllegalArgumentException(
"Too many arguments, Max=1 argument allowed");
}
} else if (arg[0].equals("-clrQuota")) {
if (arg.length > 2) {
throw new IllegalArgumentException(
"Too many arguments, Max=1 argument allowed");
}
} else if (arg[0].equals("-safemode")) {
if (arg.length > 2) {
throw new IllegalArgumentException(
"Too many arguments, Max=1 argument allowed only");
}
} else if (arg[0].equals("-nameservice")) {
if (arg.length > 3) {
throw new IllegalArgumentException(
"Too many arguments, Max=2 arguments allowed");
}
} else if (arg[0].equals("-getDisabledNameservices")) {
if (arg.length > 1) {
throw new IllegalArgumentException("No arguments allowed");
}
}
}
@Override @Override
public int run(String[] argv) throws Exception { public int run(String[] argv) throws Exception {
if (argv.length < 1) { if (argv.length < 1) {
@ -222,6 +259,7 @@ public class RouterAdmin extends Configured implements Tool {
Exception debugException = null; Exception debugException = null;
exitCode = 0; exitCode = 0;
try { try {
validateMax(argv);
if ("-add".equals(cmd)) { if ("-add".equals(cmd)) {
if (addMount(argv, i)) { if (addMount(argv, i)) {
System.out.println("Successfully added mount point " + argv[i]); System.out.println("Successfully added mount point " + argv[i]);
@ -251,10 +289,6 @@ public class RouterAdmin extends Configured implements Tool {
"Successfully clear quota for mount point " + argv[i]); "Successfully clear quota for mount point " + argv[i]);
} }
} else if ("-safemode".equals(cmd)) { } else if ("-safemode".equals(cmd)) {
if (argv.length > 2) {
throw new IllegalArgumentException(
"Too many arguments, Max=1 argument allowed only");
}
manageSafeMode(argv[i]); manageSafeMode(argv[i]);
} else if ("-nameservice".equals(cmd)) { } else if ("-nameservice".equals(cmd)) {
String subcmd = argv[i]; String subcmd = argv[i];
@ -641,6 +675,9 @@ public class RouterAdmin extends Configured implements Tool {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Cannot parse ssQuota: " + parameters[i]); "Cannot parse ssQuota: " + parameters[i]);
} }
} else {
throw new IllegalArgumentException(
"Invalid argument : " + parameters[i]);
} }
i++; i++;

View File

@ -267,6 +267,13 @@ public class TestRouterAdminCLI {
assertEquals(0, ToolRunner.run(admin, argv)); assertEquals(0, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains(src)); assertTrue(out.toString().contains(src));
// Test with wrong number of arguments
argv = new String[] {"-ls", srcWithSlash, "check", "check2"};
System.setErr(new PrintStream(err));
ToolRunner.run(admin, argv);
assertTrue(
err.toString().contains("Too many arguments, Max=1 argument allowed"));
out.reset(); out.reset();
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance("/"); .newInstance("/");
@ -317,6 +324,13 @@ public class TestRouterAdminCLI {
assertEquals(0, ToolRunner.run(admin, argv)); assertEquals(0, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains( assertTrue(out.toString().contains(
"Cannot remove mount point " + invalidPath)); "Cannot remove mount point " + invalidPath));
// test wrong number of arguments
System.setErr(new PrintStream(err));
argv = new String[] {"-rm", src, "check" };
ToolRunner.run(admin, argv);
assertTrue(err.toString()
.contains("Too many arguments, Max=1 argument allowed"));
} }
@Test @Test
@ -572,6 +586,18 @@ public class TestRouterAdminCLI {
// verify if quota unset successfully // verify if quota unset successfully
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota()); assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota());
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota()); assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota());
// verify wrong arguments
System.setErr(new PrintStream(err));
argv = new String[] {"-clrQuota", src, "check"};
ToolRunner.run(admin, argv);
assertTrue(err.toString(),
err.toString().contains("Too many arguments, Max=1 argument allowed"));
argv = new String[] {"-setQuota", src, "check", "check2"};
err.reset();
ToolRunner.run(admin, argv);
assertTrue(err.toString().contains("Invalid argument : check"));
} }
@Test @Test
@ -686,6 +712,15 @@ public class TestRouterAdminCLI {
new String[] {"-nameservice", "wrong", "ns0"})); new String[] {"-nameservice", "wrong", "ns0"}));
assertTrue("Got error: " + err.toString(), assertTrue("Got error: " + err.toString(),
err.toString().startsWith("nameservice: Unknown command: wrong")); err.toString().startsWith("nameservice: Unknown command: wrong"));
err.reset();
ToolRunner.run(admin,
new String[] {"-nameservice", "enable", "ns0", "check"});
assertTrue(
err.toString().contains("Too many arguments, Max=2 arguments allowed"));
err.reset();
ToolRunner.run(admin, new String[] {"-getDisabledNameservices", "check"});
assertTrue(err.toString().contains("No arguments allowed"));
} }
/** /**