Add the proxy/ssl config and pass on to kamal-proxy

This commit is contained in:
Donal McBreen
2024-09-10 14:47:33 +01:00
parent 2fdc59a3aa
commit e9d480b514
4 changed files with 47 additions and 1 deletions

View File

@@ -56,6 +56,13 @@ proxy:
# requests for other apps that do have a host set. # requests for other apps that do have a host set.
host: foo.example.com host: foo.example.com
# SSL
#
# Kamal Proxy can automatically obtain and renew TLS certificates for your applications.
# To ensure this set, the ssl flag. This only works if we are deploying to one server and
# the host flag is set.
ssl: true
# Deploy timeout # Deploy timeout
# #
# How long to wait for the app to boot when deploying, defaults to 30 seconds # How long to wait for the app to boot when deploying, defaults to 30 seconds

View File

@@ -10,7 +10,7 @@ class Kamal::Configuration::Proxy
def initialize(config:) def initialize(config:)
@proxy_config = config.raw_config.proxy || {} @proxy_config = config.raw_config.proxy || {}
validate! proxy_config validate! proxy_config, with: Kamal::Configuration::Validator::Proxy
end end
def enabled? def enabled?
@@ -37,9 +37,14 @@ class Kamal::Configuration::Proxy
argumentize "--publish", [ "#{DEFAULT_HTTP_PORT}:#{DEFAULT_HTTP_PORT}", "#{DEFAULT_HTTPS_PORT}:#{DEFAULT_HTTPS_PORT}" ] argumentize "--publish", [ "#{DEFAULT_HTTP_PORT}:#{DEFAULT_HTTP_PORT}", "#{DEFAULT_HTTPS_PORT}:#{DEFAULT_HTTPS_PORT}" ]
end end
def ssl?
proxy_config.fetch("ssl", false)
end
def deploy_options def deploy_options
{ {
host: proxy_config["host"], host: proxy_config["host"],
tls: proxy_config["ssl"],
"deploy-timeout": proxy_config["deploy_timeout"], "deploy-timeout": proxy_config["deploy_timeout"],
"drain-timeout": proxy_config["drain_timeout"], "drain-timeout": proxy_config["drain_timeout"],
"health-check-interval": proxy_config.dig("health_check", "interval"), "health-check-interval": proxy_config.dig("health_check", "interval"),

View File

@@ -0,0 +1,9 @@
class Kamal::Configuration::Validator::Proxy < Kamal::Configuration::Validator
def validate!
super
if config["host"].blank? && config["ssl"]
error "Must set a host to enable automatic SSL"
end
end
end

View File

@@ -0,0 +1,25 @@
require "test_helper"
class ConfigurationEnvTest < ActiveSupport::TestCase
setup do
@deploy = {
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" },
builder: { "arch" => "amd64" }, servers: [ "1.1.1.1" ]
}
end
test "ssl with host" do
@deploy[:proxy] = { "ssl" => true, "host" => "example.com" }
assert_equal true, config.proxy.ssl?
end
test "ssl with no host" do
@deploy[:proxy] = { "ssl" => true }
assert_raises(Kamal::ConfigurationError) { config.proxy.ssl? }
end
private
def config
Kamal::Configuration.new(@deploy)
end
end