Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConsolePasswordFinder to read from Console #338

Merged
merged 4 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {

testCompile "junit:junit:4.11"
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.mockito:mockito-core:2.8.47"
testCompile "org.apache.sshd:sshd-core:1.2.0"
testRuntime "ch.qos.logback:logback-classic:1.1.2"
testCompile 'org.glassfish.grizzly:grizzly-http-server:2.3.17'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.userauth.password;

import java.io.Console;
import java.util.IllegalFormatException;

/** A PasswordFinder that reads a password from a console */
public class ConsolePasswordFinder implements PasswordFinder {

private final Console console;
private final String promptFormat;
private final int maxTries;

private int numTries;

public static ConsolePasswordFinderBuilder builder() {
return new ConsolePasswordFinderBuilder();
}

public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) {
this.console = console;
this.promptFormat = promptFormat;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not checking the format of the prompt here, but the constructor is public.

I don't think we actually need the builder, it's just 3 parameters. I'm fine with using just a constructor.

In any case we need to do the check here also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! OK, I'll take out the builder (good to have less code to maintain) and put the check here

this.maxTries = maxTries;
this.numTries = 0;
}

@Override
public char[] reqPassword(Resource<?> resource) {
numTries++;
if (console == null) {
// the request cannot be serviced
return null;
}
return console.readPassword(promptFormat, resource.toString());
}

@Override
public boolean shouldRetry(Resource<?> resource) {
return numTries < maxTries;
}

public static class ConsolePasswordFinderBuilder {
private Console console;
private String promptFormat;
private int maxTries;

/** Builder constructor should only be called from parent class */
private ConsolePasswordFinderBuilder() {
console = System.console();
promptFormat = "Enter passphrase for %s:";
maxTries = 3;
}

public ConsolePasswordFinder build() {
return new ConsolePasswordFinder(console, promptFormat, maxTries);
}

public ConsolePasswordFinderBuilder setConsole(Console console) {
this.console = console;
return this;
}

public Console getConsole() {
return console;
}

/**
* @param promptFormat a StringFormatter string that may contain up to one "%s"
*/
public ConsolePasswordFinderBuilder setPromptFormat(String promptFormat) {
checkFormatString(promptFormat);
this.promptFormat = promptFormat;
return this;
}

public String getPromptFormat() {
return promptFormat;
}

public ConsolePasswordFinderBuilder setMaxTries(int maxTries) {
this.maxTries = maxTries;
return this;
}

public int getMaxTries() {
return maxTries;
}

private static void checkFormatString(String promptFormat) {
try {
String.format(promptFormat, "");
} catch (IllegalFormatException e) {
throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.userauth.password;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.Console;

public class TestConsolePasswordFinder {

@Test
public void testReqPassword() {
char[] expectedPassword = "password".toCharArray();

Console console = Mockito.mock(Console.class);
Mockito.when(console.readPassword(Mockito.anyString(), Mockito.any()))
.thenReturn(expectedPassword);

Resource resource = Mockito.mock(Resource.class);
char[] password = ConsolePasswordFinder.builder()
.setConsole(console)
.build()
.reqPassword(resource);

Assert.assertArrayEquals("Password should match mocked return value",
expectedPassword, password);
Mockito.verifyNoMoreInteractions(resource);
}

@Test
public void testReqPasswordNullConsole() {
Resource<?> resource = Mockito.mock(Resource.class);
char[] password = ConsolePasswordFinder.builder()
.setConsole(null)
.build()
.reqPassword(resource);

Assert.assertNull("Password should be null with null console", password);
Mockito.verifyNoMoreInteractions(resource);
}

@Test
public void testShouldRetry() {
Resource<String> resource = new PrivateKeyStringResource("");
ConsolePasswordFinder finder = ConsolePasswordFinder.builder()
.setConsole(null)
.setMaxTries(1)
.build();
Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource));

finder.reqPassword(resource);
Assert.assertFalse("Should stop allowing retries after one interaction", finder.shouldRetry(resource));
}

@Test
public void testPromptFormat() {
Assert.assertNotNull(
"Empty format should create valid ConsolePasswordFinder",
ConsolePasswordFinder.builder().setPromptFormat("").build());
Assert.assertNotNull(
"Single-string format should create valid ConsolePasswordFinder",
ConsolePasswordFinder.builder().setPromptFormat("%s").build());
}

@Test(expected = IllegalArgumentException.class)
public void testPromptFormatTooManyMarkers() {
ConsolePasswordFinder.builder().setPromptFormat("%s%s");
}

@Test(expected = IllegalArgumentException.class)
public void testPromptFormatWrongMarkerType() {
ConsolePasswordFinder.builder().setPromptFormat("%d");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# incubating feature to allow mocking final classes
mock-maker-inline