From 3223e8fcf42f7ee61bcee453135c11ee1f0faccd 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 | 4 ++-- elasticsearch/lib/elasticsearch.rb | 7 ++++--- elasticsearch/spec/unit/api_key_spec.rb | 1 + 5 files changed, 10 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 71ef01cb31..7c8eb10728 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 f5222aaa4d..f9faec60eb 100644 --- a/elasticsearch/elasticsearch.gemspec +++ b/elasticsearch/elasticsearch.gemspec @@ -47,8 +47,8 @@ Gem::Specification.new do |s| s.add_dependency 'elastic-transport', '~> 8.3' s.add_dependency 'elasticsearch-api', '8.13.0' - s.add_dependency 'base64' - + + s.add_development_dependency 'base64' s.add_development_dependency 'bundler' s.add_development_dependency 'debug' unless defined?(JRUBY_VERSION) s.add_development_dependency 'pry' diff --git a/elasticsearch/lib/elasticsearch.rb b/elasticsearch/lib/elasticsearch.rb index f159bc1f90..6b1639f383 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 @@ -108,7 +107,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(':') @@ -150,7 +150,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