Skip to content

Commit 009d270

Browse files
committed
Add warning for directory permission to in_tail.rb
Signed-off-by: Tomoaki Kobayashi <tomoaki.kobayashi.t3@alumni.tohoku.ac.jp>
1 parent 8df1086 commit 009d270

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

lib/fluent/plugin/in_tail.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def start
253253
FileUtils.mkdir_p(pos_file_dir, mode: @dir_perm) unless Dir.exist?(pos_file_dir)
254254
@pf_file = File.open(@pos_file, File::RDWR|File::CREAT|File::BINARY, @file_perm)
255255
@pf_file.sync = true
256-
@pf = PositionFile.load(@pf_file, @follow_inodes, expand_paths, logger: log)
256+
@pf = PositionFile.load(@pf_file, @follow_inodes, expand_paths(true), logger: log)
257257

258258
if @pos_file_compaction_interval
259259
timer_execute(:in_tail_refresh_compact_pos_file, @pos_file_compaction_interval) do
@@ -321,7 +321,7 @@ def use_glob?(path)
321321
end
322322
end
323323

324-
def expand_paths
324+
def expand_paths(check_permission = false)
325325
date = Fluent::EventTime.now
326326
paths = []
327327
@paths.each { |path|
@@ -370,6 +370,19 @@ def expand_paths
370370
# filter out non existing files, so in case pattern is without '*' we don't do unnecessary work
371371
hash = {}
372372
(paths - excluded).select { |path|
373+
if check_permission && !FileTest.readable?(path)
374+
is_bad_permission = begin
375+
current_path = File.expand_path(path)
376+
loop do
377+
current_path = File.dirname(current_path)
378+
break true unless FileTest.executable?(current_path)
379+
break false if current_path == "/"
380+
end
381+
end
382+
if is_bad_permission
383+
$log.warn "Skip #{path} because a directory in the path lacks execute permission."
384+
end
385+
end
373386
FileTest.exist?(path)
374387
}.each { |path|
375388
# Even we just checked for existence, there is a race condition here as

test/plugin/test_in_tail.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,60 @@ def test_EACCES
25912591
d.instance_shutdown if d && d.instance
25922592
end
25932593

2594+
def test_warn_without_directory_permission
2595+
omit "Cannot test with root user" if Process::UID.eid == 0
2596+
path = "#{@tmp_dir}/noaccess/tail.txt"
2597+
begin
2598+
FileUtils.mkdir_p("#{@tmp_dir}/noaccess")
2599+
FileUtils.touch(path)
2600+
FileUtils.chmod(0400, path)
2601+
FileUtils.chmod(0600, "#{@tmp_dir}/noaccess")
2602+
config = config_element('', '', {
2603+
'tag' => "tail",
2604+
'path' => path,
2605+
'format' => 'none',
2606+
"pos_file" => "#{@tmp_dir}/tail.pos",
2607+
})
2608+
d = create_driver(config, false)
2609+
assert_nothing_raised do
2610+
d.run(shutdown: false) {}
2611+
end
2612+
assert($log.out.logs.any?{|log| log.include?("Skip #{path} because a directory in the path lacks execute permission.\n") })
2613+
end
2614+
ensure
2615+
d.instance_shutdown if d && d.instance
2616+
FileUtils.chmod(0755, "#{@tmp_dir}/noaccess")
2617+
if File.exist?("#{@tmp_dir}/noaccess")
2618+
FileUtils.rm_rf("#{@tmp_dir}/noaccess")
2619+
end
2620+
end unless Fluent.windows?
2621+
2622+
def test_no_warn_with_directory_permission
2623+
omit "Cannot test with root user" if Process::UID.eid == 0
2624+
path = "#{@tmp_dir}/noaccess/tail.txt"
2625+
begin
2626+
FileUtils.mkdir_p("#{@tmp_dir}/noaccess")
2627+
FileUtils.chmod(0100, "#{@tmp_dir}/noaccess")
2628+
config = config_element('', '', {
2629+
'tag' => "tail",
2630+
'path' => path,
2631+
'format' => 'none',
2632+
"pos_file" => "#{@tmp_dir}/tail.pos",
2633+
})
2634+
d = create_driver(config, false)
2635+
assert_nothing_raised do
2636+
d.run(shutdown: false) {}
2637+
end
2638+
assert($log.out.logs.all?{|log| !log.include?("Skip #{path} because a directory in the path lacks execute permission.\n") })
2639+
end
2640+
ensure
2641+
d.instance_shutdown if d && d.instance
2642+
FileUtils.chmod(0755, "#{@tmp_dir}/noaccess")
2643+
if File.exist?("#{@tmp_dir}/noaccess")
2644+
FileUtils.rm_rf("#{@tmp_dir}/noaccess")
2645+
end
2646+
end unless Fluent.windows?
2647+
25942648
def test_shutdown_timeout
25952649
Fluent::FileWrapper.open("#{@tmp_dir}/tail.txt", "wb") do |f|
25962650
# Should be large enough to take too long time to consume

0 commit comments

Comments
 (0)