File tree Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -12,9 +12,11 @@ services:
1212 - elasticsearch
1313
1414 elasticsearch :
15- image : " docker. elastic.co /elasticsearch/elasticsearch-oss:7 .4.0 "
15+ image : ' elastic/elasticsearch:8 .4.2 '
1616 environment :
1717 - " discovery.type=${ES_DISCOVERY_TYPE:-single-node}"
1818 - " ES_JAVA_OPTS=${ES_JAVA_OPTS:--Xms512m -Xmx512m}"
19+ - xpack.security.enabled=true
20+ - ELASTIC_PASSWORD=mySecretPassword
1921 ports :
2022 - " 9200:9200"
Original file line number Diff line number Diff line change 66
77use Amp \Http \Client \HttpClient ;
88use Amp \Http \Client \HttpClientBuilder ;
9+ use Amp \Http \Client \Interceptor \SetRequestHeaderIfUnset ;
910use Amp \Http \Client \Request ;
1011use Amp \Http \Client \Response ;
11- use function Amp \call ;
1212use Amp \Promise ;
13+ use function Amp \call ;
1314
1415class Client
1516{
@@ -22,9 +23,16 @@ class Client
2223 */
2324 private $ httpClient ;
2425
25- public function __construct (string $ baseUri )
26+ public function __construct (string $ baseUri, ? string $ username = null , ? string $ password = null )
2627 {
2728 $ this ->httpClient = HttpClientBuilder::buildDefault ();
29+
30+ if ($ username && $ password ) {
31+ $ authHeader = base64_encode ("$ username: $ password " );
32+ $ this ->httpClient = (new HttpClientBuilder ())
33+ ->intercept (new SetRequestHeaderIfUnset ('Authorization ' , 'Basic ' . $ authHeader ))
34+ ->build ();
35+ }
2836 $ this ->baseUri = rtrim ($ baseUri , '/ ' );
2937 }
3038
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ class ClientTest extends TestCase
1313{
1414 const TEST_INDEX = 'test_index ' ;
1515 const DEFAULT_ES_URL = 'http://127.0.0.1:9200 ' ;
16+ const DEFAULT_ES_USER = 'elastic ' ;
17+ const DEFAULT_ES_PASSWORD = 'mySecretPassword ' ;
1618
1719 /**
1820 * @var Client
@@ -22,7 +24,9 @@ class ClientTest extends TestCase
2224 protected function setUp (): void
2325 {
2426 $ esUrl = getenv ('ES_URL ' ) ?: self ::DEFAULT_ES_URL ;
25- $ this ->client = new Client ($ esUrl );
27+ $ esUser = getenv ('ES_USER ' ) ?: self ::DEFAULT_ES_USER ;
28+ $ esPassword = getenv ('ES_PASSWORD ' ) ?: self ::DEFAULT_ES_PASSWORD ;
29+ $ this ->client = new Client ($ esUrl , $ esUser , $ esPassword );
2630 $ indices = Promise \wait ($ this ->client ->catIndices ());
2731 foreach ($ indices as $ index ) {
2832 Promise \wait ($ this ->client ->deleteIndex ($ index ['index ' ]));
You can’t perform that action at this time.
0 commit comments