From 9c5c9427feebe80607e76705c388ca08512840fc Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 20 Sep 2024 18:20:50 -0300 Subject: [PATCH] Make kamal use ssh keys from config when performing commands --- lib/kamal/commands/base.rb | 4 ++++ test/commands/app_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/kamal/commands/base.rb b/lib/kamal/commands/base.rb index 7521780a..5183e46e 100644 --- a/lib/kamal/commands/base.rb +++ b/lib/kamal/commands/base.rb @@ -17,6 +17,10 @@ def run_over_ssh(*command, host:) elsif config.ssh.proxy && config.ssh.proxy.is_a?(Net::SSH::Proxy::Command) cmd << " -o ProxyCommand='#{config.ssh.proxy.command_line_template}'" end + config.ssh.keys.each do |key| + cmd << " -i #{key}" + end + cmd << " -o IdentitiesOnly=yes" if config.ssh.keys_only cmd << " -t #{config.ssh.user}@#{host} -p #{config.ssh.port} '#{command.join(" ").gsub("'", "'\\\\''")}'" end end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 6704adb6..3b39daf9 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -291,6 +291,16 @@ class CommandsAppTest < ActiveSupport::TestCase assert_equal "ssh -J root@2.2.2.2 -t app@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") end + test "run over ssh with keys config" do + @config[:ssh] = { "keys" => ["path_to_key.pem"] } + assert_equal "ssh -i path_to_key.pem -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + end + + test "run over ssh with keys config with keys_only" do + @config[:ssh] = { "keys" => ["path_to_key.pem"], "keys_only" => true } + assert_equal "ssh -i path_to_key.pem -o IdentitiesOnly=yes -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1") + end + test "run over ssh with proxy_command" do @config[:ssh] = { "proxy_command" => "ssh -W %h:%p user@proxy-server" } assert_equal "ssh -o ProxyCommand='ssh -W %h:%p user@proxy-server' -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")