rabbitmq-queues: validate cluster membership of the argument

Specifically of the node where new replicas should be
placed.

Closes #8007
This commit is contained in:
Michael Klishin 2023-05-02 10:55:31 +04:00
parent 1e8399be44
commit d4a2d48cea
No known key found for this signature in database
GPG Key ID: FF4F6501646A9C9A
5 changed files with 35 additions and 3 deletions

View File

@ -77,6 +77,11 @@ defmodule RabbitMQ.CLI.Core.Helpers do
end
end
def cluster_member?(target_node, node_to_check, timeout \\ 30_000) do
node_to_check_a = DataCoercion.to_atom(node_to_check)
Enum.member?(nodes_in_cluster(target_node, timeout), node_to_check_a)
end
def node_running?(node) do
:net_adm.ping(node) == :pong
end

View File

@ -47,6 +47,13 @@ defmodule RabbitMQ.CLI.Core.Validators do
end
end
def existing_cluster_member([potential_member | _], %{node: node_name}) do
case Helpers.cluster_member?(node_name, potential_member) do
false -> {:validation_failure, {:not_a_cluster_member, potential_member}}
true -> :ok
end
end
def data_dir_is_set(_, opts) do
case require_data_dir(opts) do
:ok -> :ok

View File

@ -25,7 +25,16 @@ defmodule RabbitMQ.CLI.Queues.Commands.AddMemberCommand do
use RabbitMQ.CLI.Core.AcceptsDefaultSwitchesAndTimeout
use RabbitMQ.CLI.Core.AcceptsTwoPositionalArguments
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
def validate_execution_environment(args, opts) do
Validators.chain(
[
&Validators.rabbit_is_running/2,
&Validators.existing_cluster_member/2
],
[args, opts]
)
end
def run([name, node] = _args, %{vhost: vhost, node: node_name, timeout: timeout}) do
case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :add_member, [

View File

@ -5,7 +5,7 @@
## Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
alias RabbitMQ.CLI.Core.DocGuide
alias RabbitMQ.CLI.Core.{DocGuide, Validators}
import RabbitMQ.CLI.Core.DataCoercion
@behaviour RabbitMQ.CLI.CommandBehaviour
@ -44,7 +44,15 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
end
end
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
def validate_execution_environment(args, opts) do
Validators.chain(
[
&Validators.rabbit_is_running/2,
&Validators.existing_cluster_member/2
],
[args, opts]
)
end
def run([node, strategy], %{
node: node_name,

View File

@ -393,6 +393,9 @@ defmodule RabbitMQCtl do
defp format_validation_error(:unsupported_formatter),
do: "the requested formatter is not supported by this command"
defp format_validation_error({:not_a_cluster_member, potential_member}),
do: "node #{potential_member} is not a member of the cluster"
defp format_validation_error(err), do: inspect(err)
@spec exit_program(integer()) :: no_return()