Skip to content

Commit fb83f53

Browse files
committed
[GEM] Fixes bug instantiating client with api_key and transport_options
When passing in `api_key` and `transport_options` that don't include headers to the client, the api_key code would overwrite the arguments passed in for `transport_options`. This code fixes this, checking if there are transport options and merging the auth headers with that instead. Fixes #1940
1 parent 56e015a commit fb83f53

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

elasticsearch/lib/elasticsearch.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ def api_key(arguments)
144144
if (headers = arguments.dig(:transport_options, :headers))
145145
headers.merge!(authorization)
146146
else
147-
arguments[:transport_options] = {
148-
headers: authorization
149-
}
147+
arguments[:transport_options] ||= {}
148+
arguments[:transport_options].merge!({ headers: authorization })
150149
end
151150
end
152151

elasticsearch/spec/unit/api_key_spec.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
end
5858
end
5959

60-
context 'when other headers where specified' do
60+
context 'when other headers were specified' do
6161
let(:client) do
6262
described_class.new(
6363
api_key: 'elasticsearch_api_key',
@@ -72,6 +72,42 @@
7272
end
7373
end
7474

75+
context 'when sending transport_options but no headers were specified' do
76+
let(:client) do
77+
described_class.new(
78+
api_key: 'elasticsearch_api_key',
79+
transport_options: { ssl: { verify: false } }
80+
)
81+
end
82+
83+
it 'Adds the ApiKey header to the connection and keeps the options' do
84+
header = client.transport.connections.first.connection.headers
85+
expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
86+
expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
87+
expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
88+
end
89+
end
90+
91+
context 'when other headers and options were specified' do
92+
let(:client) do
93+
described_class.new(
94+
api_key: 'elasticsearch_api_key',
95+
transport_options: {
96+
headers: { 'x-test-header' => 'test' },
97+
ssl: { verify: false }
98+
}
99+
)
100+
end
101+
102+
it 'Adds the ApiKey header to the connection and keeps the header' do
103+
header = client.transport.connections.first.connection.headers
104+
expect(header['X-Test-Header']).to eq('test')
105+
expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
106+
expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
107+
expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
108+
end
109+
end
110+
75111
context 'Metaheader' do
76112
let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
77113
let(:meta_header) do

0 commit comments

Comments
 (0)