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

sql: enforce admin role for resetting sql stats and index usage stats #79810

Merged
merged 1 commit into from
Apr 20, 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
27 changes: 27 additions & 0 deletions pkg/server/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3539,3 +3539,30 @@ func TestTransactionContentionEvents(t *testing.T) {
})
}
}

func TestUnprivilegedUserResetIndexUsageStats(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()

s, conn, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)

sqlConn := sqlutils.MakeSQLRunner(conn)
sqlConn.Exec(t, "CREATE USER nonAdminUser")

ie := s.InternalExecutor().(*sql.InternalExecutor)

_, err := ie.ExecEx(
ctx,
"test-reset-index-usage-stats-as-non-admin-user",
nil, /* txn */
sessiondata.InternalExecutorOverride{
User: security.MakeSQLUsernameFromPreNormalizedString("nonAdminUser"),
},
"SELECT crdb_internal.reset_index_usage_stats()",
)

require.Contains(t, err.Error(), "requires admin privilege")
}
20 changes: 18 additions & 2 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -6421,12 +6421,20 @@ table's zone configuration this will return NULL.`,
),
"crdb_internal.reset_index_usage_stats": makeBuiltin(
tree.FunctionProperties{
Category: categorySystemInfo,
Category: categorySystemInfo,
DistsqlBlocklist: true, // applicable only on the gateway
},
tree.Overload{
Types: tree.ArgTypes{},
ReturnType: tree.FixedReturnType(types.Bool),
Fn: func(evalCtx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
isAdmin, err := evalCtx.SessionAccessor.HasAdminRole(evalCtx.Ctx())
if err != nil {
return nil, err
}
if !isAdmin {
return nil, errors.New("crdb_internal.reset_index_usage_stats() requires admin privilege")
}
if evalCtx.IndexUsageStatsController == nil {
return nil, errors.AssertionFailedf("index usage stats controller not set")
}
Expand All @@ -6442,12 +6450,20 @@ table's zone configuration this will return NULL.`,
),
"crdb_internal.reset_sql_stats": makeBuiltin(
tree.FunctionProperties{
Category: categorySystemInfo,
Category: categorySystemInfo,
DistsqlBlocklist: true, // applicable only on the gateway
},
tree.Overload{
Types: tree.ArgTypes{},
ReturnType: tree.FixedReturnType(types.Bool),
Fn: func(evalCtx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
isAdmin, err := evalCtx.SessionAccessor.HasAdminRole(evalCtx.Ctx())
if err != nil {
return nil, err
}
if !isAdmin {
return nil, errors.New("crdb_internal.reset_sql_stats() requires admin privilege")
}
if evalCtx.SQLStatsController == nil {
return nil, errors.AssertionFailedf("sql stats controller not set")
}
Expand Down
29 changes: 0 additions & 29 deletions pkg/sql/sem/builtins/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,35 +469,6 @@ func TestExtractTimeSpanFromTimeTZ(t *testing.T) {
}
}

// TestResetIndexUsageStatsOnRemoteSQLNode asserts that the built-in for
// resetting index usage statistics works when it's being set up on a remote
// node via DistSQL.
func TestResetIndexUsageStatsOnRemoteSQLNode(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unfamiliar with the terminology here - does remote node mean not gateway?

I don't think we have a test here to run crdb_internal.reset_index_usage_stats() on a gateway node either.

defer leaktest.AfterTest(t)()
ctx := context.Background()

testCluster := serverutils.StartNewTestCluster(t, 3 /* numNodes */, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{},
})
defer testCluster.Stopper().Stop(ctx)
testConn := testCluster.ServerConn(2 /* idx */)
sqlDB := sqlutils.MakeSQLRunner(testConn)

query := `
CREATE TABLE t (k INT PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 30);

ALTER TABLE t SPLIT AT VALUES (10), (20);
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE SELECT 1, 1;
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE SELECT 2, 15;
ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE SELECT 3, 25;

SELECT count(*) FROM t WHERE crdb_internal.reset_index_usage_stats();
`

sqlDB.Exec(t, query)
}

func TestExtractTimeSpanFromInterval(t *testing.T) {
defer leaktest.AfterTest(t)()

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqlstats/sslocal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ go_test(
"//pkg/settings",
"//pkg/settings/cluster",
"//pkg/sql",
"//pkg/sql/sessiondata",
"//pkg/sql/sessionphase",
"//pkg/sql/sqlstats",
"//pkg/sql/sqlstats/persistedsqlstats",
Expand Down
29 changes: 29 additions & 0 deletions pkg/sql/sqlstats/sslocal/sql_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats/persistedsqlstats"
Expand Down Expand Up @@ -640,3 +642,30 @@ WHERE
`,
[][]string{{"0"}})
}

func TestUnprivilegedUserReset(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()

s, conn, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)

sqlConn := sqlutils.MakeSQLRunner(conn)
sqlConn.Exec(t, "CREATE USER nonAdminUser")

ie := s.InternalExecutor().(*sql.InternalExecutor)

_, err := ie.ExecEx(
ctx,
"test-reset-sql-stats-as-non-admin-user",
nil, /* txn */
sessiondata.InternalExecutorOverride{
User: security.MakeSQLUsernameFromPreNormalizedString("nonAdminUser"),
},
"SELECT crdb_internal.reset_sql_stats()",
)

require.Contains(t, err.Error(), "requires admin privilege")
}