Skip to content

Commit b6818d4

Browse files
committed
PR: scrub logged OCI spec
Signed-off-by: Hamza El-Saawy <[email protected]>
1 parent 18cf1cc commit b6818d4

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,15 @@ func writeSpecToFile(ctx context.Context, configFile string, spec *specs.Spec) e
708708
}
709709

710710
if logrus.IsLevelEnabled(logrus.TraceLevel) {
711-
log.G(ctx).WithFields(logrus.Fields{
712-
logfields.Path: configFile,
713-
"config": strings.TrimSpace(buf.String()),
714-
}).Trace("wrote OCI spec to config.json file")
711+
entry := log.G(ctx).WithField(logfields.Path, configFile)
712+
713+
if b, err := log.ScrubOCISpec(buf.Bytes()); err != nil {
714+
entry.WithError(err).Warning("could not scrub OCI spec written to config.json")
715+
} else {
716+
log.G(ctx).WithField(
717+
"config", string(bytes.TrimSpace(b)),
718+
).Trace("wrote OCI spec to config.json")
719+
}
715720
}
716721

717722
return nil

internal/log/scrub.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// This package scrubs objects of potentially sensitive information to pass to logging
1313

14-
type genMap = map[string]interface{}
14+
type genMap = map[string]any
1515
type scrubberFunc func(genMap) error
1616

1717
const _scrubbedReplacement = "<scrubbed>"
@@ -20,7 +20,11 @@ var (
2020
ErrUnknownType = errors.New("encoded object is of unknown type")
2121

2222
// case sensitive keywords, so "env" is not a substring on "Environment"
23-
_scrubKeywords = [][]byte{[]byte("env"), []byte("Environment")}
23+
_scrubKeywords = [][]byte{
24+
[]byte("env"),
25+
[]byte("Environment"),
26+
[]byte("annotations"),
27+
}
2428

2529
_scrub atomic.Bool
2630
)
@@ -32,7 +36,7 @@ func SetScrubbing(enable bool) { _scrub.Store(enable) }
3236
func IsScrubbingEnabled() bool { return _scrub.Load() }
3337

3438
// ScrubProcessParameters scrubs HCS Create Process requests with config parameters of
35-
// type internal/hcs/schema2.ScrubProcessParameters (aka hcsshema.ScrubProcessParameters)
39+
// type [hcsschema.ProcessParameters].
3640
func ScrubProcessParameters(s string) (string, error) {
3741
// todo: deal with v1 ProcessConfig
3842
b := []byte(s)
@@ -81,19 +85,34 @@ func scrubBridgeCreate(m genMap) error {
8185

8286
func scrubLinuxHostedSystem(m genMap) error {
8387
if m, ok := index(m, "OciSpecification"); ok { //nolint:govet // shadow
84-
if _, ok := m["annotations"]; ok {
85-
m["annotations"] = map[string]string{_scrubbedReplacement: _scrubbedReplacement}
86-
}
87-
if m, ok := index(m, "process"); ok { //nolint:govet // shadow
88-
if _, ok := m["env"]; ok {
89-
m["env"] = []string{_scrubbedReplacement}
90-
return nil
91-
}
92-
}
88+
return scrubOCISpec(m)
9389
}
9490
return ErrUnknownType
9591
}
9692

93+
// ScrubOCISpec scrubs a JSON encoded [github.com/opencontainers/runtime-spec/specs-go.Spec].
94+
//
95+
// Ideally the spec struct would be scrubbed directly, but that would need a deep clone to
96+
// prevent modifying the original, and, absent one implemented on the Spec
97+
// (e.g., [google.golang.org/protobuf/proto.CloneOf]), unmarshalling a marshalled struct
98+
// functions as a deep clone.
99+
func ScrubOCISpec(b []byte) ([]byte, error) {
100+
return scrubBytes(b, scrubOCISpec)
101+
}
102+
103+
func scrubOCISpec(m genMap) error {
104+
if _, ok := m["annotations"]; ok {
105+
m["annotations"] = map[string]string{_scrubbedReplacement: _scrubbedReplacement}
106+
}
107+
if m, ok := index(m, "process"); ok { //nolint:govet // shadow
108+
if _, ok := m["env"]; ok {
109+
m["env"] = []string{_scrubbedReplacement}
110+
}
111+
}
112+
113+
return nil
114+
}
115+
97116
// ScrubBridgeExecProcess scrubs requests sent over the bridge of type
98117
// internal/gcs/protocol.containerExecuteProcess
99118
func ScrubBridgeExecProcess(b []byte) ([]byte, error) {

0 commit comments

Comments
 (0)