-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #471 from basecamp/extract-env-writer
Extract Kamal::EnvFile
- Loading branch information
Showing
11 changed files
with
156 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Encode an env hash as a string where secret values have been looked up and all values escaped for Docker. | ||
class Kamal::EnvFile | ||
def initialize(env) | ||
@env = env | ||
end | ||
|
||
def to_s | ||
env_file = StringIO.new.tap do |contents| | ||
if (secrets = @env["secret"]).present? | ||
@env.fetch("secret", @env)&.each do |key| | ||
contents << docker_env_file_line(key, ENV.fetch(key)) | ||
end | ||
|
||
@env["clear"]&.each do |key, value| | ||
contents << docker_env_file_line(key, value) | ||
end | ||
else | ||
@env.fetch("clear", @env)&.each do |key, value| | ||
contents << docker_env_file_line(key, value) | ||
end | ||
end | ||
end.string | ||
|
||
# Ensure the file has some contents to avoid the SSHKIT empty file warning | ||
env_file.presence || "\n" | ||
end | ||
|
||
alias to_str to_s | ||
|
||
private | ||
def docker_env_file_line(key, value) | ||
"#{key.to_s}=#{escape_docker_env_file_value(value)}\n" | ||
end | ||
|
||
# Escape a value to make it safe to dump in a docker file. | ||
def escape_docker_env_file_value(value) | ||
# Doublequotes are treated literally in docker env files | ||
# so remove leading and trailing ones and unescape any others | ||
value.to_s.dump[1..-2].gsub(/\\"/, "\"") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
require "test_helper" | ||
|
||
class EnvFileTest < ActiveSupport::TestCase | ||
test "env file simple" do | ||
env = { | ||
"foo" => "bar", | ||
"baz" => "haz" | ||
} | ||
|
||
assert_equal "foo=bar\nbaz=haz\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
end | ||
|
||
test "env file clear" do | ||
env = { | ||
"clear" => { | ||
"foo" => "bar", | ||
"baz" => "haz" | ||
} | ||
} | ||
|
||
assert_equal "foo=bar\nbaz=haz\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
end | ||
|
||
test "env file empty" do | ||
assert_equal "\n", Kamal::EnvFile.new({}).to_s | ||
end | ||
|
||
test "env file secret" do | ||
ENV["PASSWORD"] = "hello" | ||
env = { | ||
"secret" => [ "PASSWORD" ] | ||
} | ||
|
||
assert_equal "PASSWORD=hello\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
ensure | ||
ENV.delete "PASSWORD" | ||
end | ||
|
||
test "env file secret escaped newline" do | ||
ENV["PASSWORD"] = "hello\\nthere" | ||
env = { | ||
"secret" => [ "PASSWORD" ] | ||
} | ||
|
||
assert_equal "PASSWORD=hello\\\\nthere\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
ensure | ||
ENV.delete "PASSWORD" | ||
end | ||
|
||
test "env file secret newline" do | ||
ENV["PASSWORD"] = "hello\nthere" | ||
env = { | ||
"secret" => [ "PASSWORD" ] | ||
} | ||
|
||
assert_equal "PASSWORD=hello\\nthere\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
ensure | ||
ENV.delete "PASSWORD" | ||
end | ||
|
||
test "env file missing secret" do | ||
env = { | ||
"secret" => [ "PASSWORD" ] | ||
} | ||
|
||
assert_raises(KeyError) { Kamal::EnvFile.new(env).to_s } | ||
|
||
ensure | ||
ENV.delete "PASSWORD" | ||
end | ||
|
||
test "env file secret and clear" do | ||
ENV["PASSWORD"] = "hello" | ||
env = { | ||
"secret" => [ "PASSWORD" ], | ||
"clear" => { | ||
"foo" => "bar", | ||
"baz" => "haz" | ||
} | ||
} | ||
|
||
assert_equal "PASSWORD=hello\nfoo=bar\nbaz=haz\n", \ | ||
Kamal::EnvFile.new(env).to_s | ||
ensure | ||
ENV.delete "PASSWORD" | ||
end | ||
|
||
test "stringIO conversion" do | ||
env = { | ||
"foo" => "bar", | ||
"baz" => "haz" | ||
} | ||
|
||
assert_equal "foo=bar\nbaz=haz\n", \ | ||
StringIO.new(Kamal::EnvFile.new(env)).read | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters