From 664fc648c0a5b645cda8677cc391009b6142a739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Nu=C3=9Fbaum?= Date: Wed, 12 Sep 2018 12:07:00 +0200 Subject: [PATCH] FEATURE: Redis connection with AUTH password (#12) FEATURE: Redis connection with AUTH password --- Classes/Queue/RedisQueue.php | 15 +++- Configuration/Settings.yaml | 6 ++ README.md | 12 ++-- .../Queue/RedisAuthenticationTest.php | 70 +++++++++++++++++++ 4 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 Tests/Functional/Queue/RedisAuthenticationTest.php diff --git a/Classes/Queue/RedisQueue.php b/Classes/Queue/RedisQueue.php index b67a460..3b638f3 100644 --- a/Classes/Queue/RedisQueue.php +++ b/Classes/Queue/RedisQueue.php @@ -23,7 +23,6 @@ */ class RedisQueue implements QueueInterface { - /** * @var string */ @@ -296,10 +295,22 @@ protected function connectClient() { $host = isset($this->clientOptions['host']) ? $this->clientOptions['host'] : '127.0.0.1'; $port = isset($this->clientOptions['port']) ? $this->clientOptions['port'] : 6379; + $password = isset($this->clientOptions['password']) ? $this->clientOptions['password'] : ''; $database = isset($this->clientOptions['database']) ? $this->clientOptions['database'] : 0; // The connection read timeout should be higher than the timeout for blocking operations! $timeout = isset($this->clientOptions['timeout']) ? $this->clientOptions['timeout'] : round($this->defaultTimeout * 1.5); - $connected = $this->client->connect($host, $port, $timeout) && $this->client->select($database); + + $connected = $this->client->connect($host, $port, $timeout); + if ($connected) { + if ($password !== '') { + $authSuccess = $this->client->auth($password); + if ($authSuccess !== true) { + throw new JobQueueException('Redis authentication failed.', 1536735535); + } + } + $connected = $this->client->select($database); + } + // Break the cycle that could cause a high CPU load if (!$connected) { usleep($this->reconnectDelay * 1e6); diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index e4cb32d..5d705db 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -8,3 +8,9 @@ Flowpack: host: 127.0.0.1 port: 6379 database: 15 + # Set password in your redis config + passwordProtectedClient: + host: 127.0.0.1 + port: 6379 + password: 'My_Secret_Password' + database: 15 diff --git a/README.md b/README.md index cbb946a..754ea4e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ composer require flowpack/jobqueue-redis ``` If not already installed, that will fetch its requirements, namely `jobqueue-common`. + *NOTE:* This package needs a [redis](http://redis.io/) server and the [PHP redis extension](https://github.com/phpredis/phpredis) to be installed Now the queue can be configured like this: @@ -28,6 +29,7 @@ Flowpack: host: 127.0.0.1 port: 6379 database: 15 + password: 'some long secret' defaultTimeout: 20 ``` @@ -36,10 +38,10 @@ Flowpack: The `RedisQueue` supports following options: -| Option | Type | Default | Description | -| ----------------------- |---------| ---------------------------------------------------------------------------------------------:| ---------------------------------------- | -| defaultTimeout | integer | 60 | Number of seconds new messages are waited for before a timeout occurs (This is overridden by a "timeout" argument in the `waitAndTake()` and `waitAndReserve()` methods | -| client | array | ['host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'timeout' => ] | Redis connection settings | +| Option | Type | Default | Description | +|----------------|---------|----------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| defaultTimeout | integer | 60 | Number of seconds new messages are waited for before a timeout occurs (This is overridden by a "timeout" argument in the `waitAndTake()` and `waitAndReserve()` methods | +| client | array | ['host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'timeout' => , password => ''] | Redis connection settings | ### Submit options @@ -55,4 +57,4 @@ This package is licensed under the MIT license ## Contributions -Pull-Requests are more than welcome. Make sure to read the [Code Of Conduct](CodeOfConduct.rst). \ No newline at end of file +Pull-Requests are more than welcome. Make sure to read the [Code Of Conduct](CodeOfConduct.rst). diff --git a/Tests/Functional/Queue/RedisAuthenticationTest.php b/Tests/Functional/Queue/RedisAuthenticationTest.php new file mode 100644 index 0000000..ee5ee81 --- /dev/null +++ b/Tests/Functional/Queue/RedisAuthenticationTest.php @@ -0,0 +1,70 @@ +objectManager->get(ConfigurationManager::class); + $packageKey = $this->objectManager->getPackageKeyByObjectName( + TypeHandling::getTypeForValue($this) + ); + $packageSettings = $configurationManager->getConfiguration( + ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $packageKey + ); + if (!isset($packageSettings['testing']['enabled']) || $packageSettings['testing']['enabled'] !== true) { + $this->markTestSkipped(sprintf('Queue is not configured (%s.testing.enabled != TRUE)', $packageKey)); + } + $this->queueSettings = $packageSettings['testing']; + $this->options['client'] = $this->queueSettings['passwordProtectedClient']; + $this->queue = new RedisQueue('Test queue', $this->options); + $this->queue->flush(); + } + + /** + * @test + */ + public function testConnectionByAddingAMessage() + { + $this->queue->submit('First message'); + $this->assertSame(1, $this->queue->countReady()); + } +}