LOGSTASH-503: Gracefully reject relative file input paths

Will raise an ArgumentError if the user tries to use a relative path for a file
nput path.  Also added tests for this functionality.
This commit is contained in:
Marc Huffnagle 2012-06-24 15:12:50 -04:00
parent f97c97a35a
commit 05c7dfd962
2 changed files with 44 additions and 0 deletions

View file

@ -1,6 +1,7 @@
require "logstash/inputs/base"
require "logstash/namespace"
require "pathname"
require "socket" # for Socket.gethostname
require "addressable/uri"
@ -18,6 +19,7 @@ class LogStash::Inputs::File < LogStash::Inputs::Base
# The path to the file to use as an input.
# You can use globs here, such as "/var/log/*.log"
# Paths must be absolute and cannot be relative.
config :path, :validate => :array, :required => true
# Exclusions (matched against the filename, not full path). Globs
@ -47,6 +49,17 @@ class LogStash::Inputs::File < LogStash::Inputs::Base
# monitored log files.
config :sincedb_write_interval, :validate => :number, :default => 15
public
def initialize(params)
super
@path.each do |path|
if Pathname.new(path).relative?
raise ArgumentError.new("File paths must be absolute, relative path specified: #{path}")
end
end
end
public
def register
require "filewatch/tail"

View file

@ -86,5 +86,36 @@ describe LogStash::Inputs::File do
logfile.unlink
end
end
test "file input should raise an ArgumentError when a relative path is specified" do
assert_raises(ArgumentError) {
LogStash::Inputs::File.new("type" => ["testing"], "path" => ["./relative/path"])
}
assert_raises(ArgumentError) {
LogStash::Inputs::File.new("type" => ["testing"], "path" => ["../relative/path"])
}
assert_raises(ArgumentError) {
LogStash::Inputs::File.new("type" => ["testing"], "path" => ["relative/path"])
}
assert_raises(ArgumentError) {
LogStash::Inputs::File.new("type" => ["testing"], "path" => ["~/relative/path"])
}
end
test "file input should raise an ArgumentError with a useful message when a relative path is specified" do
relative_path = "./relative/path"
the_exception = nil
begin
LogStash::Inputs::File.new("type" => ["testing"], "path" => [relative_path])
rescue ArgumentError => e
the_exception = e
end
refute_nil(the_exception)
assert_equal("File paths must be absolute, relative path specified: #{relative_path}", e.message)
end
end # testing for LogStash::Inputs::File