Skip to content

Commit 609b941

Browse files
committed
Fix Api Key issue where headers would be overwritten
1 parent 00e879e commit 609b941

File tree

1 file changed

+12
-8
lines changed
  • elasticsearch-transport/lib/elasticsearch/transport

1 file changed

+12
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ def initialize(arguments={}, &block)
103103
@arguments[:http] ||= {}
104104
@options[:http] ||= {}
105105

106-
if (@api_key = @arguments[:api_key])
107-
@api_key = __encode(@api_key) if @api_key.is_a? Hash
108-
@arguments[:transport_options].merge!(
109-
headers: { 'Authorization' => "ApiKey #{@api_key}" }
110-
)
111-
@arguments.delete(:user)
112-
@arguments.delete(:password)
113-
end
106+
set_api_key if (@api_key = @arguments[:api_key])
114107

115108

116109
@seeds ||= __extract_hosts(@arguments[:hosts] ||
@@ -158,6 +151,17 @@ def perform_request(method, path, params={}, body=nil, headers=nil)
158151

159152
private
160153

154+
def set_api_key
155+
@api_key = __encode(@api_key) if @api_key.is_a? Hash
156+
headers = @arguments[:transport_options]&.[](:headers) || {}
157+
headers.merge!('Authorization' => "ApiKey #{@api_key}")
158+
@arguments[:transport_options].merge!(
159+
headers: headers
160+
)
161+
@arguments.delete(:user)
162+
@arguments.delete(:password)
163+
end
164+
161165
# Normalizes and returns hosts configuration.
162166
#
163167
# Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.

0 commit comments

Comments
 (0)