Skip to content

Redis dsn and password fixes #656

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 5 commits into from
Nov 23, 2018
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
7 changes: 7 additions & 0 deletions pkg/dsn/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ public static function parse(string $dsn): array
$schemeExtensions = array_values($schemeParts);

$user = parse_url($dsn, PHP_URL_USER) ?: null;
if (is_string($user)) {
$user = rawurldecode($user);
}

$password = parse_url($dsn, PHP_URL_PASS) ?: null;
if (is_string($password)) {
$password = rawurldecode($password);
}

$path = parse_url($dsn, PHP_URL_PATH) ?: null;
if ($path) {
Expand Down
30 changes: 30 additions & 0 deletions pkg/dsn/Tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,36 @@ public function testShouldParseExpectedNumberOfMultipleDsns()
$this->assertCount(3, $dsns);
}

public function testShouldParseDsnWithOnlyUser()
{
$dsn = Dsn::parseFirst('foo://user@host');

$this->assertSame('user', $dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public function testShouldUrlEncodeUser()
{
$dsn = Dsn::parseFirst('foo://us%3Aer@host');

$this->assertSame('us:er', $dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public function testShouldUrlEncodePassword()
{
$dsn = Dsn::parseFirst('foo://user:pass%3Aword@host');

$this->assertSame('user', $dsn->getUser());
$this->assertSame('pass:word', $dsn->getPassword());
$this->assertSame('foo', $dsn->getScheme());
$this->assertSame('host', $dsn->getHost());
}

public static function provideSchemes()
{
yield [':', '', '', []];
Expand Down
2 changes: 1 addition & 1 deletion pkg/enqueue/Client/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function getKnownDrivers(): array
'packages' => ['enqueue/enqueue', 'enqueue/gps'],
];
$map[] = [
'schemes' => ['redis'],
'schemes' => ['redis', 'rediss'],
'driverClass' => RedisDriver::class,
'requiredSchemeExtensions' => [],
'packages' => ['enqueue/enqueue', 'enqueue/redis'],
Expand Down
2 changes: 1 addition & 1 deletion pkg/enqueue/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static function getKnownConnections(): array
'package' => 'enqueue/rdkafka',
];
$map[RedisConnectionFactory::class] = [
'schemes' => ['redis'],
'schemes' => ['redis', 'rediss'],
'supportedSchemeExtensions' => ['predis', 'phpredis'],
'package' => 'enqueue/redis',
];
Expand Down
4 changes: 2 additions & 2 deletions pkg/enqueue/Tests/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testShouldGetAvailableConnectionsInExpectedFormat()

$connectionInfo = $availableConnections[RedisConnectionFactory::class];
$this->assertArrayHasKey('schemes', $connectionInfo);
$this->assertSame(['redis'], $connectionInfo['schemes']);
$this->assertSame(['redis', 'rediss'], $connectionInfo['schemes']);

$this->assertArrayHasKey('supportedSchemeExtensions', $connectionInfo);
$this->assertSame(['predis', 'phpredis'], $connectionInfo['supportedSchemeExtensions']);
Expand All @@ -51,7 +51,7 @@ public function testShouldGetKnownConnectionsInExpectedFormat()

$connectionInfo = $availableConnections[RedisConnectionFactory::class];
$this->assertArrayHasKey('schemes', $connectionInfo);
$this->assertSame(['redis'], $connectionInfo['schemes']);
$this->assertSame(['redis', 'rediss'], $connectionInfo['schemes']);

$this->assertArrayHasKey('supportedSchemeExtensions', $connectionInfo);
$this->assertSame(['predis', 'phpredis'], $connectionInfo['supportedSchemeExtensions']);
Expand Down
12 changes: 6 additions & 6 deletions pkg/redis/PRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function eval(string $script, array $keys = [], array $args = [])
// mixed eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null)
return call_user_func_array([$this->redis, 'eval'], array_merge([$script, count($keys)], $keys, $args));
} catch (PRedisServerException $e) {
throw new ServerException('eval command has failed', null, $e);
throw new ServerException('eval command has failed', 0, $e);
}
}

Expand All @@ -68,7 +68,7 @@ public function zadd(string $key, string $value, float $score): int
try {
return $this->redis->zadd($key, [$value => $score]);
} catch (PRedisServerException $e) {
throw new ServerException('zadd command has failed', null, $e);
throw new ServerException('zadd command has failed', 0, $e);
}
}

Expand All @@ -77,7 +77,7 @@ public function zrem(string $key, string $value): int
try {
return $this->redis->zrem($key, [$value]);
} catch (PRedisServerException $e) {
throw new ServerException('zrem command has failed', null, $e);
throw new ServerException('zrem command has failed', 0, $e);
}
}

