Skip to content

Commit

Permalink
Merge pull request #210 from hkulekci/password-credential-support-for…
Browse files Browse the repository at this point in the history
…-provider

basicAuthentication support for ElasticsearchServiceProvider
  • Loading branch information
matchish authored Jun 16, 2022
2 parents 2677c4d + e7ab09e commit fa7be59
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

return [
'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' => [
Expand Down
4 changes: 4 additions & 0 deletions src/ElasticSearch/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

/**
* @method static array hosts()
* @method static user()
* @method static password()
* @method static elasticCloudId()
* @method static apiKey()
*/
class Config
{
Expand Down
32 changes: 32 additions & 0 deletions src/ElasticSearch/Config/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ 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');
}

/**
* @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
Expand Down
18 changes: 15 additions & 3 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -19,12 +21,22 @@ 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());
}

if ($cloudId = Config::elasticCloudId()) {
$clientBuilder->setElasticCloudId($cloudId)
->setApiKey(Config::apiKey());
}

return $clientBuilder->build();
});

$this->app->bind(
'Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate',
'Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate'
HitsIteratorAggregate::class,
EloquentHitsIteratorAggregate::class
);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/ElasticSearch/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Unit\ElasticSearch\Config;

use Illuminate\Support\Facades\Config;
use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config as ScoutConfig;
use Tests\TestCase;

class ConfigTest extends TestCase
{
public function test_parse_host(): void
{
Config::set('elasticsearch.host', 'http://localhost:9200');

$config = new ScoutConfig();
$this->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());
}
}

0 comments on commit fa7be59

Please sign in to comment.