Skip to content

Commit c9aeaac

Browse files
committed
API-Key: Drop Basic Auth when using API Key
1 parent f9fbc9d commit c9aeaac

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ def initialize(arguments={}, &block)
100100
@arguments[:http] ||= {}
101101
@options[:http] ||= {}
102102

103-
@seeds = __extract_hosts(@arguments[:hosts] ||
103+
if (@api_key = @arguments[:api_key])
104+
@api_key = __encode(@api_key) if @api_key.is_a? Hash
105+
@arguments[:transport_options].merge!(
106+
headers: { 'Authorization' => "ApiKey #{@api_key}" }
107+
)
108+
@arguments.delete(:user)
109+
@arguments.delete(:password)
110+
end
111+
112+
113+
@seeds ||= __extract_hosts(@arguments[:hosts] ||
104114
@arguments[:host] ||
105115
@arguments[:url] ||
106116
@arguments[:urls] ||
@@ -109,13 +119,6 @@ def initialize(arguments={}, &block)
109119

110120
@send_get_body_as = @arguments[:send_get_body_as] || 'GET'
111121

112-
if (api_key = @arguments[:api_key])
113-
api_key = __encode(api_key) if api_key.is_a? Hash
114-
@arguments[:transport_options].merge!(
115-
headers: { 'Authorization' => "ApiKey #{api_key}" }
116-
)
117-
end
118-
119122
if @arguments[:request_timeout]
120123
@arguments[:transport_options][:request] = { :timeout => @arguments[:request_timeout] }
121124
end
@@ -209,8 +212,14 @@ def __parse_host(host)
209212
raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
210213
end
211214

212-
@options[:http][:user] ||= host_parts[:user]
213-
@options[:http][:password] ||= host_parts[:password]
215+
if @api_key
216+
# Remove Basic Auth if using API KEY
217+
host_parts.delete(:user)
218+
host_parts.delete(:password)
219+
else
220+
@options[:http][:user] ||= host_parts[:user]
221+
@options[:http][:password] ||= host_parts[:password]
222+
end
214223

215224
host_parts[:port] = host_parts[:port].to_i if host_parts[:port]
216225
host_parts[:path].chomp!('/') if host_parts[:path]

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,47 @@
4949
let(:client) do
5050
described_class.new(api_key: 'an_api_key')
5151
end
52+
let(:authorization_header) do
53+
client.transport.connections.first.connection.headers['Authorization']
54+
end
5255

5356
it 'Adds the ApiKey header to the connection' do
54-
expect(client.transport.connections.first.connection.headers['Authorization']).to eq('ApiKey an_api_key')
57+
expect(authorization_header).to eq('ApiKey an_api_key')
5558
end
5659
end
5760

5861
context 'when an un-encoded api_key is provided' do
5962
let(:client) do
60-
described_class.new(api_key: {id: 'my_id', api_key: 'my_api_key'})
63+
described_class.new(api_key: { id: 'my_id', api_key: 'my_api_key' })
64+
end
65+
let(:authorization_header) do
66+
client.transport.connections.first.connection.headers['Authorization']
6167
end
6268

6369
it 'Adds the ApiKey header to the connection' do
64-
expect(
65-
client.transport.connections.first.connection.headers['Authorization']
66-
).to eq("ApiKey #{Base64.strict_encode64('my_id:my_api_key')}")
70+
expect(authorization_header).to eq("ApiKey #{Base64.strict_encode64('my_id:my_api_key')}")
6771
end
6872
end
6973

70-
describe 'adapter' do
74+
context 'when basic auth and api_key are provided' do
75+
let(:client) do
76+
described_class.new(
77+
api_key: { id: 'my_id', api_key: 'my_api_key' },
78+
host: 'http://elastic:password@localhost:9200'
79+
)
80+
end
81+
let(:authorization_header) do
82+
client.transport.connections.first.connection.headers['Authorization']
83+
end
7184

72-
context 'when no adapter is specified' do
85+
it 'removes basic auth credentials' do
86+
expect(authorization_header).not_to match(/^Basic/)
87+
expect(authorization_header).to match(/^ApiKey/)
88+
end
89+
end
7390

91+
describe 'adapter' do
92+
context 'when no adapter is specified' do
7493
let(:adapter) do
7594
client.transport.connections.all.first.connection.builder.handlers
7695
end

0 commit comments

Comments
 (0)