From 0b80bcfe6fd3154936aeb2eacc6381d6504641b5 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Mon, 12 Jan 2015 15:01:11 -0500 Subject: [PATCH] Extract HTTP BasicAuth from host details if present --- docs/configuration.asciidoc | 14 ++++++++------ .../Connections/AbstractConnection.php | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index 9cf7e2e4b..ca7ec6db0 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -17,12 +17,14 @@ The client accepts an array of hosts that you would like to connect to. Each va ---- $params = array(); $params['hosts'] = array ( - '192.168.1.1:9200', // IP + Port - '192.168.1.2', // Just IP - 'mydomain.server.com:9201', // Domain + Port - 'mydomain2.server.com', // Just Domain - 'https://localhost', // SSL to localhost - 'https://192.168.1.3:9200' // SSL to IP + Port + '192.168.1.1:9200', // IP + Port + '192.168.1.2', // Just IP + 'mydomain.server.com:9201', // Domain + Port + 'mydomain2.server.com', // Just Domain + 'https://localhost', // SSL to localhost + 'https://192.168.1.3:9200', // SSL to IP + Port + 'http://user:pass@localhost:9200', // HTTP Basic Auth + 'https://user:pass@localhost:9200', // SSL + HTTP Basic Auth ); $client = new Elasticsearch\Client($params); diff --git a/src/Elasticsearch/Connections/AbstractConnection.php b/src/Elasticsearch/Connections/AbstractConnection.php index 82d1d417b..14d31116a 100644 --- a/src/Elasticsearch/Connections/AbstractConnection.php +++ b/src/Elasticsearch/Connections/AbstractConnection.php @@ -93,6 +93,18 @@ public function __construct($hostDetails, $connectionParams, LoggerInterface $lo $this->transportSchema = $hostDetails['scheme']; } + if (isset($hostDetails['user']) && isset($hostDetails['pass'])) { + if (isset($connectionParams['auth'][0]) !== true) { + $connectionParams['auth'][0] = $hostDetails['user']; + } + if (isset($connectionParams['auth'][1]) !== true) { + $connectionParams['auth'][1] = $hostDetails['pass']; + } + if (isset($connectionParams['auth'][2]) !== true) { + $connectionParams['auth'][2] = 'Basic'; + } + } + $host = $this->transportSchema.'://'.$hostDetails['host'].':'.$hostDetails['port']; if (isset($hostDetails['path']) === true) { $host .= $hostDetails['path'];