From 01bbbffdb0c237e710e40f07dd0d933ac58def97 Mon Sep 17 00:00:00 2001 From: Mike Hermans Date: Thu, 8 Jun 2023 15:46:05 +0300 Subject: [PATCH 1/3] Add support for the proxyjump key --- features/aliases.feature | 30 ++++++++++++++++++++++++++++++ php/WP_CLI/Configurator.php | 2 ++ php/WP_CLI/Runner.php | 12 +++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/features/aliases.feature b/features/aliases.feature index 6c6ca38457..9eb0cb1f59 100644 --- a/features/aliases.feature +++ b/features/aliases.feature @@ -174,6 +174,36 @@ Feature: Create shortcuts to specific WordPress installs Error: No alias found with key '@someotherfoo'. """ + Scenario: Adds proxyjump to ssh command + Given a WP installation in 'foo' + And a wp-cli.yml file: + """ + @foo: + ssh: user@host:/path/to/wordpress + proxyjump: proxyhost + """ + + When I try `wp @foo --debug --version` + Then STDERR should contain: + """ + Running SSH command: ssh -q -J proxyhost + """ + + Scenario: Adds key to ssh command + Given a WP installation in 'foo' + And a wp-cli.yml file: + """ + @foo: + ssh: user@host:/path/to/wordpress + key: identityfile.key + """ + + When I try `wp @foo --debug --version` + Then STDERR should contain: + """ + Running SSH command: ssh -q -i identityfile.key + """ + Scenario: Add an alias Given a WP installation in 'foo' And a wp-cli.yml file: diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 575aebee4d..58e4fceb51 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -57,6 +57,8 @@ class Configurator { 'path', 'ssh', 'http', + 'proxyjump', + 'key', ]; /** diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index e77a6b90dc..4d1f1bc112 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -551,7 +551,7 @@ private function generate_ssh_command( $bits, $wp_command ) { $escaped_command = ''; // Set default values. - foreach ( [ 'scheme', 'user', 'host', 'port', 'path', 'key' ] as $bit ) { + foreach ( [ 'scheme', 'user', 'host', 'port', 'path', 'key', 'proxyjump' ] as $bit ) { if ( ! isset( $bits[ $bit ] ) ) { $bits[ $bit ] = null; } @@ -640,7 +640,17 @@ private function generate_ssh_command( $bits, $wp_command ) { $bits['host'] = $bits['user'] . '@' . $bits['host']; } + if ( ! empty( $this->alias ) ) { + $alias_config = isset( $this->aliases[ $this->alias ] ) ? $this->aliases[ $this->alias ] : false; + + if ( is_array( $alias_config ) ) { + $bits['proxyjump'] = isset( $alias_config['proxyjump'] ) ? $alias_config['proxyjump'] : ''; + $bits['key'] = isset( $alias_config['key'] ) ? $alias_config['key'] : ''; + } + } + $command_args = [ + $bits['proxyjump'] ? sprintf( '-J %s ', $bits['proxyjump'] ) : '', $bits['port'] ? '-p ' . (int) $bits['port'] . ' ' : '', $bits['key'] ? sprintf( '-i %s', $bits['key'] ) : '', $is_tty ? '-t' : '-T', From aa51cadf006cd0530bc6aa7949ab44895319c530 Mon Sep 17 00:00:00 2001 From: Mike Hermans Date: Thu, 8 Jun 2023 16:03:10 +0300 Subject: [PATCH 2/3] Escape shell args for proxyjump and key Co-authored-by: Niels de Blaauw --- php/WP_CLI/Runner.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 4d1f1bc112..15efe924b3 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -650,9 +650,9 @@ private function generate_ssh_command( $bits, $wp_command ) { } $command_args = [ - $bits['proxyjump'] ? sprintf( '-J %s ', $bits['proxyjump'] ) : '', + $bits['proxyjump'] ? sprintf( '-J %s ', escapeshellarg( $bits['proxyjump'] ) ) : '', $bits['port'] ? '-p ' . (int) $bits['port'] . ' ' : '', - $bits['key'] ? sprintf( '-i %s', $bits['key'] ) : '', + $bits['key'] ? sprintf( '-i %s', escapeshellarg( $bits['key'] ) ) : '', $is_tty ? '-t' : '-T', ]; From 25f55896039894c0bfc0c9ddc926640ec86bf676 Mon Sep 17 00:00:00 2001 From: Mike Hermans Date: Fri, 9 Jun 2023 11:07:10 +0300 Subject: [PATCH 3/3] Update tests to work with escaped shell args --- features/aliases.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/aliases.feature b/features/aliases.feature index 9eb0cb1f59..5f87078250 100644 --- a/features/aliases.feature +++ b/features/aliases.feature @@ -186,7 +186,7 @@ Feature: Create shortcuts to specific WordPress installs When I try `wp @foo --debug --version` Then STDERR should contain: """ - Running SSH command: ssh -q -J proxyhost + Running SSH command: ssh -q -J 'proxyhost' """ Scenario: Adds key to ssh command @@ -201,7 +201,7 @@ Feature: Create shortcuts to specific WordPress installs When I try `wp @foo --debug --version` Then STDERR should contain: """ - Running SSH command: ssh -q -i identityfile.key + Running SSH command: ssh -q -i 'identityfile.key' """ Scenario: Add an alias