Skip to content

Commit 3e1c451

Browse files
authored
Merge pull request #28 from ensi-platform/task-105513-v8
Поддержка старого варианта авторизации в v8
2 parents 0607f42 + 2f45b63 commit 3e1c451

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ Hosts should be comma seperated string of hosts with protocol prefix and port su
278278
ELASTICSEARCH_SSL_VERIFICATION=true,
279279
```
280280

281+
## Elasticsearch 7 and 8 support.
282+
283+
Due to the incompatibility of clients for Elasticsearch 7 and 8, separate releases will be created for these versions.
284+
Development for each version is carried out in the corresponding branch.
285+
286+
To make changes to version 7, you need to create a task branch based on v7 and make a pull request to it.
287+
For version 8 it is similar, but based on the v8 branch.
288+
281289
## Contributing
282290

283291
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

src/ElasticClient.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,34 @@ public function getQueryLog(): Collection
128128

129129
public static function fromConfig(array $config): static
130130
{
131-
$client = (new ClientBuilder())
131+
$builder = (new ClientBuilder())
132132
->setHosts($config['hosts'])
133-
->setBasicAuthentication($config['username'] ?? '', $config['password'] ?? '')
134133
->setRetries($config['retries'] ?? 1)
135-
->setSSLVerification($config['ssl_verification'] ?? false)
136-
->build();
134+
->setSSLVerification($config['ssl_verification'] ?? false);
137135

138-
return new static($client);
136+
[$username, $password] = static::resolveBasicAuthData($config);
137+
138+
if (filled($username)) {
139+
$builder->setBasicAuthentication($username, $password);
140+
}
141+
142+
return new static($builder->build());
143+
}
144+
145+
public static function resolveBasicAuthData(array $config): array
146+
{
147+
if (filled($config['username'] ?? null)) {
148+
return [$config['username'], $config['password'] ?? ''];
149+
}
150+
151+
foreach ($config['hosts'] as $host) {
152+
$components = parse_url($host);
153+
154+
if (filled($components['user'] ?? null)) {
155+
return [$components['user'], $components['pass'] ?? ''];
156+
}
157+
}
158+
159+
return ['', ''];
139160
}
140161
}

tests/Unit/ElasticClientTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Tests\Unit;
4+
5+
use Ensi\LaravelElasticQuery\ElasticClient;
6+
7+
class ElasticClientTest extends UnitTestCase
8+
{
9+
/**
10+
* @dataProvider provideBasicAuthData
11+
*/
12+
public function testResolveBasicAuthData(array $config, array $expected): void
13+
{
14+
$this->assertEquals($expected, ElasticClient::resolveBasicAuthData($config));
15+
}
16+
17+
public function provideBasicAuthData(): array
18+
{
19+
return [
20+
'separate username and password' => [
21+
[
22+
'hosts' => ['https://elastic.domain.io:9200'],
23+
'username' => 'foo',
24+
'password' => 'bar',
25+
],
26+
['foo', 'bar'],
27+
],
28+
'separate username without password' => [
29+
[
30+
'hosts' => ['https://elastic.domain.io:9200'],
31+
'username' => 'foo',
32+
],
33+
['foo', ''],
34+
],
35+
'username and password in the host' => [
36+
['hosts' => ['https://elastic1.domain.io:9200', 'https://foo:bar@elastic2.domain.io:9200']],
37+
['foo', 'bar'],
38+
],
39+
'only username in the host' => [
40+
['hosts' => ['https://foo@elastic1.domain.io:9200', 'https://elastic2.domain.io:9200']],
41+
['foo', ''],
42+
],
43+
'missing auth data' => [
44+
['hosts' => ['https://elastic.domain.io:9200']],
45+
['', ''],
46+
],
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)