Skip to content

Commit bdb7f36

Browse files
committed
[Client] Fix extracting cloud host when cloud host provides a port
Fixes #1081
1 parent 96c739d commit bdb7f36

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
@@ -195,17 +195,25 @@ def set_api_key
195195
end
196196

197197
def extract_cloud_creds(arguments)
198-
return unless arguments[:cloud_id]
198+
return unless arguments[:cloud_id] && !arguments[:cloud_id].empty?
199199

200200
name = arguments[:cloud_id].split(':')[0]
201201
cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub("#{name}:", '')).split('$')
202+
203+
if cloud_url.include?(':')
204+
url, port = cloud_url.split(':')
205+
host = "#{elasticsearch_instance}.#{url}"
206+
else
207+
host = "#{elasticsearch_instance}.#{cloud_url}"
208+
port = arguments[:port] || DEFAULT_CLOUD_PORT
209+
end
202210
[
203211
{
204212
scheme: 'https',
205213
user: arguments[:user],
206214
password: arguments[:password],
207-
host: "#{elasticsearch_instance}.#{cloud_url}",
208-
port: arguments[:port] || DEFAULT_CLOUD_PORT
215+
host: host,
216+
port: port.to_i
209217
}
210218
]
211219
end

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

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

433478
shared_examples_for 'a client that extracts hosts' do

0 commit comments

Comments
 (0)