Elasticsearch-php: Documented PHP Extended Host settings lead to connection exception

Created on 5 Sep 2017  路  5Comments  路  Source: elastic/elasticsearch-php

Summary of problem or feature request

When attempting to use the PHP client and to specify the ES host and port via the Extended Host Configuration notation, a connection exception is thrown. It may be that perhaps the documented example is not complete?

https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_configuration.html#_extended_host_configuration

For example, the following code snippet results in the exception that follows:

$hosts = [
        'host' => 'localhost',
        'port' => '9200',
        'scheme' => 'http',
        'user' => 'elastic',
        'pass' => 'changeme'
];

Exception:

Fatal error: Uncaught exception 'Elasticsearch\Common\Exceptions\BadRequest400Exception' with message '{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication token for REST request [/products/product/2]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication token for REST request [/products/product/2]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}' ...

However, specifying the host and port in the following way against the same instance works successfully:

$hosts = [
     'http://elastic:changeme@localhost:9200',
];

Results:
Array ( [_index] => products [_type] => product [_id] => 2 [_version] => 1 [found] => 1 [_source] => Array ( [brandName] => Caress [customerRating] => 5 [price] => 3 [grp_id] => 2 [quantitySold] => 760139 [upc12] => 011111065925 [productName] => Caress Velvet Bliss Ultra Silkening Beauty Bar - 6 Ct ) )

Attached is a zip of the test case Apache site, where PHP pages showing the full code for working (testWorking.php) and non-working (test NotWorking.php) examples. This was reported in the field and reproduced locally on an Apache Httpd instance running against ES + XPack (no SSL) in the following config:

System details

  • OS: Mac Os Sierra 10.12.5
  • PHP Version: 5.6.30
  • ES-PHP client version
    phpTest.zip
    : 5.0
  • Elasticsearch version: 5.5.0 (w/ XPack 5.5.0)

Most helpful comment

The extended syntax is an array of maps, while your's is just a single map. It should look like:

$hosts = [
  [  // <----------------- here
        'host' => 'localhost',
        'port' => '9200',
        'scheme' => 'http',
        'user' => 'elastic',
        'pass' => 'changeme'
  ] // <----------------- and here
];

We could probably add some better validation though, so that it only accepts an array of strings (simple syntax) or array of maps (extended syntax), otherwise throw an exception so the user knows it is bad config rather than something else.

All 5 comments

The extended syntax is an array of maps, while your's is just a single map. It should look like:

$hosts = [
  [  // <----------------- here
        'host' => 'localhost',
        'port' => '9200',
        'scheme' => 'http',
        'user' => 'elastic',
        'pass' => 'changeme'
  ] // <----------------- and here
];

We could probably add some better validation though, so that it only accepts an array of strings (simple syntax) or array of maps (extended syntax), otherwise throw an exception so the user knows it is bad config rather than something else.

Ah, thanks for pointing out the mistake. I and the person who originally alerted me to this mis-read the example.

I agree, though it is demonstrated in the example, perhaps it could be made clearer like you mentioned.

Thanks,
Alex

Happy to help! :)

I'll try to work up some validation code this week. Will leave this ticket open as a reminder.

Thank so much!

Outdated issue

Was this page helpful?
0 / 5 - 0 ratings