Class: SearchFlip::AwsSigv4Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/search_flip/aws_sigv4_plugin.rb

Overview

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.

Examples:

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: "..."
      )
    ]
  )
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AwsSigv4Plugin

Returns a new instance of AwsSigv4Plugin.



26
27
28
# File 'lib/search_flip/aws_sigv4_plugin.rb', line 26

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

Instance Attribute Details

#signerObject

Returns the value of attribute signer.



24
25
26
# File 'lib/search_flip/aws_sigv4_plugin.rb', line 24

def signer
  @signer
end

Instance Method Details

#call(request, method, uri, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/search_flip/aws_sigv4_plugin.rb', line 30

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