5
5
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
6
6
7
7
defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
8
+ alias RabbitMQ.CLI.Core . { Input }
8
9
@ behaviour RabbitMQ.CLI.CommandBehaviour
9
-
10
10
use RabbitMQ.CLI.Core.MergesNoDefaults
11
11
12
12
def run ( [ cleartextpassword ] , % { node: node_name } ) do
13
- r =
13
+ hash_password ( cleartextpassword , node_name )
14
+ end
15
+
16
+ def run ( [ ] , % { node: node_name } = opts ) do
17
+ case Input . infer_password ( "Password: " , opts ) do
18
+ :eof ->
19
+ { :error , :not_enough_args }
20
+
21
+ password ->
22
+ hash_password ( password , node_name )
23
+ end
24
+ end
25
+
26
+ def hash_password ( password , node_name ) do
27
+ hashed_pwd =
14
28
:rabbit_misc . rpc_call (
15
29
node_name ,
16
30
:rabbit_password ,
17
31
:hash ,
18
- [ cleartextpassword ]
32
+ [ password ]
19
33
)
20
34
21
- Base . encode64 ( r )
35
+ Base . encode64 ( hashed_pwd )
22
36
end
23
37
24
38
def validate ( args , _options ) when length ( args ) > 1 do
25
39
{ :validation_failure , :too_many_args }
26
40
end
27
41
28
- def validate ( args , _options ) when length ( args ) < 1 do
29
- { :validation_failure , :not_enough_args }
30
- end
31
-
32
42
def validate ( [ "" ] , _options ) do
33
43
{ :bad_argument , "password cannot be an empty string" }
34
44
end
@@ -37,10 +47,17 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand do
37
47
:ok
38
48
end
39
49
50
+ def validate ( [ ] , _options ) do
51
+ :ok
52
+ end
53
+
40
54
use RabbitMQ.CLI.DefaultOutput
41
55
42
56
def usage , do: "hash_password <cleartext_password>"
43
57
44
58
def banner ( [ arg ] , _options ) ,
45
59
do: "Will hash password #{ arg } "
60
+
61
+ def banner ( [ ] , _options ) ,
62
+ do: "Will hash provided password"
46
63
end
0 commit comments