elasticsearch-transport 6.8.2 → 7.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +17 -0
  3. data/LICENSE.txt +199 -10
  4. data/README.md +32 -86
  5. data/Rakefile +23 -4
  6. data/elasticsearch-transport.gemspec +78 -44
  7. data/lib/elasticsearch-transport.rb +16 -3
  8. data/lib/elasticsearch/transport.rb +17 -3
  9. data/lib/elasticsearch/transport/client.rb +62 -85
  10. data/lib/elasticsearch/transport/redacted.rb +5 -9
  11. data/lib/elasticsearch/transport/transport/base.rb +63 -55
  12. data/lib/elasticsearch/transport/transport/connections/collection.rb +16 -3
  13. data/lib/elasticsearch/transport/transport/connections/connection.rb +16 -3
  14. data/lib/elasticsearch/transport/transport/connections/selector.rb +16 -3
  15. data/lib/elasticsearch/transport/transport/errors.rb +16 -3
  16. data/lib/elasticsearch/transport/transport/http/curb.rb +18 -5
  17. data/lib/elasticsearch/transport/transport/http/faraday.rb +18 -5
  18. data/lib/elasticsearch/transport/transport/http/manticore.rb +17 -4
  19. data/lib/elasticsearch/transport/transport/loggable.rb +85 -0
  20. data/lib/elasticsearch/transport/transport/response.rb +16 -3
  21. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +16 -3
  22. data/lib/elasticsearch/transport/transport/sniffer.rb +17 -5
  23. data/lib/elasticsearch/transport/version.rb +17 -4
  24. data/spec/elasticsearch/transport/base_spec.rb +8 -187
  25. data/spec/elasticsearch/transport/client_spec.rb +27 -123
  26. data/spec/elasticsearch/transport/sniffer_spec.rb +19 -0
  27. data/spec/spec_helper.rb +2 -4
  28. data/test/integration/transport_test.rb +18 -5
  29. data/test/profile/client_benchmark_test.rb +16 -3
  30. data/test/test_helper.rb +63 -14
  31. data/test/unit/connection_collection_test.rb +17 -4
  32. data/test/unit/connection_selector_test.rb +17 -4
  33. data/test/unit/connection_test.rb +17 -4
  34. data/test/unit/response_test.rb +18 -5
  35. data/test/unit/serializer_test.rb +17 -4
  36. data/test/unit/transport_base_test.rb +21 -8
  37. data/test/unit/transport_curb_test.rb +17 -4
  38. data/test/unit/transport_faraday_test.rb +17 -4
  39. data/test/unit/transport_manticore_test.rb +24 -6
  40. metadata +67 -76
@@ -1,58 +1,92 @@
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
+
1
18
  # coding: utf-8
2
19
  lib = File.expand_path('../lib', __FILE__)
3
20
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
21
  require 'elasticsearch/transport/version'
5
22
 
6
23
  Gem::Specification.new do |s|
7
- s.name = 'elasticsearch-transport'
24
+ s.name = "elasticsearch-transport"
8
25
  s.version = Elasticsearch::Transport::VERSION
9
- s.authors = ['Karel Minarik']
10
- s.email = ['karel.minarik@elasticsearch.org']
11
- s.summary = 'Ruby client for Elasticsearch.'
12
- s.homepage = 'https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html'
13
- s.license = 'Apache-2.0'
14
- s.metadata = {
15
- 'homepage_uri' => 'https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html',
16
- 'changelog_uri' => 'https://github.com/elastic/elasticsearch-ruby/blob/6.x/CHANGELOG.md',
17
- 'source_code_uri' => 'https://github.com/elastic/elasticsearch-ruby/tree/6.x/elasticsearch-transport',
18
- 'bug_tracker_uri' => 'https://github.com/elastic/elasticsearch-ruby/issues'
19
- }
26
+ s.authors = ["Karel Minarik"]
27
+ s.email = ["karel.minarik@elasticsearch.org"]
28
+ s.summary = "Ruby client for Elasticsearch."
29
+ s.homepage = "https://github.com/elasticsearch/elasticsearch-ruby/tree/master/elasticsearch-transport"
30
+ s.license = "Apache-2.0"
31
+
20
32
  s.files = `git ls-files`.split($/)
21
33
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
34
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
23
- s.require_paths = ['lib']
24
-
25
- s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
26
- s.rdoc_options = ['--charset=UTF-8']
27
-
28
- s.required_ruby_version = '>= 2.4'
29
-
30
- s.add_dependency 'multi_json'
31
- s.add_dependency 'faraday', '~> 1'
32
- s.add_development_dependency 'ansi'
33
- s.add_development_dependency 'bundler'
34
- s.add_development_dependency 'elasticsearch-extensions'
35
- s.add_development_dependency 'mocha'
36
- s.add_development_dependency 'pry'
37
- s.add_development_dependency 'rake', '~> 13'
38
- s.add_development_dependency 'shoulda-context'
39
- s.add_development_dependency 'turn'
40
- s.add_development_dependency 'yard'
35
+ s.require_paths = ["lib"]
36
+
37
+ s.extra_rdoc_files = [ "README.md", "LICENSE.txt" ]
38
+ s.rdoc_options = [ "--charset=UTF-8" ]
39
+
40
+ s.required_ruby_version = '>= 1.9'
41
+
42
+ s.add_dependency "multi_json"
43
+ s.add_dependency "faraday"
44
+
45
+ if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
46
+ s.add_dependency "system_timer"
47
+ end
48
+
49
+ s.add_development_dependency "bundler"
50
+
51
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
52
+ s.add_development_dependency "rake", "~> 11.1"
53
+ else
54
+ s.add_development_dependency "rake", "< 11.0"
55
+ end
56
+
57
+ s.add_development_dependency "ansi"
58
+ s.add_development_dependency "shoulda-context"
59
+ s.add_development_dependency "mocha"
60
+ s.add_development_dependency "yard"
61
+ s.add_development_dependency "pry"
62
+
41
63
  # Gems for testing integrations
42
- s.add_development_dependency 'cane'
43
- s.add_development_dependency 'hashie'
44
- s.add_development_dependency 'manticore', '~> 0.5.2' if defined? JRUBY_VERSION
45
- s.add_development_dependency 'minitest', '~> 4.0'
46
- s.add_development_dependency 'net-http-persistent'
47
- s.add_development_dependency 'simplecov', '~> 0.17', '< 0.18'
48
- s.add_development_dependency 'simplecov-rcov'
49
- s.add_development_dependency 'test-unit', '~> 2'
50
- s.add_development_dependency 'typhoeus', '~> 0.6'
51
- unless defined?(JRUBY_VERSION) || defined?(Rubinius)
52
- s.add_development_dependency 'curb'
53
- s.add_development_dependency 'patron'
54
- s.add_development_dependency 'require-prof'
55
- s.add_development_dependency 'ruby-prof'
64
+ s.add_development_dependency "curb" unless defined? JRUBY_VERSION
65
+ s.add_development_dependency "patron" unless defined? JRUBY_VERSION
66
+ s.add_development_dependency "typhoeus", '~> 0.6'
67
+ s.add_development_dependency "net-http-persistent"
68
+ s.add_development_dependency "manticore", '~> 0.6' if defined? JRUBY_VERSION
69
+ s.add_development_dependency "hashie"
70
+
71
+ # Prevent unit test failures on Ruby 1.8
72
+ if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
73
+ s.add_development_dependency "test-unit", '~> 2'
74
+ s.add_development_dependency "json", '~> 1.8'
75
+ end
76
+
77
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
78
+ s.add_development_dependency "minitest"
79
+ s.add_development_dependency "minitest-reporters"
80
+ s.add_development_dependency "elasticsearch-extensions"
81
+ s.add_development_dependency "ruby-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
82
+ s.add_development_dependency "require-prof" unless defined?(JRUBY_VERSION) || defined?(Rubinius)
83
+ s.add_development_dependency "simplecov"
84
+ s.add_development_dependency "simplecov-rcov"
85
+ s.add_development_dependency "cane"
86
+ end
87
+
88
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
89
+ s.add_development_dependency "test-unit", '~> 2'
56
90
  end
57
91
 
58
92
  s.description = <<-DESC.gsub(/^ /, '')
@@ -1,5 +1,18 @@
1
- # Licensed to Elasticsearch B.V under one or more agreements.
2
- # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
- # See the LICENSE file in the project root for more information
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.
4
17
 
5
18
  require 'elasticsearch/transport'
@@ -1,6 +1,19 @@
1
- # Licensed to Elasticsearch B.V under one or more agreements.
2
- # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
- # See the LICENSE file in the project root for more information
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.
4
17
 
5
18
  require "uri"
6
19
  require "time"
@@ -8,6 +21,7 @@ require "timeout"
8
21
  require "multi_json"
9
22
  require "faraday"
10
23
 
24
+ require "elasticsearch/transport/transport/loggable"
11
25
  require "elasticsearch/transport/transport/serializer/multi_json"
12
26
  require "elasticsearch/transport/transport/sniffer"
13
27
  require "elasticsearch/transport/transport/response"
@@ -1,6 +1,19 @@
1
- # Licensed to Elasticsearch B.V under one or more agreements.
2
- # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
- # See the LICENSE file in the project root for more information
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.
4
17
 
5
18
  module Elasticsearch
6
19
  module Transport
@@ -89,11 +102,6 @@ module Elasticsearch
89
102
  # @option arguments [String] :send_get_body_as Specify the HTTP method to use for GET requests with a body.
