Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 7f48aa0

Browse files
committed
Merge pull request #66 from vpx/feature/redis-auth
Redis auth parameter
2 parents 74db050 + f25844e commit 7f48aa0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ Validate that a Redis service is running.
547547
<?php
548548
use ZendDiagnostics\Check\Redis;
549549

550-
$redisCheck = new Redis('localhost', 6379);
550+
$redisCheck = new Redis('localhost', 6379, 'secret');
551551
```
552552

553553
### SecurityAdvisory

src/ZendDiagnostics/Check/Redis.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class Redis extends AbstractCheck
1616
{
17+
/**
18+
* @var string|null
19+
*/
20+
protected $auth;
21+
1722
/**
1823
* @var string
1924
*/
@@ -25,13 +30,15 @@ class Redis extends AbstractCheck
2530
protected $port;
2631

2732
/**
28-
* @param string $host
33+
* @param string $host
2934
* @param int $port
35+
* @param string|null $auth
3036
*/
31-
public function __construct($host = 'localhost', $port = 6379)
37+
public function __construct($host = 'localhost', $port = 6379, $auth = null)
3238
{
3339
$this->host = $host;
3440
$this->port = $port;
41+
$this->auth = $auth;
3542
}
3643

3744
/**
@@ -49,22 +56,33 @@ public function check()
4956
/**
5057
* @return PredisClient|RedisExtensionClient
5158
*
59+
* @throws \RedisException
5260
* @throws \RuntimeException
5361
*/
5462
private function createClient()
5563
{
5664
if (class_exists('\Redis')) {
5765
$client = new RedisExtensionClient();
58-
$client->connect($this->host);
66+
$client->connect($this->host, $this->port);
67+
68+
if ($this->auth && false === $client->auth($this->auth)) {
69+
throw new \RedisException('Failed to AUTH connection');
70+
}
5971

6072
return $client;
6173
}
6274

6375
if (class_exists('Predis\Client')) {
64-
return new PredisClient(array(
76+
$parameters = array(
6577
'host' => $this->host,
6678
'port' => $this->port,
67-
));
79+
);
80+
81+
if ($this->auth) {
82+
$parameters['password'] = $this->auth;
83+
}
84+
85+
return new PredisClient($parameters);
6886
}
6987

7088
throw new \RuntimeException('Neither the PHP Redis extension or Predis are installed');

0 commit comments

Comments
 (0)