Skip to content

Commit cff945f

Browse files
committed
builtins: add crdb_internal.can_view_job
This builtin determines if the current user can view a job owned by the specified user. Release note: none. Epic: none.
1 parent 65468cf commit cff945f

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

pkg/sql/faketreeeval/evalctx.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,14 @@ func (ep *DummySessionAccessor) HasViewActivityOrViewActivityRedactedRole(
647647
return false, false, errors.WithStack(errEvalSessionVar)
648648
}
649649

650+
// HasViewAccessToJob implements SessionAccessor.
651+
func (ep *DummySessionAccessor) HasViewAccessToJob(
652+
ctx context.Context, owner username.SQLUsername,
653+
) bool {
654+
// This is a no-op in the dummy implementation.
655+
return false
656+
}
657+
650658
func (ep *DummySessionAccessor) ForEachSessionPendingJob(
651659
_ func(job jobspb.PendingJob) error,
652660
) error {

pkg/sql/jobs_collection.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
package sql
77

88
import (
9+
"context"
10+
911
"github.com/cockroachdb/cockroach/pkg/jobs"
12+
"github.com/cockroachdb/cockroach/pkg/jobs/jobsauth"
1013
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
14+
"github.com/cockroachdb/cockroach/pkg/security/username"
1115
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
1216
)
1317

@@ -100,3 +104,11 @@ func (p *planner) ForEachSessionPendingJob(fn func(job jobspb.PendingJob) error)
100104
})
101105
})
102106
}
107+
108+
func (p *planner) HasViewAccessToJob(ctx context.Context, owner username.SQLUsername) bool {
109+
privs, err := jobsauth.GetGlobalJobPrivileges(ctx, p)
110+
if err != nil {
111+
return false
112+
}
113+
return jobsauth.Authorize(ctx, p, jobspb.InvalidJobID, owner, jobsauth.ViewAccess, privs) == nil
114+
}

pkg/sql/sem/builtins/builtins.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,6 +4631,24 @@ value if you rely on the HLC for accuracy.`,
46314631
Volatility: volatility.Volatile,
46324632
}),
46334633

4634+
"crdb_internal.can_view_job": makeBuiltin(
4635+
tree.FunctionProperties{Category: builtinconstants.CategorySystemInfo, DistsqlBlocklist: true},
4636+
tree.Overload{
4637+
Types: tree.ParamTypes{
4638+
{Name: "owner", Typ: types.String},
4639+
},
4640+
ReturnType: tree.FixedReturnType(types.Bool),
4641+
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
4642+
ownerStr := string(tree.MustBeDString(args[0]))
4643+
owner := username.MakeSQLUsernameFromPreNormalizedString(ownerStr)
4644+
ok := evalCtx.SessionAccessor.HasViewAccessToJob(ctx, owner)
4645+
return tree.MakeDBool(tree.DBool(ok)), nil
4646+
},
4647+
Info: "Returns true if the current user can view a job owned by the specified owner.",
4648+
Volatility: volatility.Stable,
4649+
},
4650+
),
4651+
46344652
"crdb_internal.read_file": makeBuiltin(
46354653
tree.FunctionProperties{
46364654
Category: builtinconstants.CategorySystemInfo,

pkg/sql/sem/builtins/fixed_oids.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,7 @@ var builtinOidsArray = []string{
26662666
2703: `crdb_internal.show_create_all_routines(database_name: string) -> string`,
26672667
2704: `crdb_internal.show_create_all_triggers(database_name: string) -> string`,
26682668
2705: `crdb_internal.session_pending_jobs() -> tuple{int AS job_id, string AS job_type, string AS description, string AS user_name}`,
2669+
2706: `crdb_internal.can_view_job(owner: string) -> bool`,
26692670
}
26702671

26712672
var builtinOidsBySignature map[string]oid.Oid

pkg/sql/sem/eval/deps.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ type SessionAccessor interface {
527527
// CheckPrivilege verifies that the current user has `privilege` on `descriptor`.
528528
CheckPrivilege(ctx context.Context, privilegeObject privilege.Object, privilege privilege.Kind) error
529529

530+
// HasViewAccessToJob checks if the current user has access to a job owned by the specified owner.
531+
HasViewAccessToJob(ctx context.Context, owner username.SQLUsername) bool
532+
530533
// HasViewActivityOrViewActivityRedactedRole returns true iff the current session user has the
531534
// VIEWACTIVITY or VIEWACTIVITYREDACTED permission.
532535
HasViewActivityOrViewActivityRedactedRole(ctx context.Context) (bool, bool, error)

0 commit comments

Comments
 (0)