Skip to content

Commit f988cab

Browse files
committed
[Client] Fix extracting cloud host when cloud host provides a port
Fixes #1081
1 parent 38a0683 commit f988cab

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,25 @@ def set_api_key
190190
end
191191

192192
def extract_cloud_creds(arguments)
193-
return unless arguments[:cloud_id]
193+
return unless arguments[:cloud_id] && !arguments[:cloud_id].empty?
194194

195195
name = arguments[:cloud_id].split(':')[0]
196196
cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub("#{name}:", '')).split('$')
197+
198+
if cloud_url.include?(':')
199+
url, port = cloud_url.split(':')
200+
host = "#{elasticsearch_instance}.#{url}"
201+
else
202+
host = "#{elasticsearch_instance}.#{cloud_url}"
203+
port = arguments[:port] || DEFAULT_CLOUD_PORT
204+
end
197205
[
198206
{
199207
scheme: 'https',
200208
user: arguments[:user],
201209
password: arguments[:password],
202-
host: "#{elasticsearch_instance}.#{cloud_url}",
203-
port: arguments[:port] || DEFAULT_CLOUD_PORT
210+
host: host,
211+
port: port.to_i
204212
}
205213
]
206214
end

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,51 @@
433433
).to eq('https://elasticfantastic:[email protected]:9243')
434434
end
435435
end
436+
437+
context 'when the cloud host provides a port' do
438+
let(:client) do
439+
described_class.new(
440+
cloud_id: 'name:ZWxhc3RpY19zZXJ2ZXI6OTI0MyRlbGFzdGljX2lk',
441+
user: 'elastic',
442+
password: 'changeme'
443+
)
444+
end
445+
446+
let(:hosts) do
447+
client.transport.hosts
448+
end
449+
450+
it 'creates the correct full url' do
451+
expect(hosts[0][:host]).to eq('elastic_id.elastic_server')
452+
expect(hosts[0][:protocol]).to eq('https')
453+
expect(hosts[0][:user]).to eq('elastic')
454+
expect(hosts[0][:password]).to eq('changeme')
455+
expect(hosts[0][:port]).to eq(9243)
456+
end
457+
end
458+
459+
context 'when the cloud host provides a port and the port is also specified' do
460+
let(:client) do
461+
described_class.new(
462+
cloud_id: 'name:ZWxhc3RpY19zZXJ2ZXI6OTI0MyRlbGFzdGljX2lk',
463+
user: 'elastic',
464+
password: 'changeme',
465+
port: 9200
466+
)
467+
end
468+
469+
let(:hosts) do
470+
client.transport.hosts
471+
end
472+
473+
it 'creates the correct full url' do
474+
expect(hosts[0][:host]).to eq('elastic_id.elastic_server')
475+
expect(hosts[0][:protocol]).to eq('https')
476+
expect(hosts[0][:user]).to eq('elastic')
477+
expect(hosts[0][:password]).to eq('changeme')
478+
expect(hosts[0][:port]).to eq(9243)
479+
end
480+
end
436481
end
437482

438483
shared_examples_for 'a client that extracts hosts' do

0 commit comments

Comments
 (0)