Sha256: a0f76f0023f1568cd34330ed96236fc8683874d9d20b721181897be1f2cb78c7

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8
require "opensearch"
require "base64"

module LogStash
  module Filters
    class OpenSearchClient

      attr_reader :client

      def initialize(logger, hosts, options = {})
        ssl = options.fetch(:ssl, false)
        user = options.fetch(:user, nil)
        password = options.fetch(:password, nil)
        api_key = options.fetch(:api_key, nil)

        transport_options = {:headers => {}}
        transport_options[:headers].merge!(setup_basic_auth(user, password))
        transport_options[:headers].merge!(setup_api_key(api_key))

        hosts = hosts.map { |host| { host: host, scheme: 'https' } } if ssl
        # set ca_file even if ssl isn't on, since the host can be an https url
        ssl_options = { ssl: true, ca_file: options[:ca_file] } if options[:ca_file]
        ssl_options ||= {}

        logger.info("New OpenSearch filter client", :hosts => hosts)
        @client = ::OpenSearch::Client.new(hosts: hosts, transport_options: transport_options, :ssl => ssl_options)
      end

      def search(params)
        @client.search(params)
      end

      private

      def setup_basic_auth(user, password)
        return {} unless user && password && password.value
        
        token = ::Base64.strict_encode64("#{user}:#{password.value}")
        { Authorization: "Basic #{token}" }
      end

      def setup_api_key(api_key)
        return {} unless (api_key && api_key.value)

        token = ::Base64.strict_encode64(api_key.value)
        { Authorization: "ApiKey #{token}" }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-filter-opensearch-0.1.1 lib/logstash/filters/opensearch/client.rb