From 271e31dec448171f01b2e6fa565ed373c9ac6c25 Mon Sep 17 00:00:00 2001 From: Nicolas Duchon Date: Sat, 7 Dec 2024 19:33:53 +0100 Subject: [PATCH] fix: wildcard certificates should only work one level deep --- nginx.tmpl | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 7cb4114..d18b2ab 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -688,13 +688,25 @@ proxy_set_header Proxy ""; {{ $vhost_containers = concat $vhost_containers $vpath_containers }} {{- end }} - {{- $certName := groupByKeys $vhost_containers "Env.CERT_NAME" | first }} - {{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $hostname) }} - {{- $vhostCert = trimSuffix ".crt" $vhostCert }} - {{- $vhostCert = trimSuffix ".key" $vhostCert }} + {{- $userIdentifiedCert := groupByKeys $vhost_containers "Env.CERT_NAME" | first }} + + {{- $vhostCert := "" }} + {{- if exists (printf "/etc/nginx/certs/%s.crt" $hostname) }} + {{- $vhostCert = $hostname }} + {{- end }} + + {{- $parentVhostCert := "" }} + {{- if gt ($hostname | sprigSplit "." | len) 2 }} + {{- $parentHostname := ($hostname | sprigSplitn "." 2)._1 }} + {{- if exists (printf "/etc/nginx/certs/%s.crt" $parentHostname) }} + {{- $parentVhostCert = $parentHostname }} + {{- end }} + {{- end }} + {{- $trust_default_cert := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.trust-default-cert" | keys | first | default $globals.config.trust_default_cert | parseBool }} - {{- $cert := and $trust_default_cert $globals.config.default_cert_ok | ternary "default" "" }} - {{- $cert = or $certName $vhostCert $cert }} + {{- $defaultCert := and $trust_default_cert $globals.config.default_cert_ok | ternary "default" "" }} + + {{- $cert := or $userIdentifiedCert $vhostCert $parentVhostCert $defaultCert }} {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }} {{- $enable_debug_endpoint := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.debug-endpoint" | keys | first | default $globals.config.enable_debug_endpoint | parseBool }}