Skip to content

Commit 9cec4a5

Browse files
lukebakkenmergify[bot]
authored andcommitted
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 d9d6e1b) (cherry picked from commit 33bf1d8)
1 parent 73fa5d9 commit 9cec4a5

8 files changed

+57
-18
lines changed

deps/rabbit/src/rabbit_password.erl renamed to deps/rabbit_common/src/rabbit_password.erl

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
%%
77

88
-module(rabbit_password).
9-
-include_lib("rabbit_common/include/rabbit.hrl").
109

1110
-define(DEFAULT_HASHING_MODULE, rabbit_password_hashing_sha256).
1211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2011-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
%%
7+
8+
-module(unit_password_hashing_SUITE).
9+
10+
-compile(export_all).
11+
12+
all() -> [password_hashing].
13+
14+
%% -------------------------------------------------------------------
15+
%% Testsuite setup/teardown
16+
%% -------------------------------------------------------------------
17+
18+
init_per_suite(Config) -> Config.
19+
end_per_suite(Config) -> Config.
20+
21+
init_per_group(_Group, Config) -> Config.
22+
end_per_group(_Group, Config) -> Config.
23+
24+
init_per_testcase(_Testcase, Config) -> Config.
25+
end_per_testcase(_Testcase, Config) -> Config.
26+
27+
%% ---------------------------------------------------------------------------
28+
%% Test Cases
29+
%% ---------------------------------------------------------------------------
30+
31+
password_hashing(_Config) ->
32+
rabbit_password_hashing_sha256 = rabbit_password:hashing_mod(),
33+
application:set_env(rabbit, password_hashing_module,
34+
rabbit_password_hashing_md5),
35+
rabbit_password_hashing_md5 = rabbit_password:hashing_mod(),
36+
application:set_env(rabbit, password_hashing_module,
37+
rabbit_password_hashing_sha256),
38+
rabbit_password_hashing_sha256 = rabbit_password:hashing_mod(),
39+
40+
rabbit_password_hashing_sha256 =
41+
rabbit_password:hashing_mod(rabbit_password_hashing_sha256),
42+
rabbit_password_hashing_md5 =
43+
rabbit_password:hashing_mod(rabbit_password_hashing_md5),
44+
rabbit_password_hashing_md5 =
45+
rabbit_password:hashing_mod(undefined),
46+
47+
passed.

deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Copyright (c) 2016-2023 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Core.Distribution do
8-
alias RabbitMQ.CLI.Core.{ANSI, Config, Helpers}
8+
alias RabbitMQ.CLI.Core.{Config, Helpers}
99

1010
#
1111
# API

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

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

77
defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
8-
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, FeatureFlags, Helpers}
8+
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
99

1010
@behaviour RabbitMQ.CLI.CommandBehaviour
1111

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,27 @@
66

77
defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
88
alias RabbitMQ.CLI.Core.{Input}
9+
910
@behaviour RabbitMQ.CLI.CommandBehaviour
1011
use RabbitMQ.CLI.Core.MergesNoDefaults
12+
use RabbitMQ.CLI.DefaultOutput
1113

12-
def run([cleartextpassword], %{node: node_name}) do
13-
hash_password(cleartextpassword, node_name)
14+
def run([cleartextpassword], _opts) do
15+
hash_password(cleartextpassword)
1416
end
1517

16-
def run([], %{node: node_name} = opts) do
18+
def run([], opts) do
1719
case Input.infer_password("Password: ", opts) do
1820
:eof ->
1921
{:error, :not_enough_args}
2022

2123
password ->
22-
hash_password(password, node_name)
24+
hash_password(password)
2325
end
2426
end
2527

26-
def hash_password(password, node_name) do
27-
hashed_pwd =
28-
:rabbit_misc.rpc_call(
29-
node_name,
30-
:rabbit_password,
31-
:hash,
32-
[password]
33-
)
34-
28+
def hash_password(password) do
29+
hashed_pwd = :rabbit_password.hash(password)
3530
Base.encode64(hashed_pwd)
3631
end
3732

@@ -51,8 +46,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
5146
:ok
5247
end
5348

54-
use RabbitMQ.CLI.DefaultOutput
55-
5649
def usage, do: "hash_password <cleartext_password>"
5750

5851
def banner([arg], _options),

0 commit comments

Comments
 (0)