Skip to content

Commit defa33a

Browse files
MCLOUD-7694: Add possibility to use authentication for Redis/Elasticache (#42)
1 parent 5c1d269 commit defa33a

File tree

10 files changed

+152
-57
lines changed

10 files changed

+152
-57
lines changed

src/Config/Factory/Cache.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\MagentoCloud\Config\Factory;
99

10+
use Magento\MagentoCloud\Config\ConfigException;
1011
use Magento\MagentoCloud\Config\ConfigMerger;
1112
use Magento\MagentoCloud\Config\Stage\DeployInterface;
1213
use Magento\MagentoCloud\Service\Redis;
@@ -20,18 +21,18 @@ class Cache
2021
/**
2122
* Redis database to store default cache data
2223
*/
23-
const REDIS_DATABASE_DEFAULT = 1;
24+
public const REDIS_DATABASE_DEFAULT = 1;
2425

2526
/**
2627
* Redis database to store page cache data
2728
*/
28-
const REDIS_DATABASE_PAGE_CACHE = 2;
29+
public const REDIS_DATABASE_PAGE_CACHE = 2;
2930

30-
const REDIS_BACKEND_CM_CACHE = 'Cm_Cache_Backend_Redis';
31-
const REDIS_BACKEND_REDIS_CACHE = '\Magento\Framework\Cache\Backend\Redis';
32-
const REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE = '\Magento\Framework\Cache\Backend\RemoteSynchronizedCache';
31+
public const REDIS_BACKEND_CM_CACHE = 'Cm_Cache_Backend_Redis';
32+
public const REDIS_BACKEND_REDIS_CACHE = '\Magento\Framework\Cache\Backend\Redis';
33+
public const REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE = '\Magento\Framework\Cache\Backend\RemoteSynchronizedCache';
3334

34-
const AVAILABLE_REDIS_BACKEND = [
35+
public const AVAILABLE_REDIS_BACKEND = [
3536
self::REDIS_BACKEND_CM_CACHE,
3637
self::REDIS_BACKEND_REDIS_CACHE,
3738
self::REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE
@@ -83,7 +84,7 @@ public function __construct(
8384
* Returns an empty array in other case.
8485
*
8586
* @return array
86-
* @throws \Magento\MagentoCloud\Config\ConfigException
87+
* @throws ConfigException
8788
*/
8889
public function get(): array
8990
{
@@ -162,7 +163,7 @@ public function get(): array
162163
* @param array $envCacheConfiguration
163164
* @param array $redisConfig
164165
* @return array
165-
* @throws \Magento\MagentoCloud\Config\ConfigException
166+
* @throws ConfigException
166167
*/
167168
private function getSlaveConnection(array $envCacheConfiguration, array $redisConfig): array
168169
{
@@ -176,6 +177,11 @@ private function getSlaveConnection(array $envCacheConfiguration, array $redisCo
176177
$config['load_from_slave']['port'] = $redisSlaveConfig['port'] ?? '';
177178
$config['read_timeout'] = 1;
178179
$config['retry_reads_on_master'] = 1;
180+
181+
if (!empty($redisSlaveConfig['password'])) {
182+
$config['load_from_slave']['password'] = $redisSlaveConfig['password'];
183+
}
184+
179185
$this->logger->info('Set Redis slave connection');
180186
} else {
181187
$this->logger->notice(
@@ -211,7 +217,7 @@ private function isCacheConfigurationValid(array $cacheConfiguration): bool
211217
* @param array $envCacheConfig
212218
* @param array $redisConfig
213219
* @return bool
214-
* @throws \Magento\MagentoCloud\Config\ConfigException
220+
* @throws ConfigException
215221
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
216222
*/
217223
private function isConfigurationCompatibleWithSlaveConnection(
@@ -253,13 +259,19 @@ private function isConfigurationCompatibleWithSlaveConnection(
253259
*/
254260
private function getUnsyncedConfigStructure(string $envCacheBackendModel, array $redisConfig): array
255261
{
256-
return [
262+
$config = [
257263
'backend' => $envCacheBackendModel,
258264
'backend_options' => [
259265
'server' => $redisConfig['host'],
260266
'port' => $redisConfig['port'],
261267
]
262268
];
269+
270+
if (!empty($redisConfig['password'])) {
271+
$config['backend_options']['password'] = (string)$redisConfig['password'];
272+
}
273+
274+
return $config;
263275
}
264276

265277
/**
@@ -271,7 +283,7 @@ private function getUnsyncedConfigStructure(string $envCacheBackendModel, array
271283
*/
272284
private function getSynchronizedConfigStructure(string $envCacheBackendModel, array $redisConfig): array
273285
{
274-
return [
286+
$config = [
275287
'backend' => $envCacheBackendModel,
276288
'backend_options' => [
277289
'remote_backend' => '\Magento\Framework\Cache\Backend\Redis',
@@ -292,17 +304,24 @@ private function getSynchronizedConfigStructure(string $envCacheBackendModel, ar
292304
'write_control' => false,
293305
]
294306
];
307+
308+
if (!empty($redisConfig['password'])) {
309+
$config['backend_options']['remote_backend_options']['password'] = (string)$redisConfig['password'];
310+
}
311+
312+
return $config;
295313
}
296314

297315
/**
298316
* Checks that config contains synchronized cache model and need to use synchronized config structure.
299317
*
300318
* @return bool
301-
* @throws \Magento\MagentoCloud\Config\ConfigException
319+
* @throws ConfigException
302320
*/
303321
private function isSynchronizedConfigStructure(): bool
304322
{
305323
$model = (string)$this->stageConfig->get(DeployInterface::VAR_CACHE_REDIS_BACKEND);
324+
306325
return $model === self::REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE;
307326
}
308327
}

src/Service/Adapter/CredisFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ class CredisFactory
2222
* @param string $server
2323
* @param int $port
2424
* @param int $database
25+
* @param string|null $password
2526
* @return Credis_Client
2627
*/
27-
public function create(string $server, int $port, int $database): Credis_Client
28+
public function create(string $server, int $port, int $database, string $password = null): Credis_Client
2829
{
2930
return new Credis_Client(
3031
$server,
3132
$port,
3233
null,
3334
'',
34-
$database
35+
$database,
36+
$password
3537
);
3638
}
3739
}

src/Service/Redis/Version.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@ public function getVersion(array $redisConfig): string
4141
{
4242
$version = '0';
4343

44-
//on integration environments
44+
// On integration environments
4545
if (isset($redisConfig['type']) && strpos($redisConfig['type'], ':') !== false) {
4646
$version = explode(':', $redisConfig['type'])[1];
47-
} elseif (isset($redisConfig['host']) && isset($redisConfig['port'])) {
48-
//on dedicated environments
47+
} elseif (isset($redisConfig['host'], $redisConfig['port'])) {
48+
// On dedicated environments
49+
$cmd = sprintf('redis-cli -p %s -h %s', (string)$redisConfig['port'], (string)$redisConfig['host']);
50+
51+
if (!empty($redisConfig['password'])) {
52+
$cmd .= ' -a ' . $redisConfig['password'];
53+
}
54+
4955
try {
50-
$process = $this->shell->execute(
51-
sprintf(
52-
'redis-cli -p %s -h %s info | grep redis_version',
53-
$redisConfig['port'],
54-
$redisConfig['host']
55-
)
56-
);
56+
$process = $this->shell->execute($cmd .' info | grep redis_version');
57+
5758
preg_match('/^(?:redis_version:)(\d+\.\d+)/', $process->getOutput(), $matches);
5859
$version = $matches[1] ?? '0';
5960
} catch (ShellException $exception) {

src/Step/Deploy/InstallUpdate/ConfigUpdate/Session/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public function get(): array
136136
$defaultConfig['redis']['disable_locking'] = $disableLocking;
137137
}
138138

139+
if (!empty($redisConfig['password'])) {
140+
$defaultConfig['redis']['password'] = (string)$redisConfig['password'];
141+
}
142+
139143
return $this->configMerger->merge($defaultConfig, $envSessionConfiguration);
140144
}
141145

src/Step/Deploy/PreDeploy/CleanRedisCache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
/**
1919
* Cleans Redis cache.
20+
*
21+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
22+
* @SuppressWarnings(PHPMD.NPathComplexity)
2023
*/
2124
class CleanRedisCache implements StepInterface
2225
{
@@ -79,7 +82,8 @@ public function execute(): void
7982
$client = $this->credisFactory->create(
8083
isset($redisConfig['server']) ? (string)$redisConfig['server'] : '127.0.0.1',
8184
isset($redisConfig['port']) ? (int)$redisConfig['port'] : 6379,
82-
isset($redisConfig['database']) ? (int)$redisConfig['database'] : 0
85+
isset($redisConfig['database']) ? (int)$redisConfig['database'] : 0,
86+
!empty($redisConfig['password']) ? (string)$redisConfig['password'] : null
8387
);
8488

8589
try {

0 commit comments

Comments
 (0)