From 71681cb8be92fe623919b45c74a539ea9febff0b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 31 Jan 2023 13:59:19 +0100 Subject: [PATCH] Use single string-based proxy declaration --- README.md | 14 ++++++++++---- lib/mrsk/configuration.rb | 27 ++++++++------------------- test/configuration_test.rb | 7 ++++++- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e5783cb0..d9110c0f 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,20 @@ ssh: user: app ``` -### Using a bastion/proxy/jump SSH host +### Using a proxy SSH host -If you need to connect to server through a bastion host, you can use `ssh/proxy_host`: +If you need to connect to server through a proxy host, you can use `ssh/proxy`: ```yaml ssh: - proxy_host: 192.168.0.1 - user_proxy_host: app # defaults to root + proxy: "192.168.0.1" # defaults to root as the user +``` + +Or with specific user: + +```yaml +ssh: + proxy: "app@192.168.0.1" ``` ### Using env variables diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index 71f9909f..1f03d8f4 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -112,26 +112,15 @@ class Mrsk::Configuration end end + def ssh_proxy + if raw_config.ssh.present? && raw_config.ssh["proxy"] + Net::SSH::Proxy::Jump.new \ + raw_config.ssh["proxy"].include?("@") ? raw_config.ssh["proxy"] : "root@#{raw_config.ssh["proxy"]}" + end + end + def ssh_options - options = { user: ssh_user, auth_methods: [ "publickey" ] } - - options[:proxy] = ::Net::SSH::Proxy::Jump.new(ssh_proxy_host) if ssh_proxy_host - - options - end - - def ssh_proxy_host - if raw_config.ssh && raw_config.ssh["proxy_host"] - "#{ssh_user_proxy_host}@#{raw_config.ssh['proxy_host']}" - end - end - - def ssh_user_proxy_host - if raw_config.ssh.present? - raw_config.ssh["user_proxy_host"] || "root" - else - "root" - end + { user: ssh_user, proxy: ssh_proxy, auth_methods: [ "publickey" ] }.compact end def master_key diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 64f81454..937abed0 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -145,10 +145,15 @@ class ConfigurationTest < ActiveSupport::TestCase end test "ssh options with proxy host" do - config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "proxy_host" => "1.2.3.4" }) }) + config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "proxy" => "1.2.3.4" }) }) assert_equal "root@1.2.3.4", @config.ssh_options[:proxy].jump_proxies end + test "ssh options with proxy host and user" do + config = Mrsk::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "proxy" => "app@1.2.3.4" }) }) + assert_equal "app@1.2.3.4", @config.ssh_options[:proxy].jump_proxies + end + test "master key" do assert_equal "456", @config.master_key end