Skip to content

Commit

Permalink
Merge pull request #7064 from rabbitmq/mergify/bp/v3.10.x/pr-7062
Browse files Browse the repository at this point in the history
See #5957. Accept empty arg and prompt for password (backport #7060) (backport #7062)
  • Loading branch information
michaelklishin authored Jan 27, 2023
2 parents 86716d2 + 7ff987a commit 214b824
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
alias RabbitMQ.CLI.Core.{Input}
@behaviour RabbitMQ.CLI.CommandBehaviour

use RabbitMQ.CLI.Core.MergesNoDefaults

def run([cleartextpassword], %{node: node_name}) do
r =
hash_password(cleartextpassword, node_name)
end

def run([], %{node: node_name} = opts) do
case Input.infer_password("Password: ", opts) do
:eof ->
{:error, :not_enough_args}

password ->
hash_password(password, node_name)
end
end

def hash_password(password, node_name) do
hashed_pwd =
:rabbit_misc.rpc_call(
node_name,
:rabbit_password,
:hash,
[cleartextpassword]
[password]
)

Base.encode64(r)
Base.encode64(hashed_pwd)
end

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

def validate(args, _options) when length(args) < 1 do
{:validation_failure, :not_enough_args}
end

def validate([""], _options) do
{:bad_argument, "password cannot be an empty string"}
end
Expand All @@ -37,10 +47,17 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
:ok
end

def validate([], _options) do
:ok
end

use RabbitMQ.CLI.DefaultOutput

def usage, do: "hash_password <cleartext_password>"

def banner([arg], _options),
do: "Will hash password #{arg}"

def banner([], _options),
do: "Will hash provided password"
end
4 changes: 0 additions & 4 deletions deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ defmodule HashPasswordCommandTest do
{:validation_failure, :too_many_args}
end

test "validate: too few arguments", context do
assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args}
end

test "validate: empty string", context do
assert @command.validate([""], context[:opts]) ==
{:bad_argument, "password cannot be an empty string"}
Expand Down

0 comments on commit 214b824

Please sign in to comment.