Skip to content

Commit a7e8885

Browse files
authored
Merge pull request #198 from michaelw85/113_add_ssl_flag
2 parents c99b4fb + 9f3e367 commit a7e8885

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

features/config-create.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,16 @@ Feature: Create a wp-config file
242242
"""
243243
PasswordWith'SingleQuotes'
244244
"""
245+
246+
@require-mysql @require-mysql-5.7
247+
Scenario: Configure with required SSL connection
248+
Given an empty directory
249+
And WP files
250+
And I run `MYSQL_PWD='{DB_ROOT_PASSWORD}' MYSQL_HOST='{MYSQL_HOST}' MYSQL_TCP_PORT='{MYSQL_PORT}' mysql -u root -e "CREATE USER IF NOT EXISTS 'wp_cli_test_ssl'@'%' IDENTIFIED BY 'password2' REQUIRE SSL;"`
251+
252+
When I try `wp config create --dbhost=127.0.0.1 --dbname=wp_cli_test --dbuser=wp_cli_test_ssl --dbpass=password2 --ssl`
253+
Then the return code should be 0
254+
And the wp-config.php file should contain:
255+
"""
256+
define( 'DB_USER', 'wp_cli_test_ssl' )
257+
"""

src/Config_Command.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ private static function get_initial_locale() {
166166
* [--insecure]
167167
* : Retry API download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
168168
*
169+
* [--ssl]
170+
* : Use SSL when checking the database connection.
171+
*
169172
* ## EXAMPLES
170173
*
171174
* # Standard wp-config.php file
@@ -201,6 +204,7 @@ public function create( $_, $assoc_args ) {
201204
'dbcollate' => '',
202205
'locale' => self::get_initial_locale(),
203206
'config-file' => rtrim( ABSPATH, '/\\' ) . '/wp-config.php',
207+
'ssl' => false,
204208
];
205209
$assoc_args = array_merge( $defaults, $assoc_args );
206210
if ( empty( $assoc_args['dbprefix'] ) ) {
@@ -231,12 +235,18 @@ public function create( $_, $assoc_args ) {
231235
$host = substr( $host, 0, $socket_pos );
232236
}
233237

238+
$flags = 0;
239+
240+
if ( $assoc_args['ssl'] ) {
241+
$flags = MYSQLI_CLIENT_SSL;
242+
}
243+
234244
if ( file_exists( $socket ) ) {
235245
// If dbhost is a path to a socket
236-
mysqli_real_connect( $mysql, null, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, $socket );
246+
mysqli_real_connect( $mysql, null, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, $socket, $flags );
237247
} else {
238248
// If dbhost is a hostname or IP address
239-
mysqli_real_connect( $mysql, $host, $assoc_args['dbuser'], $assoc_args['dbpass'] );
249+
mysqli_real_connect( $mysql, $host, $assoc_args['dbuser'], $assoc_args['dbpass'], null, null, null, $flags );
240250
}
241251
} catch ( mysqli_sql_exception $exception ) {
242252
WP_CLI::error( 'Database connection error (' . $exception->getCode() . ') ' . $exception->getMessage() );

0 commit comments

Comments
 (0)