diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex index 0acef8ddd93b..9cd1ee83433f 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex @@ -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 @@ -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 " def banner([arg], _options), do: "Will hash password #{arg}" + + def banner([], _options), + do: "Will hash provided password" end diff --git a/deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs b/deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs index 06ebb3f8e37e..52668931c2af 100644 --- a/deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs +++ b/deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs @@ -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"}