|
| 1 | +## This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +## License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +## file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +## |
| 5 | +## Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | + |
| 7 | +alias RabbitMQ.CLI.Core.Helpers |
| 8 | + |
| 9 | +defmodule RabbitMQ.CLI.Ctl.Commands.DecryptConfValueCommand do |
| 10 | + alias RabbitMQ.CLI.Core.{DocGuide, Input} |
| 11 | + |
| 12 | + @behaviour RabbitMQ.CLI.CommandBehaviour |
| 13 | + use RabbitMQ.CLI.DefaultOutput |
| 14 | + |
| 15 | + def switches() do |
| 16 | + [ |
| 17 | + cipher: :string, |
| 18 | + hash: :string, |
| 19 | + iterations: :integer |
| 20 | + ] |
| 21 | + end |
| 22 | + |
| 23 | + @atomized_keys [:cipher, :hash] |
| 24 | + @prefix "encrypted:" |
| 25 | + |
| 26 | + def distribution(_), do: :none |
| 27 | + |
| 28 | + def merge_defaults(args, opts) do |
| 29 | + with_defaults = |
| 30 | + Map.merge( |
| 31 | + %{ |
| 32 | + cipher: :rabbit_pbe.default_cipher(), |
| 33 | + hash: :rabbit_pbe.default_hash(), |
| 34 | + iterations: :rabbit_pbe.default_iterations() |
| 35 | + }, |
| 36 | + opts |
| 37 | + ) |
| 38 | + |
| 39 | + {args, Helpers.atomize_values(with_defaults, @atomized_keys)} |
| 40 | + end |
| 41 | + |
| 42 | + def validate(args, _) when length(args) < 1 do |
| 43 | + {:validation_failure, {:not_enough_args, "Please provide a value to decode and a passphrase"}} |
| 44 | + end |
| 45 | + |
| 46 | + def validate(args, _) when length(args) > 2 do |
| 47 | + {:validation_failure, :too_many_args} |
| 48 | + end |
| 49 | + |
| 50 | + def validate(_args, opts) do |
| 51 | + case {supports_cipher(opts.cipher), supports_hash(opts.hash), opts.iterations > 0} do |
| 52 | + {false, _, _} -> |
| 53 | + {:validation_failure, {:bad_argument, "The requested cipher is not supported"}} |
| 54 | + |
| 55 | + {_, false, _} -> |
| 56 | + {:validation_failure, {:bad_argument, "The requested hash is not supported"}} |
| 57 | + |
| 58 | + {_, _, false} -> |
| 59 | + {:validation_failure, |
| 60 | + {:bad_argument, |
| 61 | + "The requested number of iterations is incorrect (must be a positive integer)"}} |
| 62 | + |
| 63 | + {true, true, true} -> |
| 64 | + :ok |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def run([value], %{cipher: cipher, hash: hash, iterations: iterations} = opts) do |
| 69 | + case Input.consume_single_line_string_with_prompt("Passphrase: ", opts) do |
| 70 | + :eof -> |
| 71 | + {:error, :not_enough_args} |
| 72 | + |
| 73 | + passphrase -> |
| 74 | + try do |
| 75 | + term_value = Helpers.evaluate_input_as_term(value) |
| 76 | + |
| 77 | + term_to_decrypt = |
| 78 | + case term_value do |
| 79 | + prefixed_val when is_bitstring(prefixed_val) or is_list(prefixed_val) -> |
| 80 | + tag_input_value_with_encrypted(prefixed_val) |
| 81 | + |
| 82 | + {:encrypted, _} = encrypted -> |
| 83 | + encrypted |
| 84 | + |
| 85 | + _ -> |
| 86 | + {:encrypted, term_value} |
| 87 | + end |
| 88 | + |
| 89 | + result = :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, term_to_decrypt) |
| 90 | + {:ok, result} |
| 91 | + catch |
| 92 | + _, _ -> |
| 93 | + IO.inspect(__STACKTRACE__) |
| 94 | + {:error, |
| 95 | + "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?"} |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def run([value, passphrase], %{cipher: cipher, hash: hash, iterations: iterations}) do |
| 101 | + try do |
| 102 | + term_value = Helpers.evaluate_input_as_term(value) |
| 103 | + |
| 104 | + term_to_decrypt = |
| 105 | + case term_value do |
| 106 | + prefixed_val when is_bitstring(prefixed_val) or is_list(prefixed_val) -> |
| 107 | + tag_input_value_with_encrypted(prefixed_val) |
| 108 | + |
| 109 | + {:encrypted, _} = encrypted -> |
| 110 | + encrypted |
| 111 | + |
| 112 | + _ -> |
| 113 | + {:encrypted, term_value} |
| 114 | + end |
| 115 | + |
| 116 | + result = :rabbit_pbe.decrypt_term(cipher, hash, iterations, passphrase, term_to_decrypt) |
| 117 | + {:ok, result} |
| 118 | + catch |
| 119 | + _, _ -> |
| 120 | + IO.inspect(__STACKTRACE__) |
| 121 | + {:error, |
| 122 | + "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?"} |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + def formatter(), do: RabbitMQ.CLI.Formatters.Erlang |
| 127 | + |
| 128 | + def banner(_, _) do |
| 129 | + "Decrypting a rabbitmq.conf string value..." |
| 130 | + end |
| 131 | + |
| 132 | + def usage, |
| 133 | + do: "decrypt_conf_value value passphrase [--cipher <cipher>] [--hash <hash>] [--iterations <iterations>]" |
| 134 | + |
| 135 | + def usage_additional() do |
| 136 | + [ |
| 137 | + ["<value>", "a double-quoted rabbitmq.conf string value to decode"], |
| 138 | + ["<passphrase>", "passphrase to use with the config value encryption key"], |
| 139 | + ["--cipher <cipher>", "cipher suite to use"], |
| 140 | + ["--hash <hash>", "hashing function to use"], |
| 141 | + ["--iterations <iterations>", "number of iteration to apply"] |
| 142 | + ] |
| 143 | + end |
| 144 | + |
| 145 | + def usage_doc_guides() do |
| 146 | + [ |
| 147 | + DocGuide.configuration() |
| 148 | + ] |
| 149 | + end |
| 150 | + |
| 151 | + def help_section(), do: :configuration |
| 152 | + |
| 153 | + def description(), do: "Decrypts an encrypted configuration value" |
| 154 | + |
| 155 | + # |
| 156 | + # Implementation |
| 157 | + # |
| 158 | + |
| 159 | + defp supports_cipher(cipher), do: Enum.member?(:rabbit_pbe.supported_ciphers(), cipher) |
| 160 | + |
| 161 | + defp supports_hash(hash), do: Enum.member?(:rabbit_pbe.supported_hashes(), hash) |
| 162 | + |
| 163 | + defp tag_input_value_with_encrypted(value) when is_bitstring(value) or is_list(value) do |
| 164 | + bin_val = :rabbit_data_coercion.to_binary(value) |
| 165 | + untagged_val = String.replace_prefix(bin_val, @prefix, "") |
| 166 | + |
| 167 | + {:encrypted, untagged_val} |
| 168 | + end |
| 169 | + defp tag_input_value_with_encrypted(value) do |
| 170 | + {:encrypted, value} |
| 171 | + end |
| 172 | +end |
0 commit comments