MDL-54704 dml: support for SSL with MySQL / MariaDB and PostgreSQL

This commit is contained in:
Srdjan
2023-06-30 15:41:39 +10:00
parent 8dbb6183ff
commit bb1ecd8ea4
5 changed files with 152 additions and 3 deletions
+17
View File
@@ -44,6 +44,14 @@ class pgsql_native_moodle_database extends moodle_database {
query_end as read_slave_query_end;
}
/** @var array $sslmodes */
private static $sslmodes = [
'disable',
'prefer',
'require',
'verify-full'
];
/** @var array $serverinfo cache */
private $serverinfo = [];
@@ -130,6 +138,7 @@ class pgsql_native_moodle_database extends moodle_database {
* @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
* @param array $dboptions driver specific options
* @return bool true
* @throws moodle_exception
* @throws dml_connection_exception if error
*/
public function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, array $dboptions=null): bool {
@@ -187,6 +196,14 @@ class pgsql_native_moodle_database extends moodle_database {
$connection .= " options='" . implode(' ', $options) . "'";
}
if (isset($this->dboptions['ssl'])) {
$sslmode = $this->dboptions['ssl'];
if (!in_array($sslmode, self::$sslmodes, true)) {
throw new moodle_exception('validateerrorlist', 'admin', '', "'dboptions''ssl': $sslmode");
}
$connection .= " sslmode=$sslmode";
}
ob_start();
// It seems that pg_connect() handles some errors differently.
// For example, name resolution error will raise an exception, and non-existing