Skip to content

Commit 98d30fe

Browse files
committed
feat(db): add SSL/TLS support for PostgreSQL
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 76e1a98 commit 98d30fe

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

config/config.sample.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,17 @@
20352035
*/
20362036
'mysql.collation' => null,
20372037

2038+
/**
2039+
* PostgreSQL SSL connection
2040+
*/
2041+
'pgsql_ssl' => [
2042+
'mode' => '',
2043+
'cert' => '',
2044+
'rootcert' => '',
2045+
'key' => '',
2046+
'crl' => '',
2047+
],
2048+
20382049
/**
20392050
* Database types that are supported for installation.
20402051
*

lib/private/DB/ConnectionFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ public function createConnectionParams(string $configPrefix = '', array $additio
198198
'tablePrefix' => $connectionParams['tablePrefix']
199199
];
200200

201+
if ($type === 'pgsql') {
202+
$pgsqlSsl = $this->config->getValue('pgsql_ssl', false);
203+
if (is_array($pgsqlSsl)) {
204+
$connectionParams['sslmode'] = $pgsqlSsl['mode'] ?? '';
205+
$connectionParams['sslrootcert'] = $pgsqlSsl['rootcert'] ?? '';
206+
$connectionParams['sslcert'] = $pgsqlSsl['cert'] ?? '';
207+
$connectionParams['sslkey'] = $pgsqlSsl['key'] ?? '';
208+
$connectionParams['sslcrl'] = $pgsqlSsl['crl'] ?? '';
209+
}
210+
}
211+
201212
if ($type === 'mysql' && $this->config->getValue('mysql.utf8mb4', false)) {
202213
$connectionParams['defaultTableOptions'] = [
203214
'collate' => 'utf8mb4_bin',

tests/lib/DB/ConnectionFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,33 @@ public function testSplitHostFromPortAndSocket($host, array $expected): void {
3939

4040
$this->assertEquals($expected, self::invokePrivate($factory, 'splitHostFromPortAndSocket', [$host]));
4141
}
42+
43+
public function testPgsqlSslConnection(): void {
44+
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject $config */
45+
$config = $this->createMock(SystemConfig::class);
46+
$config->method('getValue')
47+
->willReturnCallback(function ($key, $default) {
48+
return match ($key) {
49+
'dbtype' => 'pgsql',
50+
'pgsql_ssl' => [
51+
'mode' => 'verify-full',
52+
'cert' => 'client.crt',
53+
'key' => 'client.key',
54+
'crl' => 'client.crl',
55+
'rootcert' => 'rootCA.crt',
56+
],
57+
default => $default,
58+
};
59+
});
60+
$factory = new ConnectionFactory($config);
61+
62+
$params = $factory->createConnectionParams();
63+
64+
$this->assertEquals('pdo_pgsql', $params['driver']);
65+
$this->assertEquals('verify-full', $params['sslmode']);
66+
$this->assertEquals('rootCA.crt', $params['sslrootcert']);
67+
$this->assertEquals('client.crt', $params['sslcert']);
68+
$this->assertEquals('client.key', $params['sslkey']);
69+
$this->assertEquals('client.crl', $params['sslcrl']);
70+
}
4271
}

0 commit comments

Comments
 (0)