From dd117b1ea0d66ffdba43a52b0b78a3e7f29a8865 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 5 Feb 2024 13:39:15 +0100 Subject: [PATCH] Drop runtime dependency on base64 --- elasticsearch-api/api-spec-testing/test_file/task_group.rb | 2 ++ .../integration/api_key/api_key_invalidation_spec.rb | 1 + elasticsearch/elasticsearch.gemspec | 3 +-- elasticsearch/lib/elasticsearch.rb | 7 ++++--- elasticsearch/spec/unit/api_key_spec.rb | 1 + 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/elasticsearch-api/api-spec-testing/test_file/task_group.rb b/elasticsearch-api/api-spec-testing/test_file/task_group.rb index a94578eb29..b8b900c357 100644 --- a/elasticsearch-api/api-spec-testing/test_file/task_group.rb +++ b/elasticsearch-api/api-spec-testing/test_file/task_group.rb @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +require 'base64' + module Elasticsearch module RestAPIYAMLTests class TestFile diff --git a/elasticsearch-api/spec/platinum/integration/api_key/api_key_invalidation_spec.rb b/elasticsearch-api/spec/platinum/integration/api_key/api_key_invalidation_spec.rb index 5162a77830..5464412f4d 100644 --- a/elasticsearch-api/spec/platinum/integration/api_key/api_key_invalidation_spec.rb +++ b/elasticsearch-api/spec/platinum/integration/api_key/api_key_invalidation_spec.rb @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +require 'base64' require_relative '../platinum_helper' describe 'API keys API invalidation' do diff --git a/elasticsearch/elasticsearch.gemspec b/elasticsearch/elasticsearch.gemspec index f0caeaadcb..2b0295c386 100644 --- a/elasticsearch/elasticsearch.gemspec +++ b/elasticsearch/elasticsearch.gemspec @@ -46,10 +46,9 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.5' s.add_dependency 'elastic-transport', '~> 8.3' - s.add_dependency 'elasticsearch-api', '8.11.1' - s.add_dependency 'base64' + s.add_development_dependency 'base64' s.add_development_dependency 'bundler' s.add_development_dependency 'byebug' unless defined?(JRUBY_VERSION) || defined?(Rubinius) s.add_development_dependency 'pry' diff --git a/elasticsearch/lib/elasticsearch.rb b/elasticsearch/lib/elasticsearch.rb index f366f4224c..a30541f357 100644 --- a/elasticsearch/lib/elasticsearch.rb +++ b/elasticsearch/lib/elasticsearch.rb @@ -18,7 +18,6 @@ require 'elasticsearch/version' require 'elastic/transport' require 'elasticsearch/api' -require 'base64' module Elasticsearch NOT_ELASTICSEARCH_WARNING = 'The client noticed that the server is not Elasticsearch and we do not support this unknown product.'.freeze @@ -112,7 +111,8 @@ def verify_elasticsearch(*args, &block) def setup_cloud_host(cloud_id, user, password, port) name = cloud_id.split(':')[0] - cloud_url, elasticsearch_instance = Base64.decode64(cloud_id.gsub("#{name}:", '')).split('$') + base64_decoded = cloud_id.gsub("#{name}:", '').unpack1('m') + cloud_url, elasticsearch_instance = base64_decoded.split('$') if cloud_url.include?(':') url, port = cloud_url.split(':') @@ -154,7 +154,8 @@ def setup_cloud(arguments) # Credentials is the base64 encoding of id and api_key joined by a colon # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html def encode(api_key) - Base64.strict_encode64([api_key[:id], api_key[:api_key]].join(':')) + credentials = [api_key[:id], api_key[:api_key]].join(':') + [credentials].pack('m0') end def elasticsearch_validation_request diff --git a/elasticsearch/spec/unit/api_key_spec.rb b/elasticsearch/spec/unit/api_key_spec.rb index cc43f65604..176d1b57b6 100644 --- a/elasticsearch/spec/unit/api_key_spec.rb +++ b/elasticsearch/spec/unit/api_key_spec.rb @@ -16,6 +16,7 @@ # under the License. require 'spec_helper' +require 'base64' describe Elasticsearch::Client do context 'when using API Key' do