Skip to content

Commit

Permalink
Redact APM API key and secret token
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Oct 28, 2024
1 parent dbdd056 commit 6d1ed7a
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 @@ -186,6 +186,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 @@ -306,6 +306,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 6d1ed7a

Please sign in to comment.