Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #147 from rabbitmq/rabbitmq-cli-141
Browse files Browse the repository at this point in the history
Introduce `rabbitmq-diagnostics cipher_suites [--openssl-format]`
  • Loading branch information
acogoluegnes authored Dec 7, 2016
2 parents c27226a + 6d18e76 commit 24a09ab
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 10 deletions.
44 changes: 44 additions & 0 deletions lib/rabbitmq/cli/diagnostics/commands/cipher_suites_command.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.


defmodule RabbitMQ.CLI.Diagnostics.Commands.CipherSuitesCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour
use RabbitMQ.CLI.DefaultOutput

def merge_defaults(args, opts), do: {args, Map.merge(%{openssl_format: false}, opts)}

def switches(), do: [openssl_format: :boolean]

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

def usage, do: "cipher_suites [--openssl-format]"

def run([], %{node: node_name, timeout: timeout, openssl_format: openssl_format}) do
args = case openssl_format do
true -> [:openssl]
false -> []
end
:rabbit_misc.rpc_call(node_name, :ssl, :cipher_suites, args, timeout)
end

def banner([], %{openssl_format: true}), do: "Listing available cipher suites in the OpenSSL format"
def banner([], %{openssl_format: false}), do: "Listing available cipher suites in the Erlang term format"

def formatter(), do: RabbitMQ.CLI.Formatters.Erlang
end
23 changes: 16 additions & 7 deletions lib/rabbitmq/cli/formatters/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ defmodule RabbitMQ.CLI.Formatters.Erlang do
end

def format_stream(stream, options) do
elements = Stream.map(stream, fn
({:error, msg}) ->
{:error, msg};
(element) ->
format_output(element, options)
end)
elements = Stream.scan(stream, :empty,
fn
({:error, msg}, _) ->
{:error, msg};
(element, previous) ->
separator = case previous do
:empty -> "";
_ -> ","
end
format_element(element, separator, options)
end)
Stream.concat([["["], elements, ["]"]])
end
end

def format_element(val, separator, options) do
separator <> format_output(val, options)
end
end
5 changes: 2 additions & 3 deletions lib/rabbitmq/cli/formatters/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ defmodule RabbitMQ.CLI.Formatters.Inspect do
@behaviour RabbitMQ.CLI.FormatterBehaviour

def format_output(output, _) do
res = case is_binary(output) do
case is_binary(output) do
true -> output;
false -> inspect(output)
end
res
end

def format_stream(stream, options) do
Expand All @@ -42,4 +41,4 @@ defmodule RabbitMQ.CLI.Formatters.Inspect do
def format_element(val, separator, options) do
separator <> format_output(val, options)
end
end
end
88 changes: 88 additions & 0 deletions test/diagnostics/cipher_suites_command_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at http://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.


defmodule CipherSuitesCommandTest do
use ExUnit.Case
import TestHelper

@command RabbitMQ.CLI.Diagnostics.Commands.CipherSuitesCommand

setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:net_kernel.connect_node(get_rabbit_hostname)

on_exit([], fn ->
:erlang.disconnect_node(get_rabbit_hostname)

end)

:ok
end

setup context do
{:ok, opts: %{
node: get_rabbit_hostname,
timeout: context[:test_timeout] || 15,
openssl_format: context[:openssl_format] || false
}}
end

test "merge_defaults: defaults to the Erlang term format (that is, non-OpenSSL)" do
assert @command.merge_defaults([], %{}) == {[], %{openssl_format: false}}
end

test "merge_defaults: OpenSSL format can be switched on" do
assert @command.merge_defaults([], %{openssl_format: true}) == {[], %{openssl_format: true}}
end

test "merge_defaults: OpenSSL format can be switched off" do
assert @command.merge_defaults([], %{openssl_format: false}) == {[], %{openssl_format: false}}
end

test "validate: treats positional arguments as a failure" do
assert @command.validate(["extra-arg"], %{openssl_format: false}) == {:validation_failure, :too_many_args}
end

test "validate: treats empty positional arguments and default switches as a success" do
assert @command.validate([], %{openssl_format: false}) == :ok
end

test "validate: treats empty positional arguments and an OpenSSL format flag as a success" do
assert @command.validate([], %{openssl_format: true}) == :ok
end

@tag test_timeout: 0
test "run: targeting an unreachable node throws a badrpc", context do
target = :jake@thedog
:net_kernel.connect_node(target)
opts = %{node: target}
assert @command.run([], Map.merge(context[:opts], opts)) == {:badrpc, :nodedown}
end

test "run: returns a list of cipher suites", context do
res = @command.run([], context[:opts])
# the list is long and its values are environment-specific,
# so we simply assert that it is non-empty. MK.
assert length(res) > 0
end

@tag openssl_format: true
test "run: returns a list cipher suites in the OpenSSL format", context do
res = @command.run([], context[:opts])
# see the test above
assert length(res) > 0
end
end

0 comments on commit 24a09ab

Please sign in to comment.