Skip to content

Commit ce9f632

Browse files
committed
Merge pull request #40 from robertlemke/http-basic-auth-bugfix
[BUGFIX] Request drops username / password in some cases This change makes the RequestService more resilient for some cases when the HTTP Request sent to Elasticsearch did not contain the username and password anymore, even though it has been configured in the settings. While the root cause is the Http\Request::create() method in Flow, this change at least provides a workaround which allows for connecting with Elasticsearch instances protected with HTTP Basic Auth. Also circumvents a rare case where the content of a PUT request contains "null" as a string.
2 parents 82ddab0 + dfdc26c commit ce9f632

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Classes/Flowpack/ElasticSearch/Transfer/RequestService.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
* source code.
1212
*/
1313

14+
use Flowpack\ElasticSearch\Domain\Model\Client\ClientConfiguration;
1415
use TYPO3\Flow\Annotations as Flow;
1516
use TYPO3\Flow\Http\Client\CurlEngine;
17+
use TYPO3\Flow\Http\Request;
1618

1719
/**
1820
* Handles the requests
@@ -63,21 +65,27 @@ public function request($method, \Flowpack\ElasticSearch\Domain\Model\Client $cl
6365
{
6466
$clientConfigurations = $client->getClientConfigurations();
6567
$clientConfiguration = $clientConfigurations[0];
68+
/** @var ClientConfiguration $clientConfiguration */
6669

6770
$uri = clone $clientConfiguration->getUri();
6871
if ($path !== null) {
6972
$uri->setPath($uri->getPath() . $path);
7073
}
7174

75+
$request = Request::create($uri, $method, $arguments, array(), array());
76+
// In some cases, $content will contain "null" as a string. Better be safe and handle this weird case:
77+
if ($content === 'null') {
78+
$request->setContent(null);
79+
} else {
80+
$request->setContent((is_array($content) ? json_encode($content) : $content));
81+
}
7282
if ($uri->getUsername()) {
73-
$requestEngine = $this->browser->getRequestEngine();
74-
$requestEngine->setOption(CURLOPT_USERPWD, $uri->getUsername() . ':' . $uri->getPassword() );
75-
$requestEngine->setOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
83+
$requestUri = $request->getUri();
84+
$requestUri->setUsername($uri->getUsername());
85+
$requestUri->setPassword($uri->getPassword());
7686
}
7787

78-
$response = $this->browser->request($uri, $method, $arguments, array(), array(),
79-
is_array($content) ? json_encode($content) : $content);
80-
88+
$response = $this->browser->sendRequest($request);
8189
return new Response($response, $this->browser->getLastRequest());
8290
}
8391
}

Documentation/Index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ and password in your client settings::
4141
clients:
4242
# default bundle that will be used if no more specific bundle name was supplied.
4343
default:
44-
- host: localhost
45-
port: 9200
44+
- host: my.elasticsearch-service.com
45+
port: 443
46+
scheme: https
4647
username: john
4748
password: mysecretpassword
4849

0 commit comments

Comments
 (0)