From 9cec4a59079d05e771759622fb2c2477618e20cc Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Sat, 25 Feb 2023 09:53:46 -0800 Subject: [PATCH] Move rabbit_password to rabbit_common This allows `rabbitmqctl hash_password` to run without having RabbitMQ up. Follow-up to: * #5957 * #7003 (cherry picked from commit d9d6e1bef671cc4edf8bdbeb8a91b00e9e14ac21) (cherry picked from commit 33bf1d8d7b3f3fe182ef51feb730ea34a8b437d5) --- .../src/rabbit_password.erl | 1 - .../src/rabbit_password_hashing_md5.erl | 0 .../src/rabbit_password_hashing_sha256.erl | 0 .../src/rabbit_password_hashing_sha512.erl | 0 .../test/unit_password_hashing_SUITE.erl | 47 +++++++++++++++++++ .../lib/rabbitmq/cli/core/distribution.ex | 2 +- .../cli/ctl/commands/add_vhost_command.ex | 2 +- .../cli/ctl/commands/hash_password_command.ex | 23 ++++----- 8 files changed, 57 insertions(+), 18 deletions(-) rename deps/{rabbit => rabbit_common}/src/rabbit_password.erl (96%) rename deps/{rabbit => rabbit_common}/src/rabbit_password_hashing_md5.erl (100%) rename deps/{rabbit => rabbit_common}/src/rabbit_password_hashing_sha256.erl (100%) rename deps/{rabbit => rabbit_common}/src/rabbit_password_hashing_sha512.erl (100%) create mode 100644 deps/rabbit_common/test/unit_password_hashing_SUITE.erl diff --git a/deps/rabbit/src/rabbit_password.erl b/deps/rabbit_common/src/rabbit_password.erl similarity index 96% rename from deps/rabbit/src/rabbit_password.erl rename to deps/rabbit_common/src/rabbit_password.erl index 7ebebbb31812..21c3357472f2 100644 --- a/deps/rabbit/src/rabbit_password.erl +++ b/deps/rabbit_common/src/rabbit_password.erl @@ -6,7 +6,6 @@ %% -module(rabbit_password). --include_lib("rabbit_common/include/rabbit.hrl"). -define(DEFAULT_HASHING_MODULE, rabbit_password_hashing_sha256). diff --git a/deps/rabbit/src/rabbit_password_hashing_md5.erl b/deps/rabbit_common/src/rabbit_password_hashing_md5.erl similarity index 100% rename from deps/rabbit/src/rabbit_password_hashing_md5.erl rename to deps/rabbit_common/src/rabbit_password_hashing_md5.erl diff --git a/deps/rabbit/src/rabbit_password_hashing_sha256.erl b/deps/rabbit_common/src/rabbit_password_hashing_sha256.erl similarity index 100% rename from deps/rabbit/src/rabbit_password_hashing_sha256.erl rename to deps/rabbit_common/src/rabbit_password_hashing_sha256.erl diff --git a/deps/rabbit/src/rabbit_password_hashing_sha512.erl b/deps/rabbit_common/src/rabbit_password_hashing_sha512.erl similarity index 100% rename from deps/rabbit/src/rabbit_password_hashing_sha512.erl rename to deps/rabbit_common/src/rabbit_password_hashing_sha512.erl diff --git a/deps/rabbit_common/test/unit_password_hashing_SUITE.erl b/deps/rabbit_common/test/unit_password_hashing_SUITE.erl new file mode 100644 index 000000000000..ea73e01e2d2c --- /dev/null +++ b/deps/rabbit_common/test/unit_password_hashing_SUITE.erl @@ -0,0 +1,47 @@ +%% This Source Code Form is subject to the terms of the Mozilla Public +%% License, v. 2.0. If a copy of the MPL was not distributed with this +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. +%% +%% Copyright (c) 2011-2023 VMware, Inc. or its affiliates. All rights reserved. +%% + +-module(unit_password_hashing_SUITE). + +-compile(export_all). + +all() -> [password_hashing]. + +%% ------------------------------------------------------------------- +%% Testsuite setup/teardown +%% ------------------------------------------------------------------- + +init_per_suite(Config) -> Config. +end_per_suite(Config) -> Config. + +init_per_group(_Group, Config) -> Config. +end_per_group(_Group, Config) -> Config. + +init_per_testcase(_Testcase, Config) -> Config. +end_per_testcase(_Testcase, Config) -> Config. + +%% --------------------------------------------------------------------------- +%% Test Cases +%% --------------------------------------------------------------------------- + +password_hashing(_Config) -> + rabbit_password_hashing_sha256 = rabbit_password:hashing_mod(), + application:set_env(rabbit, password_hashing_module, + rabbit_password_hashing_md5), + rabbit_password_hashing_md5 = rabbit_password:hashing_mod(), + application:set_env(rabbit, password_hashing_module, + rabbit_password_hashing_sha256), + rabbit_password_hashing_sha256 = rabbit_password:hashing_mod(), + + rabbit_password_hashing_sha256 = + rabbit_password:hashing_mod(rabbit_password_hashing_sha256), + rabbit_password_hashing_md5 = + rabbit_password:hashing_mod(rabbit_password_hashing_md5), + rabbit_password_hashing_md5 = + rabbit_password:hashing_mod(undefined), + + passed. diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex index 817e0839a17b..90875f9ca61f 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex @@ -5,7 +5,7 @@ ## Copyright (c) 2016-2023 VMware, Inc. or its affiliates. All rights reserved. defmodule RabbitMQ.CLI.Core.Distribution do - alias RabbitMQ.CLI.Core.{ANSI, Config, Helpers} + alias RabbitMQ.CLI.Core.{Config, Helpers} # # API diff --git a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex index 71061de66df4..fec34ff90042 100644 --- a/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex +++ b/deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex @@ -5,7 +5,7 @@ ## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved. defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do - alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, FeatureFlags, Helpers} + alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers} @behaviour RabbitMQ.CLI.CommandBehaviour 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 9cd1ee83433f..5e64e9514eae 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 @@ -6,32 +6,27 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do alias RabbitMQ.CLI.Core.{Input} + @behaviour RabbitMQ.CLI.CommandBehaviour use RabbitMQ.CLI.Core.MergesNoDefaults + use RabbitMQ.CLI.DefaultOutput - def run([cleartextpassword], %{node: node_name}) do - hash_password(cleartextpassword, node_name) + def run([cleartextpassword], _opts) do + hash_password(cleartextpassword) end - def run([], %{node: node_name} = opts) do + def run([], opts) do case Input.infer_password("Password: ", opts) do :eof -> {:error, :not_enough_args} password -> - hash_password(password, node_name) + hash_password(password) end end - def hash_password(password, node_name) do - hashed_pwd = - :rabbit_misc.rpc_call( - node_name, - :rabbit_password, - :hash, - [password] - ) - + def hash_password(password) do + hashed_pwd = :rabbit_password.hash(password) Base.encode64(hashed_pwd) end @@ -51,8 +46,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do :ok end - use RabbitMQ.CLI.DefaultOutput - def usage, do: "hash_password " def banner([arg], _options),