forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: add create_redactable column to crdb_internal.create_statements
Add a new `create_redactable` column to the `crdb_internal.create_statements` virtual table which provides the `CREATE` statement for the table or view with all constants and literals surrounded by redaction markers. Combined with the `crdb_internal.redact` function this can be used to obtain a redacted `CREATE` statement for any table. Part of: cockroachdb#68570 Epic: CRDB-19756 Release note: None
- Loading branch information
Showing
18 changed files
with
307 additions
and
45 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
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,134 @@ | ||
// Copyright 2023 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 sqlccl | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/internal/sqlsmith" | ||
"github.com/cockroachdb/cockroach/pkg/sql/tests" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
"github.com/cockroachdb/errors" | ||
"github.com/cockroachdb/redact" | ||
) | ||
|
||
// TestShowCreateRedactableValues tests that ShowCreateTable and ShowCreateView | ||
// do not leak PII when called with RedactableValues set to true. | ||
func TestShowCreateRedactableValues(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
|
||
ctx := context.Background() | ||
rng, seed := randutil.NewTestRand() | ||
t.Log("seed:", seed) | ||
|
||
params, _ := tests.CreateTestServerParams() | ||
s, sqlDB, _ := serverutils.StartServer(t, params) | ||
defer s.Stopper().Stop(ctx) | ||
defer sqlDB.Close() | ||
|
||
// To check for PII leaks, we inject a single unlikely string into some of the | ||
// query constants produced by SQLSmith, and then search the redacted SHOW | ||
// CREATE statement for this string. | ||
pii := "allosaurus" | ||
containsPII := func(create, createRedactable string) error { | ||
createRedacted := string(redact.RedactableString(createRedactable).Redact()) | ||
lowerCreateRedacted := strings.ToLower(createRedacted) | ||
if strings.Contains(lowerCreateRedacted, pii) { | ||
return errors.Newf( | ||
"SHOW CREATE output contained PII (%q):\noriginal:\n%s\nredactable:\n%s\nredacted:\n%s\n", | ||
pii, create, createRedactable, createRedacted, | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
// Check all redactable SHOW CREATE statements at once by using | ||
// crdb_internal.create_statements. | ||
checkAllShowCreateRedactable := func() { | ||
rows, err := sqlDB.QueryContext( | ||
ctx, "SELECT create_statement, create_redactable FROM crdb_internal.create_statements", | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for rows.Next() { | ||
var create, createRedactable string | ||
if err := rows.Scan(&create, &createRedactable); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := containsPII(create, createRedactable); err != nil { | ||
t.Error(err) | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// Perform a few random initial CREATE TABLEs and check for PII leaks. | ||
setup := sqlsmith.RandTablesPrefixStringConsts(rng, pii) | ||
setup = append(setup, "SET statement_timeout = '5s';") | ||
for _, stmt := range setup { | ||
t.Log(stmt) | ||
if _, err := sqlDB.ExecContext(ctx, stmt); err != nil { | ||
// Ignore errors. | ||
t.Log("-- ignoring error:", err) | ||
continue | ||
} | ||
} | ||
checkAllShowCreateRedactable() | ||
|
||
// Perform a few random ALTERs (and additional CREATE TABLEs) and check for | ||
// PII leaks. | ||
alterSmith, err := sqlsmith.NewSmither(sqlDB, rng, | ||
sqlsmith.PrefixStringConsts(pii), | ||
sqlsmith.DisableEverything(), | ||
sqlsmith.EnableAlters(), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer alterSmith.Close() | ||
for i := 0; i < 5; i++ { | ||
alter := alterSmith.Generate() | ||
t.Log(alter) | ||
if _, err := sqlDB.ExecContext(ctx, alter); err != nil { | ||
// Ignore errors. | ||
t.Log("-- ignoring error:", err) | ||
continue | ||
} | ||
} | ||
checkAllShowCreateRedactable() | ||
|
||
// Perform a few random CREATE VIEWs and check for PII leaks. | ||
smith, err := sqlsmith.NewSmither(sqlDB, rng, | ||
sqlsmith.PrefixStringConsts(pii), | ||
sqlsmith.DisableMutations(), | ||
sqlsmith.DisableWith(), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer smith.Close() | ||
for i := 0; i < 5; i++ { | ||
view := "CREATE VIEW v" + strconv.Itoa(i) + " AS " + smith.Generate() | ||
t.Log(view) | ||
if _, err := sqlDB.ExecContext(ctx, view); err != nil { | ||
// Ignore errors. | ||
t.Log("-- ignoring error:", err) | ||
continue | ||
} | ||
} | ||
checkAllShowCreateRedactable() | ||
} |
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
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
Oops, something went wrong.