A free open source IT asset/license management system
Fixed #19411 - better handle conditional options in env/config
snipe8d4515d2fb75
diff --git a/config/database.php b/config/database.phpindex f773e6a04b..867abeea43 100755--- a/config/database.php+++ b/config/database.php@@ -33,6 +33,40 @@ if (env('DB_DUMP_SKIP_SSL') === 'true') {$dump_options['skip_ssl'] = true;}+// PDO SSL options for the app database connection. Each entry is only added+// when its env var is actually set - passing PDO::MYSQL_ATTR_SSL_* options+// with a null value is NOT the same as omitting them. libmysql / libmariadb+// tries to negotiate SSL against a null spec and the connect fails with+// "Cannot connect to MySQL using SSL".+// This shape also lets a user legitimately configure CA-only SSL by+// leaving KEY / CERT unset without breaking the client-cert branch below.+$ssl_options = [];+if (env('DB_SSL')) {+ if (env('DB_SSL_IS_PAAS')) {+ // Managed cloud DB flow (RDS, Azure Database, Cloud SQL): server CA+ // only, no client cert.+ if (env('DB_SSL_CA_PATH') !== null) {+ $ssl_options[PDO::MYSQL_ATTR_SSL_CA] = env('DB_SSL_CA_PATH');+ }+ $ssl_options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = (bool) env('DB_SSL_VERIFY_SERVER', false);+ } else {+ // Self-hosted flow: full mutual TLS with client key / cert.+ if (env('DB_SSL_KEY_PATH') !== null) {+ $ssl_options[PDO::MYSQL_ATTR_SSL_KEY] = env('DB_SSL_KEY_PATH');+ }+ if (env('DB_SSL_CERT_PATH') !== null) {+ $ssl_options[PDO::MYSQL_ATTR_SSL_CERT] = env('DB_SSL_CERT_PATH');+ }+ if (env('DB_SSL_CA_PATH') !== null) {+ $ssl_options[PDO::MYSQL_ATTR_SSL_CA] = env('DB_SSL_CA_PATH');+ }+ if (env('DB_SSL_CIPHER') !== null) {+ $ssl_options[PDO::MYSQL_ATTR_SSL_CIPHER] = env('DB_SSL_CIPHER');+ }+ $ssl_options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = (bool) env('DB_SSL_VERIFY_SERVER', false);+ }+}+return [/*@@ -107,16 +141,7 @@ return ['dump' => $dump_options,'dump_command_timeout' => 60 * 5, // 5 minute timeout'dump_using_single_transaction' => true, // perform dump using a single transaction- 'options' => (env('DB_SSL')) ? ((env('DB_SSL_IS_PAAS')) ? [- PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'), // /path/to/ca.pem- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false), // true/false- ] : [- PDO::MYSQL_ATTR_SSL_KEY => env('DB_SSL_KEY_PATH'), // /path/to/key.pem- PDO::MYSQL_ATTR_SSL_CERT => env('DB_SSL_CERT_PATH'), // /path/to/cert.pem- PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'), // /path/to/ca.pem- PDO::MYSQL_ATTR_SSL_CIPHER => env('DB_SSL_CIPHER'),- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false), // true/false- ]) : [],+ 'options' => $ssl_options,],// Use this connection (DB_CONNECTION=mariadb) when the server is@@ -139,16 +164,7 @@ return ['dump' => $dump_options,'dump_command_timeout' => 60 * 5,'dump_using_single_transaction' => true,- 'options' => (env('DB_SSL')) ? ((env('DB_SSL_IS_PAAS')) ? [- PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'),- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false),- ] : [- PDO::MYSQL_ATTR_SSL_KEY => env('DB_SSL_KEY_PATH'),- PDO::MYSQL_ATTR_SSL_CERT => env('DB_SSL_CERT_PATH'),- PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'),- PDO::MYSQL_ATTR_SSL_CIPHER => env('DB_SSL_CIPHER'),- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false),- ]) : [],+ 'options' => $ssl_options,],'pgsql' => [diff --git a/tests/Unit/DatabaseSslConfigurationTest.php b/tests/Unit/DatabaseSslConfigurationTest.phpindex 4e408f23c1..4ee4858cb6 100644--- a/tests/Unit/DatabaseSslConfigurationTest.php+++ b/tests/Unit/DatabaseSslConfigurationTest.php@@ -5,97 +5,156 @@ namespace Tests\Unit;use PDO;use Tests\TestCase;+/**+ * Coverage for the PDO SSL options block in config/database.php. Loads the+ * real config file under different env combinations rather than a hand-rolled+ * simulator, so the tests would actually catch a regression in the file+ * itself (which the earlier simulator-based version could not, see #19411).+ */class DatabaseSslConfigurationTest extends TestCase{- public function test_mysql_ssl_configuration_with_paas_mode(): void+ /**+ * Set the env vars this test needs, re-require the config file, and+ * return the fully-built connections array. Any env key not passed is+ * unset so it doesn't leak in from the surrounding test env.+ *+ * @param array<string, string|null> $env+ * @return array<string, mixed>+ */+ private function loadDatabaseConfig(array $env): array{- config([- 'database.connections.mysql.options' => null,- ]);+ $keys = [+ 'DB_SSL',+ 'DB_SSL_IS_PAAS',+ 'DB_SSL_KEY_PATH',+ 'DB_SSL_CERT_PATH',+ 'DB_SSL_CA_PATH',+ 'DB_SSL_CIPHER',+ 'DB_SSL_VERIFY_SERVER',+ ];- // Test PAAS mode SSL configuration- config([- 'database.connections.mysql.options' => $this->getSslOptions(true, false, true), // isPaas=true, verifyServer=false, sslEnabled=true- ]);+ foreach ($keys as $key) {+ unset($_ENV[$key], $_SERVER[$key]);+ putenv($key);+ }- $options = config('database.connections.mysql.options');+ foreach ($env as $key => $value) {+ if ($value === null) {+ $_ENV[$key] = 'null';+ $_SERVER[$key] = 'null';+ putenv("$key=null");+ } else {+ $_ENV[$key] = $value;+ $_SERVER[$key] = $value;+ putenv("$key=$value");+ }+ }- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options);- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);+ return require base_path('config/database.php');+ }++ public function test_ssl_disabled_yields_no_pdo_options(): void+ {+ $config = $this->loadDatabaseConfig(['DB_SSL' => 'false']);++ $this->assertSame([], $config['connections']['mysql']['options']);+ $this->assertSame([], $config['connections']['mariadb']['options']);+ }++ public function test_ssl_enabled_paas_mode_includes_ca_and_verify_only(): void+ {+ $config = $this->loadDatabaseConfig([+ 'DB_SSL' => 'true',+ 'DB_SSL_IS_PAAS' => 'true',+ 'DB_SSL_CA_PATH' => '/path/to/ca.pem',+ 'DB_SSL_VERIFY_SERVER' => 'true',+ ]);- // PAAS mode should not include client certificate attributes+ $options = $config['connections']['mysql']['options'];++ $this->assertSame('/path/to/ca.pem', $options[PDO::MYSQL_ATTR_SSL_CA]);+ $this->assertTrue($options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]);$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options);$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options);$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);}- public function test_mysql_ssl_configuration_with_non_paas_mode(): void+ public function test_ssl_enabled_self_hosted_mode_includes_full_client_cert_set(): void{- config([- 'database.connections.mysql.options' => null,+ $config = $this->loadDatabaseConfig([+ 'DB_SSL' => 'true',+ 'DB_SSL_KEY_PATH' => '/path/to/key.pem',+ 'DB_SSL_CERT_PATH' => '/path/to/cert.pem',+ 'DB_SSL_CA_PATH' => '/path/to/ca.pem',+ 'DB_SSL_CIPHER' => 'ECDHE-RSA-AES256-GCM-SHA384',]);- // Test non-PAAS mode SSL configuration- config([- 'database.connections.mysql.options' => $this->getSslOptions(false, false, true), // isPaas=false, verifyServer=false, sslEnabled=true- ]);+ $options = $config['connections']['mysql']['options'];- $options = config('database.connections.mysql.options');-- // Non-PAAS mode should include all SSL attributes- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options);- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options);- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options);- $this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);+ $this->assertSame('/path/to/key.pem', $options[PDO::MYSQL_ATTR_SSL_KEY]);+ $this->assertSame('/path/to/cert.pem', $options[PDO::MYSQL_ATTR_SSL_CERT]);+ $this->assertSame('/path/to/ca.pem', $options[PDO::MYSQL_ATTR_SSL_CA]);+ $this->assertSame('ECDHE-RSA-AES256-GCM-SHA384', $options[PDO::MYSQL_ATTR_SSL_CIPHER]);}- public function test_mysql_ssl_configuration_without_ssl(): void+ public function test_null_cipher_is_omitted_not_passed_as_null(): void{- config([- 'database.connections.mysql.options' => null,- ]);-- // Test SSL disabled configuration- config([- 'database.connections.mysql.options' => $this->getSslOptions(true, false, false), // isPaas=true, verifyServer=false, sslEnabled=false+ // Regression for #19411. .env.example ships DB_SSL_CIPHER=null and+ // the old code passed that through as PDO::MYSQL_ATTR_SSL_CIPHER =>+ // null, which made libmysql / libmariadb fail with "Cannot connect+ // to MySQL using SSL". A null cipher MUST be omitted so the driver+ // negotiates a default.+ $config = $this->loadDatabaseConfig([+ 'DB_SSL' => 'true',+ 'DB_SSL_KEY_PATH' => '/path/to/key.pem',+ 'DB_SSL_CERT_PATH' => '/path/to/cert.pem',+ 'DB_SSL_CA_PATH' => '/path/to/ca.pem',+ 'DB_SSL_CIPHER' => null,]);- $options = config('database.connections.mysql.options');+ $options = $config['connections']['mysql']['options'];- // When SSL is disabled, options should be empty- $this->assertEmpty($options);+ $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);}- public function test_ssl_verify_server_defaults_to_false(): void+ public function test_null_paths_are_omitted_across_every_ssl_key(): void{- // Test that SSL_VERIFY_SERVER defaults to false when not explicitly set- $options = $this->getSslOptions(true, null, true); // isPaas=true, verifyServer=null, sslEnabled=true+ // Same shape trap as CIPHER: any of the path envs left at the+ // .env.example default of `null` would land as PDO::MYSQL_ATTR_SSL_*+ // => null and break the SSL handshake. Every path key gets the+ // omit-when-null treatment.+ $config = $this->loadDatabaseConfig([+ 'DB_SSL' => 'true',+ 'DB_SSL_KEY_PATH' => null,+ 'DB_SSL_CERT_PATH' => null,+ 'DB_SSL_CA_PATH' => null,+ 'DB_SSL_CIPHER' => null,+ ]);+ $options = $config['connections']['mysql']['options'];++ $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options);+ $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options);+ $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CA, $options);+ $this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);+ // VERIFY_SERVER_CERT is always included with a bool cast because+ // both true and false are valid values.$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);$this->assertFalse($options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]);}- private function getSslOptions(bool $isPaas, ?bool $verifyServer = false, bool $sslEnabled = true): array+ public function test_mariadb_connection_shares_the_same_ssl_options(): void{- // simulates the SSL options logic from database.php- if (! $sslEnabled) {- return [];- }-- if ($isPaas) {- return [- PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem',- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false,- ];- }+ $config = $this->loadDatabaseConfig([+ 'DB_SSL' => 'true',+ 'DB_SSL_KEY_PATH' => '/path/to/key.pem',+ 'DB_SSL_CERT_PATH' => '/path/to/cert.pem',+ 'DB_SSL_CA_PATH' => '/path/to/ca.pem',+ ]);- return [- PDO::MYSQL_ATTR_SSL_KEY => '/path/to/key.pem',- PDO::MYSQL_ATTR_SSL_CERT => '/path/to/cert.pem',- PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem',- PDO::MYSQL_ATTR_SSL_CIPHER => null,- PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false,- ];+ $this->assertSame(+ $config['connections']['mysql']['options'],+ $config['connections']['mariadb']['options']+ );}}