Skip to content

Commit

Permalink
diagnostics: Fixed a crashing bug when creating diagnostics report
Browse files Browse the repository at this point in the history
This commit fixed a bug where the node would sometimes crash when
creating diagnostics report.

Release note: None
  • Loading branch information
Xiang-Gu committed Jun 7, 2023
1 parent bb7aca4 commit fefac4a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/server/diagnostics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ go_test(
size = "medium",
srcs = [
"main_test.go",
"reporter_test.go",
"update_checker_test.go",
],
args = ["-test.timeout=295s"],
embed = [":diagnostics"],
tags = ["no-remote-exec"],
deps = [
":diagnostics",
"//pkg/base",
"//pkg/build",
"//pkg/security/securityassets",
Expand All @@ -71,6 +72,7 @@ go_test(
"//pkg/util/cloudinfo",
"//pkg/util/leaktest",
"//pkg/util/log",
"@com_github_mitchellh_reflectwalk//:reflectwalk",
"@com_github_stretchr_testify//require",
],
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/diagnostics/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func anonymizeZoneConfig(dst *zonepb.ZoneConfig, src zonepb.ZoneConfig, secret s
type stringRedactor struct{}

func (stringRedactor) Primitive(v reflect.Value) error {
if v.Kind() == reflect.String && v.String() != "" {
if v.Kind() == reflect.String && v.String() != "" && v.CanSet() {
v.Set(reflect.ValueOf("_").Convert(v.Type()))
}
return nil
Expand Down
49 changes: 49 additions & 0 deletions pkg/server/diagnostics/reporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package diagnostics

import (
"testing"

"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/mitchellh/reflectwalk"
"github.com/stretchr/testify/require"
)

// TestStringRedactor_Primitive tests that fields of type `*string` will be
// correctly redacted to "_", and all other field types ( including `string`)
// will be unchanged.
func TestStringRedactor_Primitive(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

type Foo struct {
A string
B *string
C map[string]string
}

string1 := "string 1"
string2 := "string 2"
foo := Foo{
A: string1,
B: &string2,
C: map[string]string{"3": "string 3"},
}

reflectwalk.Walk(foo, stringRedactor{})
require.Equal(t, "string 1", string1)
require.Equal(t, "string 1", foo.A)
require.Equal(t, "_", string2)
require.Equal(t, "_", *foo.B)
require.Equal(t, "string 3", foo.C["3"])
}

0 comments on commit fefac4a

Please sign in to comment.