Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for host/hosts placeholders for ipv6 addresses #1030

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/fluent/plugin/out_elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ def get_escaped_userinfo(host_str)
end
end

def is_ipv6_host(host_str)
begin
IPAddr.new(host_str).ipv6?
rescue IPAddr::InvalidAddressError
return false
end
end

def get_connection_options(con_host=nil)

hosts = if con_host || @hosts
Expand All @@ -664,6 +672,21 @@ def get_connection_options(con_host=nil)
port: (host_str.split(':')[1] || @port).to_i,
scheme: @scheme.to_s
}
# Support ipv6 for host/host placeholders
elsif is_ipv6_host(host_str)
if Resolv::IPv6::Regex.match(host_str)
{
host: "[#{host_str}]",
port: @port.to_i,
scheme: @scheme.to_s
}
else
{
host: host_str,
port: @port.to_i,
scheme: @scheme.to_s
}
end
else
# New hosts format expects URLs such as http://logs.foo.com,https://john:[email protected]/elastic
uri = URI(get_escaped_userinfo(host_str))
Expand Down
98 changes: 98 additions & 0 deletions test/plugin/test_out_elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,33 @@ def test_hosts_list
assert_equal '/default_path', host2[:path]
end

def test_hosts_list_with_existing_connection
stub_elastic_info("https://john:password@host1:443/elastic/")
stub_elastic_info("http://host2")
config = %{
path /default_path
user default_user
password default_password
}
instance = driver(config).instance

assert_equal 2, instance.get_connection_options("https://john:password@host1:443/elastic/,http://host2")[:hosts].length
host1, host2 = instance.get_connection_options("https://john:password@host1:443/elastic/,http://host2")[:hosts]

assert_equal 'host1', host1[:host]
assert_equal 443, host1[:port]
assert_equal 'https', host1[:scheme]
assert_equal 'john', host1[:user]
assert_equal 'password', host1[:password]
assert_equal '/elastic/', host1[:path]

assert_equal 'host2', host2[:host]
assert_equal 'http', host2[:scheme]
assert_equal 'default_user', host2[:user]
assert_equal 'default_password', host2[:password]
assert_equal '/default_path', host2[:path]
end

def test_hosts_list_with_escape_placeholders
config = %{
hosts https://%{j+hn}:%{passw@rd}@host1:443/elastic/,http://host2
Expand Down Expand Up @@ -3670,6 +3697,31 @@ def test_hosts_list
assert_equal '/default_path', host2[:path]
end

def test_hosts_list_with_existing_connection
config = %{
path /default_path
user default_user
password default_password
}
instance = driver(config).instance

assert_equal 2, instance.get_connection_options("https://john:password@[2404:7a80:d440:3000:192a:a292:bd7f:ca19]:443/elastic/,http://host2")[:hosts].length
host1, host2 = instance.get_connection_options("https://john:password@[2404:7a80:d440:3000:192a:a292:bd7f:ca19]:443/elastic/,http://host2")[:hosts]

assert_equal '[2404:7a80:d440:3000:192a:a292:bd7f:ca19]', host1[:host]
assert_equal 443, host1[:port]
assert_equal 'https', host1[:scheme]
assert_equal 'john', host1[:user]
assert_equal 'password', host1[:password]
assert_equal '/elastic/', host1[:path]

assert_equal 'host2', host2[:host]
assert_equal 'http', host2[:scheme]
assert_equal 'default_user', host2[:user]
assert_equal 'default_password', host2[:password]
assert_equal '/default_path', host2[:path]
end

def test_hosts_list_with_escape_placeholders
config = %{
hosts https://%{j+hn}:%{passw@rd}@[2404:7a80:d440:3000:192a:a292:bd7f:ca19]:443/elastic/,http://host2
Expand Down Expand Up @@ -3716,6 +3768,24 @@ def test_single_host_params_and_defaults
assert_equal nil, host1[:path]
end

def test_single_existing_connection
config = %{
user john
password doe
}
instance = driver(config).instance

assert_equal 1, instance.get_connection_options("logs.google.com")[:hosts].length
host1 = instance.get_connection_options("logs.google.com")[:hosts][0]

assert_equal 'logs.google.com', host1[:host]
assert_equal 9200, host1[:port]
assert_equal 'http', host1[:scheme]
assert_equal 'john', host1[:user]
assert_equal 'doe', host1[:password]
assert_equal nil, host1[:path]
end

def test_single_host_params_and_defaults_with_escape_placeholders
config = %{
host logs.google.com
Expand Down Expand Up @@ -3770,6 +3840,34 @@ def test_single_host_params_and_defaults
assert_equal nil, host1[:path]
end

def test_single_existing_connection
config = %{
user john
password doe
}
instance = driver(config).instance

assert_equal 1, instance.get_connection_options("2404:7a80:d440:3000:192a:a292:bd7f:ca19")[:hosts].length
host1 = instance.get_connection_options("2404:7a80:d440:3000:192a:a292:bd7f:ca19")[:hosts][0]

assert_equal 1, instance.get_connection_options("[2404:7a80:d440:3000:192a:a292:bd7f:ca19]")[:hosts].length
host2 = instance.get_connection_options("[2404:7a80:d440:3000:192a:a292:bd7f:ca19]")[:hosts][0]

assert_equal '[2404:7a80:d440:3000:192a:a292:bd7f:ca19]', host1[:host]
assert_equal 9200, host1[:port]
assert_equal 'http', host1[:scheme]
assert_equal 'john', host1[:user]
assert_equal 'doe', host1[:password]
assert_equal nil, host1[:path]

assert_equal '[2404:7a80:d440:3000:192a:a292:bd7f:ca19]', host2[:host]
assert_equal 9200, host2[:port]
assert_equal 'http', host2[:scheme]
assert_equal 'john', host2[:user]
assert_equal 'doe', host2[:password]
assert_equal nil, host2[:path]
end

def test_single_host_params_and_defaults_with_escape_placeholders
config = %{
host 2404:7a80:d440:3000:192a:a292:bd7f:ca19
Expand Down