Skip to content

Commit 4f98616

Browse files
ENGCOM-5861: Fix ElasticSearch issue over secure connection #24636
- Merge Pull Request #24636 from ihor-sviziev/magento2:elasticsearch-fix-secure-connection-issues - Merged commits: 1. b5bc848 2. dcdf364 3. 2c46bd5 4. e6adf50 5. f9f5d12
2 parents 68d2137 + f9f5d12 commit 4f98616

File tree

5 files changed

+169
-18
lines changed

5 files changed

+169
-18
lines changed

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Client/Elasticsearch.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,28 @@ public function testConnection()
108108
*/
109109
private function buildConfig($options = [])
110110
{
111-
$host = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
111+
$hostname = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
112112
// @codingStandardsIgnoreStart
113113
$protocol = parse_url($options['hostname'], PHP_URL_SCHEME);
114114
// @codingStandardsIgnoreEnd
115115
if (!$protocol) {
116116
$protocol = 'http';
117117
}
118-
if (!empty($options['port'])) {
119-
$host .= ':' . $options['port'];
118+
119+
$authString = '';
120+
if (!empty($options['enableAuth']) && (int)$options['enableAuth'] === 1) {
121+
$authString = "{$options['username']}:{$options['password']}@";
120122
}
121-
if (!empty($options['enableAuth']) && ($options['enableAuth'] == 1)) {
122-
$host = sprintf('%s://%s:%s@%s', $protocol, $options['username'], $options['password'], $host);
123+
124+
$portString = '';
125+
if (!empty($options['port'])) {
126+
$portString = ':' . $options['port'];
123127
}
124128

129+
$host = $protocol . '://' . $authString . $hostname . $portString;
130+
125131
$options['hosts'] = [$host];
132+
126133
return $options;
127134
}
128135

app/code/Magento/Elasticsearch/Model/Client/Elasticsearch.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,28 @@ public function testConnection()
103103
*/
104104
private function buildConfig($options = [])
105105
{
106-
$host = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
106+
$hostname = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
107107
// @codingStandardsIgnoreStart
108108
$protocol = parse_url($options['hostname'], PHP_URL_SCHEME);
109109
// @codingStandardsIgnoreEnd
110110
if (!$protocol) {
111111
$protocol = 'http';
112112
}
113-
if (!empty($options['port'])) {
114-
$host .= ':' . $options['port'];
113+
114+
$authString = '';
115+
if (!empty($options['enableAuth']) && (int)$options['enableAuth'] === 1) {
116+
$authString = "{$options['username']}:{$options['password']}@";
115117
}
116-
if (!empty($options['enableAuth']) && ($options['enableAuth'] == 1)) {
117-
$host = sprintf('%s://%s:%s@%s', $protocol, $options['username'], $options['password'], $host);
118+
119+
$portString = '';
120+
if (!empty($options['port'])) {
121+
$portString = ':' . $options['port'];
118122
}
119123

124+
$host = $protocol . '://' . $authString . $hostname . $portString;
125+
120126
$options['hosts'] = [$host];
127+
121128
return $options;
122129
}
123130

app/code/Magento/Elasticsearch/Test/Unit/Elasticsearch5/Model/Client/ElasticsearchTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch\Test\Unit\Elasticsearch5\Model\Client;
78

9+
use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch;
810
use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
911
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1012

@@ -38,7 +40,7 @@ class ElasticsearchTest extends \PHPUnit\Framework\TestCase
3840
*
3941
* @return void
4042
*/
41-
protected function setUp()
43+
protected function setUp(): void
4244
{
4345
$this->elasticsearchClientMock = $this->getMockBuilder(\Elasticsearch\Client::class)
4446
->setMethods(
@@ -497,6 +499,40 @@ public function testDeleteMapping()
497499
);
498500
}
499501

502+
/**
503+
* Ensure that configuration returns correct url.
504+
*
505+
* @param array $options
506+
* @param string $expectedResult
507+
* @throws \Magento\Framework\Exception\LocalizedException
508+
* @throws \ReflectionException
509+
* @dataProvider getOptionsDataProvider
510+
*/
511+
public function testBuildConfig(array $options, $expectedResult): void
512+
{
513+
$buildConfig = new Elasticsearch($options);
514+
$config = $this->getPrivateMethod(Elasticsearch::class, 'buildConfig');
515+
$result = $config->invoke($buildConfig, $options);
516+
$this->assertEquals($expectedResult, $result['hosts'][0]);
517+
}
518+
519+
/**
520+
* Return private method for elastic search class.
521+
*
522+
* @param $className
523+
* @param $methodName
524+
* @return \ReflectionMethod
525+
* @throws \ReflectionException
526+
*/
527+
private function getPrivateMethod($className, $methodName)
528+
{
529+
$reflector = new \ReflectionClass($className);
530+
$method = $reflector->getMethod($methodName);
531+
$method->setAccessible(true);
532+
533+
return $method;
534+
}
535+
500536
/**
501537
* Test deleteMapping() method
502538
* @expectedException \Exception
@@ -545,6 +581,35 @@ public function testSuggest()
545581
$this->assertEquals([], $this->model->suggest($query));
546582
}
547583

584+
/**
585+
* Get options data provider.
586+
*/
587+
public function getOptionsDataProvider()
588+
{
589+
return [
590+
[
591+
'without_protocol' => [
592+
'hostname' => 'localhost',
593+
'port' => '9200',
594+
'timeout' => 15,
595+
'index' => 'magento2',
596+
'enableAuth' => 0,
597+
],
598+
'expected_result' => 'http://localhost:9200'
599+
],
600+
[
601+
'with_protocol' => [
602+
'hostname' => 'https://localhost',
603+
'port' => '9200',
604+
'timeout' => 15,
605+
'index' => 'magento2',
606+
'enableAuth' => 0,
607+
],
608+
'expected_result' => 'https://localhost:9200'
609+
]
610+
];
611+
}
612+
548613
/**
549614
* Get elasticsearch client options
550615
*

app/code/Magento/Elasticsearch6/Model/Client/Elasticsearch.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,28 @@ public function testConnection()
103103
*/
104104
private function buildConfig($options = [])
105105
{
106-
$host = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
106+
$hostname = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
107107
// @codingStandardsIgnoreStart
108108
$protocol = parse_url($options['hostname'], PHP_URL_SCHEME);
109109
// @codingStandardsIgnoreEnd
110110
if (!$protocol) {
111111
$protocol = 'http';
112112
}
113-
if (!empty($options['port'])) {
114-
$host .= ':' . $options['port'];
113+
114+
$authString = '';
115+
if (!empty($options['enableAuth']) && (int)$options['enableAuth'] === 1) {
116+
$authString = "{$options['username']}:{$options['password']}@";
115117
}
116-
if (!empty($options['enableAuth']) && ($options['enableAuth'] == 1)) {
117-
$host = sprintf('%s://%s:%s@%s', $protocol, $options['username'], $options['password'], $host);
118+
119+
$portString = '';
120+
if (!empty($options['port'])) {
121+
$portString = ':' . $options['port'];
118122
}
119123

124+
$host = $protocol . '://' . $authString . $hostname . $portString;
125+
120126
$options['hosts'] = [$host];
127+
121128
return $options;
122129
}
123130

app/code/Magento/Elasticsearch6/Test/Unit/Model/Client/ElasticsearchTest.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch6\Test\Unit\Model\Client;
78

89
use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
11+
use Magento\Elasticsearch6\Model\Client\Elasticsearch;
1012

1113
/**
1214
* Class ElasticsearchTest
@@ -83,7 +85,7 @@ protected function setUp()
8385

8486
$this->objectManager = new ObjectManagerHelper($this);
8587
$this->model = $this->objectManager->getObject(
86-
\Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
88+
Elasticsearch::class,
8789
[
8890
'options' => $this->getOptions(),
8991
'elasticsearchClient' => $this->elasticsearchClientMock
@@ -97,7 +99,7 @@ protected function setUp()
9799
public function testConstructorOptionsException()
98100
{
99101
$result = $this->objectManager->getObject(
100-
\Magento\Elasticsearch6\Model\Client\Elasticsearch::class,
102+
Elasticsearch::class,
101103
[
102104
'options' => []
103105
]
@@ -119,6 +121,69 @@ public function testConstructorWithOptions()
119121
$this->assertNotNull($result);
120122
}
121123

124+
/**
125+
* Ensure that configuration returns correct url.
126+
*
127+
* @param array $options
128+
* @param string $expectedResult
129+
* @throws \Magento\Framework\Exception\LocalizedException
130+
* @throws \ReflectionException
131+
* @dataProvider getOptionsDataProvider
132+
*/
133+
public function testBuildConfig(array $options, $expectedResult): void
134+
{
135+
$buildConfig = new Elasticsearch($options);
136+
$config = $this->getPrivateMethod(Elasticsearch::class, 'buildConfig');
137+
$result = $config->invoke($buildConfig, $options);
138+
$this->assertEquals($expectedResult, $result['hosts'][0]);
139+
}
140+
141+
/**
142+
* Return private method for elastic search class.
143+
*
144+
* @param $className
145+
* @param $methodName
146+
* @return \ReflectionMethod
147+
* @throws \ReflectionException
148+
*/
149+
private function getPrivateMethod($className, $methodName)
150+
{
151+
$reflector = new \ReflectionClass($className);
152+
$method = $reflector->getMethod($methodName);
153+
$method->setAccessible(true);
154+
155+
return $method;
156+
}
157+
158+
/**
159+
* Get options data provider.
160+
*/
161+
public function getOptionsDataProvider()
162+
{
163+
return [
164+
[
165+
'without_protocol' => [
166+
'hostname' => 'localhost',
167+
'port' => '9200',
168+
'timeout' => 15,
169+
'index' => 'magento2',
170+
'enableAuth' => 0,
171+
],
172+
'expected_result' => 'http://localhost:9200'
173+
],
174+
[
175+
'with_protocol' => [
176+
'hostname' => 'https://localhost',
177+
'port' => '9200',
178+
'timeout' => 15,
179+
'index' => 'magento2',
180+
'enableAuth' => 0,
181+
],
182+
'expected_result' => 'https://localhost:9200'
183+
]
184+
];
185+
}
186+
122187
/**
123188
* Test ping functionality
124189
*/

0 commit comments

Comments
 (0)