From 1a0eb7ee8576e5d5a97121acd4eeb1f565263d7e Mon Sep 17 00:00:00 2001 From: Mehmet Tolga Avcioglu Date: Thu, 6 Oct 2022 18:14:47 +0300 Subject: [PATCH] sentinel default to no password when replica authentication is used (#807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * set default nopass if password is not provided * remove debug output * added tests for noauth sentinel * Update SentinelReplication.php Co-authored-by: Mehmet Tolga Avcioglu Co-authored-by: Till Krüss --- .../Replication/SentinelReplication.php | 6 +++++ .../Replication/SentinelReplicationTest.php | 25 +++++++++++++++++++ .../Connection/StreamConnectionTest.php | 1 - 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Connection/Replication/SentinelReplication.php b/src/Connection/Replication/SentinelReplication.php index 8ad2f480..b91807a7 100644 --- a/src/Connection/Replication/SentinelReplication.php +++ b/src/Connection/Replication/SentinelReplication.php @@ -269,6 +269,12 @@ class SentinelReplication implements ReplicationInterface $parameters['database'] = null; $parameters['username'] = null; + // don't leak password from between configurations + // https://github.com/predis/predis/pull/807/#discussion_r985764770 + if (! isset($parameters['password'])) { + $parameters['password'] = null; + } + if (!isset($parameters['timeout'])) { $parameters['timeout'] = $this->sentinelTimeout; } diff --git a/tests/Predis/Connection/Replication/SentinelReplicationTest.php b/tests/Predis/Connection/Replication/SentinelReplicationTest.php index 636a1de8..a4d57fbd 100644 --- a/tests/Predis/Connection/Replication/SentinelReplicationTest.php +++ b/tests/Predis/Connection/Replication/SentinelReplicationTest.php @@ -62,6 +62,7 @@ class SentinelReplicationTest extends PredisTestCase $this->assertArrayNotHasKey('database', $parameters, 'Parameter `database` was expected to not exist in connection parameters'); $this->assertArrayNotHasKey('username', $parameters, 'Parameter `username` was expected to not exist in connection parameters'); + $this->assertArrayNotHasKey('password', $parameters, 'Parameter `password` was expected to not exist in connection parameters'); } /** @@ -117,6 +118,30 @@ class SentinelReplicationTest extends PredisTestCase $this->assertNotNull($parameters->database); } + /** + * @group disconnected + */ + public function testConnectionParametersInstanceForSentinelConnectionIsNotModifiedEmptyPassword(): void + { + $sentinel1 = Connection\Parameters::create('tcp://127.0.0.1:5381?role=sentinel&database=1&password='); + $sentinel2 = Connection\Parameters::create('tcp://127.0.0.1:5381?role=sentinel&database=1'); + + $replication1 = $this->getReplicationConnection('svc', array($sentinel1)); + $replication2 = $this->getReplicationConnection('svc', array($sentinel2)); + + $parameters1 = $replication1->getSentinelConnection()->getParameters(); + $parameters2 = $replication2->getSentinelConnection()->getParameters(); + + $this->assertSame($sentinel1, $parameters1); + $this->assertSame($sentinel2, $parameters2); + + $this->assertNull($parameters1->password); + $this->assertNull($parameters2->password); + + $this->assertNotNull($parameters1->database); + $this->assertNotNull($parameters2->database); + } + /** * @group disconnected */ diff --git a/tests/Predis/Connection/StreamConnectionTest.php b/tests/Predis/Connection/StreamConnectionTest.php index 81bbfb70..a31a6c3e 100644 --- a/tests/Predis/Connection/StreamConnectionTest.php +++ b/tests/Predis/Connection/StreamConnectionTest.php @@ -62,7 +62,6 @@ class StreamConnectionTest extends PredisConnectionTestCase */ public function testDoesntThrowErrorOnInvalidResource(): void { - var_dump('PHP v' . PHP_VERSION); $this->expectException('Predis\Connection\ConnectionException'); $cmdSelect = RawCommand::create('SELECT', '1000');