Skip to content

Commit 532f580

Browse files
SimonUngemergify[bot]
authored andcommitted
See #5957. Accept empty arg and prompt for password
(cherry picked from commit 03617f6)
1 parent 6764d31 commit 532f580

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,40 @@
55
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
8+
alias RabbitMQ.CLI.Core.{Input}
89
@behaviour RabbitMQ.CLI.CommandBehaviour
9-
1010
use RabbitMQ.CLI.Core.MergesNoDefaults
1111

1212
def run([cleartextpassword], %{node: node_name}) do
13-
r =
13+
hash_password(cleartextpassword, node_name)
14+
end
15+
16+
def run([], %{node: node_name} = opts) do
17+
case Input.infer_password("Password: ", opts) do
18+
:eof ->
19+
{:error, :not_enough_args}
20+
21+
password ->
22+
hash_password(password, node_name)
23+
end
24+
end
25+
26+
def hash_password(password, node_name) do
27+
hashed_pwd =
1428
:rabbit_misc.rpc_call(
1529
node_name,
1630
:rabbit_password,
1731
:hash,
18-
[cleartextpassword]
32+
[password]
1933
)
2034

21-
Base.encode64(r)
35+
Base.encode64(hashed_pwd)
2236
end
2337

2438
def validate(args, _options) when length(args) > 1 do
2539
{:validation_failure, :too_many_args}
2640
end
2741

28-
def validate(args, _options) when length(args) < 1 do
29-
{:validation_failure, :not_enough_args}
30-
end
31-
3242
def validate([""], _options) do
3343
{:bad_argument, "password cannot be an empty string"}
3444
end
@@ -37,10 +47,17 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
3747
:ok
3848
end
3949

50+
def validate([], _options) do
51+
:ok
52+
end
53+
4054
use RabbitMQ.CLI.DefaultOutput
4155

4256
def usage, do: "hash_password <cleartext_password>"
4357

4458
def banner([arg], _options),
4559
do: "Will hash password #{arg}"
60+
61+
def banner([], _options),
62+
do: "Will hash provided password"
4663
end

deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs

-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ defmodule HashPasswordCommandTest do
2525
{:validation_failure, :too_many_args}
2626
end
2727

28-
test "validate: too few arguments", context do
29-
assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args}
30-
end
31-
3228
test "validate: empty string", context do
3329
assert @command.validate([""], context[:opts]) ==
3430
{:bad_argument, "password cannot be an empty string"}

0 commit comments

Comments
 (0)