Class: OpenSearch::Aws::Sigv4Client

Inherits:
Client
  • Object
show all
Defined in:
lib/opensearch-aws-sigv4.rb

Overview

AWS Sigv4 Wrapper for OpenSearch::Client. This client accepts a Sigv4 Signer during initialization, and signs every request with a Sigv4 Signature with the provided signer.

Examples:

signer = Aws::Sigv4::Signer.new(service: 'es',
                                region: 'us-east-1',
                                access_key_id: '<access_key_id>',
                                secret_access_key: '<secret_access_key>',
                                session_token: '<session_token>')

client = OpenSearch::Aws::Sigv4Client.new(
    { host: 'https://my-os-domain.us-east-1.es.amazonaws.com/' },
    signer
)

puts client.cat.health

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport_args, sigv4_signer, options: {}, &block) ⇒ Sigv4Client

Returns a new instance of Sigv4Client.

Parameters:

  • transport_args (Hash)

    arguments for OpenSearch::Transport::Client.

  • block (&block)

    code block to be passed to OpenSearch::Transport::Client.

  • sigv4_signer (Aws::Sigv4::Signer)

    an instance of AWS Sigv4 Signer.

  • options (Hash) (defaults to: {})

Options Hash (options:):

  • :sigv4_debug (Boolean)

    whether to log debug info for Sigv4 Signing



47
48
49
50
51
52
53
54
55
56
# File 'lib/opensearch-aws-sigv4.rb', line 47

def initialize(transport_args, sigv4_signer, options: {}, &block)
  unless sigv4_signer.is_a?(::Aws::Sigv4::Signer)
    raise ArgumentError, "Please pass a Aws::Sigv4::Signer. A #{sigv4_signer.class} was given."
  end

  @sigv4_signer = sigv4_signer
  @sigv4_debug = options[:sigv4_debug]
  @logger = nil
  super(transport_args, &block)
end

Instance Attribute Details

#sigv4_signerAws::Sigv4::Signer

Signer used to sign every request

Returns:

  • (Aws::Sigv4::Signer)

    the current value of sigv4_signer



39
40
41
# File 'lib/opensearch-aws-sigv4.rb', line 39

def sigv4_signer
  @sigv4_signer
end

Instance Method Details

#perform_request(method, path, params = {}, body = nil, headers = nil) ⇒ Object

See Also:

  • Transport::Transport::Base::perform_request


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/opensearch-aws-sigv4.rb', line 59

def perform_request(method, path, params = {}, body = nil, headers = nil)
  signature_body = body.is_a?(Hash) ? body.to_json : body.to_s
  signature = sigv4_signer.sign_request(
    http_method: method,
    url: signature_url(path, params),
    headers: headers,
    body: signature_body
  )
  headers = (headers || {}).merge(signature.headers)

  log_signature_info(signature)
  super(method, path, params, signature_body, headers)
end