-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added redis authentication support (#3) * several improvements to support auth on redis 5 and 6 Co-authored-by: Amit Kumar Kannaujiya <[email protected]>
- Loading branch information
1 parent
4f91d29
commit ef4da71
Showing
16 changed files
with
333 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,4 @@ services: | |
- .:/app | ||
redis: | ||
container_name: redis | ||
image: redislabs/rebloom:2.2.0 | ||
|
||
|
||
image: redislabs/rebloom:2.2.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ protected function parseResult($result): bool | |
{ | ||
return $result === 'OK' ? true : (bool) $result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Palicao\PhpRebloom\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class RedisAuthenticationException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/* @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Palicao\PhpRebloom\Tests\Integration; | ||
|
||
use Palicao\PhpRebloom\BloomFilter; | ||
use Palicao\PhpRebloom\Exception\RedisAuthenticationException; | ||
use Palicao\PhpRebloom\RedisClient; | ||
use Palicao\PhpRebloom\RedisConnectionParams; | ||
use Redis; | ||
|
||
class AuthRedis5Test extends IntegrationTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
if (self::getRedisMajorVersion() > 5) { | ||
self::markTestSkipped('This test is supposed to run on Redis version 5 or lower'); | ||
} | ||
parent::setUp(); | ||
} | ||
|
||
public function testAuthWithPasswordSuccess(): void | ||
{ | ||
$this->redisClient->executeCommand(['CONFIG', 'SET', 'requirepass', 'pass123']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($authorizedRedisClient); | ||
$result = $bloomFilter->reserve('reserveTest', .0001, 100); | ||
self::assertTrue($result); | ||
$authorizedRedisClient->executeCommand(['CONFIG', 'SET', 'requirepass', '']); | ||
} | ||
|
||
public function testAuthWithPasswordFailure(): void | ||
{ | ||
$this->expectException(RedisAuthenticationException::class); | ||
$this->redisClient->executeCommand(['CONFIG', 'SET', 'requirepass', 'pass123']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'foobar'); | ||
$nonAuthorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($nonAuthorizedRedisClient); | ||
try { | ||
$bloomFilter->reserve('reserveTest', .0001, 100); | ||
} catch (RedisAuthenticationException $exception) { | ||
throw $exception; | ||
} finally { | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$authorizedRedisClient->executeCommand(['CONFIG', 'SET', 'requirepass', '']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Palicao\PhpRebloom\Tests\Integration; | ||
|
||
use Palicao\PhpRebloom\BloomFilter; | ||
use Palicao\PhpRebloom\Exception\RedisAuthenticationException; | ||
use Palicao\PhpRebloom\RedisClient; | ||
use Palicao\PhpRebloom\RedisConnectionParams; | ||
use Redis; | ||
|
||
class AuthRedis6Test extends IntegrationTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
if (self::getRedisMajorVersion() < 6) { | ||
self::markTestSkipped('This test is supposed to run on Redis version 6 or higher'); | ||
} | ||
parent::setUp(); | ||
} | ||
|
||
public function testAuthWithPasswordSuccess(): void | ||
{ | ||
$this->redisClient->executeCommand(['ACL', 'SETUSER', 'default', '>pass123', '+@all']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($authorizedRedisClient); | ||
$result = $bloomFilter->reserve('reserveTest', .0001, 100); | ||
self::assertTrue($result); | ||
$authorizedRedisClient->executeCommand(['ACL', 'SETUSER', 'default', 'nopass']); | ||
} | ||
|
||
public function testAuthWithPasswordFailure(): void | ||
{ | ||
$this->expectException(RedisAuthenticationException::class); | ||
$this->redisClient->executeCommand(['ACL', 'SETUSER', 'default', '>pass123', '+@all']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'foobar'); | ||
$nonAuthorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($nonAuthorizedRedisClient); | ||
try { | ||
$bloomFilter->reserve('reserveTest', .0001, 100); | ||
} catch (RedisAuthenticationException $exception) { | ||
throw $exception; | ||
} finally { | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), null, 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$authorizedRedisClient->executeCommand(['ACL', 'SETUSER', 'default', 'nopass']); | ||
} | ||
} | ||
|
||
public function testAuthWithUsernameAndPasswordSuccess(): void | ||
{ | ||
$this->redisClient->executeCommand(['ACL', 'SETUSER', 'username', 'on', '>pass123', '~*', '+@all']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), 'username', 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($authorizedRedisClient); | ||
$result = $bloomFilter->reserve('reserveTest', .0001, 100); | ||
self::assertTrue($result); | ||
$authorizedRedisClient->executeCommand(['ACL', 'DELUSER', 'username']); | ||
} | ||
|
||
public function testAuthWithUsernameAndPasswordFailure(): void | ||
{ | ||
$this->expectException(RedisAuthenticationException::class); | ||
$this->redisClient->executeCommand(['ACL', 'SETUSER', 'username', 'on', '>pass123', '~*', '+@all']); | ||
$this->redis->close(); | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), 'username', 'foobar'); | ||
$nonAuthorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$bloomFilter = new BloomFilter($nonAuthorizedRedisClient); | ||
try { | ||
$bloomFilter->reserve('reserveTest', .0001, 100); | ||
} catch (RedisAuthenticationException $exception) { | ||
throw $exception; | ||
} finally { | ||
$connectionParams = new RedisConnectionParams(self::getHost(), self::getPort(), 'username', 'pass123'); | ||
$authorizedRedisClient = new RedisClient(new Redis(), $connectionParams); | ||
$authorizedRedisClient->executeCommand(['ACL', 'DELUSER', 'username']); | ||
} | ||
} | ||
} |
Oops, something went wrong.