Skip to content

[Cache] Send Predis SSL options in the $hosts parameter #50074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2023

Conversation

magnusnordlander
Copy link
Contributor

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #46465
License MIT

Predis accepts SSL options in the $hosts parameter, not in the $params parameter. From my perspective, this is really only applicable when using rediss://host:port style DSNs, where you might want to add ?ssl[verify_peer]=0 or something similar.

I'm unsure how to write a good test for this, since there doesn't seem to be any standard Redis host with TLS that requires additional options in the test runner. Happy to take suggestions on how to approach a test, if that's deemed necessary.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm that this change would still work?

diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
index e326dba76b..a35859d2b6 100644
--- a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
+++ b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
@@ -48,4 +48,29 @@ class PredisAdapterTest extends AbstractRedisAdapterTestCase
         ];
         $this->assertSame($params, $connection->getParameters()->toArray());
     }
+
+    public function testCreateSslConnection()
+    {
+        $redisHost = getenv('REDIS_HOST');
+
+        $redis = RedisAdapter::createConnection('rediss://'.$redisHost.'/1?ssl[verify_peer]=0', ['class' => \Predis\Client::class, 'timeout' => 3]);
+        $this->assertInstanceOf(\Predis\Client::class, $redis);
+
+        $connection = $redis->getConnection();
+        $this->assertInstanceOf(StreamConnection::class, $connection);
+
+        $redisHost = explode(':', $redisHost);
+        $params = [
+            'scheme' => 'tls',
+            'host' => $redisHost[0],
+            'port' => (int) ($redisHost[1] ?? 6379),
+            'ssl' => ['verify_peer' => '0'],
+            'persistent' => 0,
+            'timeout' => 3,
+            'read_write_timeout' => 0,
+            'tcp_nodelay' => true,
+            'database' => '1',
+        ];
+        $this->assertSame($params, $connection->getParameters()->toArray());
+    }
 }
diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php
index 07633a9d3f..67d8663169 100644
--- a/src/Symfony/Component/Cache/Traits/RedisTrait.php
+++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php
@@ -349,7 +349,7 @@ trait RedisTrait
             }
             $params['exceptions'] = false;
 
-            $redis = new $class($hosts, array_diff_key($params, array_diff_key(self::$defaultConnectionOptions, ['ssl' => null])));
+            $redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
             if (isset($params['redis_sentinel'])) {
                 $redis->getConnection()->setSentinelTimeout($params['timeout']);
             }

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied my patch, please report back if there's an issue with it.

@nicolas-grekas
Copy link
Member

Thank you @magnusnordlander.

@nicolas-grekas nicolas-grekas merged commit 638703d into symfony:5.4 Apr 21, 2023
This was referenced Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants