Skip to content

Commit db25015

Browse files
Merge pull request #4263 from rabbitmq/mergify/bp/v3.10.x/pr-4258
rabbitmqct {encode, decode}: accept more values via standard input (backport #4258)
2 parents 5939981 + 12500ae commit db25015

File tree

5 files changed

+80
-25
lines changed

5 files changed

+80
-25
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/core/input.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## License, v. 2.0. If a copy of the MPL was not distributed with this
33
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
##
5-
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
5+
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Core.Input do
88
alias RabbitMQ.CLI.Core.Config

deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/decode_command.ex

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
## License, v. 2.0. If a copy of the MPL was not distributed with this
33
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
##
5-
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
5+
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
alias RabbitMQ.CLI.Core.Helpers
88

99
defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do
10-
alias RabbitMQ.CLI.Core.DocGuide
10+
alias RabbitMQ.CLI.Core.{DocGuide, Input}
1111

1212
@behaviour RabbitMQ.CLI.CommandBehaviour
1313
use RabbitMQ.CLI.DefaultOutput
@@ -32,15 +32,15 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do
3232
{args, Helpers.atomize_values(with_defaults, @atomized_keys)}
3333
end
3434

35-
def validate(args, _) when length(args) < 2 do
35+
def validate(args, _) when length(args) < 1 do
3636
{:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase"}}
3737
end
3838

3939
def validate(args, _) when length(args) > 2 do
4040
{:validation_failure, :too_many_args}
4141
end
4242

43-
def validate(args, opts) when length(args) === 2 do
43+
def validate(_args, opts) do
4444
case {supports_cipher(opts.cipher), supports_hash(opts.hash), opts.iterations > 0} do
4545
{false, _, _} ->
4646
{:validation_failure, {:bad_argument, "The requested cipher is not supported"}}
@@ -58,6 +58,31 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do
5858
end
5959
end
6060

61+
def run([value], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
62+
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
63+
:eof -> {:error, :not_enough_args}
64+
passphrase ->
65+
try do
66+
term_value = Helpers.evaluate_input_as_term(value)
67+
68+
term_to_decrypt =
69+
case term_value do
70+
{:encrypted, _} = encrypted ->
71+
encrypted
72+
_ ->
73+
{:encrypted, term_value}
74+
end
75+
76+
result = :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, term_to_decrypt)
77+
{:ok, result}
78+
catch
79+
_, _ ->
80+
{:error,
81+
"Failed to decrypt the value. Things to check: is the passphrase correct? Are the cipher and hash algorithms the same as those used for encryption?"}
82+
end
83+
end
84+
end
85+
6186
def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do
6287
try do
6388
term_value = Helpers.evaluate_input_as_term(value)
@@ -81,8 +106,8 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do
81106

82107
def formatter(), do: RabbitMQ.CLI.Formatters.Erlang
83108

84-
def banner([_, _], _) do
85-
"Decrypting value ..."
109+
def banner(_, _) do
110+
"Decrypting value..."
86111
end
87112

88113
def usage, do: "decode value passphrase [--cipher <cipher>] [--hash <hash>] [--iterations <iterations>]"

deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/encode_command.ex

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Ctl.Commands.EncodeCommand do
8-
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
8+
alias RabbitMQ.CLI.Core.{DocGuide, Helpers, Input}
99

1010
@behaviour RabbitMQ.CLI.CommandBehaviour
1111
use RabbitMQ.CLI.DefaultOutput
@@ -30,15 +30,11 @@ defmodule RabbitMQ.CLI.Ctl.Commands.EncodeCommand do
3030
{args, Helpers.atomize_values(with_defaults, @atomized_keys)}
3131
end
3232

33-
def validate(args, _) when length(args) < 2 do
34-
{:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase."}}
35-
end
36-
3733
def validate(args, _) when length(args) > 2 do
3834
{:validation_failure, :too_many_args}
3935
end
4036

41-
def validate(args, opts) when length(args) === 2 do
37+
def validate(_args, opts) do
4238
case {supports_cipher(opts.cipher), supports_hash(opts.hash), opts.iterations > 0} do
4339
{false, _, _} ->
4440
{:validation_failure, {:bad_argument, "The requested cipher is not supported."}}
@@ -54,20 +50,54 @@ defmodule RabbitMQ.CLI.Ctl.Commands.EncodeCommand do
5450
end
5551
end
5652

53+
def run([], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
54+
case Input.consume_single_line_string_with_prompt("Value to encode: ", opts) do
55+
:eof -> {:error, :not_enough_args}
56+
value ->
57+
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
58+
:eof -> {:error, :not_enough_args}
59+
passphrase ->
60+
try do
61+
term_value = Helpers.evaluate_input_as_term(value)
62+
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
63+
{:ok, result}
64+
catch
65+
_, _ ->
66+
{:error, "Error during cipher operation"}
67+
end
68+
end
69+
end
70+
end
71+
72+
def run([value], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
73+
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
74+
:eof -> {:error, :not_enough_args}
75+
passphrase ->
76+
try do
77+
term_value = Helpers.evaluate_input_as_term(value)
78+
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
79+
{:ok, result}
80+
catch
81+
_, _ ->
82+
{:error, "Error during cipher operation"}
83+
end
84+
end
85+
end
86+
5787
def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do
5888
try do
5989
term_value = Helpers.evaluate_input_as_term(value)
6090
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
6191
{:ok, result}
6292
catch
6393
_, _ ->
64-
{:error, "Error during cipher operation."}
94+
{:error, "Error during cipher operation"}
6595
end
6696
end
6797

6898
def formatter(), do: RabbitMQ.CLI.Formatters.Erlang
6999

70-
def banner([_, _], _) do
100+
def banner(_, _) do
71101
"Encrypting value ..."
72102
end
73103

deps/rabbitmq_cli/test/ctl/decode_command_test.exs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## License, v. 2.0. If a copy of the MPL was not distributed with this
33
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
##
5-
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
5+
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule DecodeCommandTest do
88
use ExUnit.Case, async: false
@@ -20,11 +20,13 @@ defmodule DecodeCommandTest do
2020
assert :ok == @command.validate(["value", "secret"], context[:opts])
2121
end
2222

23-
test "validate: providing zero or one positional argument fails", context do
23+
test "validate: providing no positional arguments fails", context do
2424
assert match?({:validation_failure, {:not_enough_args, _}},
2525
@command.validate([], context[:opts]))
26-
assert match?({:validation_failure, {:not_enough_args, _}},
27-
@command.validate(["value"], context[:opts]))
26+
end
27+
28+
test "validate: providing one positional argument passes", context do
29+
assert :ok == @command.validate(["value"], context[:opts])
2830
end
2931

3032
test "validate: providing three or more positional argument fails", context do

deps/rabbitmq_cli/test/ctl/encode_command_test.exs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## License, v. 2.0. If a copy of the MPL was not distributed with this
33
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
##
5-
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
5+
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule EncodeCommandTest do
88
use ExUnit.Case, async: false
@@ -21,11 +21,9 @@ defmodule EncodeCommandTest do
2121
assert :ok == @command.validate(["value", "secret"], context[:opts])
2222
end
2323

24-
test "validate: providing zero or one positional argument fails", context do
25-
assert match?({:validation_failure, {:not_enough_args, _}},
26-
@command.validate([], context[:opts]))
27-
assert match?({:validation_failure, {:not_enough_args, _}},
28-
@command.validate(["value"], context[:opts]))
24+
test "validate: providing zero or one positional argument passes", context do
25+
assert :ok == @command.validate([], context[:opts])
26+
assert :ok == @command.validate(["value"], context[:opts])
2927
end
3028

3129
test "validate: providing three or more positional argument fails", context do

0 commit comments

Comments
 (0)