From dd20e839e8afd9539e73c08e449a21087bbae480 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 15 Jun 2022 10:29:03 +0300 Subject: [PATCH 1/3] basicAuthentication support for ElasticsearchServiceProvider namespace order changed for phpcs namespace order changed for phpcs --- config/elasticsearch.php | 2 ++ src/ElasticSearch/Config/Storage.php | 16 ++++++++++++++++ src/ElasticSearchServiceProvider.php | 13 ++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 9a4a1dd9..9f9826b0 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -2,6 +2,8 @@ return [ 'host' => env('ELASTICSEARCH_HOST'), + 'user' => env('ELASTICSEARCH_USER'), + 'password' => env('ELASTICSEARCH_PASSWORD'), 'indices' => [ 'mappings' => [ 'default' => [ diff --git a/src/ElasticSearch/Config/Storage.php b/src/ElasticSearch/Config/Storage.php index ef10cd6a..182c252d 100644 --- a/src/ElasticSearch/Config/Storage.php +++ b/src/ElasticSearch/Config/Storage.php @@ -31,6 +31,22 @@ public function hosts(): array return explode(',', $this->loadConfig('host')); } + /** + * @return string + */ + public function user(): string + { + return $this->loadConfig('user'); + } + + /** + * @return string + */ + public function password(): string + { + return $this->loadConfig('password'); + } + /** * @param string $path * @return mixed diff --git a/src/ElasticSearchServiceProvider.php b/src/ElasticSearchServiceProvider.php index ddee3e7e..a1ace161 100644 --- a/src/ElasticSearchServiceProvider.php +++ b/src/ElasticSearchServiceProvider.php @@ -8,6 +8,8 @@ use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\ServiceProvider; use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config; +use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate; +use Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate; final class ElasticSearchServiceProvider extends ServiceProvider { @@ -19,12 +21,17 @@ public function register(): void $this->mergeConfigFrom(__DIR__.'/../config/elasticsearch.php', 'elasticsearch'); $this->app->bind(Client::class, function () { - return ClientBuilder::create()->setHosts(Config::hosts())->build(); + $clientBuilder = ClientBuilder::create()->setHosts(Config::hosts()); + if ($user = Config::user()) { + $clientBuilder->setBasicAuthentication($user, Config::password()); + } + + return $clientBuilder->build(); }); $this->app->bind( - 'Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate', - 'Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate' + HitsIteratorAggregate::class, + EloquentHitsIteratorAggregate::class ); } From ee71b0fd3dd9491504f51a14de3fa083af26356e Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 15 Jun 2022 10:36:40 +0300 Subject: [PATCH 2/3] fix for the method return type of the user and password method of config storage --- src/ElasticSearch/Config/Storage.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ElasticSearch/Config/Storage.php b/src/ElasticSearch/Config/Storage.php index 182c252d..5bcf29be 100644 --- a/src/ElasticSearch/Config/Storage.php +++ b/src/ElasticSearch/Config/Storage.php @@ -32,17 +32,17 @@ public function hosts(): array } /** - * @return string + * @return ?string */ - public function user(): string + public function user(): ?string { return $this->loadConfig('user'); } /** - * @return string + * @return ?string */ - public function password(): string + public function password(): ?string { return $this->loadConfig('password'); } From e7ab09e8a96b1d7b6a662ed51612df0b311084dc Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Thu, 16 Jun 2022 22:05:37 +0300 Subject: [PATCH 3/3] elastic cloud provider connection credential implemented and config tests improved --- config/elasticsearch.php | 2 + src/ElasticSearch/Config/Config.php | 4 ++ src/ElasticSearch/Config/Storage.php | 16 +++++++ src/ElasticSearchServiceProvider.php | 5 ++ .../Unit/ElasticSearch/Config/ConfigTest.php | 48 +++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 tests/Unit/ElasticSearch/Config/ConfigTest.php diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 9f9826b0..58c3a91b 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -4,6 +4,8 @@ 'host' => env('ELASTICSEARCH_HOST'), 'user' => env('ELASTICSEARCH_USER'), 'password' => env('ELASTICSEARCH_PASSWORD'), + 'cloud_id' => env('ELASTICSEARCH_CLOUD_ID'), + 'api_key' => env('ELASTICSEARCH_API_KEY'), 'indices' => [ 'mappings' => [ 'default' => [ diff --git a/src/ElasticSearch/Config/Config.php b/src/ElasticSearch/Config/Config.php index cb2de016..f892eefd 100644 --- a/src/ElasticSearch/Config/Config.php +++ b/src/ElasticSearch/Config/Config.php @@ -4,6 +4,10 @@ /** * @method static array hosts() + * @method static user() + * @method static password() + * @method static elasticCloudId() + * @method static apiKey() */ class Config { diff --git a/src/ElasticSearch/Config/Storage.php b/src/ElasticSearch/Config/Storage.php index 5bcf29be..d583c5c9 100644 --- a/src/ElasticSearch/Config/Storage.php +++ b/src/ElasticSearch/Config/Storage.php @@ -47,6 +47,22 @@ public function password(): ?string return $this->loadConfig('password'); } + /** + * @return ?string + */ + public function elasticCloudId(): ?string + { + return $this->loadConfig('cloud_id'); + } + + /** + * @return ?string + */ + public function apiKey(): ?string + { + return $this->loadConfig('api_key'); + } + /** * @param string $path * @return mixed diff --git a/src/ElasticSearchServiceProvider.php b/src/ElasticSearchServiceProvider.php index a1ace161..6fb5dd87 100644 --- a/src/ElasticSearchServiceProvider.php +++ b/src/ElasticSearchServiceProvider.php @@ -26,6 +26,11 @@ public function register(): void $clientBuilder->setBasicAuthentication($user, Config::password()); } + if ($cloudId = Config::elasticCloudId()) { + $clientBuilder->setElasticCloudId($cloudId) + ->setApiKey(Config::apiKey()); + } + return $clientBuilder->build(); }); diff --git a/tests/Unit/ElasticSearch/Config/ConfigTest.php b/tests/Unit/ElasticSearch/Config/ConfigTest.php new file mode 100644 index 00000000..d45ce9e7 --- /dev/null +++ b/tests/Unit/ElasticSearch/Config/ConfigTest.php @@ -0,0 +1,48 @@ +assertEquals(['http://localhost:9200'], $config::hosts()); + } + + public function test_parse_multihost(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + + $config = new ScoutConfig(); + $this->assertEquals(['http://localhost:9200', 'http://localhost:9201'], $config::hosts()); + } + + public function test_parse_username_password(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + Config::set('elasticsearch.user', 'elastic'); + Config::set('elasticsearch.password', 'pass'); + + $config = new ScoutConfig(); + $this->assertEquals('elastic', $config::user()); + $this->assertEquals('pass', $config::password()); + } + + public function test_parse_elastic_cloud_id(): void + { + Config::set('elasticsearch.host', 'http://localhost:9200,http://localhost:9201'); + Config::set('elasticsearch.cloud_id', 'cloud-id'); + Config::set('elasticsearch.api_key', '123456'); + + $config = new ScoutConfig(); + $this->assertEquals('cloud-id', $config::elasticCloudId()); + $this->assertEquals('123456', $config::apiKey()); + } +}