Skip to content

Commit

Permalink
[11.x] Added the ability to disable secure SSL connection for DB 8.0 …
Browse files Browse the repository at this point in the history
…versions and refactoring options

fixes (laravel#54269)

--ssl
--ssl-ca
  • Loading branch information
yuriizee committed Feb 7, 2025
1 parent 917a3fe commit 05532f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('MYSQL_ATTR_SSL_VERIFY_SERVER_CERT'),
]) : [],
],

Expand Down
37 changes: 33 additions & 4 deletions src/Illuminate/Database/Schema/MySqlSchemaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ protected function connectionString()
? ' --socket="${:LARAVEL_LOAD_SOCKET}"'
: ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"';

if (isset($config['options'][\PDO::MYSQL_ATTR_SSL_CA])) {
$value .= ' --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"';
}
$value .= $this->getAdditionalConnectionOptions($config['options']);

return $value;
}
Expand All @@ -135,7 +133,6 @@ protected function baseVariables(array $config)
'LARAVEL_LOAD_USER' => $config['username'],
'LARAVEL_LOAD_PASSWORD' => $config['password'] ?? '',
'LARAVEL_LOAD_DATABASE' => $config['database'],
'LARAVEL_LOAD_SSL_CA' => $config['options'][\PDO::MYSQL_ATTR_SSL_CA] ?? '',
];
}

Expand Down Expand Up @@ -174,4 +171,36 @@ protected function executeDumpProcess(Process $process, $output, array $variable

return $process;
}

/**
* Get allowed options with mapped mysql params
*
* @return string[]
*/
private function getOptionMap(): array
{
return [
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => '--ssl',
\PDO::MYSQL_ATTR_SSL_CA => '--ssl-ca',
];
}

/**
* Get mysql options connection string
*
* @param array $options
* @return string
*/
private function getAdditionalConnectionOptions(array $options): string
{
$connectionString = '';

foreach ($this->getOptionMap() as $optionKey => $optionValue) {
if (isset($options[$optionKey])) {
$connectionString .= sprintf(' %s=%s', $optionValue, $options[$optionKey]);
}
}

return $connectionString;
}
}

0 comments on commit 05532f4

Please sign in to comment.