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 <[email protected]>
  • Loading branch information
cosmo0920 committed Nov 16, 2020
1 parent be43311 commit 42bf09d
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 @@ -106,6 +106,7 @@ def add_capabilities(opts, target_file)
@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 +121,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 All @@ -140,6 +142,23 @@ def get_capabilities(opts, target_file)
end
end

def get_valid_capabilities
capabilities = []
cap = CapNG::Capability.new
cap.each do |_code, capability|
capabilities << capability
end
capabilities
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 parse_options!(argv)
begin
rest = @op.parse(argv)
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 42bf09d

Please sign in to comment.