From 6a7a5b4cc3be737441b5118ea64ea550a6bc9f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= <1005065+DeepDiver1975@users.noreply.github.com> Date: Thu, 3 Mar 2022 15:11:25 +0100 Subject: [PATCH] [9.x] Fix MySqlSchemaState does not add --ssl-ca to mysql cli (#41315) * fix: MySqlSchemaState does not add --ssl-ca to mysql cli it not configured * Update MySqlSchemaState.php Co-authored-by: Taylor Otwell --- .../Database/Schema/MySqlSchemaState.php | 10 ++- .../Database/DatabaseMySqlSchemaStateTest.php | 86 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 tests/Database/DatabaseMySqlSchemaStateTest.php diff --git a/src/Illuminate/Database/Schema/MySqlSchemaState.php b/src/Illuminate/Database/Schema/MySqlSchemaState.php index 4ea985f48e84..d28ab10ad0f2 100644 --- a/src/Illuminate/Database/Schema/MySqlSchemaState.php +++ b/src/Illuminate/Database/Schema/MySqlSchemaState.php @@ -103,9 +103,15 @@ protected function connectionString() { $value = ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}"'; - $value .= $this->connection->getConfig()['unix_socket'] ?? false + $config = $this->connection->getConfig(); + + $value .= $config['unix_socket'] ?? false ? ' --socket="${:LARAVEL_LOAD_SOCKET}"' - : ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"'; + : ' --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"'; + + if (isset($config['options'][\PDO::MYSQL_ATTR_SSL_CA])) { + $value .= ' --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"'; + } return $value; } diff --git a/tests/Database/DatabaseMySqlSchemaStateTest.php b/tests/Database/DatabaseMySqlSchemaStateTest.php new file mode 100644 index 000000000000..fe3f4c8b20d9 --- /dev/null +++ b/tests/Database/DatabaseMySqlSchemaStateTest.php @@ -0,0 +1,86 @@ +createMock(MySqlConnection::class); + $connection->method('getConfig')->willReturn($dbConfig); + + $schemaState = new MySqlSchemaState($connection); + + // test connectionString + $method = new \ReflectionMethod(get_class($schemaState), 'connectionString'); + $connString = tap($method)->setAccessible(true)->invoke($schemaState); + + self::assertEquals($expectedConnectionString, $connString); + + // test baseVariables + $method = new \ReflectionMethod(get_class($schemaState), 'baseVariables'); + $variables = tap($method)->setAccessible(true)->invoke($schemaState, $dbConfig); + + self::assertEquals($expectedVariables, $variables); + } + + public function provider(): \Generator + { + yield 'default' => [ + ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}"', [ + 'LARAVEL_LOAD_SOCKET' => '', + 'LARAVEL_LOAD_HOST' => '127.0.0.1', + 'LARAVEL_LOAD_PORT' => '', + 'LARAVEL_LOAD_USER' => 'root', + 'LARAVEL_LOAD_PASSWORD' => '', + 'LARAVEL_LOAD_DATABASE' => 'forge', + 'LARAVEL_LOAD_SSL_CA' => '', + ], [ + 'username' => 'root', + 'host' => '127.0.0.1', + 'database' => 'forge', + ], + ]; + + yield 'ssl_ca' => [ + ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --host="${:LARAVEL_LOAD_HOST}" --port="${:LARAVEL_LOAD_PORT}" --ssl-ca="${:LARAVEL_LOAD_SSL_CA}"', [ + 'LARAVEL_LOAD_SOCKET' => '', + 'LARAVEL_LOAD_HOST' => '', + 'LARAVEL_LOAD_PORT' => '', + 'LARAVEL_LOAD_USER' => 'root', + 'LARAVEL_LOAD_PASSWORD' => '', + 'LARAVEL_LOAD_DATABASE' => 'forge', + 'LARAVEL_LOAD_SSL_CA' => 'ssl.ca', + ], [ + 'username' => 'root', + 'database' => 'forge', + 'options' => [ + \PDO::MYSQL_ATTR_SSL_CA => 'ssl.ca', + ], + ], + ]; + + yield 'unix socket' => [ + ' --user="${:LARAVEL_LOAD_USER}" --password="${:LARAVEL_LOAD_PASSWORD}" --socket="${:LARAVEL_LOAD_SOCKET}"', [ + 'LARAVEL_LOAD_SOCKET' => '/tmp/mysql.sock', + 'LARAVEL_LOAD_HOST' => '', + 'LARAVEL_LOAD_PORT' => '', + 'LARAVEL_LOAD_USER' => 'root', + 'LARAVEL_LOAD_PASSWORD' => '', + 'LARAVEL_LOAD_DATABASE' => 'forge', + 'LARAVEL_LOAD_SSL_CA' => '', + ], [ + 'username' => 'root', + 'database' => 'forge', + 'unix_socket' => '/tmp/mysql.sock', + ], + ]; + } +}