-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70759 from cockroachdb/blathers/backport-release-…
…21.2-70562 release-21.2: log,kvserver: hand redacted KV traces to tenants
- Loading branch information
Showing
27 changed files
with
522 additions
and
168 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,108 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package kvtenantccl_test | ||
|
||
import ( | ||
"context" | ||
gosql "database/sql" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver" | ||
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/security" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestTenantTracesAreRedacted is an end-to-end version of | ||
// `kvserver.TestMaybeRedactRecording`. | ||
func TestTenantTracesAreRedacted(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
ctx := context.Background() | ||
|
||
const ( | ||
sensitiveString = "super-secret-stuff" | ||
visibleString = "tenant-can-see-this" | ||
) | ||
|
||
getTrace := func(t *testing.T, db *gosql.DB) [][]string { | ||
runner := sqlutils.MakeSQLRunner(db) | ||
runner.Exec(t, `CREATE TABLE kv(k STRING PRIMARY KEY, v STRING)`) | ||
runner.Exec(t, ` | ||
SET tracing = on; | ||
INSERT INTO kv VALUES('k', 'v'); | ||
SELECT * FROM kv; | ||
SET tracing = off; | ||
`) | ||
sl := runner.QueryStr(t, `SELECT * FROM [ SHOW TRACE FOR SESSION ]`) | ||
t.Log(sqlutils.MatrixToStr(sl)) | ||
return sl | ||
} | ||
|
||
knobs := &kvserver.StoreTestingKnobs{} | ||
knobs.EvalKnobs.TestingEvalFilter = func(args kvserverbase.FilterArgs) *roachpb.Error { | ||
log.Eventf(args.Ctx, "%v", sensitiveString) | ||
log.Eventf(args.Ctx, "%v", log.Safe(visibleString)) | ||
return nil | ||
} | ||
var args base.TestClusterArgs | ||
args.ServerArgs.Knobs.Store = knobs | ||
tc := serverutils.StartNewTestCluster(t, 1, args) | ||
defer tc.Stopper().Stop(ctx) | ||
|
||
t.Run("system-tenant", func(t *testing.T) { | ||
db := tc.ServerConn(0) | ||
defer db.Close() | ||
results := getTrace(t, db) | ||
|
||
var found bool | ||
for _, sl := range results { | ||
for _, s := range sl { | ||
if strings.Contains(s, sensitiveString) { | ||
found = true | ||
} | ||
} | ||
} | ||
require.True(t, found, "did not find '%q' in trace:\n%s", | ||
sensitiveString, sqlutils.MatrixToStr(results), | ||
) | ||
}) | ||
|
||
t.Run("regular-tenant", func(t *testing.T) { | ||
_, tenDB := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{ | ||
TenantID: roachpb.MakeTenantID(security.EmbeddedTenantIDs()[0]), | ||
}) | ||
defer tenDB.Close() | ||
results := getTrace(t, tenDB) | ||
|
||
var found bool | ||
for _, sl := range results { | ||
for _, s := range sl { | ||
if strings.Contains(s, sensitiveString) { | ||
t.Fatalf( | ||
"trace for tenant contained KV-level trace message '%q':\n%s", | ||
sensitiveString, sqlutils.MatrixToStr(results), | ||
) | ||
} | ||
if strings.Contains(s, visibleString) { | ||
found = true | ||
} | ||
} | ||
} | ||
require.True(t, found, "trace for tenant missing trace message '%q':\n%s", | ||
visibleString, sqlutils.MatrixToStr(results)) | ||
}) | ||
} |
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
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,54 @@ | ||
// 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 server | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/tracing" | ||
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb" | ||
"github.com/cockroachdb/redact" | ||
) | ||
|
||
var sRedactedMarker = redact.RedactableString(redact.EscapeBytes(nil)) | ||
|
||
func maybeRedactRecording(tenID roachpb.TenantID, rec tracing.Recording) { | ||
if tenID == roachpb.SystemTenantID { | ||
return | ||
} | ||
// For tenants, strip the verbose log messages. See: | ||
// https://github.com/cockroachdb/cockroach/issues/70407 | ||
for i := range rec { | ||
sp := &rec[i] | ||
sp.Tags = nil | ||
for j := range sp.Logs { | ||
record := &sp.Logs[j] | ||
for k := range record.Fields { | ||
field := &record.Fields[k] | ||
if field.Key != tracingpb.LogMessageField { | ||
// We don't have any of these fields, but let's not take any | ||
// chances (our dependencies might slip them in). | ||
field.Value = sRedactedMarker | ||
continue | ||
} | ||
if !sp.RedactableLogs { | ||
// If we're handling a span that originated from an (early patch | ||
// release) 22.1 node, all the containing information will be | ||
// stripped. Note that this is not the common path here, as most | ||
// information in the trace will be from the local node, which | ||
// always creates redactable logs. | ||
field.Value = sRedactedMarker | ||
continue | ||
} | ||
field.Value = field.Value.Redact() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.