Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: make key/value printing explicitly redactable #81495

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/roachpb/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,9 @@ var _ = (SequencedWriteBySeq{}).Find

func init() {
// Inject the format dependency into the enginepb package.
enginepb.FormatBytesAsKey = func(k []byte) string { return Key(k).String() }
enginepb.FormatBytesAsKey = func(k []byte) redact.RedactableString {
return redact.Sprint(Key(k))
}
}

// SafeValue implements the redact.SafeValue interface.
Expand Down
1 change: 1 addition & 0 deletions pkg/storage/enginepb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ go_test(
"//pkg/util/uuid",
"@com_github_cockroachdb_redact//:redact",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)
10 changes: 4 additions & 6 deletions pkg/storage/enginepb/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,11 @@ func (t TxnMeta) SafeFormat(w redact.SafePrinter, _ rune) {
}

// FormatBytesAsKey is injected by module roachpb as dependency upon initialization.
// TODO(sarkesian): Make this explicitly redactable. See #70288
var FormatBytesAsKey = func(k []byte) string {
return string(k)
var FormatBytesAsKey = func(k []byte) redact.RedactableString {
return redact.Sprint(string(k))
}

// FormatBytesAsValue is injected by module roachpb as dependency upon initialization.
// TODO(sarkesian): Make this explicitly redactable. See #70288
var FormatBytesAsValue = func(v []byte) string {
return string(v)
var FormatBytesAsValue = func(v []byte) redact.RedactableString {
return redact.Sprint(string(v))
}
12 changes: 12 additions & 0 deletions pkg/storage/enginepb/mvcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/redact"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFormatMVCCMetadata(t *testing.T) {
Expand Down Expand Up @@ -105,3 +106,14 @@ func TestTxnSeqIsIgnored(t *testing.T) {
}
}
}

func TestFormatBytesAsKeyAndValue(t *testing.T) {
// Injected by roachpb
require.Equal(t, string(enginepb.FormatBytesAsKey([]byte("foo"))), "‹\"foo\"›")
require.Equal(t, string(enginepb.FormatBytesAsKey([]byte("foo")).Redact()), "‹×›")

// Injected by storage
encodedIntVal := []byte{0x0, 0x0, 0x0, 0x0, 0x1, 0xf}
require.Equal(t, string(enginepb.FormatBytesAsValue(encodedIntVal)), "‹/INT/-8›")
require.Equal(t, string(enginepb.FormatBytesAsValue(encodedIntVal).Redact()), "‹×›")
}
7 changes: 3 additions & 4 deletions pkg/storage/mvcc_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package storage

import (
"encoding/binary"
"fmt"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
Expand Down Expand Up @@ -279,11 +278,11 @@ func decodeExtendedMVCCValue(buf []byte) (MVCCValue, error) {

func init() {
// Inject the format dependency into the enginepb package.
enginepb.FormatBytesAsValue = func(v []byte) string {
enginepb.FormatBytesAsValue = func(v []byte) redact.RedactableString {
val, err := DecodeMVCCValue(v)
if err != nil {
return fmt.Sprintf("err=%v", err)
return redact.Sprintf("err=%v", err)
}
return val.String()
return redact.Sprint(val)
}
}