-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Heartbeat] Add stringer redaction to synthexec cmd (#39535)
Redact synthetics cmd output to prevent logging of user-defined parameters. --------- Co-authored-by: Vignesh Shanmugam <[email protected]>
- Loading branch information
1 parent
fab441b
commit 7306bd4
Showing
6 changed files
with
134 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
//go:build linux || darwin || synthetics | ||
|
||
package synthexec | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// Variant of exec.command with redacted params and playwright options, | ||
// which might contain sensitive information. | ||
type SynthCmd struct { | ||
*exec.Cmd | ||
} | ||
|
||
func (cmd *SynthCmd) String() string { | ||
b := new(strings.Builder) | ||
b.WriteString(cmd.Path) | ||
for i := 1; i < len(cmd.Args); i++ { | ||
b.WriteString(" ") | ||
a := cmd.Args[i] | ||
switch a { | ||
case "--params": | ||
fallthrough | ||
case "--playwright-options": | ||
b.WriteString(fmt.Sprintf("%s { REDACTED }", a)) | ||
i++ | ||
default: | ||
b.WriteString(a) | ||
} | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
// Formatter override redacting params | ||
func (cmd SynthCmd) Format(f fmt.State, verb rune) { | ||
|
||
f.Write([]byte(cmd.String())) | ||
} |
66 changes: 66 additions & 0 deletions
66
x-pack/heartbeat/monitors/browser/synthexec/synthcmd_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
//go:build linux || synthetics | ||
|
||
package synthexec | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSynthCmdStringOutput(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
stringer func(cmd SynthCmd) string | ||
}{ | ||
{ | ||
name: "fmt.Sprintf", | ||
stringer: func(cmd SynthCmd) string { | ||
return fmt.Sprintf("%s", cmd) | ||
}, | ||
}, | ||
{ | ||
name: "fmt.Println", | ||
stringer: func(cmd SynthCmd) string { | ||
r, w, err := os.Pipe() | ||
assert.NoError(t, err) | ||
fmt.Fprint(w, cmd) | ||
w.Close() | ||
defer r.Close() | ||
|
||
o, err := io.ReadAll(r) | ||
assert.NoError(t, err) | ||
|
||
return string(o) | ||
}, | ||
}, | ||
{ | ||
name: "cmd.String()", | ||
stringer: func(cmd SynthCmd) string { | ||
return cmd.String() | ||
}, | ||
}, | ||
} | ||
|
||
redacted := []string{"secret", "mysecrettoken", "auth", "mysecretauth"} | ||
cmd := SynthCmd{ | ||
exec.Command("/nil", "--params", "{'secret':'mysecrettoken'}", "--playwright-options", "{'auth':'mysecretauth'}"), | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := tt.stringer(cmd) | ||
for _, r := range redacted { | ||
assert.NotContains(t, s, r) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters