From bdb7f366120745f37d08b2674fdacc96990761c1 Mon Sep 17 00:00:00 2001 From: Fernando Date: Mon, 16 Nov 2020 10:57:38 +0000 Subject: [PATCH] [Client] Fix extracting cloud host when cloud host provides a port Fixes #1081 --- .../lib/elasticsearch/transport/client.rb | 14 ++++-- .../elasticsearch/transport/client_spec.rb | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/elasticsearch-transport/lib/elasticsearch/transport/client.rb b/elasticsearch-transport/lib/elasticsearch/transport/client.rb index 8472ed2c77..2d7d97d41e 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/client.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/client.rb @@ -195,17 +195,25 @@ def set_api_key end def extract_cloud_creds(arguments) - return unless arguments[:cloud_id] + return unless arguments[:cloud_id] && !arguments[:cloud_id].empty? name = arguments[:cloud_id].split(':')[0] cloud_url, elasticsearch_instance = Base64.decode64(arguments[:cloud_id].gsub("#{name}:", '')).split('$') + + if cloud_url.include?(':') + url, port = cloud_url.split(':') + host = "#{elasticsearch_instance}.#{url}" + else + host = "#{elasticsearch_instance}.#{cloud_url}" + port = arguments[:port] || DEFAULT_CLOUD_PORT + end [ { scheme: 'https', user: arguments[:user], password: arguments[:password], - host: "#{elasticsearch_instance}.#{cloud_url}", - port: arguments[:port] || DEFAULT_CLOUD_PORT + host: host, + port: port.to_i } ] end diff --git a/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb b/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb index db2092b360..04fa905de9 100644 --- a/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb +++ b/elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb @@ -428,6 +428,51 @@ ).to eq('https://elasticfantastic:changeme@abcd.localhost:9243') end end + + context 'when the cloud host provides a port' do + let(:client) do + described_class.new( + cloud_id: 'name:ZWxhc3RpY19zZXJ2ZXI6OTI0MyRlbGFzdGljX2lk', + user: 'elastic', + password: 'changeme' + ) + end + + let(:hosts) do + client.transport.hosts + end + + it 'creates the correct full url' do + expect(hosts[0][:host]).to eq('elastic_id.elastic_server') + expect(hosts[0][:protocol]).to eq('https') + expect(hosts[0][:user]).to eq('elastic') + expect(hosts[0][:password]).to eq('changeme') + expect(hosts[0][:port]).to eq(9243) + end + end + + context 'when the cloud host provides a port and the port is also specified' do + let(:client) do + described_class.new( + cloud_id: 'name:ZWxhc3RpY19zZXJ2ZXI6OTI0MyRlbGFzdGljX2lk', + user: 'elastic', + password: 'changeme', + port: 9200 + ) + end + + let(:hosts) do + client.transport.hosts + end + + it 'creates the correct full url' do + expect(hosts[0][:host]).to eq('elastic_id.elastic_server') + expect(hosts[0][:protocol]).to eq('https') + expect(hosts[0][:user]).to eq('elastic') + expect(hosts[0][:password]).to eq('changeme') + expect(hosts[0][:port]).to eq(9243) + end + end end shared_examples_for 'a client that extracts hosts' do