Skip to content

Commit 25e23ea

Browse files
authored
Redact APM API key and secret token (#4057)
1 parent 8dceee0 commit 25e23ea

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

internal/pkg/config/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ func redactServer(cfg *Config) Server {
186186
redacted.TLS = &newTLS
187187
}
188188

189+
if redacted.Instrumentation.APIKey != "" {
190+
redacted.Instrumentation.APIKey = kRedacted
191+
}
192+
193+
if redacted.Instrumentation.SecretToken != "" {
194+
redacted.Instrumentation.SecretToken = kRedacted
195+
}
196+
189197
return redacted
190198
}
191199

internal/pkg/config/config_test.go

+126
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,132 @@ func TestLoadServerLimits(t *testing.T) {
306306

307307
}
308308

309+
func TestConfigRedact(t *testing.T) {
310+
311+
testcases := []struct {
312+
name string
313+
inputCfg *Config
314+
redactedCfg *Config
315+
}{
316+
{
317+
name: "do not modify empty APM secrets",
318+
inputCfg: &Config{
319+
Inputs: []Input{
320+
{
321+
Type: "fleet-server",
322+
Server: Server{
323+
Instrumentation: Instrumentation{
324+
SecretToken: "",
325+
APIKey: "",
326+
},
327+
},
328+
},
329+
},
330+
},
331+
redactedCfg: &Config{
332+
Inputs: []Input{
333+
{
334+
Server: Server{
335+
Instrumentation: Instrumentation{
336+
SecretToken: "",
337+
APIKey: "",
338+
},
339+
},
340+
},
341+
},
342+
},
343+
},
344+
{
345+
name: "redact APM secret token",
346+
inputCfg: &Config{
347+
Inputs: []Input{
348+
{
349+
Type: "fleet-server",
350+
Server: Server{
351+
Instrumentation: Instrumentation{
352+
SecretToken: "secret value that noone should know",
353+
},
354+
},
355+
},
356+
},
357+
},
358+
redactedCfg: &Config{
359+
Inputs: []Input{
360+
{
361+
Server: Server{
362+
Instrumentation: Instrumentation{
363+
SecretToken: kRedacted,
364+
},
365+
},
366+
},
367+
},
368+
},
369+
},
370+
{
371+
name: "redact APM API key",
372+
inputCfg: &Config{
373+
Inputs: []Input{
374+
{
375+
Type: "fleet-server",
376+
Server: Server{
377+
Instrumentation: Instrumentation{
378+
APIKey: "secret value that noone should know",
379+
},
380+
},
381+
},
382+
},
383+
},
384+
redactedCfg: &Config{
385+
Inputs: []Input{
386+
{
387+
Server: Server{
388+
Instrumentation: Instrumentation{
389+
APIKey: kRedacted,
390+
},
391+
},
392+
},
393+
},
394+
},
395+
},
396+
{
397+
name: "redact both APM API key and secret token",
398+
inputCfg: &Config{
399+
Inputs: []Input{
400+
{
401+
Type: "fleet-server",
402+
Server: Server{
403+
Instrumentation: Instrumentation{
404+
APIKey: "secret value that noone should know",
405+
SecretToken: "another value that noone should know",
406+
},
407+
},
408+
},
409+
},
410+
},
411+
redactedCfg: &Config{
412+
Inputs: []Input{
413+
{
414+
Server: Server{
415+
Instrumentation: Instrumentation{
416+
APIKey: kRedacted,
417+
SecretToken: kRedacted,
418+
},
419+
},
420+
},
421+
},
422+
},
423+
},
424+
}
425+
426+
for _, tt := range testcases {
427+
t.Run(tt.name, func(t *testing.T) {
428+
require.NotNil(t, tt.inputCfg, "input config cannot be nil")
429+
actualRedacted := tt.inputCfg.Redact()
430+
assert.Equal(t, tt.redactedCfg, actualRedacted)
431+
})
432+
}
433+
}
434+
309435
// Stub out the defaults so that the above is easier to maintain
310436

311437
func defaultCache() Cache {

0 commit comments

Comments
 (0)