Skip to content

Commit

Permalink
Improve commandline help for encodepassword command in the CLI
Browse files Browse the repository at this point in the history
Closes gh-38203
  • Loading branch information
mhalbritter committed Nov 7, 2023
1 parent 717d7a4 commit fc6d4ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* {@link Command} to encode passwords for use with Spring Security.
*
* @author Phillip Webb
* @author Moritz Halbritter
* @since 2.0.0
*/
public class EncodePasswordCommand extends OptionParsingCommand {
Expand All @@ -70,8 +71,8 @@ public String getUsageHelp() {
@Override
public Collection<HelpExample> getExamples() {
List<HelpExample> examples = new ArrayList<>();
examples
.add(new HelpExample("To encode a password with the default encoder", "spring encodepassword mypassword"));
examples.add(new HelpExample("To encode a password with the default (bcrypt) encoder",
"spring encodepassword mypassword"));
examples.add(new HelpExample("To encode a password with pbkdf2", "spring encodepassword -a pbkdf2 mypassword"));
return examples;
}
Expand All @@ -82,12 +83,16 @@ private static final class EncodePasswordOptionHandler extends OptionHandler {

@Override
protected void options() {
this.algorithm = option(Arrays.asList("algorithm", "a"), "The algorithm to use").withRequiredArg()
this.algorithm = option(Arrays.asList("algorithm", "a"),
"The algorithm to use. Supported algorithms: "
+ StringUtils.collectionToDelimitedString(ENCODERS.keySet(), ", ")
+ ". The default algorithm uses bcrypt")
.withRequiredArg()
.defaultsTo("default");
}

@Override
protected ExitStatus run(OptionSet options) throws Exception {
protected ExitStatus run(OptionSet options) {
if (options.nonOptionArguments().size() != 1) {
Log.error("A single password option must be provided");
return ExitStatus.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Tests for {@link EncodePasswordCommand}.
*
* @author Phillip Webb
* @author Moritz Halbritter
*/
@ExtendWith(MockitoExtension.class)
class EncodePasswordCommandTests {
Expand Down Expand Up @@ -67,6 +68,17 @@ void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception {
assertThat(status).isEqualTo(ExitStatus.OK);
}

@Test
void encodeWithDefaultShouldUseBcrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("-a", "default", "boot");
then(this.log).should().info(this.message.capture());
assertThat(this.message.getValue()).startsWith("{bcrypt}");
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", this.message.getValue()))
.isTrue();
assertThat(status).isEqualTo(ExitStatus.OK);
}

@Test
void encodeWithBCryptShouldUseBCrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand();
Expand Down

0 comments on commit fc6d4ef

Please sign in to comment.