Skip to content

Commit

Permalink
sql: fix "no user specified" errors in statement bundles
Browse files Browse the repository at this point in the history
A regression was introduced in cockroachdb#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 cockroachdb#80396

Release note: None
  • Loading branch information
mgartner committed Apr 22, 2022
1 parent 2f8938f commit f0413eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
10 changes: 8 additions & 2 deletions pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -431,14 +432,19 @@ 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) {
row, err := c.ie.QueryRowEx(
c.ctx,
"stmtEnvCollector",
nil, /* txn */
sessiondata.NoSessionDataOverride,
explainBundleUserSessionDataOverride,
query,
)
if err != nil {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 18 additions & 9 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f0413eb

Please sign in to comment.