Skip to content

Commit

Permalink
Merge pull request #142 from acelaya-forks/feature/deprecated-stuff
Browse files Browse the repository at this point in the history
Remove deprecated symbols
  • Loading branch information
acelaya authored Feb 13, 2024
2 parents a309824 + 58dc6a1 commit 3f6b243
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this

### Removed
* Remove `BackwardsCompatibleMonologProcessorDelegator`.
* Remove support for redis URIs with just a password in place of the username (like `tcp://password@1.2.3.4`).
* Remove support for non-URL-encoded credentials in redis URIs.
* Remove `json_decode` and `json_encode` functions.

### Fixed
* *Nothing*
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ return [

'redis' => [
'servers' => [
// These should be valid URIs. Make sure credentials are URL-encoded
'tcp://1.1.1.1:6379',
'tcp://2.2.2.2:6379',
'tcp://3.3.3.3:6379',
'tcp://user:password@4.4.4.4:6379', // Redis ACL (https://redis.io/docs/management/security/acl/)
'tcp://user:pass%40word@4.4.4.4:6379', // Redis ACL (https://redis.io/docs/management/security/acl/)
'tcp://:password@5.5.5.5:6379', // Redis security (https://redis.io/docs/management/security/)
'tcp://password@6.6.6.6:6379', // Same as above, but it's deprecated, as it's not a standard URI
'tls://server_with_encryption:6379',
],
'sentinel_service' => 'the_service', // Optional.
'decode_credentials' => true // Optional. Defaults to false
],
],

Expand All @@ -72,7 +71,6 @@ You can allow caching to be done on a redis instance, redis cluster or redis sen

* `servers`: A list of redis servers. If one is provided, it will be treated as a single instance, and otherwise, a cluster will be assumed.
* `sentinel_service`: Lets you enable sentinel mode. When provided, the servers will be treated as sentinel instances.
* `decode_credentials`: Indicates if server credentials (if present) should be URL decoded before passing to redis connection. Otherwise, they are passed verbatim.

> **Note**
> The entries in `servers` support credentials in the form of `tcp://password@my-server:6379` or `tcp://username:password@my-server:6379`.
Expand Down
15 changes: 0 additions & 15 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,10 @@
namespace Shlinkio\Shlink\Common;

use Cake\Chronos\Chronos;
use JsonSerializable;
use Shlinkio\Shlink\Common\Util\DateRange;

use function array_pad;
use function explode;
use function Shlinkio\Shlink\Json\json_decode as shlink_json_decode;
use function Shlinkio\Shlink\Json\json_encode as shlink_json_encode;

/** @deprecated Use the same function from shlinkio/shlink-json */
function json_decode(string $json): array
{
return shlink_json_decode($json);
}

/** @deprecated Use the same function from shlinkio/shlink-json */
function json_encode(array|JsonSerializable $payload): string
{
return shlink_json_encode($payload);
}

function buildDateRange(?Chronos $startDate, ?Chronos $endDate): DateRange
{
Expand Down
31 changes: 11 additions & 20 deletions src/Cache/RedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ public function __invoke(ContainerInterface $container): PredisClient
private function resolveServers(array $redisConfig): array
{
$servers = $redisConfig['servers'] ?? [];
$decodeCredentials = $redisConfig['decode_credentials'] ?? false;

$servers = array_map(
fn (string $server) => $this->normalizeServer($server, $decodeCredentials),
fn (string $server) => $this->normalizeServer($server),
is_string($servers) ? explode(',', $servers) : $servers,
);

// If there's only one server, we return it as is. If an array is provided, predis expects cluster config
return count($servers) === 1 ? $servers[0] : $servers;
}

private function normalizeServer(string $server, bool $decodeCredentials): array
private function normalizeServer(string $server): array
{
$parsedServer = parse_url(trim($server));
if (! is_array($parsedServer)) {
Expand All @@ -62,25 +61,17 @@ private function normalizeServer(string $server, bool $decodeCredentials): array
$parsedServer['ssl'] = SSL::OPTIONS;
}

if (! isset($parsedServer['user']) && ! isset($parsedServer['pass'])) {
return $parsedServer;
}
// Set credentials if set
$user = $parsedServer['user'] ?? null;
$pass = $parsedServer['pass'] ?? null;
unset($parsedServer['user'], $parsedServer['pass']);

// Deprecated. Apply URL decoding only if explicitly requested, for BC. Next major version will always do it
$credentialsCallback = static fn (string $val) => ($decodeCredentials ? urldecode($val) : $val);

if (isset($parsedServer['user']) && ! isset($parsedServer['pass'])) {
// For historical reasons, we support URLs in the form of `tcp://redis_password@redis_host:1234`, but this
// is deprecated
$parsedServer['password'] = $credentialsCallback($parsedServer['user']);
} elseif (isset($parsedServer['user'], $parsedServer['pass'])) {
if ($parsedServer['user'] !== '') {
$parsedServer['username'] = $credentialsCallback($parsedServer['user']);
}
$parsedServer['password'] = $credentialsCallback($parsedServer['pass']);
if ($user !== null && $user !== '') {
$parsedServer['username'] = urldecode($user);
}
if ($pass !== null) {
$parsedServer['password'] = urldecode($pass);
}

unset($parsedServer['user'], $parsedServer['pass']);

return $parsedServer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RabbitMq/AMQPConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __invoke(ContainerInterface $container): AMQPStreamConnection
// We have to pass the config as the ssl_protocol to avoid an internal deprecation warning
// When the ssl_protocol is a config instance, it is internally set as config.
// See https://github.com/php-amqplib/php-amqplib/blob/b4ade54ebe4685873f6316f9a05fc2c77a9e28f9/PhpAmqpLib/Connection/AMQPStreamConnection.php#L48-L55
ssl_protocol: $connectionConfig, // @phpstan-ignore-line
ssl_protocol: $connectionConfig,
);
}

Expand Down
12 changes: 2 additions & 10 deletions test/Cache/RedisFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,11 @@ public static function provideServersWithCredentials(): iterable
yield 'password only' => [[
'servers' => ['tcp://:baz@1.1.1.1:6379'],
], null, 'baz', null];
yield 'password only (deprecated)' => [[
yield 'username only' => [[
'servers' => ['tcp://foo@1.1.1.1:6379'],
], null, 'foo', null];
], 'foo', null, null];
yield 'URL-encoded' => [[
'servers' => ['tcp://user%3Aname:pass%40word@1.1.1.1:6379'],
], 'user%3Aname', 'pass%40word', null];
yield 'URL-encoded, no request decode' => [[
'servers' => ['tcp://user%3Aname:pass%40word@1.1.1.1:6379'],
'decode_credentials' => false,
], 'user%3Aname', 'pass%40word', null];
yield 'URL-encoded, request decode' => [[
'servers' => ['tcp://user%3Aname:pass%40word@1.1.1.1:6379'],
'decode_credentials' => true,
], 'user:name', 'pass@word', null];
yield 'tls encryption' => [[
'servers' => ['tls://1.1.1.1:6379'],
Expand Down

0 comments on commit 3f6b243

Please sign in to comment.