Skip to content

Commit

Permalink
Redact APM API key and secret token (#4057)
Browse files Browse the repository at this point in the history
(cherry picked from commit 25e23ea)
  • Loading branch information
pchila authored and mergify[bot] committed Oct 31, 2024
1 parent 289b5e0 commit c9fcb13
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ func redactServer(cfg *Config) Server {
redacted.TLS = &newTLS
}

if redacted.Instrumentation.APIKey != "" {
redacted.Instrumentation.APIKey = kRedacted
}

if redacted.Instrumentation.SecretToken != "" {
redacted.Instrumentation.SecretToken = kRedacted
}

return redacted
}

Expand Down
126 changes: 126 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,132 @@ func TestLoadServerLimits(t *testing.T) {

}

func TestConfigRedact(t *testing.T) {

testcases := []struct {
name string
inputCfg *Config
redactedCfg *Config
}{
{
name: "do not modify empty APM secrets",
inputCfg: &Config{
Inputs: []Input{
{
Type: "fleet-server",
Server: Server{
Instrumentation: Instrumentation{
SecretToken: "",
APIKey: "",
},
},
},
},
},
redactedCfg: &Config{
Inputs: []Input{
{
Server: Server{
Instrumentation: Instrumentation{
SecretToken: "",
APIKey: "",
},
},
},
},
},
},
{
name: "redact APM secret token",
inputCfg: &Config{
Inputs: []Input{
{
Type: "fleet-server",
Server: Server{
Instrumentation: Instrumentation{
SecretToken: "secret value that noone should know",
},
},
},
},
},
redactedCfg: &Config{
Inputs: []Input{
{
Server: Server{
Instrumentation: Instrumentation{
SecretToken: kRedacted,
},
},
},
},
},
},
{
name: "redact APM API key",
inputCfg: &Config{
Inputs: []Input{
{
Type: "fleet-server",
Server: Server{
Instrumentation: Instrumentation{
APIKey: "secret value that noone should know",
},
},
},
},
},
redactedCfg: &Config{
Inputs: []Input{
{
Server: Server{
Instrumentation: Instrumentation{
APIKey: kRedacted,
},
},
},
},
},
},
{
name: "redact both APM API key and secret token",
inputCfg: &Config{
Inputs: []Input{
{
Type: "fleet-server",
Server: Server{
Instrumentation: Instrumentation{
APIKey: "secret value that noone should know",
SecretToken: "another value that noone should know",
},
},
},
},
},
redactedCfg: &Config{
Inputs: []Input{
{
Server: Server{
Instrumentation: Instrumentation{
APIKey: kRedacted,
SecretToken: kRedacted,
},
},
},
},
},
},
}

for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
require.NotNil(t, tt.inputCfg, "input config cannot be nil")
actualRedacted := tt.inputCfg.Redact()
assert.Equal(t, tt.redactedCfg, actualRedacted)
})
}
}

// Stub out the defaults so that the above is easier to maintain

func defaultCache() Cache {
Expand Down

0 comments on commit c9fcb13

Please sign in to comment.