Skip to content

Move rabbit_password to rabbit_common (backport #7429) #7434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deps/rabbit_common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ rabbitmq_suite(
size = "small",
)

rabbitmq_suite(
name = "unit_password_hashing_SUITE",
size = "small",
)

rabbitmq_suite(
name = "unit_SUITE",
size = "medium",
Expand Down
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,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

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