@marcel91200 / caddy Mirror · read-only / commits / 99ffe933
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 09:15 UTC

logging: Fix `skip_hosts` with wildcards (#5102)

Fix #4859

Francis Lavoie committed Oct 5, 2022 at 14:14 UTC 99ffe9338837eff80def7e5d51c2a0e02f56abdb
5 files changed +23 -38
caddyconfig/httpcaddyfile/builtins.go
+1 -1
@@ -735,7 +735,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
735 // reference the default logger. See the
736 // setupNewDefault function in the logging
737 // package for where this is configured.
738 - globalLogName = "default"
738 + globalLogName = caddy.DefaultLoggerName
739 }
740
741 // Verify this name is unused.
caddyconfig/httpcaddyfile/httptype.go
+8 -25
@@ -219,7 +219,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
219 if ncl.name == "" {
220 return
221 }
222 - if ncl.name == "default" {
222 + if ncl.name == caddy.DefaultLoggerName {
223 hasDefaultLog = true
224 }
225 if _, ok := options["debug"]; ok && ncl.log.Level == "" {
@@ -240,7 +240,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
240 // configure it with any applicable options
241 if _, ok := options["debug"]; ok {
242 customLogs = append(customLogs, namedCustomLog{
243 - name: "default",
243 + name: caddy.DefaultLoggerName,
244 log: &caddy.CustomLog{Level: zap.DebugLevel.CapitalString()},
245 })
246 }
@@ -299,11 +299,11 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
299 // most users seem to prefer not writing access logs
300 // to the default log when they are directed to a
301 // file or have any other special customization
302 - if ncl.name != "default" && len(ncl.log.Include) > 0 {
303 - defaultLog, ok := cfg.Logging.Logs["default"]
302 + if ncl.name != caddy.DefaultLoggerName && len(ncl.log.Include) > 0 {
303 + defaultLog, ok := cfg.Logging.Logs[caddy.DefaultLoggerName]
304 if !ok {
305 defaultLog = new(caddy.CustomLog)
306 - cfg.Logging.Logs["default"] = defaultLog
306 + cfg.Logging.Logs[caddy.DefaultLoggerName] = defaultLog
307 }
308 defaultLog.Exclude = append(defaultLog.Exclude, ncl.log.Include...)
309 }
@@ -518,15 +518,6 @@ func (st *ServerType) serversFromPairings(
518 var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
519 autoHTTPSWillAddConnPolicy := autoHTTPS != "off"
520
521 - // if a catch-all server block (one which accepts all hostnames) exists in this pairing,
522 - // we need to know that so that we can configure logs properly (see #3878)
523 - var catchAllSblockExists bool
524 - for _, sblock := range p.serverBlocks {
525 - if len(sblock.hostsFromKeys(false)) == 0 {
526 - catchAllSblockExists = true
527 - }
528 - }
529 -
521 // if needed, the ServerLogConfig is initialized beforehand so
522 // that all server blocks can populate it with data, even when not
523 // coming with a log directive
@@ -658,18 +649,10 @@ func (st *ServerType) serversFromPairings(
649 } else {
650 // map each host to the user's desired logger name
651 for _, h := range sblockLogHosts {
661 - // if the custom logger name is non-empty, add it to the map;
662 - // otherwise, only map to an empty logger name if this or
663 - // another site block on this server has a catch-all host (in
664 - // which case only requests with mapped hostnames will be
665 - // access-logged, so it'll be necessary to add them to the
666 - // map even if they use default logger)
667 - if ncl.name != "" || catchAllSblockExists {
668 - if srv.Logs.LoggerNames == nil {
669 - srv.Logs.LoggerNames = make(map[string]string)
670 - }
671 - srv.Logs.LoggerNames[h] = ncl.name
652 + if srv.Logs.LoggerNames == nil {
653 + srv.Logs.LoggerNames = make(map[string]string)
654 }
655 + srv.Logs.LoggerNames[h] = ncl.name
656 }
657 }
658 }
caddytest/integration/caddyfile_adapt/log_skip_hosts.txt
+3
@@ -62,6 +62,9 @@ example.com {
62 }
63 ],
64 "logs": {
65 + "logger_names": {
66 + "one.example.com": ""
67 + },
68 "skip_hosts": [
69 "three.example.com",
70 "two.example.com",
logging.go
+5 -3
@@ -105,7 +105,7 @@ func (logging *Logging) openLogs(ctx Context) error {
105 // then set up any other custom logs
106 for name, l := range logging.Logs {
107 // the default log is already set up
108 - if name == "default" {
108 + if name == DefaultLoggerName {
109 continue
110 }
111
@@ -138,7 +138,7 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
138
139 // extract the user-defined default log, if any
140 newDefault := new(defaultCustomLog)
141 - if userDefault, ok := logging.Logs["default"]; ok {
141 + if userDefault, ok := logging.Logs[DefaultLoggerName]; ok {
142 newDefault.CustomLog = userDefault
143 } else {
144 // if none, make one with our own default settings
@@ -147,7 +147,7 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
147 if err != nil {
148 return fmt.Errorf("setting up default Caddy log: %v", err)
149 }
150 - logging.Logs["default"] = newDefault.CustomLog
150 + logging.Logs[DefaultLoggerName] = newDefault.CustomLog
151 }
152
153 // set up this new log
@@ -702,6 +702,8 @@ var (
702
703 var writers = NewUsagePool()
704
705 +const DefaultLoggerName = "default"
706 +
707 // Interface guards
708 var (
709 _ io.WriteCloser = (*notClosable)(nil)
modules/caddyhttp/server.go
+6 -9
@@ -639,21 +639,18 @@ func (s *Server) shouldLogRequest(r *http.Request) bool {
639 // logging is disabled
640 return false
641 }
642 + if _, ok := s.Logs.LoggerNames[r.Host]; ok {
643 + // this host is mapped to a particular logger name
644 + return true
645 + }
646 for _, dh := range s.Logs.SkipHosts {
647 // logging for this particular host is disabled
648 if certmagic.MatchWildcard(r.Host, dh) {
649 return false
650 }
651 }
648 - if _, ok := s.Logs.LoggerNames[r.Host]; ok {
649 - // this host is mapped to a particular logger name
650 - return true
651 - }
652 - if s.Logs.SkipUnmappedHosts {
653 - // this host is not mapped and thus must not be logged
654 - return false
655 - }
656 - return true
652 + // if configured, this host is not mapped and thus must not be logged
653 + return !s.Logs.SkipUnmappedHosts
654 }
655
656 // protocol returns true if the protocol proto is configured/enabled.