Skip to content

Commit 5b5695f

Browse files
Fixed #18312: support aws rds mysql with force tls
1 parent 35fdca3 commit 5b5695f

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,26 @@ DB_SANITIZE_BY_DEFAULT=false
4040
# --------------------------------------------
4141
# OPTIONAL: SSL DATABASE SETTINGS
4242
# --------------------------------------------
43+
# Enable SSL connection to database (true/false)
4344
DB_SSL=false
45+
46+
# Set to true for cloud databases like AWS RDS, Azure Database, Google Cloud SQL
47+
# Set to false for self-hosted databases with client certificates
4448
DB_SSL_IS_PAAS=false
49+
50+
# Required when DB_SSL_IS_PAAS=false (client certificate authentication)
4551
DB_SSL_KEY_PATH=null
4652
DB_SSL_CERT_PATH=null
53+
54+
# Path to CA certificate bundle (required for SSL connections)
55+
# For AWS RDS, download from: https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
4756
DB_SSL_CA_PATH=null
57+
58+
# SSL cipher (optional, leave null for default)
4859
DB_SSL_CIPHER=null
60+
61+
# Verify server certificate (true/false, defaults to false if not set)
62+
# Set to false for development or when using self-signed certificates
4963
DB_SSL_VERIFY_SERVER=null
5064

5165
# --------------------------------------------

config/database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@
101101
'dump_using_single_transaction' => true, // perform dump using a single transaction
102102
'options' => (env('DB_SSL')) ? ((env('DB_SSL_IS_PAAS')) ? [
103103
PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'), // /path/to/ca.pem
104+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false), //true/false
104105
] : [
105106
PDO::MYSQL_ATTR_SSL_KEY => env('DB_SSL_KEY_PATH'), // /path/to/key.pem
106107
PDO::MYSQL_ATTR_SSL_CERT => env('DB_SSL_CERT_PATH'), // /path/to/cert.pem
107108
PDO::MYSQL_ATTR_SSL_CA => env('DB_SSL_CA_PATH'), // /path/to/ca.pem
108109
PDO::MYSQL_ATTR_SSL_CIPHER => env('DB_SSL_CIPHER'),
109-
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER'), //true/false
110+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => env('DB_SSL_VERIFY_SERVER', false), //true/false
110111
]) : [],
111112
],
112113

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use PDO;
6+
use Tests\TestCase;
7+
8+
class DatabaseSslConfigurationTest extends TestCase
9+
{
10+
public function testMysqlSslConfigurationWithPaasMode(): void
11+
{
12+
config([
13+
'database.connections.mysql.options' => null
14+
]);
15+
16+
// Test PAAS mode SSL configuration
17+
config([
18+
'database.connections.mysql.options' => $this->getSslOptions(true, false, true) // isPaas=true, verifyServer=false, sslEnabled=true
19+
]);
20+
21+
$options = config('database.connections.mysql.options');
22+
23+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options);
24+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);
25+
26+
// PAAS mode should not include client certificate attributes
27+
$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options);
28+
$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options);
29+
$this->assertArrayNotHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);
30+
}
31+
32+
public function testMysqlSslConfigurationWithNonPaasMode(): void
33+
{
34+
config([
35+
'database.connections.mysql.options' => null
36+
]);
37+
38+
// Test non-PAAS mode SSL configuration
39+
config([
40+
'database.connections.mysql.options' => $this->getSslOptions(false, false, true) // isPaas=false, verifyServer=false, sslEnabled=true
41+
]);
42+
43+
$options = config('database.connections.mysql.options');
44+
45+
// Non-PAAS mode should include all SSL attributes
46+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CA, $options);
47+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);
48+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_KEY, $options);
49+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CERT, $options);
50+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_CIPHER, $options);
51+
}
52+
53+
public function testMysqlSslConfigurationWithoutSsl(): void
54+
{
55+
config([
56+
'database.connections.mysql.options' => null
57+
]);
58+
59+
// Test SSL disabled configuration
60+
config([
61+
'database.connections.mysql.options' => $this->getSslOptions(true, false, false) // isPaas=true, verifyServer=false, sslEnabled=false
62+
]);
63+
64+
$options = config('database.connections.mysql.options');
65+
66+
// When SSL is disabled, options should be empty
67+
$this->assertEmpty($options);
68+
}
69+
70+
public function testSslVerifyServerDefaultsToFalse(): void
71+
{
72+
// Test that SSL_VERIFY_SERVER defaults to false when not explicitly set
73+
$options = $this->getSslOptions(true, null, true); // isPaas=true, verifyServer=null, sslEnabled=true
74+
75+
$this->assertArrayHasKey(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, $options);
76+
$this->assertFalse($options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]);
77+
}
78+
79+
private function getSslOptions(bool $isPaas, ?bool $verifyServer=false, bool $sslEnabled=true): array
80+
{
81+
// simulates the SSL options logic from database.php
82+
if (!$sslEnabled) {
83+
return [];
84+
}
85+
86+
if ($isPaas) {
87+
return [
88+
PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem',
89+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false,
90+
];
91+
}
92+
93+
return [
94+
PDO::MYSQL_ATTR_SSL_KEY => '/path/to/key.pem',
95+
PDO::MYSQL_ATTR_SSL_CERT => '/path/to/cert.pem',
96+
PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca.pem',
97+
PDO::MYSQL_ATTR_SSL_CIPHER => null,
98+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => $verifyServer ?? false,
99+
];
100+
}
101+
}

0 commit comments

Comments
 (0)