Sha256: bcc37baadd49f1f70fc2786e54b97cac95464f34fa5a2dd911684eaea7a40643

Contents?: true

Size: 1.39 KB

Versions: 24

Compression:

Stored size: 1.39 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_request[:body] = options[:json].respond_to?(:to_str) ? options[:json] : JSON.generate(options[:json]) if options[:json]

      signature = signer.sign_request(signature_request)

      request.headers(signature.headers)
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
search_flip-4.0.0.beta12 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.7.1 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta11 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.7.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta10 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.6.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.5.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta9 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta8 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.4.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.3.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta7 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta6 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta5 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta4 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.2.1 lib/search_flip/aws_sigv4_plugin.rb
search_flip-3.2.0 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta3 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta2 lib/search_flip/aws_sigv4_plugin.rb
search_flip-4.0.0.beta1 lib/search_flip/aws_sigv4_plugin.rb