From cd9e972de6cf9cbe852b60c33122ff2382b86d92 Mon Sep 17 00:00:00 2001 From: Oliver Tan Date: Thu, 7 Jul 2022 10:10:32 +1000 Subject: [PATCH] builtins: check privileges for crdb_internal builtins Release note (sql change): crdb_internal.validate_ttl_scheduled_jobs and crdb_internal.repair_ttl_table_scheduled_job can now only be run by admins. --- .../logictest/testdata/logic_test/row_level_ttl | 13 +++++++++++++ pkg/sql/sem/builtins/builtins.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/sql/logictest/testdata/logic_test/row_level_ttl b/pkg/sql/logictest/testdata/logic_test/row_level_ttl index 6b1f1e95f6e9..e6a9b6bb0da0 100644 --- a/pkg/sql/logictest/testdata/logic_test/row_level_ttl +++ b/pkg/sql/logictest/testdata/logic_test/row_level_ttl @@ -61,6 +61,19 @@ SELECT crdb_internal.repair_ttl_table_scheduled_job('tbl'::regclass::oid) statement ok SELECT crdb_internal.validate_ttl_scheduled_jobs() +let $tbl_oid +SELECT 'tbl'::regclass::oid + +user testuser + +statement error insufficient privilege +SELECT crdb_internal.repair_ttl_table_scheduled_job($tbl_oid) + +statement error insufficient privilege +SELECT crdb_internal.validate_ttl_scheduled_jobs() + +user root + statement error "ttl_expire_after" and/or "ttl_expiration_expression" must be set ALTER TABLE tbl RESET (ttl_expire_after) diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go index 3e239594917a..dfd8e9169ade 100644 --- a/pkg/sql/sem/builtins/builtins.go +++ b/pkg/sql/sem/builtins/builtins.go @@ -6519,6 +6519,13 @@ table's zone configuration this will return NULL.`, Types: tree.ArgTypes{}, ReturnType: tree.FixedReturnType(types.Void), Fn: func(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) { + isAdmin, err := evalCtx.SessionAccessor.HasAdminRole(evalCtx.Context) + if err != nil { + return nil, err + } + if !isAdmin { + return nil, errInsufficientPriv + } return tree.DVoidDatum, evalCtx.Planner.ValidateTTLScheduledJobsInCurrentDB(evalCtx.Context) }, Info: `Validate all TTL tables have a valid scheduled job attached.`, @@ -6534,6 +6541,13 @@ table's zone configuration this will return NULL.`, Types: tree.ArgTypes{{"oid", types.Oid}}, ReturnType: tree.FixedReturnType(types.Void), Fn: func(evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) { + isAdmin, err := evalCtx.SessionAccessor.HasAdminRole(evalCtx.Context) + if err != nil { + return nil, err + } + if !isAdmin { + return nil, errInsufficientPriv + } oid := tree.MustBeDOid(args[0]) if err := evalCtx.Planner.RepairTTLScheduledJobForTable(evalCtx.Ctx(), int64(oid.Oid)); err != nil { return nil, err