mirror of
https://github.com/predis/predis.git
synced 2026-09-15 12:57:10 +00:00
Extended core support by implementing ACL SETUSER, GETUSER, DRYRUN (#1193)
* Added support for ACL GETUSER, SETUSER, DRYRUN commands * Change test to support Redis > 6.0.0 * Removed selectors check
This commit is contained in:
committed by
GitHub
parent
b898172009
commit
0e019cc08d
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Predis\Client;
|
||||
|
||||
require __DIR__ . '/../shared.php';
|
||||
|
||||
// Example of ACL DRYRUN command usage:
|
||||
|
||||
// 1. Set user with permissions to call only 'SET' command.
|
||||
$client = new Client($single_server);
|
||||
$response = $client->acl->setUser('Test_dry', '+SET', '~*');
|
||||
$created = ($response == 'OK') ? 'Yes' : 'No';
|
||||
|
||||
echo "User with username 'Test' was created: {$created}. Permissions only to use SET command\n";
|
||||
|
||||
// 2. Dry run 'SET' command under 'Test_dry' user
|
||||
$response = $client->acl->dryRun('Test_dry', 'SET', 'foo', 'bar');
|
||||
|
||||
echo 'Dry run "SET" command.' . "\n";
|
||||
echo 'Response: ' . $response . "\n";
|
||||
|
||||
// 3. Dry run 'GET' command under 'Test_dry' user
|
||||
$response = $client->acl->dryRun('Test_dry', 'GET', 'foo');
|
||||
|
||||
echo 'Dry run "GET" command.' . "\n";
|
||||
echo 'Response: ' . $response;
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Predis\Client;
|
||||
|
||||
require __DIR__ . '/../shared.php';
|
||||
|
||||
// Example of ACL GETUSER command usage:
|
||||
|
||||
// 1. Set user
|
||||
$client = new Client($single_server);
|
||||
$response = $client->acl->setUser('Test');
|
||||
|
||||
// 2. Retrieve user rules:
|
||||
|
||||
echo 'Rules: ' . "\n";
|
||||
print_r(
|
||||
$client->acl->getUser('Test')
|
||||
);
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Predis\Client;
|
||||
|
||||
require __DIR__ . '/../shared.php';
|
||||
|
||||
// Example of ACL SETUSER command usage:
|
||||
|
||||
// 1. Set user
|
||||
$client = new Client($single_server);
|
||||
$response = $client->acl->setUser('Test');
|
||||
$created = ($response == 'OK') ? 'Yes' : 'No';
|
||||
|
||||
echo "User with username 'Test' was created: {$created}";
|
||||
@@ -17,6 +17,7 @@ use Predis\Command\Argument\Geospatial\FromInterface;
|
||||
use Predis\Command\Argument\Server\LimitOffsetCount;
|
||||
use Predis\Command\Argument\Server\To;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\Redis\Container\ACL;
|
||||
use Predis\Command\Redis\Container\FunctionContainer;
|
||||
|
||||
/**
|
||||
@@ -206,6 +207,7 @@ use Predis\Command\Redis\Container\FunctionContainer;
|
||||
*
|
||||
* Container commands
|
||||
* @property FunctionContainer $function
|
||||
* @property ACL $acl
|
||||
*/
|
||||
interface ClientContextInterface
|
||||
{
|
||||
|
||||
@@ -18,6 +18,7 @@ use Predis\Command\Argument\Server\LimitOffsetCount;
|
||||
use Predis\Command\Argument\Server\To;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\FactoryInterface;
|
||||
use Predis\Command\Redis\Container\ACL;
|
||||
use Predis\Command\Redis\Container\FunctionContainer;
|
||||
use Predis\Configuration\OptionsInterface;
|
||||
use Predis\Connection\ConnectionInterface;
|
||||
@@ -224,6 +225,7 @@ use Predis\Response\Status;
|
||||
*
|
||||
* Container commands
|
||||
* @property FunctionContainer $function
|
||||
* @property ACL $acl
|
||||
*/
|
||||
interface ClientInterface
|
||||
{
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Redis;
|
||||
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
|
||||
/**
|
||||
* @see https://redis.io/commands/?name=ACL
|
||||
*
|
||||
* Container command corresponds to any ACL *.
|
||||
* Represents any ACL command with subcommand as first argument.
|
||||
*/
|
||||
class ACL extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'ACL';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Redis\Container;
|
||||
|
||||
use Predis\Response\Status;
|
||||
|
||||
/**
|
||||
* @method Status dryRun(string $username, string $command, ...$arguments)
|
||||
* @method array getUser(string $username)
|
||||
* @method Status setUser(string $username, string ...$rules)
|
||||
*/
|
||||
class ACL extends AbstractContainer
|
||||
{
|
||||
public function getContainerCommandId(): string
|
||||
{
|
||||
return 'acl';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Redis;
|
||||
|
||||
use Predis\Response\ServerException;
|
||||
|
||||
class ACL_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return ACL::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'ACL';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testSetUserFilterArguments(): void
|
||||
{
|
||||
$arguments = ['SETUSER', 'username', 'rule1', 'rule2'];
|
||||
$expected = ['SETUSER', 'username', 'rule1', 'rule2'];
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->setArguments($arguments);
|
||||
|
||||
$this->assertSameValues($expected, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testDryRunFilterArguments(): void
|
||||
{
|
||||
$arguments = ['DRYRUN', 'username', 'command', 'arg1', 'arg2'];
|
||||
$expected = ['DRYRUN', 'username', 'command', 'arg1', 'arg2'];
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->setArguments($arguments);
|
||||
|
||||
$this->assertSameValues($expected, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testGetUserFilterArguments(): void
|
||||
{
|
||||
$arguments = ['GETUSER', 'username'];
|
||||
$expected = ['GETUSER', 'username'];
|
||||
|
||||
$command = $this->getCommand();
|
||||
$command->setArguments($arguments);
|
||||
|
||||
$this->assertSameValues($expected, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisVersion >= 6.0.0
|
||||
*/
|
||||
public function testSetUserCreatesACLUser(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->assertEquals('OK', $redis->acl->setUser('Test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisVersion >= 7.0.0
|
||||
*/
|
||||
public function testDryRunSimulateExecutionOfGivenCommandByUser(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->assertEquals('OK', $redis->acl->setUser('Test', '+SET', '~*'));
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$redis->acl->dryRun('Test', 'SET', 'foo', 'bar')
|
||||
);
|
||||
$this->assertEquals(
|
||||
"This user has no permissions to run the 'get' command",
|
||||
$redis->acl->dryRun('Test', 'GET', 'foo')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisVersion >= 6.0.0
|
||||
*/
|
||||
public function testGetUserReturnsUserDefinedRules(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$redis->acl->setUser(
|
||||
'alan',
|
||||
'allkeys',
|
||||
'+@string',
|
||||
'+@set',
|
||||
'-SADD',
|
||||
'>alanpassword'
|
||||
)
|
||||
);
|
||||
|
||||
foreach (['flags', 'passwords', 'commands', 'keys', 'channels'] as $key) {
|
||||
$this->assertContains($key, $redis->acl->getUser('alan'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisVersion >= 6.0.0
|
||||
*/
|
||||
public function testSetUserThrowsExceptionOnIncorrectRuleProvided(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
|
||||
$this->expectException(ServerException::class);
|
||||
$this->expectExceptionMessage("ERR Error in ACL SETUSER modifier 'foobar'");
|
||||
|
||||
$redis->acl->setUser('Test', 'foobar');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user