Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-pack/filebeat/input/httpjson: redact authorization headers in logging #41920

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Improve S3 object size metric calculation to support situations where Content-Length is not available. {pull}41755[41755]
- Fix handling of http_endpoint request exceeding memory limits. {issue}41764[41764] {pull}41765[41765]
- Rate limiting fixes in the Okta provider of the Entity Analytics input. {issue}40106[40106] {pull}41583[41583]
- Redact authorization headers in HTTPJSON debug logs. {pull}41920[41920]

*Heartbeat*

Expand Down
56 changes: 56 additions & 0 deletions x-pack/filebeat/input/httpjson/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"

Expand All @@ -33,6 +34,7 @@
"github.com/elastic/beats/v7/libbeat/version"
"github.com/elastic/beats/v7/x-pack/filebeat/input/internal/httplog"
"github.com/elastic/beats/v7/x-pack/filebeat/input/internal/httpmon"
"github.com/elastic/beats/v7/x-pack/filebeat/input/internal/private"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/monitoring"
Expand Down Expand Up @@ -91,6 +93,60 @@
}
}

type redact struct {
value mapstrM
fields []string
}

func (r redact) MarshalLogObject(enc zapcore.ObjectEncoder) error {
v, err := private.Redact(r.value, "", r.fields)
if err != nil {
return fmt.Errorf("could not redact value: %v", err)
}
return v.MarshalLogObject(enc)
}

// mapstrM is a non-mutating version of mapstr.M.
// See https://github.com/elastic/elastic-agent-libs/issues/232.
type mapstrM mapstr.M

// MarshalLogObject implements the zapcore.ObjectMarshaler interface and allows
// for more efficient marshaling of mapstrM in structured logging.
func (m mapstrM) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if len(m) == 0 {
return nil
}

keys := make([]string, 0, len(m))
for k := range m {

Check failure on line 121 in x-pack/filebeat/input/httpjson/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

cannot range over m (variable of type mapstrM) (typecheck)
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := m[k]

Check failure on line 126 in x-pack/filebeat/input/httpjson/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

invalid operation: cannot index m (variable of type mapstrM) (typecheck)
if inner, ok := tryToMapStr(v); ok {
err := enc.AddObject(k, inner)
if err != nil {
return fmt.Errorf("failed to add object: %w", err)
}
continue
}
zap.Any(k, v).AddTo(enc)
}
return nil
}

func tryToMapStr(v interface{}) (mapstrM, bool) {
switch m := v.(type) {
case mapstrM:
return m, true
case map[string]interface{}:
return mapstrM(m), true

Check failure on line 144 in x-pack/filebeat/input/httpjson/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

cannot convert m (variable of type map[string]interface{}) to type mapstrM (typecheck)
default:
return nil, false

Check failure on line 146 in x-pack/filebeat/input/httpjson/input.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

cannot use nil as mapstrM value in return statement (typecheck)
}
}

func test(url *url.URL) error {
port := func() string {
if url.Port() != "" {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/input/httpjson/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (rf *requestFactory) newRequest(ctx *transformContext) (transformable, erro
}
}

rf.log.Debugf("new request: %#v", req)
rf.log.Debugw("new request", "req", redact{value: mapstrM(req), fields: []string{"header.Authorization"}})

return req, nil
}
Expand Down
Loading