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

Commit

Permalink
hipe_compile: reject blank, non-existent paths and those with insuffi…
Browse files Browse the repository at this point in the history
…cient permissions

References #178.
  • Loading branch information
michaelklishin committed Mar 13, 2017
1 parent dc867fb commit d0cb138
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/rabbitmq/cli/core/exit_codes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule RabbitMQ.CLI.Core.ExitCodes do
def exit_code_for({:validation_failure, {:too_many_args, _}}), do: exit_usage()
def exit_code_for({:validation_failure, {:bad_argument, _}}), do: exit_dataerr()
def exit_code_for({:validation_failure, :bad_argument}), do: exit_dataerr()
def exit_code_for({:validation_failure, :eperm}), do: exit_dataerr()
def exit_code_for({:validation_failure, {:bad_option, _}}), do: exit_usage()
def exit_code_for({:validation_failure, _}), do: exit_usage()
def exit_code_for({:badrpc, :timeout}), do: exit_tempfail()
Expand Down
2 changes: 1 addition & 1 deletion lib/rabbitmq/cli/core/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ defmodule RabbitMQ.CLI.Core.Helpers do
def require_rabbit(opts) do
try_load_rabbit_code(opts)
end

def require_rabbit_and_plugins(opts) do
with :ok <- try_load_rabbit_code(opts),
:ok <- try_load_rabbit_plugins(opts),
Expand Down
26 changes: 24 additions & 2 deletions lib/rabbitmq/cli/ctl/commands/hipe_compile_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,32 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand do
def usage, do: "hipe_compile <directory>"

def validate([], _), do: {:validation_failure, :not_enough_args}
def validate([_], opts) do
def validate([target_dir], opts) do
:ok
|> Helpers.validate_step(fn() ->
case acceptable_path?(target_dir) do
true -> :ok
false -> {:error, {:bad_argument, "Target directory path cannot be blank"}}
end
end)
|> Helpers.validate_step(fn() ->
case File.dir?(target_dir) do
true -> :ok
false ->
case File.mkdir_p(target_dir) do
:ok -> :ok
{:error, :eperm} ->
{:error, {:bad_argument, "Cannot create target directory #{target_dir}: insufficient permissions"}}
end
end
end)
|> Helpers.validate_step(fn() -> Helpers.require_rabbit(opts) end)
end
def validate(_, _), do: {:validation_failure, :too_many_args}

def run([target_dir], _opts) do
Code.ensure_loaded(:rabbit_hipe)
hipe_compile(target_dir)
hipe_compile(String.trim(target_dir))
end

def banner([target_dir], _) do
Expand All @@ -51,6 +68,11 @@ defmodule RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand do
# Implementation
#

# Accepts any non-blank path
defp acceptable_path?(value) do
String.length(String.trim(value)) != 0
end

defp hipe_compile(target_dir) do
case :rabbit_hipe.can_hipe_compile() do
true ->
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ defmodule RabbitMQCtl.MixfileBase do
override: true
},
{:amqp, "~> 0.1.5", only: :test},
{:temp, "~> 0.4", only: :test},
{:json, "~> 1.0.0"},
{:csv, "~> 1.4.2"},
{:simetric, "~> 0.1.0"}
Expand Down
24 changes: 19 additions & 5 deletions test/hipe_compile_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule HipeCompileCommandTest do
use ExUnit.Case, async: false
import TestHelper

require Temp

@command RabbitMQ.CLI.Ctl.Commands.HipeCompileCommand
@vhost "/"

Expand All @@ -32,13 +34,17 @@ defmodule HipeCompileCommandTest do
end

setup do
rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
rabbitmq_home = :rabbit_misc.rpc_call(node(), :code, :lib_dir, [:rabbit])

{:ok, tmp_dir} = Temp.path

{:ok, opts: %{
node: get_rabbit_hostname(),
vhost: @vhost,
rabbitmq_home: rabbitmq_home
}}
},
target_dir: tmp_dir
}
end

test "validate: providing no arguments fails validation", context do
Expand All @@ -56,12 +62,20 @@ defmodule HipeCompileCommandTest do
{:validation_failure, :too_many_args}
end

test "validate: providing one directory path and required options succeeds", context do
assert @command.validate(["/path/one"], context[:opts]) == :ok
test "validate: providing one blank directory path and required options fails", context do
assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([""], context[:opts]))
end

test "validate: providing one path argument that only contains spaces and required options fails", context do
assert match?({:validation_failure, {:bad_argument, _}}, @command.validate([" "], context[:opts]))
end

test "validate: providing one non-blank directory path and required options succeeds", context do
assert @command.validate([context[:target_dir]], context[:opts]) == :ok
end

test "validate: failure to load the rabbit application is reported as an error", context do
assert {:validation_failure, {:unable_to_load_rabbit, _}} =
@command.validate(["/path/to/beam/files"], Map.delete(context[:opts], :rabbitmq_home))
@command.validate([context[:target_dir]], Map.delete(context[:opts], :rabbitmq_home))
end
end

0 comments on commit d0cb138

Please sign in to comment.