Skip to content

Manticore transport unable to send custom headers since 7.2 #1428

Closed
@kares

Description

@kares

The way the elasticsearch-transport gem was refactored in 7.2 broke the Manticore backend under JRuby.
All versions since 7.2 are affected (the bug is present in 7.14.0), last working version seems to be 7.1.0.

NOTE: that the Manticore backend does not support the :user/:password options.

require "elasticsearch"
require "base64"
require "elasticsearch/transport/transport/http/manticore"

user = 'elastic'
password = 'changeme'
transport_options = {}

token = ::Base64.strict_encode64("#{user}:#{password.value}")
#transport_options[:headers] = { Authorization: "Basic #{token}" }
transport_options[:headers] = { 'Authorization' => "Basic #{token}" }

client = :Elasticsearch::Client.new(hosts: 'http://localhost:9200',
                                    transport_options: transport_options,
                                    transport_class: ::Elasticsearch::Transport::Transport::HTTP::Manticore)

puts client.search(q: '*', size: 2)

... fails with 401 despite correct credentials due the headers getting replaced by the client logic

Elasticsearch::Transport::Transport::Errors::Unauthorized: [401] {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/_search?q=%2A&size=2]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/_search?q=%2A&size=2]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}
  __raise_transport_error at /opt/local/rvm/gems/jruby-9.2.16.0@ls/gems/elasticsearch-transport-7.2.1/lib/elasticsearch/transport/transport/base.rb:219
          perform_request at /opt/local/rvm/gems/jruby-9.2.16.0@ls/gems/elasticsearch-transport-7.2.1/lib/elasticsearch/transport/transport/base.rb:340
          perform_request at /opt/local/rvm/gems/jruby-9.2.16.0@ls/gems/elasticsearch-transport-7.2.1/lib/elasticsearch/transport/transport/http/manticore.rb:84
          perform_request at /opt/local/rvm/gems/jruby-9.2.16.0@ls/gems/elasticsearch-transport-7.2.1/lib/elasticsearch/transport/client.rb:165

a patch to not override existing headers while adding once the gem forces that seems to get things going:

module Elasticsearch
  module Transport
    module Transport
      module HTTP
        class Manticore

          def apply_headers(request_options, options)
            headers = (options && options[:headers]) || {}
            headers[CONTENT_TYPE_STR] = find_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
            headers[USER_AGENT_STR] = find_value(headers, USER_AGENT_REGEX) || user_agent_header
            headers[ACCEPT_ENCODING] = GZIP if use_compression?
            (request_options[:headers] ||= {}).merge!(headers) # this is the fix
          end

        end
      end
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions