alleyinteractive / searchpress

Elasticsearch integration for WordPress.
87 stars 12 forks source link

Conditional authentication fails when updating the ES URL and immediately running an index #110

Open kevinfodness opened 5 years ago

kevinfodness commented 5 years ago

Scenario

A user wants to configure SearchPress to connect to a server that requires authentication (such as aws.found.io). There is a condition in the code similar to this one:

if ( false !== strpos( SP_Config()->get_setting( 'host' ), 'aws.found.io' ) ) {
    SP_API()->request_defaults['headers']['Authorization'] = 'Basic ' . base64_encode( 'username:password' );
}

This action runs on the after_setup_theme hook.

If a user then attempts to update the ES instance URL, and checks the box to request an immediate full sync, the action that configures the authentication runs before the option is updated in the database, so when SearchPress attempts to connect to the ES instance, the authorization headers aren't set, and it returns an error message to the user indicating that the server could not be reached. However, if the user tries again once the option has been set, it succeeds.

Suggested Action

Add a filter to the request method of SP_API that allows the request defaults to be filtered before running the remote request, to allow users to reliably set authentication headers (and other types of default request headers) before the request is sent, and/or allow users to filter the entire set of request parameters before the request is sent.