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

Make sanitize_field_names centrally configurable #856

Merged
merged 2 commits into from
Dec 8, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ endif::[]
https://github.com/elastic/apm-agent-go/compare/v1.9.0...master[View commits]

- module/apmsql: add tracingDriver.Unwrap method to get underlying driver {pull}#849[#(849)]
- module/apmgopgv10: add support for github.com/go-pg/pg/v10 {pull}857[(#857)]
- Enable central configuration of "sanitize_field_names" {pull}856[(#856)]

[[release-notes-1.x]]
=== Go Agent version 1.x
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ func (t *Tracer) updateRemoteConfig(logger WarningLogger, old, attrs map[string]
cfg.recording = recording
})
}
case envSanitizeFieldNames:
matchers := configutil.ParseWildcardPatterns(v)
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.sanitizedFieldNames = matchers
})
case envSpanFramesMinDuration:
duration, err := configutil.ParseDuration(v)
if err != nil {
Expand Down Expand Up @@ -487,4 +492,5 @@ type instrumentationConfigValues struct {
spanFramesMinDuration time.Duration
stackTraceLimit int
propagateLegacyHeader bool
sanitizedFieldNames wildcard.Matchers
}
14 changes: 14 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func TestTracerCentralConfigUpdate(t *testing.T) {
assert.Len(t, payloads.Errors, 1)
return len(payloads.Errors[0].Exception.Stacktrace) == 1
})
run("sanitize_field_names", "secret", func(tracer *apmtest.RecordingTracer) bool {
tracer.ResetPayloads()
tracer.SetSanitizedFieldNames("not_secret")
req, _ := http.NewRequest("GET", "http://server.testing/", nil)
req.AddCookie(&http.Cookie{Name: "secret", Value: "top"})
tx := tracer.StartTransaction("name", "type")
tx.Context.SetHTTPRequest(req)
tx.End()
tracer.Flush(nil)
payloads := tracer.Payloads()
assert.Len(t, payloads.Transactions, 1)
assert.Len(t, payloads.Transactions[0].Context.Request.Cookies, 1)
return payloads.Transactions[0].Context.Request.Cookies[0].Value == "[REDACTED]"
})
}

func testTracerCentralConfigUpdate(t *testing.T, serverResponse string, isRemote func(*apmtest.RecordingTracer) bool) {
Expand Down
6 changes: 3 additions & 3 deletions modelwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func (w *modelWriter) buildModelTransaction(out *model.Transaction, tx *Transact
out.Context = td.Context.build()
}

if len(w.cfg.sanitizedFieldNames) != 0 && out.Context != nil {
if len(td.sanitizedFieldNames) != 0 && out.Context != nil {
if out.Context.Request != nil {
sanitizeRequest(out.Context.Request, w.cfg.sanitizedFieldNames)
sanitizeRequest(out.Context.Request, td.sanitizedFieldNames)
}
if out.Context.Response != nil {
sanitizeResponse(out.Context.Response, w.cfg.sanitizedFieldNames)
sanitizeResponse(out.Context.Response, td.sanitizedFieldNames)
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ func newTracer(opts TracerOptions) *Tracer {
t.setLocalInstrumentationConfig(envUseElasticTraceparentHeader, func(cfg *instrumentationConfigValues) {
cfg.propagateLegacyHeader = opts.propagateLegacyHeader
})
t.setLocalInstrumentationConfig(envSanitizeFieldNames, func(cfg *instrumentationConfigValues) {
cfg.sanitizedFieldNames = opts.sanitizedFieldNames
})

if !opts.active {
t.active = 0
Expand All @@ -439,7 +442,6 @@ func newTracer(opts TracerOptions) *Tracer {
cfg.metricsInterval = opts.metricsInterval
cfg.requestDuration = opts.requestDuration
cfg.requestSize = opts.requestSize
cfg.sanitizedFieldNames = opts.sanitizedFieldNames
cfg.disabledMetrics = opts.disabledMetrics
cfg.preContext = defaultPreContext
cfg.postContext = defaultPostContext
Expand All @@ -465,7 +467,6 @@ type tracerConfig struct {
metricsGatherers []MetricsGatherer
contextSetter stacktrace.ContextSetter
preContext, postContext int
sanitizedFieldNames wildcard.Matchers
disabledMetrics wildcard.Matchers
cpuProfileDuration time.Duration
cpuProfileInterval time.Duration
Expand Down Expand Up @@ -572,6 +573,10 @@ func (t *Tracer) SetLogger(logger Logger) {
// of the the supplied patterns will have their values redacted. If
// SetSanitizedFieldNames is called with no arguments, then no fields
// will be redacted.
//
// Configuration via Kibana takes precedence over local configuration, so
// if sanitized_field_names has been configured via Kibana, this call will
// not have any effect until/unless that configuration has been removed.
func (t *Tracer) SetSanitizedFieldNames(patterns ...string) error {
var matchers wildcard.Matchers
if len(patterns) != 0 {
Expand All @@ -580,7 +585,7 @@ func (t *Tracer) SetSanitizedFieldNames(patterns ...string) error {
matchers[i] = configutil.ParseWildcardPattern(p)
}
}
t.sendConfigCommand(func(cfg *tracerConfig) {
t.setLocalInstrumentationConfig(envSanitizeFieldNames, func(cfg *instrumentationConfigValues) {
cfg.sanitizedFieldNames = matchers
})
return nil
Expand Down
4 changes: 4 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"math/rand"
"sync"
"time"

"go.elastic.co/apm/internal/wildcard"
)

// StartTransaction returns a new Transaction with the specified
Expand Down Expand Up @@ -66,6 +68,7 @@ func (t *Tracer) StartTransactionOptions(name, transactionType string, opts Tran
tx.stackTraceLimit = instrumentationConfig.stackTraceLimit
tx.Context.captureHeaders = instrumentationConfig.captureHeaders
tx.propagateLegacyHeader = instrumentationConfig.propagateLegacyHeader
tx.sanitizedFieldNames = instrumentationConfig.sanitizedFieldNames
tx.breakdownMetricsEnabled = t.breakdownMetrics.enabled

var root bool
Expand Down Expand Up @@ -343,6 +346,7 @@ type TransactionData struct {
stackTraceLimit int
breakdownMetricsEnabled bool
propagateLegacyHeader bool
sanitizedFieldNames wildcard.Matchers
timestamp time.Time

mu sync.Mutex
Expand Down