90
103
  # (Default: GET)
91
104
  #
92
- # @option api_key [String, Hash] :api_key Use API Key Authentication, either the base64 encoding of `id` and `api_key`
93
- # joined by a colon as a String, or a hash with the `id` and `api_key` values.
94
- # @option opaque_id_prefix [String] :opaque_id_prefix set a prefix for X-Opaque-Id when initializing the client. This
95
- # will be prepended to the id you set before each request if you're using X-Opaque-Id
96
- #
97
105
  # @yield [faraday] Access and configure the `Faraday::Connection` instance directly with a block
98
106
  #
99
107
  def initialize(arguments={}, &block)
@@ -107,12 +115,9 @@ module Elasticsearch
107
115
  @arguments[:randomize_hosts] ||= false
108
116
  @arguments[:transport_options] ||= {}
109
117
  @arguments[:http] ||= {}
110
- @options[:http] ||= {}
111
-
112
- set_api_key if (@api_key = @arguments[:api_key])
118
+ @options[:http] ||= {}
113
119
 
114
-
115
- @seeds ||= __extract_hosts(@arguments[:hosts] ||
120
+ @seeds = __extract_hosts(@arguments[:hosts] ||
116
121
  @arguments[:host] ||
117
122
  @arguments[:url] ||
118
123
  @arguments[:urls] ||
@@ -120,10 +125,9 @@ module Elasticsearch
120
125
  DEFAULT_HOST)
121
126
 
122
127
  @send_get_body_as = @arguments[:send_get_body_as] || 'GET'
123
- @opaque_id_prefix = @arguments[:opaque_id_prefix] || nil
124
128
 
125
129
  if @arguments[:request_timeout]
126
- @arguments[:transport_options][:request] = { timeout: @arguments[:request_timeout] }
130
+ @arguments[:transport_options][:request] = { :timeout => @arguments[:request_timeout] }
127
131
  end
128
132
 
129
133
  @arguments[:transport_options][:headers] ||= {}
@@ -137,41 +141,27 @@ module Elasticsearch
137
141
  else
138
142
  transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
139
143
  if transport_class == Transport::HTTP::Faraday
140
- @transport = transport_class.new(hosts: @seeds, options: @arguments) do |faraday|
141
- faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
142
- block&.call faraday
144
+ @transport = transport_class.new(:hosts => @seeds, :options => @arguments) do |faraday|
145
+ block.call faraday if block
146
+ unless (h = faraday.builder.handlers.last) && h.name.start_with?("Faraday::Adapter")
147
+ faraday.adapter(@arguments[:adapter] || __auto_detect_adapter)
148
+ end
143
149
  end
144
150
  else
145
- @transport = transport_class.new(hosts: @seeds, options: @arguments)
151
+ @transport = transport_class.new(:hosts => @seeds, :options => @arguments)
146
152
  end
147
153
  end
148
154
  end
149
155
 
150
156
  # Performs a request through delegation to {#transport}.
151
157
  #
152
- def perform_request(method, path, params = {}, body = nil, headers = nil)
158
+ def perform_request(method, path, params={}, body=nil, headers=nil)
153
159
  method = @send_get_body_as if 'GET' == method && body
154
- if (opaque_id = params.delete(:opaque_id))
155
- headers = {} if headers.nil?
156
- opaque_id = @opaque_id_prefix ? "#{@opaque_id_prefix}#{opaque_id}" : opaque_id
157
- headers.merge!('X-Opaque-Id' => opaque_id)
158
- end
159
160
  transport.perform_request(method, path, params, body, headers)
160
161
  end
161
162
 
162
163
  private
163
164
 
164
- def set_api_key
165
- @api_key = __encode(@api_key) if @api_key.is_a? Hash
166
- headers = @arguments[:transport_options]&.[](:headers) || {}
167
- headers.merge!('Authorization' => "ApiKey #{@api_key}")
168
- @arguments[:transport_options].merge!(
169
- headers: headers
170
- )
171
- @arguments.delete(:user)
172
- @arguments.delete(:password)
173
- end
174
-
175
165
  # Normalizes and returns hosts configuration.
176
166
  #
177
167
  # Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.
@@ -186,15 +176,15 @@ module Elasticsearch
186
176
  #
187
177
  def __extract_hosts(hosts_config)
188
178
  hosts = case hosts_config
189
- when String
190
- hosts_config.split(',').map { |h| h.strip! || h }
191
- when Array
192
- hosts_config
193
- when Hash, URI
194
- [ hosts_config ]
195
- else
196
- Array(hosts_config)
197
- end
179
+ when String
180
+ hosts_config.split(',').map { |h| h.strip! || h }
181
+ when Array
182
+ hosts_config
183
+ when Hash, URI
184
+ [ hosts_config ]
185
+ else
186
+ Array(hosts_config)
187
+ end
198
188
 
199
189
  host_list = hosts.map { |host| __parse_host(host) }
200
190
  @options[:randomize_hosts] ? host_list.shuffle! : host_list
@@ -202,42 +192,36 @@ module Elasticsearch
202
192
 
203
193
  def __parse_host(host)
204
194
  host_parts = case host
205
- when String
206
- if host =~ /^[a-z]+\:\/\//
207
- uri = URI.parse(host)
208
- { :scheme => uri.scheme,
209
- :user => uri.user,
210
- :password => uri.password,
211
- :host => uri.host,
212
- :path => uri.path,
213
- :port => uri.port }
214
- else
215
- host, port = host.split(':')
216
- { :host => host,
217
- :port => port }
218
- end
219
- when URI
220
- { :scheme => host.scheme,
221
- :user => host.user,
222
- :password => host.password,
223
- :host => host.host,
224
- :path => host.path,
225
- :port => host.port }
226
- when Hash
227
- host
228
- else
229
- raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
230
- end
231
-
232
- if @api_key
233
- # Remove Basic Auth if using API KEY
234
- host_parts.delete(:user)
235
- host_parts.delete(:password)
195
+ when String
196
+ if host =~ /^[a-z]+\:\/\//
197
+ uri = URI.parse(host)
198
+ { :scheme => uri.scheme,
199
+ :user => uri.user,
200
+ :password => uri.password,
201
+ :host => uri.host,
202
+ :path => uri.path,
203
+ :port => uri.port }
204
+ else
205
+ host, port = host.split(':')
206
+ { :host => host,
207
+ :port => port }
208
+ end
209
+ when URI
210
+ { :scheme => host.scheme,
211
+ :user => host.user,
212
+ :password => host.password,
213
+ :host => host.host,
214
+ :path => host.path,
215
+ :port => host.port }
216
+ when Hash
217
+ host
236
218
  else
237
- @options[:http][:user] ||= host_parts[:user]
238
- @options[:http][:password] ||= host_parts[:password]
219
+ raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
239
220
  end
240
221
 
222
+ @options[:http][:user] ||= host_parts[:user]
223
+ @options[:http][:password] ||= host_parts[:password]
224
+
241
225
  host_parts[:port] = host_parts[:port].to_i if host_parts[:port]
242
226
  host_parts[:path].chomp!('/') if host_parts[:path]
243
227
  host_parts
@@ -265,13 +249,6 @@ module Elasticsearch
265
249
  ::Faraday.default_adapter
266
250
  end
267
251
  end
268
-
269
- # Encode credentials for the Authorization Header
270
- # Credentials is the base64 encoding of id and api_key joined by a colon
271
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
272
- def __encode(api_key)
273
- Base64.strict_encode64([api_key[:id], api_key[:api_key]].join(':'))
274
- end
275
252
  end
276
253
  end
277
254
  end
@@ -1,7 +1,3 @@
1
- # Licensed to Elasticsearch B.V under one or more agreements.
2
- # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
- # See the LICENSE file in the project root for more information
4
-
5
1
  # Licensed to Elasticsearch B.V. under one or more contributor
6
2
  # license agreements. See the NOTICE file distributed with
7
3
  # this work for additional information regarding copyright
@@ -25,7 +21,7 @@ module Elasticsearch
25
21
  # Class for wrapping a hash that could have sensitive data.
26
22
  # When printed, the sensitive values will be redacted.
27
23
  #
28
- # @since 6.2.0
24
+ # @since 6.1.1
29
25
  class Redacted < ::Hash
30
26
 
31
27
  def initialize(elements = nil)
@@ -35,20 +31,20 @@ module Elasticsearch
35
31
 
36
32
  # The keys whose values will be redacted.
37
33
  #
38
- # @since 6.2.0
34
+ # @since 6.1.1
39
35
  SENSITIVE_KEYS = [ :password,
40
36
  :pwd ].freeze
41
37
 
42
38
  # The replacement string used in place of the value for sensitive keys.
43
39
  #
44
- # @since 6.2.0
40
+ # @since 6.1.1
45
41
  STRING_REPLACEMENT = '<REDACTED>'.freeze
46
42
 
47
43
  # Get a string representation of the hash.
48
44
  #
49
45
  # @return [ String ] The string representation of the hash.
50
46
  #
51
- # @since 6.2.0
47
+ # @since 6.1.1
52
48
  def inspect
53
49
  redacted_string(:inspect)
54
50
  end
@@ -57,7 +53,7 @@ module Elasticsearch
57
53
  #
58
54
  # @return [ String ] The string representation of the hash.
59
55
  #
60
- # @since 6.2.0
56
+ # @since 6.1.1
61
57
  def to_s
62
58
  redacted_string(:to_s)
63
59
  end