10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 312 forks source link

There is a problem with connecting to your Elasticsearch host #1700

Closed jmreicha closed 4 years ago

jmreicha commented 4 years ago

Have you searched for similar issues before submitting this one? Yes

Is this a bug, question or feature request? Question

Describe the issue you encountered:

Attempting to connect ElasticPress to a self hosted ElasticSearch server via AWS Api-Gateway using an API key for authentication. Using some custom code to add the x-api-key header to the request.

Some requests seem to return 200 but others don't. Below is an example of the errors I am seeing when I have auth turned on.


    Host: https://xxx.execute-api.us-west-2.amazonaws.com/stage/
    Time Taken: 137 ms
    URL: https://xxx.execute-api.us-west-2.amazonaws.com/stage/_nodes/plugins
    Method: GET
    Headers:
    Query Response Code: HTTP 403
    Query Result:

    {
        "message": "Forbidden"
    }

    Copy cURL Request
    Host: https://xxx.execute-api.us-west-2.amazonaws.com/stage/
    Time Taken: 218 ms
    URL: https://xxx.execute-api.us-west-2.amazonaws.com/stage/
    Method: GET
    Headers:
    Query Response Code: HTTP 403
    Query Result:

    {
        "message": "Missing Authentication Token"
    }

    Copy cURL Request 

If the API key is disabled everything works again as expected. I should also mention these curl commands work from the command line when the x-api-key is insterted into the headers.

Current WordPress version? 5.3.2

Current ElasticPress version? 3.3

Current Elasticsearch version? 6.4

Where do you host your Elasticsearch server?

AWS - Using an API Gateway to add authentication with an API key and the proxying the request to the AWS ElasticSearch service through a Lambda.

Other plugins installed (WooCommerce, Simple Redirect Manager, etc..)?

Admin Search by Post ID
Dashboard Content
Debug Bar
Debug Bar ElasticPress
Disable Gutenberg Blocks - Block Manager
Duplicate Post
ElasticPress
Import Users from CSV
JSON Basic Authentication
Meta Inspector
Plagiarism Plugin
Polylang Pro
Stream
User Switching
WP-REST-API V2 Menus
XML Sitemap & Google News

Steps to reproduce: NA

Screenshots, if needed:

Settings seem to be greyed out when the API key is enabled and can't update any config.

image
brandwaffle commented 4 years ago

@jmreicha if you haven't already, can you install the Debug Bar and Debug Bar ElasticPress plugins and see what sort of results you're getting from searches?

My suspicion is perhaps the header is getting filtered into some, but not all, requests. For example, if the filter is only running on indexing time, your searches would fail. The Debug Bar ElasticPress plugin's output includes the Headers that are sent, so this will give you exactly the debugging info you need.

jmreicha commented 4 years ago

@brandwaffle I might not be looking in the right place but the headers all appear to be the same, including successful requests.

array(1) {
  ["Content-Type"]=>
  string(16) "application/json"
}
felipeelia commented 4 years ago

Hi @jmreicha , I know this has been here for a while now but did you had the chance to fix the problem? If so, can you please share the solution? That way we can keep it for other users with the same problem.

If you didn't fix it, do you mind sharing the code you're using to add the header?

Thanks!

jmreicha commented 4 years ago

If I remember, we basically forced it to add a x-api-key header to all requests and then check if the header was there. Unfortunately I can't seem to find the code that does this.n

felipeelia commented 4 years ago

Theoretically, a code like this would add a header to all requests:

add_filter(
    'ep_format_request_headers',
    function( $headers ) {
        $headers['x-api-key'] = 'API_KEY';
        return $headers;
    }
);

Are you able to connect to your ES server through a program like https://www.postman.com/, for example? That can help to pin point the problem.

jmreicha commented 4 years ago

Yes, I think the above example is almost exactly what we ended up doing.

On Tue, Jun 9, 2020 at 9:11 AM Felipe Elia notifications@github.com wrote:

Theoretically, a code like this would add a header to all requests:

add_filter( 'ep_format_request_headers', function( $headers ) { $headers['x-api-key'] = 'API_KEY'; return $headers; } );

Are you able to connect to your ES server through a program like https://www.postman.com/, for example? That can help to pin point the problem.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/10up/ElasticPress/issues/1700#issuecomment-641323576, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH5V6R6IJ3YLBS76XR44ODRVY7IZANCNFSM4LHJVYZQ .

felipeelia commented 4 years ago

Nice, @jmreicha, thanks. I'll go ahead and close this issue, feel free to reopen it if you need.