Skip to content

Commit

Permalink
command: Show more human readable error message
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <hatake@clear-code.com>
  • Loading branch information
cosmo0920 committed Nov 16, 2020
1 parent be43311 commit 0bfd905
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/fluent/command/cap_ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def call
end
end

def get_valid_capabilities
capabilities = []
cap = CapNG::Capability.new
cap.each do |_code, capability|
capabilities << capability
end
capabilities
end

def clear_capabilities(opts, target_file)
if !!opts[:clear_capabilities]
@capng.clear(:caps)
Expand All @@ -101,11 +110,20 @@ def clear_capabilities(opts, target_file)
end
end

def check_capabilities(capabilities, valid_capabilities)
capabilities.each do |capability|
unless valid_capabilities.include?(capability)
raise ArgumentError, "'#{capability}' is not valid capability. Valid Capabilities are: #{valid_capabilities.join(", ")}"
end
end
end

def add_capabilities(opts, target_file)
if add_caps = opts[:add_capabilities]
@capng.clear(:caps)
@capng.caps_file(target_file)
capabilities = add_caps.split(/\s*,\s*/)
check_capabilities(capabilities, get_valid_capabilities)
ret = @capng.update(:add,
CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED,
capabilities)
Expand All @@ -120,6 +138,7 @@ def drop_capabilities(opts, target_file)
@capng.clear(:caps)
@capng.caps_file(target_file)
capabilities = drop_caps.split(/\s*,\s*/)
check_capabilities(capabilities, get_valid_capabilities)
ret = @capng.update(:drop,
CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED,
capabilities)
Expand Down
4 changes: 2 additions & 2 deletions test/command/test_cap_ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ class TestFluentCapCtl < Test::Unit::TestCase

sub_test_case "invalid" do
test "add capability" do
assert_raise(RuntimeError) do
assert_raise(ArgumentError) do
Fluent::CapCtl.new(["--add-cap", "nonexitent"]).call
end
end

test "drop capability" do
assert_raise(RuntimeError) do
assert_raise(ArgumentError) do
Fluent::CapCtl.new(["--drop-cap", "invalid"]).call
end
end
Expand Down

0 comments on commit 0bfd905

Please sign in to comment.