Skip to content
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

rabbitmqct {encode, decode}: accept more values via standard input #4258

Merged
merged 3 commits into from Mar 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion deps/rabbitmq_cli/lib/rabbitmq/cli/core/input.ex
Expand Up @@ -2,7 +2,7 @@
## 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-2021 VMware, Inc. or its affiliates. All rights reserved.
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.

defmodule RabbitMQ.CLI.Core.Input do
alias RabbitMQ.CLI.Core.Config
Expand Down
37 changes: 31 additions & 6 deletions deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/decode_command.ex
Expand Up @@ -2,12 +2,12 @@
## 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-2021 VMware, Inc. or its affiliates. All rights reserved.
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.

alias RabbitMQ.CLI.Core.Helpers

defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do
alias RabbitMQ.CLI.Core.DocGuide
alias RabbitMQ.CLI.Core.{DocGuide, Input}

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

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

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

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

def run([value], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
:eof -> {:error, :not_enough_args}
passphrase ->
try do
term_value = Helpers.evaluate_input_as_term(value)

term_to_decrypt =
case term_value do
{:encrypted, _} = encrypted ->
encrypted
_ ->
{:encrypted, term_value}
end

result = :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, term_to_decrypt)
{:ok, result}
catch
_, _ ->
{:error,
"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?"}
end
end
end

def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do
try do
term_value = Helpers.evaluate_input_as_term(value)
Expand All @@ -81,8 +106,8 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DecodeCommand do

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

def banner([_, _], _) do
"Decrypting value ..."
def banner(_, _) do
"Decrypting value..."
end

def usage, do: "decode value passphrase [--cipher <cipher>] [--hash <hash>] [--iterations <iterations>]"
Expand Down
46 changes: 38 additions & 8 deletions deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/encode_command.ex
Expand Up @@ -5,7 +5,7 @@
## Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.

defmodule RabbitMQ.CLI.Ctl.Commands.EncodeCommand do
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
alias RabbitMQ.CLI.Core.{DocGuide, Helpers, Input}

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

def validate(args, _) when length(args) < 2 do
{:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase."}}
end

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

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

def run([], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
case Input.consume_single_line_string_with_prompt("Value to encode: ", opts) do
:eof -> {:error, :not_enough_args}
value ->
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
:eof -> {:error, :not_enough_args}
passphrase ->
try do
term_value = Helpers.evaluate_input_as_term(value)
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
{:ok, result}
catch
_, _ ->
{:error, "Error during cipher operation"}
end
end
end
end

def run([value], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do
case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do
:eof -> {:error, :not_enough_args}
passphrase ->
try do
term_value = Helpers.evaluate_input_as_term(value)
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
{:ok, result}
catch
_, _ ->
{:error, "Error during cipher operation"}
end
end
end

def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do
try do
term_value = Helpers.evaluate_input_as_term(value)
result = {:encrypted, _} = :rabbit_pbe.encrypt_term(cipher, hash, iterations, passphrase, term_value)
{:ok, result}
catch
_, _ ->
{:error, "Error during cipher operation."}
{:error, "Error during cipher operation"}
end
end

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

def banner([_, _], _) do
def banner(_, _) do
"Encrypting value ..."
end

Expand Down
10 changes: 6 additions & 4 deletions deps/rabbitmq_cli/test/ctl/decode_command_test.exs
Expand Up @@ -2,7 +2,7 @@
## 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.
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.

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

test "validate: providing zero or one positional argument fails", context do
test "validate: providing no positional arguments fails", context do
assert match?({:validation_failure, {:not_enough_args, _}},
@command.validate([], context[:opts]))
assert match?({:validation_failure, {:not_enough_args, _}},
@command.validate(["value"], context[:opts]))
end

test "validate: providing one positional argument passes", context do
assert :ok == @command.validate(["value"], context[:opts])
end

test "validate: providing three or more positional argument fails", context do
Expand Down
10 changes: 4 additions & 6 deletions deps/rabbitmq_cli/test/ctl/encode_command_test.exs
Expand Up @@ -2,7 +2,7 @@
## 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.
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.

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

test "validate: providing zero or one positional argument fails", context do
assert match?({:validation_failure, {:not_enough_args, _}},
@command.validate([], context[:opts]))
assert match?({:validation_failure, {:not_enough_args, _}},
@command.validate(["value"], context[:opts]))
test "validate: providing zero or one positional argument passes", context do
assert :ok == @command.validate([], context[:opts])
assert :ok == @command.validate(["value"], context[:opts])
end

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