From d0a9fd4e8d3a7b7d37ff0bce2b787939186b422e Mon Sep 17 00:00:00 2001 From: James Taylor Date: Thu, 30 Jan 2025 15:23:27 -0600 Subject: [PATCH] Allow for authentication with api key --- README.md | 1 + config/laravel-elastic-query.php | 1 + src/ElasticClient.php | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 664f8f0..2ec7fcc 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,7 @@ Hosts should be comma seperated string of hosts with protocol prefix and port su ELASTICSEARCH_USERNAME=admin ELASTICSEARCH_PASSWORD=admin ELASTICSEARCH_SSL_VERIFICATION=true, + ELASTICSEARCH_API_KEY= ``` ## Async Usage diff --git a/config/laravel-elastic-query.php b/config/laravel-elastic-query.php index 421ea5e..820bc03 100644 --- a/config/laravel-elastic-query.php +++ b/config/laravel-elastic-query.php @@ -11,6 +11,7 @@ 'username' => env('ELASTICSEARCH_USERNAME', ''), 'password' => env('ELASTICSEARCH_PASSWORD', ''), 'ssl_verification' => env('ELASTICSEARCH_SSL_VERIFICATION', false), + 'api_key' => env('ELASTICSEARCH_API_KEY', ''), 'http_client' => null, // class implementing the \Psr\Http\Client\ClientInterface 'http_client_options' => null, // for call_user_func_array diff --git a/src/ElasticClient.php b/src/ElasticClient.php index 800d241..4a7cb83 100644 --- a/src/ElasticClient.php +++ b/src/ElasticClient.php @@ -205,6 +205,10 @@ public static function fromConfig(array $config): static $builder->setBasicAuthentication($username, $password); } + if (filled($config['api_key']) ?? null) { + $builder->setApiKey($config['api_key']); + } + if (filled($config['http_client_logger'] ?? null)) { $logger = call_user_func_array($config['http_client_logger'], []);