-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #5957. CLI command to generate hashed password from cleartext pas…
- Loading branch information
1 parent
ffc2dcc
commit e1602aa
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/hash_password_command.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters