From be8a66424e65ccdd864c5efc9a83b1d1b18c20e6 Mon Sep 17 00:00:00 2001 From: Bryan Gruneberg Date: Wed, 10 Jul 2024 17:17:06 -0600 Subject: [PATCH 1/2] Adds support for redis authentication, and fixes redis defaults formatting --- assets/all.settings.php | 56 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/assets/all.settings.php b/assets/all.settings.php index 26672e1..1931f3f 100644 --- a/assets/all.settings.php +++ b/assets/all.settings.php @@ -34,26 +34,36 @@ $redis = new \Redis(); $redis_host = getenv('REDIS_HOST') ?: 'redis'; $redis_port = getenv('REDIS_SERVICE_PORT') ?: 6379; + $redis_password = getenv('REDIS_PASSWORD') ?: ''; + try { # Do not use the cache during installations of Drupal. if (InstallerKernel::installationAttempted()) { - throw new \Exception('Drupal installation underway.'); + throw new \Exception('Drupal installation underway.'); } # Use a timeout to ensure that if the Redis pod is down, that Drupal will # continue to function. if ($redis->connect($redis_host, $redis_port, 1) === FALSE) { - throw new \Exception('Redis server unreachable.'); + throw new \Exception('Redis server unreachable.'); } - + + # Authenticate with the password if provided + if (!empty($redis_password)) { + if ($redis->auth($redis_password) === FALSE) { + throw new \Exception('Redis authentication failed'); + } + } + $response = $redis->ping(); if (!$response) { - throw new \Exception('Redis could be reached but is not responding correctly.'); + throw new \Exception('Redis could be reached but is not responding correctly.'); } $settings['redis.connection']['interface'] = 'PhpRedis'; $settings['redis.connection']['host'] = $redis_host; $settings['redis.connection']['port'] = $redis_port; + $settings['redis.connection']['password'] = $redis_password; $settings['cache_prefix']['default'] = getenv('REDIS_CACHE_PREFIX') ?: getenv('LAGOON_PROJECT') . '_' . getenv('LAGOON_GIT_SAFE_BRANCH'); $settings['cache']['default'] = 'cache.backend.redis'; @@ -79,29 +89,29 @@ 'parameters' => [], 'services' => [ 'redis.factory' => [ - 'class' => 'Drupal\redis\ClientFactory', + 'class' => 'Drupal\redis\ClientFactory', ], 'cache.backend.redis' => [ - 'class' => 'Drupal\redis\Cache\CacheBackendFactory', - 'arguments' => ['@redis.factory', '@cache_tags_provider.container', '@serialization.phpserialize'], - ], - 'cache.container' => [ - 'class' => '\Drupal\redis\Cache\PhpRedis', - 'factory' => ['@cache.backend.redis', 'get'], - 'arguments' => ['container'], - ], - 'cache_tags_provider.container' => [ - 'class' => 'Drupal\redis\Cache\RedisCacheTagsChecksum', - 'arguments' => ['@redis.factory'], - ], - 'serialization.phpserialize' => [ - 'class' => 'Drupal\Component\Serialization\PhpSerialize', + 'class' => 'Drupal\redis\Cache\CacheBackendFactory', + 'arguments' => ['@redis.factory', '@cache_tags_provider.container', '@serialization.phpserialize'], + ], + 'cache.container' => [ + 'class' => '\Drupal\redis\Cache\PhpRedis', + 'factory' => ['@cache.backend.redis', 'get'], + 'arguments' => ['container'], + ], + 'cache_tags_provider.container' => [ + 'class' => 'Drupal\redis\Cache\RedisCacheTagsChecksum', + 'arguments' => ['@redis.factory'], + ], + 'serialization.phpserialize' => [ + 'class' => 'Drupal\Component\Serialization\PhpSerialize', + ], ], - ], - ]; + ]; } catch (\Exception $error) { - $settings['container_yamls'][] = 'sites/default/redis-unavailable.services.yml'; - $settings['cache']['default'] = 'cache.backend.null'; + $settings['container_yamls'][] = 'sites/default/redis-unavailable.services.yml'; + $settings['cache']['default'] = 'cache.backend.null'; } } From f51ed531e8fc61284effd5ccd2591fbf1ace96b3 Mon Sep 17 00:00:00 2001 From: Bryan Gruneberg Date: Mon, 11 Nov 2024 21:21:44 -0600 Subject: [PATCH 2/2] Update assets/all.settings.php Wraps the redis.connection.password setting because the Drupal integration is not happy with null passwords Co-authored-by: Toby Bellwood --- assets/all.settings.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/assets/all.settings.php b/assets/all.settings.php index 1931f3f..c191ead 100644 --- a/assets/all.settings.php +++ b/assets/all.settings.php @@ -63,7 +63,9 @@ $settings['redis.connection']['interface'] = 'PhpRedis'; $settings['redis.connection']['host'] = $redis_host; $settings['redis.connection']['port'] = $redis_port; - $settings['redis.connection']['password'] = $redis_password; + if (!empty($redis_password)) { + $settings['redis.connection']['password'] = $redis_password; + } $settings['cache_prefix']['default'] = getenv('REDIS_CACHE_PREFIX') ?: getenv('LAGOON_PROJECT') . '_' . getenv('LAGOON_GIT_SAFE_BRANCH'); $settings['cache']['default'] = 'cache.backend.redis';