Skip to content

Commit 2f45b63

Browse files
committed
#105513 Определение логина/пароля для авторизации
1 parent 29d0bd1 commit 2f45b63

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ public static function resolveBasicAuthData(array $config): array
148148
return [$config['username'], $config['password'] ?? ''];
149149
}
150150

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+
151159
return ['', ''];
152160
}
153161
}

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)