mirror of
https://github.com/danog/postgres.git
synced 2024-11-29 20:19:10 +01:00
Add sslmode to config
This commit is contained in:
parent
54186c9d26
commit
737f98243f
@ -6,7 +6,19 @@ use Amp\Sql\ConnectionConfig as SqlConnectionConfig;
|
||||
|
||||
final class ConnectionConfig extends SqlConnectionConfig
|
||||
{
|
||||
const DEFAULT_PORT = 5432;
|
||||
public const DEFAULT_PORT = 5432;
|
||||
|
||||
public const SSL_MODES = [
|
||||
'disable',
|
||||
'allow',
|
||||
'prefer',
|
||||
'require',
|
||||
'verify-ca',
|
||||
'verify-full',
|
||||
];
|
||||
|
||||
/** @var string|null */
|
||||
private $sslMode = null;
|
||||
|
||||
/** @var string|null */
|
||||
private $string;
|
||||
@ -19,13 +31,19 @@ final class ConnectionConfig extends SqlConnectionConfig
|
||||
throw new \Error("Host must be provided in connection string");
|
||||
}
|
||||
|
||||
return new self(
|
||||
$config = new self(
|
||||
$parts["host"],
|
||||
(int) ($parts["port"] ?? self::DEFAULT_PORT),
|
||||
$parts["user"] ?? null,
|
||||
$parts["password"] ?? null,
|
||||
$parts["db"] ?? null
|
||||
);
|
||||
|
||||
if (isset($parts["sslmode"])) {
|
||||
$config = $config->withSslMode($parts["sslmode"]);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
public function __construct(
|
||||
@ -43,6 +61,29 @@ final class ConnectionConfig extends SqlConnectionConfig
|
||||
$this->string = null;
|
||||
}
|
||||
|
||||
public function getSslMode(): ?string
|
||||
{
|
||||
return $this->sslMode;
|
||||
}
|
||||
|
||||
public function withSslMode(string $mode): self
|
||||
{
|
||||
if (!\in_array($mode, self::SSL_MODES, true)) {
|
||||
throw new \Error('Invalid SSL mode, must be one of: ' . \implode(', ', self::SSL_MODES));
|
||||
}
|
||||
|
||||
$new = clone $this;
|
||||
$new->sslMode = $mode;
|
||||
return $new;
|
||||
}
|
||||
|
||||
public function withoutSslMode(): self
|
||||
{
|
||||
$new = clone $this;
|
||||
$new->sslMode = null;
|
||||
return $new;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Connection string used with ext-pgsql and pecl-pq.
|
||||
*/
|
||||
@ -72,6 +113,10 @@ final class ConnectionConfig extends SqlConnectionConfig
|
||||
$chunks[] = "dbname=" . $database;
|
||||
}
|
||||
|
||||
if ($this->sslMode !== null) {
|
||||
$chunks[] = "sslmode=" . $this->sslMode;
|
||||
}
|
||||
|
||||
return $this->string = \implode(" ", $chunks);
|
||||
}
|
||||
}
|
||||
|
@ -42,4 +42,22 @@ class ConnectionConfigTest extends TestCase
|
||||
$this->expectExceptionMessage("Host must be provided in connection string");
|
||||
$config = ConnectionConfig::fromString("invalid connection string");
|
||||
}
|
||||
|
||||
public function testSslMode(): void
|
||||
{
|
||||
$config = ConnectionConfig::fromString("host=localhost sslmode=verify-ca");
|
||||
$this->assertSame('verify-ca', $config->getSslMode());
|
||||
|
||||
$altered = $config->withoutSslMode();
|
||||
$this->assertNull($altered->getSslMode());
|
||||
$this->assertSame('verify-ca', $config->getSslMode());
|
||||
|
||||
$altered = $altered->withSslMode('allow');
|
||||
$this->assertSame('allow', $altered->getSslMode());
|
||||
|
||||
$this->expectException(\Error::class);
|
||||
$this->expectExceptionMessage('Invalid SSL mode');
|
||||
|
||||
$config->withSslMode('invalid');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user