Skip to content

See #5957. CLI cmd to generate hashed password from cleartext password #7003

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 1 commit into from
Jan 24, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## 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) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour

use RabbitMQ.CLI.Core.MergesNoDefaults

def run([cleartextpassword], %{node: node_name}) do
r =
:rabbit_misc.rpc_call(
node_name,
:rabbit_password,
:hash,
[cleartextpassword]
)

Base.encode64(r)
end

def validate(args, _options) when length(args) > 1 do
{:validation_failure, :too_many_args}
end

def validate(args, _options) when length(args) < 1 do
{:validation_failure, :not_enough_args}
end

def validate([""], _options) do
{:bad_argument, "password cannot be an empty string"}
end

def validate([_arg], _options) do
:ok
end

use RabbitMQ.CLI.DefaultOutput

def usage, do: "hash_password <cleartext_password>"

def banner([arg], _options),
do: "Will hash password #{arg}"
end
51 changes: 51 additions & 0 deletions deps/rabbitmq_cli/test/ctl/hash_password_command_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## 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) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.

defmodule HashPasswordCommandTest do
use ExUnit.Case, async: false
import TestHelper

@command RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand

setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:ok
end

setup context do
on_exit(context, fn -> delete_user(context[:user]) end)
{:ok, opts: %{node: get_rabbit_hostname()}}
end

test "validate: too many arguments", context do
assert @command.validate(["foo", "bar"], context[:opts]) ==
{:validation_failure, :too_many_args}
end

test "validate: too few arguments", context do
assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args}
end

test "validate: empty string", context do
assert @command.validate([""], context[:opts]) ==
{:bad_argument, "password cannot be an empty string"}
end

@tag user: "someone", password: "hashed_password"
test "run: successfully create user with a hashed password from cli cmd", context do
hashed_pwd = @command.run([context[:password]], context[:opts])
add_user_hashed_password(context[:user], hashed_pwd)
assert {:ok, _} = authenticate_user(context[:user], context[:password])
end

@tag user: "someone", password: "hashed_password"
test "run: Create user with a hashed password from cli cmd, use hashed pwd as cleartest password",
context do
hashed_pwd = @command.run([context[:password]], context[:opts])
add_user_hashed_password(context[:user], hashed_pwd)
assert {:refused, _, _, _} = authenticate_user(context[:user], hashed_pwd)
end
end
7 changes: 7 additions & 0 deletions deps/rabbitmq_cli/test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ defmodule TestHelper do
])
end

def add_user_hashed_password(name, hash_password) do
:rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :put_user, [
%{:name => name, :password_hash => hash_password, :tags => "administrator"},
"acting-user"
])
end

def delete_user(name) do
:rpc.call(get_rabbit_hostname(), :rabbit_auth_backend_internal, :delete_user, [
name,
Expand Down