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

support empty password #615

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/logstash/outputs/elasticsearch/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def close
@pool.close
end

def calculate_property(uris, property, default, sniff_check)
def calculate_property(uris, property, default, sniff_check, allow_empty_string = false)
values = uris.map(&property).uniq

if sniff_check && values.size > 1
Expand All @@ -180,8 +180,8 @@ def calculate_property(uris, property, default, sniff_check)

uri_value = values.first

default = nil if default.is_a?(String) && default.empty? # Blanks are as good as nil
uri_value = nil if uri_value.is_a?(String) && uri_value.empty?
default = nil if !allow_empty_string && default.is_a?(String) && default.empty? # Blanks are as good as nil
uri_value = nil if !allow_empty_string && uri_value.is_a?(String) && uri_value.empty?

if default && uri_value && (default != uri_value)
raise LogStash::ConfigurationError, "Explicit value for '#{property}' was declared, but it is different in one of the URLs given! Please make sure your URLs are inline with explicit values. The URLs have the property set to '#{uri_value}', but it was also set to '#{default}' explicitly"
Expand All @@ -199,7 +199,7 @@ def user
end

def password
calculate_property(uris, :password, @options[:password], sniffing)
calculate_property(uris, :password, @options[:password], sniffing, true)
end

def path
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/outputs/elasticsearch/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe LogStash::Outputs::ElasticSearch::HttpClient do
let(:ssl) { nil }
let(:hosts) { [::LogStash::Util::SafeURI.new("127.0.0.1")] }
let(:base_options) do
opts = {
:hosts => [::LogStash::Util::SafeURI.new("127.0.0.1")],
Expand Down Expand Up @@ -124,6 +125,25 @@
end
end

describe "#password" do
subject { described_class.new(base_options) }
context "when using empty password" do
context "with password option" do
let(:base_options) { super.merge(:password => "", :user => "hello") }
it "returns empty string" do
expect(subject.password).to eq("")
end
end
context "with password in hosts option" do
let(:uri) { LogStash::Util::SafeURI.new("http://hello:@localhost:9200") }
let(:base_options) { super.merge(:hosts => [uri]) }
it "returns empty string" do
expect(subject.password).to eq("")
end
end
end
end

describe "get" do
subject { described_class.new(base_options) }
let(:body) { "foobar" }
Expand Down