Skip to content

Commit 3a634ae

Browse files
committed
Allow ssh/config to additionally accept a string
This is accepted by Net::SSH, research done by @jeremy in basecamp#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.
1 parent e8a41af commit 3a634ae

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

lib/kamal/commands/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def ssh_config_args
101101
""
102102
when false
103103
" -F none"
104+
when String
105+
" -F #{Shellwords.escape(config.ssh.config)}"
104106
end
105107
end
106108
end

lib/kamal/configuration/ssh.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Kamal::Configuration::Ssh
77

88
def initialize(config:)
99
@ssh_config = config.raw_config.ssh || {}
10-
validate! ssh_config
10+
validate! ssh_config, with: Kamal::Configuration::Validator::Ssh
1111
end
1212

1313
def user
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Kamal::Configuration::Validator::Ssh < Kamal::Configuration::Validator
2+
SPECIAL_KEYS = [ "config" ]
3+
4+
def validate!
5+
validate_against_example! \
6+
config.except(*SPECIAL_KEYS),
7+
example.except(*SPECIAL_KEYS)
8+
9+
validate_config_key! if config.key?("config")
10+
end
11+
12+
private
13+
14+
def validate_config_key!
15+
with_context(config["config"]) do
16+
validate_type! config["config"], TrueClass, String
17+
end
18+
end
19+
end

test/commands/app_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ class CommandsAppTest < ActiveSupport::TestCase
300300
assert_equal "ssh -t root@1.1.1.1 -p 2222 'ls'", new_command.run_over_ssh("ls", host: "1.1.1.1")
301301
end
302302

303+
test "run over ssh with custom config" do
304+
@config[:ssh] = { "config" => "config/ssh config" }
305+
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")
306+
end
307+
303308
test "run over ssh with no config" do
304309
@config[:ssh] = { "config" => false }
305310
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")

test/configuration/ssh_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ class ConfigurationSshTest < ActiveSupport::TestCase
4242
config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "config" => false }) })
4343
assert_equal false, config.ssh.options[:config]
4444
end
45+
46+
test "ssh options with path to an ssh_config-file" do
47+
config = Kamal::Configuration.new(@deploy.tap { |c| c.merge!(ssh: { "config" => "config/ssh_config" }) })
48+
assert_equal "config/ssh_config", config.ssh.options[:config]
49+
end
4550
end

0 commit comments

Comments
 (0)