Skip to content

Commit

Permalink
Move rabbit_password to rabbit_common
Browse files Browse the repository at this point in the history
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)
(cherry picked from commit 9cec4a5)

# Conflicts:
#	deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex
  • Loading branch information
lukebakken authored and mergify[bot] committed Feb 26, 2023
1 parent 22b225b commit c9646a1
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
%%

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

-define(DEFAULT_HASHING_MODULE, rabbit_password_hashing_sha256).

Expand Down
47 changes: 47 additions & 0 deletions deps/rabbit_common/test/unit_password_hashing_SUITE.erl
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion deps/rabbitmq_cli/lib/rabbitmq/cli/core/distribution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
<<<<<<< HEAD
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
=======
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
>>>>>>> 9cec4a5907 (Move rabbit_password to rabbit_common)

@behaviour RabbitMQ.CLI.CommandBehaviour

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -51,8 +46,6 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
:ok
end

use RabbitMQ.CLI.DefaultOutput

def usage, do: "hash_password <cleartext_password>"

def banner([arg], _options),
Expand Down

0 comments on commit c9646a1

Please sign in to comment.