Skip to content

Commit 01fff96

Browse files
committed
[CLIENT] Support providing a cloud id
1 parent 8da4f8b commit 01fff96

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
require 'base64'
19+
1820
module Elasticsearch
1921
module Transport
2022

@@ -117,7 +119,8 @@ def initialize(arguments={}, &block)
117119
@arguments[:http] ||= {}
118120
@options[:http] ||= {}
119121

120-
@seeds = __extract_hosts(@arguments[:hosts] ||
122+
@seeds = extract_cloud_creds(@arguments)
123+
@seeds ||= __extract_hosts(@arguments[:hosts] ||
121124
@arguments[:host] ||
122125
@arguments[:url] ||
123126
@arguments[:urls] ||
@@ -156,6 +159,15 @@ def perform_request(method, path, params={}, body=nil, headers=nil)
156159

157160
private
158161

162+
def extract_cloud_creds(arguments)
163+
return unless arguments[:cloud_id]
164+
cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub('name:', '')).split('$')
165+
[ { :scheme => 'https',
166+
:user => arguments[:user],
167+
:password => arguments[:password],
168+
:host => "#{elasticsearch_instance}.#{cloud_url}" } ]
169+
end
170+
159171
# Normalizes and returns hosts configuration.
160172
#
161173
# Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.

elasticsearch-transport/lib/elasticsearch/transport/transport/base.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __build_connections
148148
Connections::Collection.new \
149149
:connections => hosts.map { |host|
150150
host[:protocol] = host[:scheme] || options[:scheme] || options[:http][:scheme] || DEFAULT_PROTOCOL
151-
host[:port] ||= options[:port] || options[:http][:port] || DEFAULT_PORT
151+
host[:port] ||= options[:port] || options[:http][:port] || DEFAULT_PORT unless options[:cloud_id]
152152
if (options[:user] || options[:http][:user]) && !host[:user]
153153
host[:user] ||= options[:user] || options[:http][:user]
154154
host[:password] ||= options[:password] || options[:http][:password]
@@ -234,7 +234,8 @@ def __convert_to_json(o=nil, options={})
234234
def __full_url(host)
235235
url = "#{host[:protocol]}://"
236236
url += "#{CGI.escape(host[:user])}:#{CGI.escape(host[:password])}@" if host[:user]
237-
url += "#{host[:host]}:#{host[:port]}"
237+
url += "#{host[:host]}"
238+
url += ":#{host[:port]}" if host[:port]
238239
url += "#{host[:path]}" if host[:path]
239240
url
240241
end

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,28 @@
270270
end
271271
end
272272

273+
context 'when cloud credentials are provided' do
274+
275+
let(:client) do
276+
described_class.new(cloud_id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==', user: 'elastic', password: 'changeme')
277+
end
278+
279+
let(:hosts) do
280+
client.transport.hosts
281+
end
282+
283+
it 'extracts the cloud credentials' do
284+
expect(hosts[0][:host]).to eq('abcd.localhost')
285+
expect(hosts[0][:protocol]).to eq('https')
286+
expect(hosts[0][:user]).to eq('elastic')
287+
expect(hosts[0][:password]).to eq('changeme')
288+
end
289+
290+
it 'creates the correct full url' do
291+
expect(client.transport.__full_url(client.transport.hosts[0])).to eq('https://elastic:changeme@abcd.localhost')
292+
end
293+
end
294+
273295
shared_examples_for 'a client that extracts hosts' do
274296

275297
context 'when the hosts are a String' do

0 commit comments

Comments
 (0)