Skip to content

Commit

Permalink
Add x-elastic-product-origin header to kibana requests (#16765)
Browse files Browse the repository at this point in the history
This commit updates the Kibana client to add a `x-elastic-product-origin` header
with the value `logstash` for any requests originating from logstash to indicate
which internal product is using the API.
  • Loading branch information
donoghuc authored Dec 9, 2024
1 parent 3954b61 commit 625439b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion logstash-core/lib/logstash/modules/kibana_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ def initialize(settings, client = nil) # allow for test mock injection
@endpoint = "#{@scheme}://#{@host}"

@client = client || Manticore::Client.new(client_options)
@http_options = {:headers => {'Content-Type' => 'application/json'}}
@http_options = {
:headers => {
'Content-Type' => 'application/json',
"x-elastic-product-origin" => "logstash"
}
}
username = @settings["var.kibana.username"]
if username
password = @settings["var.kibana.password"]
Expand Down
28 changes: 28 additions & 0 deletions logstash-core/spec/logstash/modules/kibana_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,33 @@ def call
end
end
end

context "when making requests to Kibana" do
let(:test_client) { double("client") }
let(:mock_response) { double("response",
code: 200,
body: {"version" => {"number" => "1.2.3"}}.to_json,
headers: {}
)}
let(:mock_http) { double("http") }
let(:settings) { {"var.kibana.host" => "localhost:5601"} }

it "includes product origin header" do
# The status API is checked on initialization, the `safely` method is used
# for any REST calls, so just checkiing this :get covers the header addition.
expect(test_client).to receive(:http)
.with(:get, "https://localhost:5601/api/status",
hash_including(
headers: hash_including(
"x-elastic-product-origin" => "logstash"
)
)
)
.and_return(mock_http)
expect(mock_http).to receive(:call).and_return(mock_response)

described_class.new(settings, test_client)
end
end
end
end end

0 comments on commit 625439b

Please sign in to comment.