Sha256: d9ae6410819d35f906ff2196a2b91d424bfaea7032257a6571ca516cf1074bbb

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

require "aws-sdk-core"
require "uri"

module SearchFlip
  # The SearchFlip::AwsSigV4Plugin is a plugin for the SearchFlip::HTTPClient
  # to be used with AWS Elasticsearch to sign requests, i.e. add signed
  # headers, before sending the request to Elasticsearch.
  #
  # @example
  #   MyConnection = SearchFlip::Connection.new(
  #     base_url: "https://your-elasticsearch-cluster.es.amazonaws.com",
  #     http_client: SearchFlip::HTTPClient.new(
  #       plugins: [
  #         SearchFlip::AwsSigv4Plugin.new(
  #           region: "...",
  #           access_key_id: "...",
  #           secret_access_key: "..."
  #         )
  #       ]
  #     )
  #   )

  class AwsSigv4Plugin
    attr_accessor :signer

    def initialize(options = {})
      self.signer = Aws::Sigv4::Signer.new({ service: "es" }.merge(options))
    end

    def call(request, method, uri, options = {})
      full_uri = URI.parse(uri)
      full_uri.query = URI.encode_www_form(options[:params]) if options[:params]

      signature_request = {
        http_method: method.to_s.upcase,
        url: full_uri.to_s
      }

      signature_request[:body] = options[:body] if options.key?(:body)

      signature = signer.sign_request(signature_request)

      request.headers(signature.headers)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
search_flip-3.9.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta16 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta15 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta14 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.8.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta13 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.7.2 lib/search_flip/aws_sigv4_plugin.rb