From 6e918705c42f0008ba7633db24e7198209a93661 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Mon, 16 Nov 2020 10:43:44 +0900 Subject: [PATCH] command: Show more human readable error message Signed-off-by: Hiroshi Hatake --- lib/fluent/command/cap_ctl.rb | 19 +++++++++++++++++++ test/command/test_cap_ctl.rb | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/fluent/command/cap_ctl.rb b/lib/fluent/command/cap_ctl.rb index 6d80f9f4f8..cf5c439455 100644 --- a/lib/fluent/command/cap_ctl.rb +++ b/lib/fluent/command/cap_ctl.rb @@ -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) @@ -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) @@ -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) diff --git a/test/command/test_cap_ctl.rb b/test/command/test_cap_ctl.rb index dc11236b3d..067880b468 100644 --- a/test/command/test_cap_ctl.rb +++ b/test/command/test_cap_ctl.rb @@ -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