From f0413ebe7ec7a7337325c81c751a7e23ea555ad7 Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Fri, 22 Apr 2022 16:02:49 -0400 Subject: [PATCH] sql: fix "no user specified" errors in statement bundles A regression was introduced in #71246 that caused errors when running internal queries to populate files in statement bundles. As a result, critical information was missing from the `env.sql`, `schema.sql`, and `stats*.sql` files. This commit fixes the issue by using an `InternalExecutorOverride` with a root user override. A root user is required because a node user is insufficient - it does not have privileges on user-defined relations which is needed for the `SHOW CREATE` queries that populate `schema.sql`. Fixes #80396 Release note: None --- pkg/sql/explain_bundle.go | 10 ++++++++-- pkg/sql/explain_bundle_test.go | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/pkg/sql/explain_bundle.go b/pkg/sql/explain_bundle.go index 53d8e7d9bef2..524cee0fe5ac 100644 --- a/pkg/sql/explain_bundle.go +++ b/pkg/sql/explain_bundle.go @@ -19,6 +19,7 @@ import ( "strings" "github.com/cockroachdb/cockroach/pkg/kv" + "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/settings" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" @@ -431,6 +432,11 @@ func makeStmtEnvCollector(ctx context.Context, ie *InternalExecutor) stmtEnvColl return stmtEnvCollector{ctx: ctx, ie: ie} } +// explainBundleUserSessionsDataOverride is an InternalExecutorOverride which +// overrides the users to the RootUser. +var explainBundleUserSessionDataOverride = sessiondata.InternalExecutorOverride{ + User: security.MakeSQLUsernameFromPreNormalizedString(security.RootUser)} + // environmentQuery is a helper to run a query that returns a single string // value. func (c *stmtEnvCollector) query(query string) (string, error) { @@ -438,7 +444,7 @@ func (c *stmtEnvCollector) query(query string) (string, error) { c.ctx, "stmtEnvCollector", nil, /* txn */ - sessiondata.NoSessionDataOverride, + explainBundleUserSessionDataOverride, query, ) if err != nil { @@ -574,7 +580,7 @@ func (c *stmtEnvCollector) PrintClusterSettings(w io.Writer) error { c.ctx, "stmtEnvCollector", nil, /* txn */ - sessiondata.NoSessionDataOverride, + explainBundleUserSessionDataOverride, "SELECT variable, value, description FROM [ SHOW ALL CLUSTER SETTINGS ]", ) if err != nil { diff --git a/pkg/sql/explain_bundle_test.go b/pkg/sql/explain_bundle_test.go index efa0e6d95e35..dceee3f92c33 100644 --- a/pkg/sql/explain_bundle_test.go +++ b/pkg/sql/explain_bundle_test.go @@ -189,16 +189,25 @@ func checkBundle(t *testing.T, text, tableName string, expectedFiles ...string) } files = append(files, f.Name) + r, err := f.Open() + if err != nil { + t.Fatal(err) + } + defer r.Close() + contents, err := ioutil.ReadAll(r) + if err != nil { + t.Fatal(err) + } + + if strings.Contains(string(contents), "-- error") { + t.Errorf( + "expected no errors in %s, file contents:\n%s", + f.Name, + string(contents), + ) + } + if f.Name == "schema.sql" { - r, err := f.Open() - if err != nil { - t.Fatal(err) - } - defer r.Close() - contents, err := ioutil.ReadAll(r) - if err != nil { - t.Fatal(err) - } if !strings.Contains(string(contents), tableName) { t.Errorf( "expected table name to appear in schema.sql. tableName: %s\nfile contents:\n%s",