From fc6d4ef2c096d8f511e42004579b6ba045a3616a Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 7 Nov 2023 11:22:22 +0100 Subject: [PATCH] Improve commandline help for encodepassword command in the CLI Closes gh-38203 --- .../encodepassword/EncodePasswordCommand.java | 13 +++++++++---- .../encodepassword/EncodePasswordCommandTests.java | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommand.java b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommand.java index fe0d94372f8..1d393e6ea61 100644 --- a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommand.java +++ b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommand.java @@ -44,6 +44,7 @@ import org.springframework.util.StringUtils; * {@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 { @@ -70,8 +71,8 @@ public class EncodePasswordCommand extends OptionParsingCommand { @Override public Collection getExamples() { List 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; } @@ -82,12 +83,16 @@ public class EncodePasswordCommand extends OptionParsingCommand { @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; diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java index 1dec74abf39..16cff55f345 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java @@ -37,6 +37,7 @@ import static org.mockito.BDDMockito.then; * Tests for {@link EncodePasswordCommand}. * * @author Phillip Webb + * @author Moritz Halbritter */ @ExtendWith(MockitoExtension.class) class EncodePasswordCommandTests { @@ -67,6 +68,17 @@ class EncodePasswordCommandTests { 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();