Class: ElasticGraph::Support::FaradayMiddleware::MSearchUsingGetInsteadOfPost

Inherits:
Data
  • Object
show all
Defined in:
lib/elastic_graph/support/faraday_middleware/msearch_using_get_instead_of_post.rb

Overview

Custom Faraday middleware that forces msearch calls to use an HTTP GET instead of an HTTP POST. While not necessary, it preserves a useful property: all “read” calls made by ElasticGraph use an HTTP GET, and HTTP POST requests are “write” calls. This allows the access policy to only grant HTTP GET access from the GraphQL endpoint, which leads to a more secure setup (as the GraphQL endpoint can be blocked from performing any writes).

Note: before elasticsearch-ruby 7.9.0, msearch used an HTTP GET request, so this simply restores that behavior. This results in an HTTP GET with a request body, but it works just fine and its what the Ruby Elasticsearch client did for years.

For more info, see: github.com/elastic/elasticsearch-ruby/issues/1005

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



24
25
26
# File 'lib/elastic_graph/support/faraday_middleware/msearch_using_get_instead_of_post.rb', line 24

def app
  @app
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Processes a Faraday request, converting _msearch POST requests to GET requests.

Parameters:

  • env (Faraday::Env)

    the Faraday request environment

Returns:

  • (Faraday::Response)

    the response from the next middleware in the stack



31
32
33
34
# File 'lib/elastic_graph/support/faraday_middleware/msearch_using_get_instead_of_post.rb', line 31

def call(env)
  env.method = :get if env.url.path.to_s.end_with?("/_msearch")
  app.call(env)
end