Skip to content

Commit 0e8eee0

Browse files
author
Положаев Денис Александрович
committed
IGNITE-23459 Add console input password if --password argument presented without value for ./control.sh
1 parent dd86f44 commit 0e8eee0

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayList;
2626
import java.util.Arrays;
2727
import java.util.Collections;
28+
import java.util.Iterator;
2829
import java.util.List;
2930
import java.util.Objects;
3031
import java.util.Scanner;
@@ -79,6 +80,8 @@
7980
import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
8081
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_AUTO_CONFIRMATION;
8182
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_ENABLE_EXPERIMENTAL;
83+
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_PASSWORD;
84+
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_USER;
8285
import static org.apache.ignite.internal.commandline.ArgumentParser.CMD_VERBOSE;
8386
import static org.apache.ignite.internal.commandline.CommandLogger.errorMessage;
8487
import static org.apache.ignite.internal.management.api.CommandUtils.CMD_WORDS_DELIM;
@@ -251,7 +254,12 @@ public <A extends IgniteDataTransferObject> int execute(List<String> rawArgs) {
251254

252255
verbose = F.exist(rawArgs, CMD_VERBOSE::equalsIgnoreCase);
253256

254-
ConnectionAndSslParameters<A> args = new ArgumentParser(logger, registry).parseAndValidate(rawArgs);
257+
ConnectionAndSslParameters<A> args;
258+
259+
if (checkPwdKey(rawArgs.iterator())) {
260+
args = new ArgumentParser(logger, registry).parseAndValidate(prepareConsoleRequest(rawArgs));
261+
} else
262+
args = new ArgumentParser(logger, registry).parseAndValidate(rawArgs);
255263

256264
cmdName = toFormattedCommandName(args.cmdPath().peekLast().getClass()).toUpperCase();
257265

@@ -791,6 +799,45 @@ private void printCacheHelpHeader(IgniteLogger logger) {
791799
logger.info(INDENT + "Subcommands:");
792800
}
793801

802+
/**
803+
* Check command for password console input.
804+
*
805+
* @param args Iterator of raw arguments.
806+
*/
807+
protected boolean checkPwdKey(Iterator<String> args) {
808+
while (args.hasNext()) {
809+
String arg = args.next();
810+
811+
if (!arg.startsWith(NAME_PREFIX))
812+
continue;
813+
814+
if (arg.equals(CMD_PASSWORD)) {
815+
return !args.hasNext() || args.next().startsWith(NAME_PREFIX);
816+
}
817+
}
818+
819+
return false;
820+
}
821+
822+
/**
823+
* Prepare request with console passord input.
824+
*
825+
* @param rawArgs List of raw arguments.
826+
*/
827+
protected List<String> prepareConsoleRequest(List<String> rawArgs) {
828+
List<String> args = new ArrayList<>(rawArgs);
829+
830+
// Remove --password argument to create argument-value pair for parse in the end of List when --user is present.
831+
args.remove(CMD_PASSWORD);
832+
833+
if (rawArgs.contains(CMD_USER)) {
834+
args.add(CMD_PASSWORD);
835+
args.add(new String(requestPasswordFromConsole("password: ")));
836+
}
837+
838+
return args;
839+
}
840+
794841
/**
795842
* Generates usage for base command and all of its children, if any.
796843
*

modules/control-utility/src/test/java/org/apache/ignite/internal/processors/security/GridCommandHandlerSslWithSecurityTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,88 @@ public void testConnector() throws Exception {
195195
assertContains(log, testOutput, "--keystore-password *****");
196196
assertContains(log, testOutput, "--truststore-password *****");
197197
}
198+
199+
/**
200+
* Verify that the command work correctly when request starts with the --password argument
201+
* without value that invoke console password input for user, and that it is requested only once.
202+
*
203+
* @throws Exception If failed.
204+
*/
205+
@Test
206+
public void testInputKeyUserPwdOnlyOncePwdArgStart() throws Exception {
207+
performTest(Arrays.asList(
208+
"--password",
209+
"--state",
210+
"--user", login,
211+
"--keystore", keyStorePath("connectorClient"),
212+
"--keystore-password", keyStorePassword(),
213+
"--truststore", keyStorePath("trustthree"),
214+
"--truststore-password", keyStorePassword()));
215+
}
216+
217+
/**
218+
* Verify that the command work correctly when request contains the --password argument inside
219+
* without value that invoke console password input for user, and that it is requested only once.
220+
*
221+
* @throws Exception If failed.
222+
*/
223+
@Test
224+
public void testInputKeyUserPwdOnlyOncePwdArgMiddle() throws Exception {
225+
performTest(Arrays.asList(
226+
"--state",
227+
"--user", login,
228+
"--password",
229+
"--keystore", keyStorePath("connectorClient"),
230+
"--keystore-password", keyStorePassword(),
231+
"--truststore", keyStorePath("trustthree"),
232+
"--truststore-password", keyStorePassword()));
233+
}
234+
235+
/**
236+
* Verify that the command work correctly when request ends with the --password argument
237+
* without value that invoke console password input for user, and that it is requested only once.
238+
*
239+
* @throws Exception If failed.
240+
*/
241+
@Test
242+
public void testInputKeyUserPwdOnlyOncePwdArgEnd() throws Exception {
243+
performTest(Arrays.asList(
244+
"--state",
245+
"--user", login,
246+
"--keystore", keyStorePath("connectorClient"),
247+
"--keystore-password", keyStorePassword(),
248+
"--truststore", keyStorePath("trustthree"),
249+
"--truststore-password", keyStorePassword(),
250+
"--password"));
251+
}
252+
253+
/**
254+
* Perform the test with prepared List arguments
255+
*
256+
* @param args List of query arguments.
257+
* @throws Exception If failed.
258+
*/
259+
private void performTest(List<String> args) throws Exception {
260+
IgniteEx crd = startGrid();
261+
262+
crd.cluster().state(ACTIVE);
263+
264+
TestCommandHandler hnd = newCommandHandler();
265+
266+
AtomicInteger pwdCnt = new AtomicInteger();
267+
268+
((CommandHandler)GridTestUtils.getFieldValue(hnd, "hnd")).console = new NoopConsole() {
269+
/** {@inheritDoc} */
270+
@Override public char[] readPassword(String fmt, Object... args) {
271+
pwdCnt.incrementAndGet();
272+
log.info("PASSWORD: " + pwd);
273+
return pwd.toCharArray();
274+
}
275+
};
276+
277+
int exitCode = hnd.execute(args);
278+
279+
assertEquals(EXIT_CODE_OK, exitCode);
280+
assertEquals(1, pwdCnt.get());
281+
}
198282
}

0 commit comments

Comments
 (0)