Skip to content

Commit

Permalink
Merge pull request rabbitmq#7429 from rabbitmq/rabbitmq-server-5957-f…
Browse files Browse the repository at this point in the history
…ollowup

Move rabbit_password to rabbit_common
  • Loading branch information
michaelklishin authored Feb 26, 2023
2 parents 39dbbd4 + a5b3d63 commit 1905995
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 18 deletions.
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

0 comments on commit 1905995

Please sign in to comment.