Skip to content

Commit a9b6233

Browse files
committed
[CLIENT] Adds meta header
1 parent f7c614f commit a9b6233

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ class Client
120120
#
121121
# @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
122122
# joined by a colon as a String, or a hash with the `id` and `api_key` values.
123-
# @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client. This
124-
# will be prepended to the id you set before each request if you're using X-Opaque-Id
123+
# @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client.
124+
# This will be prepended to the id you set before each request
125+
# if you're using X-Opaque-Id
126+
# @option enable_meta_header [Boolean] :enable_meta_header Enable sending the meta data header to Cloud.
127+
# (Default: true)
125128
#
126129
# @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
127130
#
@@ -136,9 +139,11 @@ def initialize(arguments={}, &block)
136139
@arguments[:randomize_hosts] ||= false
137140
@arguments[:transport_options] ||= {}
138141
@arguments[:http] ||= {}
142+
@arguments[:enable_meta_header] ||= true
139143
@options[:http] ||= {}
140144

141145
set_api_key if (@api_key = @arguments[:api_key])
146+
set_meta_header unless @arguments[:enable_meta_header] == false
142147

143148
@seeds = extract_cloud_creds(@arguments)
144149
@seeds ||= __extract_hosts(@arguments[:hosts] ||
@@ -186,13 +191,40 @@ def perform_request(method, path, params = {}, body = nil, headers = nil)
186191

187192
def set_api_key
188193
@api_key = __encode(@api_key) if @api_key.is_a? Hash
194+
add_header('Authorization' => "ApiKey #{@api_key}")
195+
@arguments.delete(:user)
196+
@arguments.delete(:password)
197+
end
198+
199+
def add_header(header)
189200
headers = @arguments[:transport_options]&.[](:headers) || {}
190-
headers.merge!('Authorization' => "ApiKey #{@api_key}")
201+
headers.merge!(header)
191202
@arguments[:transport_options].merge!(
192203
headers: headers
193204
)
194-
@arguments.delete(:user)
195-
@arguments.delete(:password)
205+
end
206+
207+
def set_meta_header
208+
meta_headers = {
209+
es: Elasticsearch::VERSION,
210+
rb: RUBY_VERSION,
211+
t: Elasticsearch::Transport::VERSION
212+
}
213+
meta_headers.merge!(meta_header_engine) if meta_header_engine
214+
add_header({ 'x-elastic-client-meta' => meta_headers.map { |k, v| "#{k}=#{v}" }.join(',') })
215+
end
216+
217+
def meta_header_engine
218+
case RUBY_ENGINE
219+
when 'ruby'
220+
{}
221+
when 'jruby'
222+
{ jr: JRUBY_VERSION }
223+
when 'rbx'
224+
{ rbx: RUBY_VERSION }
225+
else
226+
{ RUBY_ENGINE.to_sym => RUBY_VERSION }
227+
end
196228
end
197229

198230
def extract_cloud_creds(arguments)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
20+
describe Elasticsearch::Transport::Client do
21+
context 'meta-header' do
22+
let(:client) do
23+
described_class.new.tap do |klient|
24+
allow(klient).to receive(:__build_connections)
25+
end
26+
end
27+
let(:subject) { client.transport.connections.first.connection.headers }
28+
let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9.\-]+)*$/ }
29+
let(:meta_header) do
30+
if RUBY_ENGINE == 'jruby'
31+
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jr=#{JRUBY_VERSION}"
32+
else
33+
"es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
34+
end
35+
end
36+
37+
context 'single use of meta header' do
38+
it 'x-elastic-client-header value matches regexp' do
39+
expect(subject['x-elastic-client-meta']).to match(regexp)
40+
expect(subject).to include('x-elastic-client-meta' => meta_header)
41+
end
42+
end
43+
44+
context 'when using user-agent headers' do
45+
let(:client) do
46+
transport_options = { headers: { user_agent: 'My Ruby App' } }
47+
described_class.new(transport_options: transport_options).tap do |klient|
48+
allow(klient).to receive(:__build_connections)
49+
end
50+
end
51+
52+
it 'is friendly to previously set headers' do
53+
expect(subject).to include(user_agent: 'My Ruby App')
54+
expect(subject).to include('x-elastic-client-meta' => meta_header)
55+
end
56+
end
57+
58+
context 'when using API Key' do
59+
let(:client) do
60+
described_class.new(api_key: 'an_api_key')
61+
end
62+
63+
let(:authorization_header) do
64+
client.transport.connections.first.connection.headers['Authorization']
65+
end
66+
67+
it 'Adds the ApiKey header to the connection' do
68+
expect(authorization_header).to eq('ApiKey an_api_key')
69+
expect(subject['x-elastic-client-meta']).to match(regexp)
70+
end
71+
end
72+
end
73+
end

0 commit comments

Comments
 (0)