Expand All @@ -86,7 +86,7 @@ public function lpush(string $key, string $value): int
try {
return $this->redis->lpush($key, [$value]);
} catch (PRedisServerException $e) {
throw new ServerException('lpush command has failed', null, $e);
throw new ServerException('lpush command has failed', 0, $e);
}
}

Expand All @@ -99,7 +99,7 @@ public function brpop(array $keys, int $timeout): ?RedisResult

return null;
} catch (PRedisServerException $e) {
throw new ServerException('brpop command has failed', null, $e);
throw new ServerException('brpop command has failed', 0, $e);
}
}

Expand All @@ -112,7 +112,7 @@ public function rpop(string $key): ?RedisResult

return null;
} catch (PRedisServerException $e) {
throw new ServerException('rpop command has failed', null, $e);
throw new ServerException('rpop command has failed', 0, $e);
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/redis/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function eval(string $script, array $keys = [], array $args = [])
try {
return $this->redis->eval($script, array_merge($keys, $args), count($keys));
} catch (\RedisException $e) {
throw new ServerException('eval command has failed', null, $e);
throw new ServerException('eval command has failed', 0, $e);
}
}

Expand All @@ -40,7 +40,7 @@ public function zadd(string $key, string $value, float $score): int
try {
return $this->redis->zAdd($key, $score, $value);
} catch (\RedisException $e) {
throw new ServerException('zadd command has failed', null, $e);
throw new ServerException('zadd command has failed', 0, $e);
}
}

Expand All @@ -49,7 +49,7 @@ public function zrem(string $key, string $value): int
try {
return $this->redis->zRem($key, $value);
} catch (\RedisException $e) {
throw new ServerException('zrem command has failed', null, $e);
throw new ServerException('zrem command has failed', 0, $e);
}
}

Expand All @@ -58,7 +58,7 @@ public function lpush(string $key, string $value): int
try {
return $this->redis->lPush($key, $value);
} catch (\RedisException $e) {
throw new ServerException('lpush command has failed', null, $e);
throw new ServerException('lpush command has failed', 0, $e);
}
}

Expand All @@ -71,7 +71,7 @@ public function brpop(array $keys, int $timeout): ?RedisResult

return null;
} catch (\RedisException $e) {
throw new ServerException('brpop command has failed', null, $e);
throw new ServerException('brpop command has failed', 0, $e);
}
}

Expand All @@ -84,7 +84,7 @@ public function rpop(string $key): ?RedisResult

return null;
} catch (\RedisException $e) {
throw new ServerException('rpop command has failed', null, $e);
throw new ServerException('rpop command has failed', 0, $e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/redis/RedisConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function parseDsn(string $dsn): array
'port' => $dsn->getPort(),
'path' => $dsn->getPath(),
'database' => $database,
'password' => $dsn->getPassword(),
'password' => $dsn->getPassword() ?: $dsn->getUser() ?: $dsn->getString('password'),
'async' => $dsn->getBool('async'),
'persistent' => $dsn->getBool('persistent'),
'timeout' => $dsn->getFloat('timeout'),
Expand Down
44 changes: 44 additions & 0 deletions pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,50 @@ public static function provideConfigs()
],
];

// password as user
yield [
'redis://asdfqwer1234asdf@foo',
[
'host' => 'foo',
'scheme' => 'redis',
'port' => 6379,
'timeout' => 5.,
'database' => null,
'password' => 'asdfqwer1234asdf',
'scheme_extensions' => [],
'path' => null,
'async' => false,
'persistent' => false,
'lazy' => true,
'read_write_timeout' => null,
'predis_options' => null,
'ssl' => null,
'redelivery_delay' => 300,
],
];

// password as query parameter
yield [
'redis:?password=asdfqwer1234asdf',
[
'host' => '127.0.0.1',
'scheme' => 'redis',
'port' => 6379,
'timeout' => 5.,
'database' => null,
'password' => 'asdfqwer1234asdf',
'scheme_extensions' => [],
'path' => null,
'async' => false,
'persistent' => false,
'lazy' => true,
'read_write_timeout' => null,
'predis_options' => null,
'ssl' => null,
'redelivery_delay' => 300,
],
];

// from predis doc

yield [
Expand Down