-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3155 from fluent/handle-linux-capability
Handle linux capability if available
- Loading branch information
Showing
6 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Fluent | ||
# | ||
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require "fluent/env" | ||
|
||
if Fluent.linux? | ||
begin | ||
require 'capng' | ||
rescue LoadError | ||
end | ||
end | ||
|
||
module Fluent | ||
if defined?(CapNG) | ||
class Capability | ||
def initialize(target = nil, pid = nil) | ||
@capng = CapNG.new(target, pid) | ||
end | ||
|
||
def usable? | ||
true | ||
end | ||
|
||
def apply(select_set) | ||
@capng.apply(select_set) | ||
end | ||
|
||
def clear(select_set) | ||
@capng.clear(select_set) | ||
end | ||
|
||
def have_capability?(type, capability) | ||
@capng.have_capability?(type, capability) | ||
end | ||
|
||
def update(action, type, capability_or_capability_array) | ||
@capng.update(action, type, capability_or_capability_array) | ||
end | ||
|
||
def have_capabilities?(select_set) | ||
@capng.have_capabilities?(select_set) | ||
end | ||
end | ||
else | ||
class Capability | ||
def initialize(target = nil, pid = nil) | ||
end | ||
|
||
def usable? | ||
false | ||
end | ||
|
||
def apply(select_set) | ||
false | ||
end | ||
|
||
def clear(select_set) | ||
false | ||
end | ||
|
||
def have_capability?(type, capability) | ||
false | ||
end | ||
|
||
def update(action, type, capability_or_capability_array) | ||
false | ||
end | ||
|
||
def have_capabilities?(select_set) | ||
false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,8 @@ module Fluent | |
def self.windows? | ||
ServerEngine.windows? | ||
end | ||
|
||
def self.linux? | ||
/linux/ === RUBY_PLATFORM | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require_relative 'helper' | ||
require 'fluent/test' | ||
require 'fluent/capability' | ||
|
||
class FluentCapabilityTest < ::Test::Unit::TestCase | ||
setup do | ||
@capability = Fluent::Capability.new(:current_process) | ||
omit "Fluent::Capability class is not usable on this environment" unless @capability.usable? | ||
end | ||
|
||
sub_test_case "check capability" do | ||
test "effective" do | ||
@capability.clear(:both) | ||
assert_true @capability.update(:add, :effective, :dac_read_search) | ||
assert_equal CapNG::Result::PARTIAL, @capability.have_capabilities?(:caps) | ||
assert_nothing_raised do | ||
@capability.apply(:caps) | ||
end | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:bounds) | ||
assert_true @capability.have_capability?(:effective, :dac_read_search) | ||
assert_false @capability.have_capability?(:inheritable, :dac_read_search) | ||
assert_false @capability.have_capability?(:permitted, :dac_read_search) | ||
end | ||
|
||
test "inheritable" do | ||
@capability.clear(:both) | ||
capabilities = [:chown, :dac_override] | ||
assert_equal [true, true], @capability.update(:add, :inheritable, capabilities) | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:caps) | ||
assert_nothing_raised do | ||
@capability.apply(:caps) | ||
end | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:bounds) | ||
capabilities.each do |capability| | ||
assert_false @capability.have_capability?(:effective, capability) | ||
assert_true @capability.have_capability?(:inheritable, capability) | ||
assert_false @capability.have_capability?(:permitted, capability) | ||
end | ||
end | ||
|
||
test "permitted" do | ||
@capability.clear(:both) | ||
capabilities = [:fowner, :fsetid, :kill] | ||
assert_equal [true, true, true], @capability.update(:add, :permitted, capabilities) | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:caps) | ||
assert_nothing_raised do | ||
@capability.apply(:caps) | ||
end | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:bounds) | ||
capabilities.each do |capability| | ||
assert_false @capability.have_capability?(:effective, capability) | ||
assert_false @capability.have_capability?(:inheritable, capability) | ||
assert_true @capability.have_capability?(:permitted, capability) | ||
end | ||
end | ||
|
||
test "effective/inheritable/permitted" do | ||
@capability.clear(:both) | ||
capabilities = [:setpcap, :net_admin, :net_raw, :sys_boot, :sys_time] | ||
update_type = CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED | ||
assert_equal [true, true, true, true, true], @capability.update(:add, update_type, capabilities) | ||
assert_equal CapNG::Result::PARTIAL, @capability.have_capabilities?(:caps) | ||
assert_nothing_raised do | ||
@capability.apply(:caps) | ||
end | ||
assert_equal CapNG::Result::NONE, @capability.have_capabilities?(:bounds) | ||
capabilities.each do |capability| | ||
assert_true @capability.have_capability?(:effective, capability) | ||
assert_true @capability.have_capability?(:inheritable, capability) | ||
assert_true @capability.have_capability?(:permitted, capability) | ||
end | ||
end | ||
end | ||
end |