Skip to content

Commit

Permalink
Some mariadb client tools do not support --ssl-mode option
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuench committed Oct 23, 2024
1 parent 7f5c129 commit 470541c
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions src/N98/Util/Console/Helper/DatabaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace N98\Util\Console\Helper;

use InvalidArgumentException;
use Magento\Framework\Exception\FileSystemException;
use N98\Util\Exec;
use PDO;
use PDOException;
use PDOStatement;
Expand Down Expand Up @@ -57,7 +59,7 @@ public function setConnectionType($connectionType)
* @param OutputInterface|null $output
*
* @throws RuntimeException
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
* @return void
*/
public function detectDbSettings(OutputInterface $output)
Expand Down Expand Up @@ -113,7 +115,7 @@ public function detectDbSettings(OutputInterface $output)
* @param bool $reconnect = false
* @return PDO
* @throws RuntimeException pdo mysql extension is not installed
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getConnection(OutputInterface $output = null, bool $reconnect = false)
{
Expand Down Expand Up @@ -160,7 +162,7 @@ public function getConnection(OutputInterface $output = null, bool $reconnect =
$this->dbSettings['password'],
$connectionOptions
);
$this->_connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

/** @link http://bugs.mysql.com/bug.php?id=18551 */
$this->_connection->query("SET SQL_MODE=''");
Expand Down Expand Up @@ -201,7 +203,7 @@ public function getTableName($tableName)
*
* @see Zend_Db_Adapter_Pdo_Abstract
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function dsn()
{
Expand Down Expand Up @@ -240,7 +242,7 @@ public function dsn()
* @param string $privilege
*
* @return bool
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function mysqlUserHasPrivilege($privilege)
{
Expand All @@ -258,9 +260,31 @@ public function mysqlUserHasPrivilege($privilege)
return false;
}

/**
* Check if the toolstack is using mariadb
*
* @return bool
*/
public function isMariaDbClientToolUsed(): bool
{
Exec::run('mysqldump --help', $output, $exitCode);
return strpos($output, 'MariaDB') !== false;
}

/**
* Some versions of mysqldump shipped by MariaDB are not able to handle the --ssl-mode option
*
* @return bool
*/
public function isSslModeOptionSupported(): bool
{
Exec::run('mysqldump --help', $output, $exitCode);
return strpos($output, '--ssl-mode') !== false;
}

/**
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getMysqlClientToolConnectionString()
{
Expand Down Expand Up @@ -303,15 +327,15 @@ public function getMysqlClientToolConnectionString()
}
}

$isSslModeSupported = $this->isSslModeOptionSupported();

// see https://dev.mysql.com/doc/refman/8.0/en/connection-options.html#option_general_ssl-mode
if (array_key_exists(PDO::MYSQL_ATTR_SSL_CA, $connectionOptions)) {
if (!array_key_exists(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $connectionOptions)
|| $connectionOptions[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]
) {
$sslOptions[] = '--ssl-mode=VERIFY_CA';
} else {
$sslOptions[] = '--ssl-mode=REQUIRED';
}
// see https://dev.mysql.com/doc/refman/8.0/en/connection-options.html#option_general_ssl-mode
if ($isSslModeSupported && array_key_exists(PDO::MYSQL_ATTR_SSL_CA, $connectionOptions)) {
$sslOptions[] = !array_key_exists(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $connectionOptions)
|| $connectionOptions[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]
? '--ssl-mode=VERIFY_CA'
: '--ssl-mode=REQUIRED';
}

$string .= ' ' . implode(' ', $sslOptions)
Expand All @@ -333,7 +357,7 @@ public function getMysqlClientToolConnectionString()
* @param string $variable
*
* @return bool|array returns array on success, false on failure
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getMysqlVariableValue($variable)
{
Expand Down Expand Up @@ -363,7 +387,7 @@ public function getMysqlVariableValue($variable)
* @return string variable value, null if variable was not defined
* @throws RuntimeException in case a system variable is unknown (SQLSTATE[HY000]: 1193: Unknown system variable
* 'nonexistent')
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getMysqlVariable($name, $type = null)
{
Expand Down Expand Up @@ -455,7 +479,7 @@ public function getTableDefinitions(array $commandConfig)
*
* @return array
* @throws RuntimeException
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function resolveTables(array $list, array $definitions = [], array $resolved = [])
{
Expand Down Expand Up @@ -566,7 +590,7 @@ private function resolveTablesArray(array $carry = null, $item = null)
*
* @return array
* @throws RuntimeException
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getTables($withoutPrefix = null)
{
Expand Down Expand Up @@ -658,7 +682,7 @@ private function quoteLike($string, $escape = '=')
* @param bool $withoutPrefix
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getTablesStatus($withoutPrefix = false)
{
Expand Down Expand Up @@ -719,7 +743,7 @@ public function getName()

/**
* @param OutputInterface $output
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function dropDatabase(OutputInterface $output)
{
Expand All @@ -731,7 +755,7 @@ public function dropDatabase(OutputInterface $output)

/**
* @param OutputInterface $output
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function dropTables(OutputInterface $output)
{
Expand All @@ -749,7 +773,7 @@ public function dropTables(OutputInterface $output)

/**
* @param OutputInterface $output
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function createDatabase(OutputInterface $output)
{
Expand All @@ -764,7 +788,7 @@ public function createDatabase(OutputInterface $output)
* @param string|null $variable [optional]
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
private function runShowCommand($command, $variable = null)
{
Expand Down Expand Up @@ -800,7 +824,7 @@ private function runShowCommand($command, $variable = null)
* @param string|null $variable [optional]
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getGlobalVariables($variable = null)
{
Expand All @@ -811,7 +835,7 @@ public function getGlobalVariables($variable = null)
* @param string|null $variable [optional]
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
* @throws FileSystemException
*/
public function getGlobalStatus($variable = null)
{
Expand Down

0 comments on commit 470541c

Please sign in to comment.