mirror of
https://github.com/caddyserver/caddy.git
synced 2025-12-19 03:25:24 -05:00
logging: log_append Early option, Supports {http.response.body} (#7368)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.25.0, ubuntu-latest, 0, 1.25, linux) (push) Failing after 18s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.25.0, 1.25, aix) (push) Failing after 17s
Cross-Build / build (~1.25.0, 1.25, darwin) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, dragonfly) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, freebsd) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, illumos) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, linux) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, netbsd) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, openbsd) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, solaris) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, windows) (push) Failing after 15s
Lint / lint (ubuntu-latest, linux) (push) Failing after 16s
Lint / govulncheck (push) Successful in 1m30s
Lint / dependency-review (push) Failing after 16s
Tests / test (./cmd/caddy/caddy, ~1.25.0, macos-14, 0, 1.25, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.25.0, windows-latest, True, 1.25, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 17s
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.25.0, ubuntu-latest, 0, 1.25, linux) (push) Failing after 18s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Has been skipped
Cross-Build / build (~1.25.0, 1.25, aix) (push) Failing after 17s
Cross-Build / build (~1.25.0, 1.25, darwin) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, dragonfly) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, freebsd) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, illumos) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, linux) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, netbsd) (push) Failing after 16s
Cross-Build / build (~1.25.0, 1.25, openbsd) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, solaris) (push) Failing after 15s
Cross-Build / build (~1.25.0, 1.25, windows) (push) Failing after 15s
Lint / lint (ubuntu-latest, linux) (push) Failing after 16s
Lint / govulncheck (push) Successful in 1m30s
Lint / dependency-review (push) Failing after 16s
Tests / test (./cmd/caddy/caddy, ~1.25.0, macos-14, 0, 1.25, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.25.0, windows-latest, True, 1.25, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Failing after 17s
* logging: `log_append` early option
* logging: `log_append` supports `{http.response.body}`
* Convenience auto-early for request body
This commit is contained in:
parent
34fd2dfcff
commit
decc8a4d6f
@ -15,6 +15,8 @@
|
|||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
@ -26,7 +28,7 @@ func init() {
|
|||||||
|
|
||||||
// parseCaddyfile sets up the log_append handler from Caddyfile tokens. Syntax:
|
// parseCaddyfile sets up the log_append handler from Caddyfile tokens. Syntax:
|
||||||
//
|
//
|
||||||
// log_append [<matcher>] <key> <value>
|
// log_append [<matcher>] [<]<key> <value>
|
||||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
handler := new(LogAppend)
|
handler := new(LogAppend)
|
||||||
err := handler.UnmarshalCaddyfile(h.Dispenser)
|
err := handler.UnmarshalCaddyfile(h.Dispenser)
|
||||||
@ -43,6 +45,10 @@ func (h *LogAppend) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|||||||
if !d.NextArg() {
|
if !d.NextArg() {
|
||||||
return d.ArgErr()
|
return d.ArgErr()
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(h.Key, "<") && len(h.Key) > 1 {
|
||||||
|
h.Early = true
|
||||||
|
h.Key = h.Key[1:]
|
||||||
|
}
|
||||||
h.Value = d.Val()
|
h.Value = d.Val()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,8 @@
|
|||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -42,6 +44,12 @@ type LogAppend struct {
|
|||||||
// map, the value of that key will be used. Otherwise
|
// map, the value of that key will be used. Otherwise
|
||||||
// the value will be used as-is as a constant string.
|
// the value will be used as-is as a constant string.
|
||||||
Value string `json:"value,omitempty"`
|
Value string `json:"value,omitempty"`
|
||||||
|
|
||||||
|
// Early, if true, adds the log field before calling
|
||||||
|
// the next handler in the chain. By default, the log
|
||||||
|
// field is added on the way back up the middleware chain,
|
||||||
|
// after all subsequent handlers have completed.
|
||||||
|
Early bool `json:"early,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaddyModule returns the Caddy module information.
|
// CaddyModule returns the Caddy module information.
|
||||||
@ -53,13 +61,63 @@ func (LogAppend) CaddyModule() caddy.ModuleInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||||
// Run the next handler in the chain first.
|
// Determine if we need to add the log field early.
|
||||||
|
// We do if the Early flag is set, or for convenience,
|
||||||
|
// if the value is a special placeholder for the request body.
|
||||||
|
needsEarly := h.Early || h.Value == placeholderRequestBody || h.Value == placeholderRequestBodyBase64
|
||||||
|
|
||||||
|
// Check if we need to buffer the response for special placeholders
|
||||||
|
needsResponseBody := h.Value == placeholderResponseBody || h.Value == placeholderResponseBodyBase64
|
||||||
|
|
||||||
|
if needsEarly && !needsResponseBody {
|
||||||
|
// Add the log field before calling the next handler
|
||||||
|
// (but not if we need the response body, which isn't available yet)
|
||||||
|
h.addLogField(r, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rec caddyhttp.ResponseRecorder
|
||||||
|
var buf *bytes.Buffer
|
||||||
|
|
||||||
|
if needsResponseBody {
|
||||||
|
// Wrap the response writer with a recorder to capture the response body
|
||||||
|
buf = new(bytes.Buffer)
|
||||||
|
rec = caddyhttp.NewResponseRecorder(w, buf, func(status int, header http.Header) bool {
|
||||||
|
// Always buffer the response when we need to log the body
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
w = rec
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the next handler in the chain.
|
||||||
// If an error occurs, we still want to add
|
// If an error occurs, we still want to add
|
||||||
// any extra log fields that we can, so we
|
// any extra log fields that we can, so we
|
||||||
// hold onto the error and return it later.
|
// hold onto the error and return it later.
|
||||||
handlerErr := next.ServeHTTP(w, r)
|
handlerErr := next.ServeHTTP(w, r)
|
||||||
|
|
||||||
// On the way back up the chain, add the extra log field
|
if needsResponseBody {
|
||||||
|
// Write the buffered response to the client
|
||||||
|
if rec.Buffered() {
|
||||||
|
h.addLogField(r, buf)
|
||||||
|
err := rec.WriteResponse()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return handlerErr
|
||||||
|
}
|
||||||
|
|
||||||
|
if !h.Early {
|
||||||
|
// Add the log field after the handler completes
|
||||||
|
h.addLogField(r, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
return handlerErr
|
||||||
|
}
|
||||||
|
|
||||||
|
// addLogField adds the log field to the request's extra log fields.
|
||||||
|
// If buf is not nil, it contains the buffered response body for special
|
||||||
|
// response body placeholders.
|
||||||
|
func (h LogAppend) addLogField(r *http.Request, buf *bytes.Buffer) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
vars := ctx.Value(caddyhttp.VarsCtxKey).(map[string]any)
|
vars := ctx.Value(caddyhttp.VarsCtxKey).(map[string]any)
|
||||||
@ -67,7 +125,21 @@ func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
|
|||||||
extra := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields)
|
extra := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields)
|
||||||
|
|
||||||
var varValue any
|
var varValue any
|
||||||
if strings.HasPrefix(h.Value, "{") &&
|
|
||||||
|
// Handle special case placeholders for response body
|
||||||
|
if h.Value == placeholderResponseBody {
|
||||||
|
if buf != nil {
|
||||||
|
varValue = buf.String()
|
||||||
|
} else {
|
||||||
|
varValue = ""
|
||||||
|
}
|
||||||
|
} else if h.Value == placeholderResponseBodyBase64 {
|
||||||
|
if buf != nil {
|
||||||
|
varValue = base64.StdEncoding.EncodeToString(buf.Bytes())
|
||||||
|
} else {
|
||||||
|
varValue = ""
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(h.Value, "{") &&
|
||||||
strings.HasSuffix(h.Value, "}") &&
|
strings.HasSuffix(h.Value, "}") &&
|
||||||
strings.Count(h.Value, "{") == 1 {
|
strings.Count(h.Value, "{") == 1 {
|
||||||
// the value looks like a placeholder, so get its value
|
// the value looks like a placeholder, so get its value
|
||||||
@ -84,10 +156,17 @@ func (h LogAppend) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
|
|||||||
// We use zap.Any because it will reflect
|
// We use zap.Any because it will reflect
|
||||||
// to the correct type for us.
|
// to the correct type for us.
|
||||||
extra.Add(zap.Any(h.Key, varValue))
|
extra.Add(zap.Any(h.Key, varValue))
|
||||||
|
|
||||||
return handlerErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Special placeholder values that are handled by log_append
|
||||||
|
// rather than by the replacer.
|
||||||
|
placeholderRequestBody = "{http.request.body}"
|
||||||
|
placeholderRequestBodyBase64 = "{http.request.body_base64}"
|
||||||
|
placeholderResponseBody = "{http.response.body}"
|
||||||
|
placeholderResponseBodyBase64 = "{http.response.body_base64}"
|
||||||
|
)
|
||||||
|
|
||||||
// Interface guards
|
// Interface guards
|
||||||
var (
|
var (
|
||||||
_ caddyhttp.MiddlewareHandler = (*LogAppend)(nil)
|
_ caddyhttp.MiddlewareHandler = (*LogAppend)(nil)
|
||||||
Loading…
x
Reference in New Issue
Block a user