Repository: caddyserver/caddy (language: go)

Issue:
Propagate access logging config to implicit HTTP server from Auto HTTPS
If the Auto HTTPS routine creates an HTTP server to perform HTTP->HTTPS redirects, it doesn't inherit the logging config from the HTTPS server. It should, because it is a relevant request to log.


Discussion / hints:

Fix patch:
diff --git a/modules/caddyhttp/autohttps.go b/modules/caddyhttp/autohttps.go
index 4cc5d2f2df7..70f999b049d 100644
--- a/modules/caddyhttp/autohttps.go
+++ b/modules/caddyhttp/autohttps.go
@@ -93,6 +93,9 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
 	// https://github.com/caddyserver/caddy/issues/3443)
 	redirDomains := make(map[string][]caddy.NetworkAddress)
 
+	// the configured logger for an HTTPS enabled server
+	var logger *ServerLogConfig
+
 	for srvName, srv := range app.Servers {
 		// as a prerequisite, provision route matchers; this is
 		// required for all routes on all servers, and must be
@@ -172,6 +175,11 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
 			continue
 		}
 
+		// clone the logger so we can apply it to the HTTP server
+		if srv.Logs != nil {
+			logger = srv.Logs.clone()
+		}
+
 		// for all the hostnames we found, filter them so we have
 		// a deduplicated list of names for which to obtain certs
 		// (only if cert management not disabled for this server)
@@ -400,6 +408,7 @@ redirServersLoop:
 		app.Servers["remaining_auto_https_redirects"] = &Server{
 			Listen: redirServerAddrsList,
 			Routes: appendCatchAll(redirRoutes),
+			Logs:   logger,
 		}
 	}
 
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go
index e01e7c8e664..2dabf46c2b9 100644
--- a/modules/caddyhttp/server.go
+++ b/modules/caddyhttp/server.go
@@ -663,6 +663,19 @@ func (slc ServerLogConfig) getLoggerName(host string) string {
 	return slc.DefaultLoggerName
 }
 
+func (slc *ServerLogConfig) clone() *ServerLogConfig {
+	clone := &ServerLogConfig{}
+	clone.DefaultLoggerName = slc.DefaultLoggerName
+	clone.LoggerNames = make(map[string]string)
+	for k, v := range slc.LoggerNames {
+		clone.LoggerNames[k] = v
+	}
+	clone.SkipHosts = append(clone.SkipHosts, slc.SkipHosts...)
+	clone.SkipUnmappedHosts = slc.SkipUnmappedHosts
+	clone.ShouldLogCredentials = slc.ShouldLogCredentials
+	return clone
+}
+
 // PrepareRequest fills the request r for use in a Caddy HTTP handler chain. w and s can
 // be nil, but the handlers will lose response placeholders and access to the server.
 func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter, s *Server) *http.Request {


Test patch: