Skip to content

Commit 846f433

Browse files
committed
Improve mariadb client detection and only add ssl-verify-server-cert flag when using mariadb client
1 parent 398ef6e commit 846f433

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/Sql/SqlMariaDB.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Drush\Sql;
66

7+
use PDO;
8+
79
class SqlMariaDB extends SqlMysql
810
{
911
public function command(): string
@@ -15,4 +17,25 @@ public function dumpProgram(): string
1517
{
1618
return 'mariadb-dump';
1719
}
20+
21+
public function creds($hide_password = TRUE): string {
22+
$parameters = parent::creds($hide_password);
23+
24+
$dbSpec = $this->getDbSpec();
25+
$attribs = [
26+
'ssl_verify_server_cert' => (defined('Pdo\Mysql::ATTR_SSL_CA') ? Pdo\Mysql::ATTR_SSL_VERIFY_SERVER_CERT : PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT),
27+
];
28+
29+
// MariaDB >= 11.4 enables TLS by default. Its client also by default
30+
// verifies the server certificate.
31+
// If SSL/TLS server certificate verification is explicitly disabled
32+
// for the PDO connection used by Drupal, also explicitly disable it
33+
// for the mariadb client.
34+
if (($dbSpec['pdo'][$attribs['ssl_verify_server_cert']] ?? NULL) === FALSE) {
35+
$parameters .= ' ' . $this->paramsToOptions(['ssl-verify-server-cert' => 'false']);
36+
}
37+
38+
return $parameters;
39+
}
40+
1841
}

src/Sql/SqlMysql.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Drush\Sql;
66

7+
use Consolidation\SiteProcess\Util\Escape;
78
use Drush\Drush;
89
use Drush\Exec\ExecTrait;
910
use PDO;
@@ -21,11 +22,26 @@ class SqlMysql extends SqlBase
2122
*/
2223
public static function make(array $dbSpec, array $options): SqlMysql|SqlMariaDB
2324
{
24-
// If the mysql version reports that it is MariaDB, use MariaDB as client.
25-
$process = Drush::shell('mysql --version');
25+
$process = Drush::shell(Escape::isWindows() ? 'where mysql' : 'command -v mysql');
2626
$process->setSimulated(false);
2727
$process->run();
28-
if ((!$process->isSuccessful() || str_contains($process->getOutput(), 'MariaDB')) && self::programExists('mariadb') && self::programExists('mariadb-dump')) {
28+
if ($process->isSuccessful()) {
29+
$clientPath = $process->getOutput();
30+
// The mysql client might be symlinked to a mariadb client.
31+
// For example see https://pkgs.alpinelinux.org/package/edge/main/x86/mysql-client
32+
if (is_link($clientPath)) {
33+
$clientPath = readlink($clientPath) ?: throw new \Exception('Failed to read symlink ' . $clientPath);
34+
}
35+
$client = basename($clientPath);
36+
}
37+
elseif (self::programExists('mariadb')) {
38+
$client = 'mariadb';
39+
}
40+
else {
41+
throw new \Exception('Failed to locate mysql client');
42+
}
43+
44+
if ($client === 'mariadb') {
2945
$instance = new SqlMariaDB($dbSpec, $options);
3046
} else {
3147
$instance = new self($dbSpec, $options);
@@ -115,7 +131,6 @@ public function creds($hide_password = true): string
115131
'ssl_cert' => (defined('Pdo\Mysql::ATTR_SSL_CA') ? Pdo\Mysql::ATTR_SSL_CERT : PDO::MYSQL_ATTR_SSL_CERT),
116132
'ssl_cipher' => (defined('Pdo\Mysql::ATTR_SSL_CA') ? Pdo\Mysql::ATTR_SSL_CIPHER : PDO::MYSQL_ATTR_SSL_CIPHER),
117133
'ssl_key' => (defined('Pdo\Mysql::ATTR_SSL_CA') ? Pdo\Mysql::ATTR_SSL_KEY : PDO::MYSQL_ATTR_SSL_KEY),
118-
'ssl_verify_server_cert' => (defined('Pdo\Mysql::ATTR_SSL_CA') ? Pdo\Mysql::ATTR_SSL_VERIFY_SERVER_CERT : PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT),
119134
];
120135

121136
if (!empty($dbSpec['pdo'][$attribs['ssl_ca']])) {
@@ -138,10 +153,6 @@ public function creds($hide_password = true): string
138153
$parameters['ssl-key'] = $dbSpec['pdo'][$attribs['ssl_key']];
139154
}
140155

141-
if (!empty($dbSpec['pdo'][$attribs['ssl_verify_server_cert']])) {
142-
$parameters['ssl-verify-server-cert'] = $dbSpec['pdo'][$attribs['ssl_verify_server_cert']];
143-
}
144-
145156
return $this->paramsToOptions($parameters);
146157
}
147158

0 commit comments

Comments
 (0)