Skip to content

[redis] Authentication support added #497

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 3 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Make sure you installed either of them
* [Consume message](#consume-message)
* [Delete queue (purge messages)](#delete-queue-purge-messages)
* [Delete topic (purge messages)](#delete-topic-purge-messages)
* [Connect Heroku Redis](#connect-heroku-redis)

## Installation

Expand Down Expand Up @@ -61,6 +62,12 @@ $psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('redis:');

// pass redis instance directly
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
$redis->connect();

$factory = new RedisConnectionFactory($redis);
```

* With predis library:
Expand Down Expand Up @@ -155,4 +162,17 @@ $fooTopic = $psrContext->createTopic('aTopic');
$psrContext->deleteTopic($fooTopic);
```

## Connect Heroku Redis

[Heroku Redis](https://devcenter.heroku.com/articles/heroku-redis) describes how to setup Redis instance on Heroku.
To use it with Enqueue Redis you have to pass REDIS_URL to RedisConnectionFactory constructor.

```php
<?php

// REDIS_URL: redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111

$connection = new \Enqueue\Redis\RedisConnectionFactory(getenv('REDIS_URL'));
```

[back to index](../index.md)
26 changes: 24 additions & 2 deletions pkg/redis/PRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Enqueue\Redis;

use Predis\Client;
use Predis\ClientInterface;
use Predis\Response\ServerException as PRedisServerException;

class PRedis implements Redis
{
/**
* @var array
*/
private $config;

/**
* @var ClientInterface
*/
Expand All @@ -15,9 +21,19 @@ class PRedis implements Redis
/**
* @param ClientInterface $redis
*/
public function __construct(ClientInterface $redis)
public function __construct(array $config)
{
$this->redis = $redis;
$this->config = $this->config = array_replace([
'host' => null,
'port' => null,
'pass' => null,
'user' => null,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'persisted' => false,
'database' => 0,
], $config);
}

/**
Expand Down Expand Up @@ -63,6 +79,12 @@ public function rpop($key)
*/
public function connect()
{
$this->redis = new Client($this->config, ['exceptions' => true]);

if ($this->config['pass']) {
$this->redis->auth($this->config['pass']);
}

$this->redis->connect();
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/redis/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct(array $config)
$this->config = array_replace([
'host' => null,
'port' => null,
'pass' => null,
'user' => null,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
Expand Down Expand Up @@ -82,6 +84,10 @@ public function connect()
);
}

if ($this->config['pass']) {
$this->redis->auth($this->config['pass']);
}

$this->redis->select($this->config['database']);
}

Expand Down
18 changes: 15 additions & 3 deletions pkg/redis/RedisConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Enqueue\Redis;

use Interop\Queue\PsrConnectionFactory;
use Predis\Client;

class RedisConnectionFactory implements PsrConnectionFactory
{
Expand All @@ -29,17 +28,30 @@ class RedisConnectionFactory implements PsrConnectionFactory
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
* 'lazy' => the connection will be performed as later as possible, if the option set to true
* 'database' => Database index to select when connected (default value: 0)
* user - The user name to use.
* pass - Password.
* ].
*
* or
*
* redis:
* redis:?vendor=predis
*
* @param array|string|null $config
* or
*
* instance of Enqueue\Redis
*
* @param array|string|Redis|null $config
*/
public function __construct($config = 'redis:')
{
if ($config instanceof Redis) {
$this->redis = $config;
$this->config = $this->defaultConfig();

return;
}

if (empty($config) || 'redis:' === $config) {
$config = [];
} elseif (is_string($config)) {
Expand Down Expand Up @@ -88,7 +100,7 @@ private function createRedis()
}

if ('predis' == $this->config['vendor'] && false == $this->redis) {
$this->redis = new PRedis(new Client($this->config, ['exceptions' => true]));
$this->redis = new PRedis($this->config);
}

if ('custom' == $this->config['vendor'] && false == $this->redis) {
Expand Down
31 changes: 31 additions & 0 deletions pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Redis\Tests;

use Enqueue\Redis\Redis;
use Enqueue\Redis\RedisConnectionFactory;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -45,6 +46,17 @@ public function testThrowIfVendorIsInvalid()
new RedisConnectionFactory(['vendor' => 'invalidVendor']);
}

public function testCouldBeCreatedWithRedisInstance()
{
$redisMock = $this->createMock(Redis::class);

$factory = new RedisConnectionFactory($redisMock);
$this->assertAttributeSame($redisMock, 'redis', $factory);

$context = $factory->createContext();
$this->assertSame($redisMock, $context->getRedis());
}

/**
* @dataProvider provideConfigs
*
Expand Down Expand Up @@ -141,5 +153,24 @@ public static function provideConfigs()
'redis' => null,
],
];

// heroku redis
yield [
'redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111',
[
'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com',
'port' => 111,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'vendor' => 'phpredis',
'persisted' => false,
'lazy' => false,
'database' => 0,
'redis' => null,
'user' => 'h',
'pass' => 'asdfqwer1234asdf',
],
];
}
}