From 3fa9cd5a41ee8468a02386b873836722cc468691 Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 20 Sep 2024 18:20:50 -0300 Subject: [PATCH 1/2] Make kamal use ssh keys from config when performing commands --- lib/kamal/commands/base.rb | 11 ++++++++++- test/commands/app_test.rb | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/kamal/commands/base.rb b/lib/kamal/commands/base.rb index c0eac91cc..8ef584aef 100644 --- a/lib/kamal/commands/base.rb +++ b/lib/kamal/commands/base.rb @@ -11,7 +11,7 @@ def initialize(config) end def run_over_ssh(*command, host:) - "ssh#{ssh_proxy_args} -t #{config.ssh.user}@#{host} -p #{config.ssh.port} '#{command.join(" ").gsub("'", "'\\\\''")}'" + "ssh#{ssh_proxy_args}#{ssh_keys_args} -t #{config.ssh.user}@#{host} -p #{config.ssh.port} '#{command.join(" ").gsub("'", "'\\\\''")}'" end def container_id_for(container_name:, only_running: false) @@ -94,5 +94,14 @@ def ssh_proxy_args " -o ProxyCommand='#{config.ssh.proxy.command_line_template}'" end end + + def ssh_keys_args + args = "" + config.ssh.keys&.each do |key| + args << " -i #{key}" + end + args << " -o IdentitiesOnly=yes" if config.ssh&.keys_only + args + end end end diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index 1fb59e8a0..aad31f8a7 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -315,6 +315,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") From e31b98539cfe5813a7efd17a08559ecfcf5f1fe4 Mon Sep 17 00:00:00 2001 From: Matteo Giaccone Date: Fri, 22 Nov 2024 09:57:45 +0100 Subject: [PATCH 2/2] Avoid string mutation For Ruby 3.4 --- lib/kamal/commands/base.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/kamal/commands/base.rb b/lib/kamal/commands/base.rb index 8ef584aef..6d3f71ec9 100644 --- a/lib/kamal/commands/base.rb +++ b/lib/kamal/commands/base.rb @@ -96,12 +96,13 @@ def ssh_proxy_args end def ssh_keys_args - args = "" - config.ssh.keys&.each do |key| - args << " -i #{key}" + "#{ ssh_keys.join("") if ssh_keys}" + "#{" -o IdentitiesOnly=yes" if config.ssh&.keys_only}" + end + + def ssh_keys + config.ssh.keys&.map do |key| + " -i #{key}" end - args << " -o IdentitiesOnly=yes" if config.ssh&.keys_only - args end end end