Skip to content

Commit 5c1d269

Browse files
authored
MCLOUD-7695: Add possibility to use authentication for ES (#44)
1 parent 03a9008 commit 5c1d269

File tree

6 files changed

+157
-5
lines changed

6 files changed

+157
-5
lines changed

src/Config/SearchEngine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ private function getElasticSearchConfiguration(array $config): array
193193
"{$engine}_server_port" => $config['port'],
194194
];
195195

196+
if ($this->elasticSearch->isAuthEnabled()) {
197+
$elasticSearchConfig["{$engine}_enable_auth"] = 1;
198+
$elasticSearchConfig["{$engine}_username"] = $config['username'];
199+
$elasticSearchConfig["{$engine}_password"] = $config['password'];
200+
}
201+
196202
if (isset($config['query']['index'])) {
197203
$elasticSearchConfig["{$engine}_index_prefix"] = $config['query']['index'];
198204
}

src/Service/ElasticSearch.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ public function getPort(): string
146146
return (string)$this->getConfiguration()['port'];
147147
}
148148

149+
/**
150+
* Checks if authentication is enabled: password and username exists in configuration
151+
*
152+
* @return bool
153+
*/
154+
public function isAuthEnabled(): bool
155+
{
156+
return !empty($this->getConfiguration()['password']) && !empty($this->getConfiguration()['username']);
157+
}
158+
159+
/**
160+
* Returns additional options for request to elasticsearch
161+
*
162+
* @return array|array[]
163+
*/
164+
private function getRequestOptions(): array
165+
{
166+
if (!$this->isAuthEnabled()) {
167+
return [];
168+
}
169+
170+
return [
171+
'auth' => [
172+
$this->getConfiguration()['username'],
173+
$this->getConfiguration()['password']
174+
]
175+
];
176+
}
177+
149178
/**
150179
* Return full version with engine name.
151180
*
@@ -200,7 +229,7 @@ public function getTemplate(): array
200229
*/
201230
private function call(string $endpoint): array
202231
{
203-
$response = $this->clientFactory->create()->get($endpoint);
232+
$response = $this->clientFactory->create()->get($endpoint, $this->getRequestOptions());
204233
$templates = $response->getBody()->getContents();
205234

206235
return json_decode($templates, true);

src/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ private function getEsOptions(): array
237237
$options['--search-engine'] = $this->elasticSearch->getFullVersion();
238238
$options['--elasticsearch-host'] = $this->elasticSearch->getHost();
239239
$options['--elasticsearch-port'] = $this->elasticSearch->getPort();
240+
241+
$esConfig = $this->elasticSearch->getConfiguration();
242+
if ($this->elasticSearch->isAuthEnabled()) {
243+
$options['--elasticsearch-enable-auth'] = '1';
244+
$options['--elasticsearch-username'] = $esConfig['username'];
245+
$options['--elasticsearch-password'] = $esConfig['password'];
246+
}
247+
248+
if (isset($esConfig['query']['index'])) {
249+
$options['--elasticsearch-index-prefix'] = $esConfig['query']['index'];
250+
}
240251
}
241252

242253
/**

src/Test/Unit/Config/SearchEngineTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MagentoCloud\Config\Stage\DeployInterface;
1515
use Magento\MagentoCloud\Package\MagentoVersion;
1616
use Magento\MagentoCloud\Service\ElasticSearch;
17+
use Magento\MagentoCloud\Service\ServiceException;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
1920

@@ -112,13 +113,16 @@ public function getWhenCustomConfigValidWithoutMergeDataProvider(): array
112113
* @param array $customSearchConfig
113114
* @param array $esServiceConfig
114115
* @param array $expected
116+
* @param bool $authEnabled
115117
*
118+
* @throws ServiceException
116119
* @dataProvider testGetWithElasticSearchDataProvider
117120
*/
118121
public function testGetWithElasticSearch(
119122
array $customSearchConfig,
120123
array $esServiceConfig,
121-
array $expected
124+
array $expected,
125+
bool $authEnabled = false
122126
): void {
123127
$this->stageConfigMock->expects($this->once())
124128
->method('get')
@@ -127,6 +131,9 @@ public function testGetWithElasticSearch(
127131
$this->elasticSearchMock->expects($this->once())
128132
->method('getConfiguration')
129133
->willReturn($esServiceConfig);
134+
$this->elasticSearchMock->expects($this->once())
135+
->method('isAuthEnabled')
136+
->willReturn($authEnabled);
130137

131138
$expected = ['system' => ['default' => ['catalog' => ['search' => $expected]]]];
132139

@@ -219,6 +226,24 @@ public function testGetWithElasticSearchDataProvider(): array
219226
'elasticsearch_server_port' => 1234,
220227
],
221228
],
229+
[
230+
'customSearchConfig' => [],
231+
'esServiceConfig' => [
232+
'host' => 'localhost',
233+
'port' => 1234,
234+
'password' => 'secret',
235+
'username' => 'user',
236+
],
237+
'expected' => [
238+
'engine' => 'elasticsearch',
239+
'elasticsearch_server_hostname' => 'localhost',
240+
'elasticsearch_server_port' => 1234,
241+
'elasticsearch_enable_auth' => 1,
242+
'elasticsearch_username' => 'user',
243+
'elasticsearch_password' => 'secret',
244+
],
245+
true
246+
],
222247
$generateDataForVersionChecking('elasticsearch'),
223248
$generateDataForVersionChecking('elasticsearch'),
224249
];

