Skip to content

Commit 2f3147f

Browse files
committed
feat: ssh: use secret in key_data
1 parent 6f29d4e commit 2f3147f

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

lib/kamal/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def initialize(raw_config, destination: nil, version: nil, validate: true)
6969

7070
@logging = Logging.new(logging_config: @raw_config.logging)
7171
@proxy = Proxy.new(config: self, proxy_config: @raw_config.proxy || {})
72-
@ssh = Ssh.new(config: self)
72+
@ssh = Ssh.new(config: self, secrets: secrets)
7373
@sshkit = Sshkit.new(config: self)
7474

7575
ensure_destination_if_required

lib/kamal/configuration/docs/ssh.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ ssh:
5858

5959
# Key data
6060
#
61-
# An array of strings, with each element of the array being
62-
# a raw private key in PEM format.
61+
# Can be a string (for secret lookup) or array with each
62+
# element of the array being a raw private key in PEM format.
6363
key_data: [ "-----BEGIN OPENSSH PRIVATE KEY-----" ]
6464

6565
# Config

lib/kamal/configuration/ssh.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ class Kamal::Configuration::Ssh
55

66
attr_reader :ssh_config
77

8-
def initialize(config:)
8+
def initialize(config:, secrets:)
99
@ssh_config = config.raw_config.ssh || {}
10-
validate! ssh_config
10+
@secrets = secrets
11+
validate! ssh_config, with: Kamal::Configuration::Validator::Ssh
1112
end
1213

1314
def user
@@ -35,7 +36,7 @@ def keys
3536
end
3637

3738
def key_data
38-
ssh_config["key_data"]
39+
lookup("key_data")
3940
end
4041

4142
def options
@@ -47,11 +48,20 @@ def to_h
4748
end
4849

4950
private
51+
attr_reader :secrets
5052
def logger
5153
LOGGER.tap { |logger| logger.level = log_level }
5254
end
5355

5456
def log_level
5557
ssh_config.fetch("log_level", :fatal)
5658
end
59+
60+
def lookup(key)
61+
if ssh_config[key].is_a?(Array)
62+
ssh_config[key]
63+
else
64+
secrets[ssh_config[key]]
65+
end
66+
end
5767
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Kamal::Configuration::Validator::Ssh < Kamal::Configuration::Validator
2+
def validate!
3+
validate_against_example!(
4+
config.except("key_data"),
5+
example.except("key_data")
6+
)
7+
8+
validate_string_or_array! "key_data"
9+
end
10+
11+
private
12+
def validate_string_or_array!(key)
13+
value = config[key]
14+
15+
unless value.is_a?(String) || value.is_a?(Array)
16+
error "should be a string (for secret lookup) or an array"
17+
end
18+
end
19+
20+
end

0 commit comments

Comments
 (0)