Skip to content

Commit

Permalink
feat: ask for password if -p is not provided (#4153)
Browse files Browse the repository at this point in the history
  • Loading branch information
spena committed Jan 13, 2020
1 parent 4e32da6 commit 7a83bbf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ksql-cli/src/main/java/io/confluent/ksql/Ksql.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.confluent.ksql.util.ErrorMessageUtil;
import io.confluent.ksql.version.metrics.KsqlVersionCheckerAgent;
import io.confluent.ksql.version.metrics.collector.KsqlModuleType;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -67,6 +69,11 @@ public static void main(final String[] args) throws IOException {
System.exit(-1);
}

// ask for password if not set through command parameters
if (!options.getUserName().isEmpty() && !options.isPasswordSet()) {
options.setPassword(readPassword());
}

try {
new Ksql(options, System.getProperties(), KsqlRestClient::create, Cli::build).run();
} catch (final Exception e) {
Expand All @@ -77,6 +84,16 @@ public static void main(final String[] args) throws IOException {
}
}

private static String readPassword() {
final Console console = System.console();
if (console == null) {
System.err.println("Could not get console for enter password; use -p option instead.");
System.exit(-1);
}

return new String(console.readPassword("Enter password: "));
}

void run() {
final Map<String, String> configProps = options.getConfigFile()
.map(Ksql::loadProperties)
Expand Down
12 changes: 12 additions & 0 deletions ksql-cli/src/main/java/io/confluent/ksql/cli/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ public OutputFormat getOutputFormat() {
return OutputFormat.valueOf(outputFormat);
}

public String getUserName() {
return userName;
}

public void setPassword(final String password) {
this.password = password;
}

public boolean isPasswordSet() {
return (password != null && !password.trim().isEmpty());
}

public Optional<BasicCredentials> getUserNameAndPassword() {
if ((userName == null && password != null) || (password == null && userName != null)) {
throw new ConfigException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ public void shouldReturnEmptyOptionWhenUserAndPassNotPresent() throws Exception
assertFalse(options.getUserNameAndPassword().isPresent());
}

@Test
public void shouldReturnPasswordNotSetIfPasswordIsNull() throws Exception {
final Options options = Options.parse("http://foobar");
assertFalse(options.isPasswordSet());
}

@Test
public void shouldReturnPasswordNotSetIfPasswordIsEmpty() throws Exception {
final Options options = Options.parse("http://foobar", "-u", "joe", "-p", "");
assertFalse(options.isPasswordSet());
}

@Test
public void shouldReturnPasswordSetIfPasswordIsNotEmpty() throws Exception {
final Options options = Options.parse("http://foobar", "-u", "joe", "-p", "joe");
assertTrue(options.isPasswordSet());
}
}

0 comments on commit 7a83bbf

Please sign in to comment.