src/Test/Unit/Service/ElasticSearchTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testGetVersion(array $esRelationship, string $esConfiguration, s
9696
->willReturn($clientMock);
9797
$clientMock->expects($this->once())
9898
->method('get')
99-
->with($esConfig['host'] . ':' . $esConfig['port'])
99+
->with($esConfig['host'] . ':' . $esConfig['port'], ['auth' => ['user', 'secret']])
100100
->willReturn($responseMock);
101101
$responseMock->expects($this->once())
102102
->method('getBody')
@@ -119,6 +119,8 @@ public function getVersionDataProvider(): array
119119
[
120120
'host' => '127.0.0.1',
121121
'port' => '1234',
122+
'username' => 'user',
123+
'password' => 'secret'
122124
],
123125
];
124126

@@ -293,7 +295,7 @@ public function testGetVersionWithException(): void
293295

294296
public function testGetTemplate(): void
295297
{
296-
$this->environmentMock->expects($this->once())
298+
$this->environmentMock->expects($this->any())
297299
->method('getRelationship')
298300
->with('elasticsearch')
299301
->willReturn([
@@ -357,7 +359,7 @@ public function testGetTemplateNoConfig(): void
357359

358360
public function testGetTemplateWithException(): void
359361
{
360-
$this->environmentMock->expects($this->once())
362+
$this->environmentMock->expects($this->any())
361363
->method('getRelationship')
362364
->with('elasticsearch')
363365
->willReturn(
@@ -402,4 +404,41 @@ public function testIsInstalled(): void
402404
$this->assertTrue($this->elasticSearch->isInstalled());
403405
$this->assertFalse($this->elasticSearch->isInstalled());
404406
}
407+
408+
public function testAuthEnabledTrue()
409+
{
410+
$this->environmentMock->expects($this->exactly(2))
411+
->method('getRelationship')
412+
->with('elasticsearch')
413+
->willReturn(
414+
[
415+
[
416+
'host' => '127.0.0.1',
417+
'port' => '1234',
418+
'username' => 'test',
419+
'password' => 'secret',
420+
],
421+
]
422+
);
423+
424+
$this->assertTrue($this->elasticSearch->isAuthEnabled());
425+
}
426+
427+
public function testAuthEnabledFalse()
428+
{
429+
$this->environmentMock->expects($this->exactly(1))
430+
->method('getRelationship')
431+
->with('elasticsearch')
432+
->willReturn(
433+
[
434+
[
435+
'host' => '127.0.0.1',
436+
'port' => '1234',
437+
'password' => '',
438+
],
439+
]
440+
);
441+
442+
$this->assertFalse($this->elasticSearch->isAuthEnabled());
443+
}
405444
}

src/Test/Unit/Step/Deploy/InstallUpdate/Install/Setup/InstallCommandFactoryTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,46 @@ public function testExecuteWithRemoteStorageWithException(): void
412412

413413
$this->installCommandFactory->create();
414414
}
415+
416+
public function testExecuteWithESauthOptions(): void
417+
{
418+
$this->mockBaseConfig('', '', '', '', '', '');
419+
$this->magentoVersionMock->method('isGreaterOrEqual')
420+
->willReturnMap([
421+
['2.4.0', true],
422+
['2.4.2', false]
423+
]);
424+
$this->elasticSearchMock->expects($this->once())
425+
->method('isInstalled')
426+
->willReturn(true);
427+
$this->elasticSearchMock->expects($this->once())
428+
->method('isAuthEnabled')
429+
->willReturn(true);
430+
$this->elasticSearchMock->expects($this->once())
431+
->method('getFullVersion')
432+
->willReturn('7.7');
433+
$this->elasticSearchMock->expects($this->once())
434+
->method('getHost')
435+
->willReturn('127.0.0.1');
436+
$this->elasticSearchMock->expects($this->once())
437+
->method('getPort')
438+
->willReturn('1234');
439+
$this->elasticSearchMock->expects($this->once())
440+
->method('getConfiguration')
441+
->willReturn([
442+
'host' => '127.0.0.1',
443+
'port' => '1234',
444+
'username' => 'user',
445+
'password' => 'secret',
446+
'query' => [
447+
'index' => 'test'
448+
]
449+
]);
450+
451+
$command = $this->installCommandFactory->create();
452+
self::assertContains("--elasticsearch-enable-auth='1'", $command);
453+
self::assertContains("--elasticsearch-username='user'", $command);
454+
self::assertContains("--elasticsearch-password='secret'", $command);
455+
self::assertContains("--elasticsearch-index-prefix='test'", $command);
456+
}
415457
}

0 commit comments

Comments
 (0)