Skip to content

Commit

Permalink
[Client] Fix extracting cloud host when cloud host provides a port
Browse files Browse the repository at this point in the history
Fixes #1081
  • Loading branch information
picandocodigo committed Nov 16, 2020
1 parent 38a0683 commit f988cab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
14 changes: 11 additions & 3 deletions elasticsearch-transport/lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,51 @@
).to eq('https://elasticfantastic:[email protected]: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
Expand Down

0 comments on commit f988cab

Please sign in to comment.