Skip to content

Commit fb65b29

Browse files
SimonUngemergify[bot]
authored andcommitted
See #5957. CLI command to generate hashed password from cleartext password
(cherry picked from commit 67bc94e)
1 parent 711944b commit fb65b29

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
8+
@behaviour RabbitMQ.CLI.CommandBehaviour
9+
10+
use RabbitMQ.CLI.Core.MergesNoDefaults
11+
12+
def run([cleartextpassword], %{node: node_name}) do
13+
r =
14+
:rabbit_misc.rpc_call(
15+
node_name,
16+
:rabbit_password,
17+
:hash,
18+
[cleartextpassword]
19+
)
20+
21+
Base.encode64(r)
22+
end
23+
24+
def validate(args, _options) when length(args) > 1 do
25+
{:validation_failure, :too_many_args}
26+
end
27+
28+
def validate(args, _options) when length(args) < 1 do
29+
{:validation_failure, :not_enough_args}
30+
end
31+
32+
def validate([""], _options) do
33+
{:bad_argument, "password cannot be an empty string"}
34+
end
35+
36+
def validate([_arg], _options) do
37+
:ok
38+
end
39+
40+
use RabbitMQ.CLI.DefaultOutput
41+
42+
def usage, do: "hash_password <cleartext_password>"
43+
44+
def banner([arg], _options),
45+
do: "Will hash password #{arg}"
46+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
6+
7+
defmodule HashPasswordCommandTest do
8+
use ExUnit.Case, async: false
9+
import TestHelper
10+
11+
@command RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand
12+
13+
setup_all do
14+
RabbitMQ.CLI.Core.Distribution.start()
15+
:ok
16+
end
17+
18+
setup context do
19+
on_exit(context, fn -> delete_user(context[:user]) end)
20+
{:ok, opts: %{node: get_rabbit_hostname()}}
21+
end
22+
23+
test "validate: too many arguments", context do
24+
assert @command.validate(["foo", "bar"], context[:opts]) ==
25+
{:validation_failure, :too_many_args}
26+
end
27+
28+
test "validate: too few arguments", context do
29+
assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args}
30+
end
31+
32+
test "validate: empty string", context do
33+
assert @command.validate([""], context[:opts]) ==
34+
{:bad_argument, "password cannot be an empty string"}
35+
end
36+
37+
@tag user: "someone", password: "hashed_password"
38+
test "run: successfully create user with a hashed password from cli cmd", context do
39+
hashed_pwd = @command.run([context[:password]], context[:opts])
40+
add_user_hashed_password(context[:user], hashed_pwd)
41+
assert {:ok, _} = authenticate_user(context[:user], context[:password])
42+
end
43+
44+
@tag user: "someone", password: "hashed_password"
45+
test "run: Create user with a hashed password from cli cmd, use hashed pwd as cleartest password",
46+
context do
47+
hashed_pwd = @command.run([context[:password]], context[:opts])
48+
add_user_hashed_password(context[:user], hashed_pwd)
49+
assert {:refused, _, _, _} = authenticate_user(context[:user], hashed_pwd)
50+
end
51+
end

deps/rabbitmq_cli/test/test_helper.exs

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ defmodule TestHelper do
7373
])
7474
end
7575

76+
def add_user_hashed_password(name, hash_password) do
77+
:rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :put_user, [
78+
%{:name => name, :password_hash => hash_password, :tags => "administrator"},
79+
"acting-user"
80+
])
81+
end
82+
7683
def delete_user(name) do
7784
:rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :delete_user, [
7885
name,

0 commit comments

Comments
 (0)