-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathauthorization.go
124 lines (103 loc) · 3.89 KB
/
authorization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package jobsauth
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/roleoption"
)
// An AccessLevel is used to indicate how strict an authorization check should
// be.
type AccessLevel int
const (
// ViewAccess is used to perform authorization for viewing jobs (ex. SHOW JOBS).
ViewAccess AccessLevel = iota
// ControlAccess is used to perform authorization for modifying jobs (ex. PAUSE|CANCEL|RESUME JOB).
// This access level performs stricter checks than ViewAccess.
//
// The set of jobs visible via ControlAccess is a subset of jobs visible via
// ViewAccess. In other words: if a user with a given set of privileges is
// authorized to modify a job using ControlAccess, they will be authorized to
// view it using ViewAccess.
ControlAccess
)
var authorizers = make(map[jobspb.Type]Authorizer)
// Authorizer is a function which returns a pgcode.InsufficientPrivilege error if
// authorization for the job denoted by jobID and payload fails.
type Authorizer func(
ctx context.Context, a AuthorizationAccessor, jobID jobspb.JobID, payload *jobspb.Payload,
) error
// RegisterAuthorizer registers a AuthorizationCheck for a certain job type.
func RegisterAuthorizer(typ jobspb.Type, fn Authorizer) {
if _, ok := authorizers[typ]; ok {
panic(fmt.Sprintf("cannot register two authorizers for the type %s", typ))
}
authorizers[typ] = fn
}
// AuthorizationAccessor is an interface for checking authorization on jobs.
type AuthorizationAccessor interface {
// CheckPrivilegeForTableID mirrors sql.AuthorizationAccessor.
CheckPrivilegeForTableID(ctx context.Context, tableID descpb.ID, privilege privilege.Kind) error
// HasRoleOption mirrors sql.AuthorizationAccessor.
HasRoleOption(ctx context.Context, roleOption roleoption.Option) (bool, error)
// UserHasAdminRole mirrors sql.AuthorizationAccessor.
UserHasAdminRole(ctx context.Context, user username.SQLUsername) (bool, error)
// HasAdminRole mirrors sql.AuthorizationAccessor.
HasAdminRole(ctx context.Context) (bool, error)
// User mirrors sql.PlanHookState.
User() username.SQLUsername
}
// Authorize returns nil if the user is authorized to access the job.
// If the user is not authorized, then a pgcode.InsufficientPrivilege
// error will be returned.
func Authorize(
ctx context.Context,
a AuthorizationAccessor,
jobID jobspb.JobID,
payload *jobspb.Payload,
accessLevel AccessLevel,
) error {
userIsAdmin, err := a.HasAdminRole(ctx)
if err != nil {
return err
}
if userIsAdmin {
return nil
}
userHasControlJob, err := a.HasRoleOption(ctx, roleoption.CONTROLJOB)
if err != nil {
return err
}
jobOwnerUser := payload.UsernameProto.Decode()
jobOwnerIsAdmin, err := a.UserHasAdminRole(ctx, jobOwnerUser)
if err != nil {
return err
}
if jobOwnerIsAdmin {
return pgerror.Newf(pgcode.InsufficientPrivilege,
"only admins can control jobs owned by other admins")
}
if (userHasControlJob) || (accessLevel == ViewAccess && a.User() == jobOwnerUser) {
return nil
}
typ := payload.Type()
if check, ok := authorizers[typ]; ok {
return check(ctx, a, jobID, payload)
}
return pgerror.Newf(pgcode.InsufficientPrivilege,
"user %s does not have %s privilege for job $d",
a.User(), roleoption.CONTROLJOB, jobID)
}