8000 feature #45313 [Cache] Add support for ACL auth in RedisAdapter (gam6… · symfony/symfony@1211039 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1211039

Browse files
feature #45313 [Cache] Add support for ACL auth in RedisAdapter (gam6itko)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [Cache] Add support for ACL auth in RedisAdapter | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #45305 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Added support of DSN `username:password` section in RedisAdapter which allows using ACL AUTH Commits ------- 722830d [Cache] Add support for ACL auth in RedisAdapter
2 parents eb8e792 + 722830d commit 1211039

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add support for ACL auth in RedisAdapter
8+
49
6.0
510
---
611

src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,25 @@ public function testCreateConnection()
4848
];
4949
$this->assertSame($params, $connection->getParameters()->toArray());
5050
}
51+
52+
public function testAclUserPasswordAuth()
53+
{
54+
// creating user via php-redis cause Predis (v1.1.10) does not support ACL command yet
55+
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
56+
57+
if (version_compare($redis->info()['redis_version'], '6.0', '<')) {
58+
$this->markTestSkipped('Redis server >= 6.0 required');
59+
}
60+
61+
$this->assertTrue($redis->acl('SETUSER', 'predis', 'on'));
62+
$this->assertTrue($redis->acl('SETUSER', 'predis', '>password'));
63+
$this->assertTrue($redis->acl('SETUSER', 'predis', 'allkeys'));
64+
$this->assertTrue($redis->acl('SETUSER', 'predis', '+@all'));
65+
66+
$predis = RedisAdapter::createConnection('redis://predis:password@'.getenv('REDIS_HOST'), ['class' => \Predis\Client::class]);
67+
$this->assertInstanceOf(\Predis\Client::class, $predis);
68+
$this->assertSame('OK', $predis->set(__FUNCTION__, 'value2')->getPayload());
69+
70+
$this->assertSame(1, $redis->acl('DELUSER', 'predis'));
71+
}
5172
}

src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,24 @@ public function provideInvalidCreateConnection(): array
130130
['redis://'],
131131
];
132132
}
133+
134+
public function testAclUserPasswordAuth()
135+
{
136+
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
137+
138+
if (version_compare($redis->info()['redis_version'], '6.0', '<')) {
139+
$this->markTestSkipped('Redis server >= 6.0 required');
140+
}
141+
142+
$this->assertTrue($redis->acl('SETUSER', 'alice', 'on'));
143+
$this->assertTrue($redis->acl('SETUSER', 'alice', '>password'));
144+
$this->assertTrue($redis->acl('SETUSER', 'alice', 'allkeys'));
145+
$this->assertTrue($redis->acl('SETUSER', 'alice', '+@all'));
146+
147+
$redis = RedisAdapter::createConnection('redis://alice:password@'.getenv('REDIS_HOST'));
148+
$this->assertTrue($redis->set(__FUNCTION__, 'value2'));
149+
150+
$redis = RedisAdapter::createConnection('redis://'.getenv('REDIS_HOST'));
151+
$this->assertSame(1, $redis->acl('DELUSER', 'alice'));
152+
}
133153
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ public static function createConnection(string $dsn, array $options = []): \Redi
9393
throw new CacheException(sprintf('Cannot find the "redis" extension nor the "predis/predis" package: "%s".', $dsn));
9494
}
9595

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

100104
if ('' === $auth) {
101105
$auth = null;
@@ -299,7 +303,13 @@ public static function createConnection(string $dsn, array $options = []): \Redi
299303
$params['parameters']['database'] = $params['dbindex'];
300304
}
301305
if (null !== $auth) {
302-
$params['parameters']['password'] = $auth;
306+
if (\is_array($auth)) {
307+
// ACL
308+
$params['parameters']['username'] = $auth[0];
309+
$params['parameters']['password'] = $auth[1];
310+
} else {
311+
$params['parameters']['password'] = $auth;
312+
}
303313
}
304314
if (1 === \count($hosts) && !($params['redis_cluster'] || $params['redis_sentinel'])) {
305315
$hosts = $hosts[0];

0 commit comments

Comments
 (0)
0