Skip to content

Commit

Permalink
Allow ssh/config to additionally accept a string
Browse files Browse the repository at this point in the history
This is accepted by Net::SSH, research done by @jeremy in #908 (comment)

This is already documented as working correctly in https://github.com/basecamp/kamal/blob/74a06b0ccda616c86ebe1729d0795f39bcac9f00/lib/kamal/configuration/docs/ssh.yml#L65-L70

However, before this change only booleans were allowed because of the example configuration file.
  • Loading branch information
Burgestrand committed Oct 28, 2024
1 parent e8a41af commit 3a634ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/kamal/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def ssh_config_args
""
when false
" -F none"
when String
" -F #{Shellwords.escape(config.ssh.config)}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/configuration/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Kamal::Configuration::Ssh

def initialize(config:)
@ssh_config = config.raw_config.ssh || {}
validate! ssh_config
validate! ssh_config, with: Kamal::Configuration::Validator::Ssh
end

def user
Expand Down
19 changes: 19 additions & 0 deletions lib/kamal/configuration/validator/ssh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Kamal::Configuration::Validator::Ssh < Kamal::Configuration::Validator
SPECIAL_KEYS = [ "config" ]

def validate!
validate_against_example! \
config.except(*SPECIAL_KEYS),
example.except(*SPECIAL_KEYS)

validate_config_key! if config.key?("config")
end

private

def validate_config_key!
with_context(config["config"]) do
validate_type! config["config"], TrueClass, String
end
end
end
5 changes: 5 additions & 0 deletions test/commands/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ class CommandsAppTest < ActiveSupport::TestCase
assert_equal "ssh -t root@1.1.1.1 -p 2222 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
end

test "run over ssh with custom config" do
@config[:ssh] = { "config" => "config/ssh config" }
assert_equal "ssh -F config/ssh\\ config -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 no config" do
@config[:ssh] = { "config" => false }
assert_equal "ssh -F none -t root@1.1.1.1 -p 22 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
Expand Down
5 changes: 5 additions & 0 deletions test/configuration/ssh_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ class ConfigurationSshTest < ActiveSupport::TestCase
config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "config" => false }) })
assert_equal false, config.ssh.options[:config]
end

test "ssh options with path to an ssh_config-file" do
config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "config" => "config/ssh_config" }) })
assert_equal "config/ssh_config", config.ssh.options[:config]
end
end

0 comments on commit 3a634ae

Please sign in to comment.