Skip to content

Commit be238f6

Browse files
Merge pull request #55256 from nextcloud/backport/55170/stable32
[stable32] feat(db): add SSL/TLS support for PostgreSQL
2 parents af64922 + e6e89d0 commit be238f6

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
@@ -2118,6 +2118,17 @@
21182118
*/
21192119
'mysql.collation' => null,
21202120

2121+
/**
2122+
* PostgreSQL SSL connection
2123+
*/
2124+
'pgsql_ssl' => [
2125+
'mode' => '',
2126+
'cert' => '',
2127+
'rootcert' => '',
2128+
'key' => '',
2129+
'crl' => '',
2130+
],
2131+
21212132
/**
21222133
* Database types supported for installation.
21232134
*

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
@@ -40,4 +40,33 @@ public function testSplitHostFromPortAndSocket($host, array $expected): void {
4040

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

0 commit comments

Comments
 (0)