8000 [Cache] Add support for ACL auth in RedisAdapter by gam6itko · Pull Request #45313 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Add support for ACL auth in RedisAdapter #45313

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 1 commit into from
Feb 8, 2022
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add support for ACL auth in RedisAdapter

6.0
---

Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ public function testCreateConnection()
];
$this->assertSame($params, $connection->getParameters()->toArray());
}

public function testAclUserPasswordAuth()
{
// creating user via php-redis cause Predis (v1.1.10) does not support ACL command yet
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));

if (version_compare($redis->info()['redis_version'], '6.0', '<')) {
$this->markTestSkipped('Redis server >= 6.0 required');
}

$this->assertTrue($redis->acl('SETUSER', 'predis', 'on'));
$this->assertTrue($redis->acl('SETUSER', 'predis', '>password'));
$this->assertTrue($redis->acl('SETUSER', 'predis', 'allkeys'));
$this->assertTrue($redis->acl('SETUSER', 'predis', '+@all'));

$predis = RedisAdapter::createConnection('redis://predis:password@'.getenv('REDIS_HOST'), ['class' => \Predis\Client::class]);
$this->assertInstanceOf(\Predis\Client::class, $predis);
$this->assertSame('OK', $predis->set(__FUNCTION__, 'value2')->getPayload());

$this->assertSame(1, $redis->acl('DELUSER', 'predis'));
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,24 @@ public function provideInvalidCreateConnection(): array
['redis://'],
];
}

public function testAclUserPasswordAuth()
{
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));

if (version_compare($redis->info()['redis_version'], '6.0', '<')) {
$this->markTestSkipped('Redis server >= 6.0 required');
}

$this->assertTrue($redis->acl('SETUSER', 'alice', 'on'));
$this->assertTrue($redis->acl('SETUSER', 'alice', '>password'));
$this->assertTrue($redis->acl('SETUSER', 'alice', 'allkeys'));
$this->assertTrue($redis->acl('SETUSER', 'alice', '+@all'));

$redis = RedisAdapter::createConnection('redis://alice:password@'.getenv('REDIS_HOST'));
$this->assertTrue($redis->set(__FUNCTION__, 'value2'));

$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
$this->assertSame(1, $redis->acl('DELUSER', 'alice'));
}
}
18 changes: 14 additions & 4 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ public static function createConnection(string $dsn, array $options = []): \Redi
throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: "%s".', $dsn));
}

$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:[^:@]*+:)?([^@]*+)@)?#', function ($m) use (&$auth) {
if (isset($m[2])) {
$auth = $m[2];
$params = preg_replace_callback('#^'.$scheme.':(//)?(?:(?:(?<user>[^:@]*+):)?(?<password>[^@]*+)@)?#', function ($m) use (&$auth) {
if (isset($m['password'])) {
if (\in_array($m['user'], ['', 'default'], true)) {
$auth = $m['password'];
} else {
$auth = [$m['user'], $m['password']];
}

if ('' === $auth) {
$auth = null;
Expand Down Expand Up @@ -299,7 +303,13 @@ public static function createConnection(string $dsn, array $options = []): \Redi
$params['parameters']['database'] = $params['dbindex'];
}
if (null !== $auth) {
$params['parameters']['password'] = $auth;
if (\is_array($auth)) {
// ACL
$params['parameters']['username'] = $auth[0];
$params['parameters']['password'] = $auth[1];
} else {
$params['parameters']['password'] = $auth;
}
}
if (1 === \count($hosts) && !($params['redis_cluster'] || $params['redis_sentinel'])) {
$hosts = $hosts[0];
Expand Down
0