Skip to content
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

Add SSL context for RedisCluster connections #85

Merged
42 changes: 29 additions & 13 deletions src/RedisClusterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use Laminas\Cache\Exception\RuntimeException;
use Laminas\Cache\Storage\Adapter\Exception\InvalidRedisClusterConfigurationException;
use Laminas\Stdlib\AbstractOptions;
use Traversable;

use function is_array;
use function iterator_to_array;

final class RedisClusterOptions extends AdapterOptions
{
Expand Down Expand Up @@ -55,7 +58,6 @@ final class RedisClusterOptions extends AdapterOptions

private string $password = '';

/** @psalm-var SslContext|null */
private ?SslContext $sslContext = null;

/**
Expand All @@ -81,6 +83,31 @@ public function __construct($options = null)
}
}

/**
* {@inheritDoc}
*/
public function setFromArray($options)
{
if ($options instanceof AbstractOptions) {
$options = $options->toArray();
} elseif ($options instanceof Traversable) {
$options = iterator_to_array($options);
}

$sslContext = $options['sslContext'] ?? $options['ssl_context'] ?? null;
unset($options['sslContext'], $options['ssl_context']);
if (is_array($sslContext)) {
/** @psalm-suppress MixedArgumentTypeCoercion Trust upstream that they verify the array beforehand. */
$sslContext = SslContext::fromSslContextArray($sslContext);
}

if ($sslContext instanceof SslContext) {
$options['ssl_context'] = $sslContext;
}

return parent::setFromArray($options);
}

public function setTimeout(float $timeout): void
{
$this->timeout = $timeout;
Expand Down Expand Up @@ -228,24 +255,13 @@ public function setPassword(string $password): void
$this->password = $password;
}

/**
* @psalm-return SslContext|null
*/
public function getSslContext(): ?SslContext
{
return $this->sslContext;
}

/**
* @psalm-param array<non-empty-string,mixed>|SslContext|null $sslContext
*/
public function setSslContext(array|SslContext|null $sslContext): void
public function setSslContext(SslContext|null $sslContext): void
{
if (is_array($sslContext)) {
$data = $sslContext;
$sslContext = new SslContext();
$sslContext->exchangeArray($data);
}
$this->sslContext = $sslContext;
}
}
4 changes: 2 additions & 2 deletions src/RedisClusterResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster
$options->getReadTimeout(),
$options->isPersistent(),
$password,
$options->getSslContext()?->getArrayCopy()
$options->getSslContext()?->toSslContextArray()
);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ private function createRedisResourceFromName(
$readTimeout,
$persistent,
$password,
$sslContext?->getArrayCopy()
$sslContext?->toSslContextArray()
);
}

Expand Down
Loading