-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
role.go
285 lines (250 loc) · 8.88 KB
/
role.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2017 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package roleccl
import (
"context"
"github.com/cockroachdb/cockroach/pkg/ccl/utilccl"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)
func createRolePlanHook(
ctx context.Context, stmt tree.Statement, p sql.PlanHookState,
) (sql.PlanNode, error) {
createRole, ok := stmt.(*tree.CreateRole)
if !ok {
return nil, nil
}
cfg := p.ExecCfg()
if err := utilccl.CheckEnterpriseEnabled(
cfg.Settings, cfg.ClusterID(), cfg.Organization(), "CREATE ROLE",
); err != nil {
return nil, err
}
// Call directly into the OSS code.
return p.CreateUserNode(ctx, createRole.Name, nil /* password */, createRole.IfNotExists, true /* isRole */, "CREATE ROLE")
}
func dropRolePlanHook(
ctx context.Context, stmt tree.Statement, p sql.PlanHookState,
) (sql.PlanNode, error) {
dropRole, ok := stmt.(*tree.DropRole)
if !ok {
return nil, nil
}
cfg := p.ExecCfg()
if err := utilccl.CheckEnterpriseEnabled(
cfg.Settings, cfg.ClusterID(), cfg.Organization(), "DROP ROLE",
); err != nil {
return nil, err
}
// Call directly into the OSS code.
return p.DropUserNode(ctx, dropRole.Names, dropRole.IfExists, true /* isRole */, "DROP ROLE")
}
func grantRolePlanHook(
ctx context.Context, stmt tree.Statement, p sql.PlanHookState,
) (sql.PlanNode, error) {
grant, ok := stmt.(*tree.GrantRole)
if !ok {
return nil, nil
}
if err := utilccl.CheckEnterpriseEnabled(
p.ExecCfg().Settings, p.ExecCfg().ClusterID(), p.ExecCfg().Organization(), "GRANT <role>",
); err != nil {
return nil, err
}
if _, err := p.RequireSuperUser(ctx, "grant role"); err != nil {
// Not a superuser: check permissions on each role.
allRoles, err := p.MemberOfWithAdminOption(ctx, p.User())
if err != nil {
return nil, err
}
for _, r := range grant.Roles {
if isAdmin, ok := allRoles[string(r)]; !ok || !isAdmin {
return nil, pgerror.Newf(pgerror.CodeInsufficientPrivilegeError,
"%s is not a superuser or role admin for role %s", p.User(), r)
}
}
}
// Check that users and roles exist.
// TODO(mberhault): just like GRANT/REVOKE privileges, we fetch the list of all users.
// This is wasteful when we have a LOT of users compared to the number of users being operated on.
users, err := p.GetAllUsersAndRoles(ctx)
if err != nil {
return nil, err
}
// NOTE: membership manipulation involving the "public" pseudo-role fails with
// "role public does not exist". This matches postgres behavior.
// Check roles: these have to be roles.
for _, r := range grant.Roles {
if isRole, ok := users[string(r)]; !ok || !isRole {
return nil, pgerror.Newf(pgerror.CodeUndefinedObjectError, "role %s does not exist", r)
}
}
// Check grantees: these can be users or roles.
for _, m := range grant.Members {
if _, ok := users[string(m)]; !ok {
return nil, pgerror.Newf(pgerror.CodeUndefinedObjectError, "user or role %s does not exist", m)
}
}
// Given an acyclic directed membership graph, adding a new edge (grant.Member ∈ grant.Role)
// means checking whether we have an expanded relationship (grant.Role ∈ ... ∈ grant.Member)
// For each grant.Role, we lookup all the roles it is a member of.
// After adding a given edge (grant.Member ∈ grant.Role), we add the edge to the list as well.
allRoleMemberships := make(map[string]map[string]bool)
for _, rawR := range grant.Roles {
r := string(rawR)
allRoles, err := p.MemberOfWithAdminOption(ctx, r)
if err != nil {
return nil, err
}
allRoleMemberships[r] = allRoles
}
// Since we perform no queries here, check all role/member pairs for cycles.
// Only if there are no errors do we proceed to write them.
for _, rawR := range grant.Roles {
r := string(rawR)
for _, rawM := range grant.Members {
m := string(rawM)
if r == m {
// self-cycle.
return nil, pgerror.Newf(pgerror.CodeInvalidGrantOperationError, "%s cannot be a member of itself", m)
}
// Check if grant.Role ∈ ... ∈ grant.Member
if memberOf, ok := allRoleMemberships[r]; ok {
if _, ok = memberOf[m]; ok {
return nil, pgerror.Newf(pgerror.CodeInvalidGrantOperationError,
"making %s a member of %s would create a cycle", m, r)
}
}
// Add the new membership. We don't care about the actual bool value.
if _, ok := allRoleMemberships[m]; !ok {
allRoleMemberships[m] = make(map[string]bool)
}
allRoleMemberships[m][r] = false
}
}
// Add memberships. Existing memberships are allowed.
// If admin option is false, we do not remove it from existing memberships.
memberStmt := `INSERT INTO system.role_members ("role", "member", "isAdmin") VALUES ($1, $2, $3) ON CONFLICT ("role", "member")`
if grant.AdminOption {
// admin option: true, set "isAdmin" even if the membership exists.
memberStmt += ` DO UPDATE SET "isAdmin" = true`
} else {
// admin option: false, do not clear it from existing memberships.
memberStmt += ` DO NOTHING`
}
var rowsAffected int
for _, r := range grant.Roles {
for _, m := range grant.Members {
affected, err := p.ExecCfg().InternalExecutor.Exec(
ctx, "grant-role", p.Txn(),
memberStmt,
r, m, grant.AdminOption,
)
if err != nil {
return nil, err
}
rowsAffected += affected
}
}
// We need to bump the table version to trigger a refresh if anything changed.
if rowsAffected > 0 {
if err := p.BumpRoleMembershipTableVersion(ctx); err != nil {
return nil, err
}
}
return sql.NewZeroNode(nil /* columns */), nil
}
func revokeRolePlanHook(
ctx context.Context, stmt tree.Statement, p sql.PlanHookState,
) (sql.PlanNode, error) {
revoke, ok := stmt.(*tree.RevokeRole)
if !ok {
return nil, nil
}
if err := utilccl.CheckEnterpriseEnabled(
p.ExecCfg().Settings, p.ExecCfg().ClusterID(), p.ExecCfg().Organization(), "REVOKE <role>",
); err != nil {
return nil, err
}
if _, err := p.RequireSuperUser(ctx, "revoke role"); err != nil {
// Not a superuser: check permissions on each role.
allRoles, err := p.MemberOfWithAdminOption(ctx, p.User())
if err != nil {
return nil, err
}
for _, r := range revoke.Roles {
if isAdmin, ok := allRoles[string(r)]; !ok || !isAdmin {
return nil, pgerror.Newf(pgerror.CodeInsufficientPrivilegeError,
"%s is not a superuser or role admin for role %s", p.User(), r)
}
}
}
// Check that users and roles exist.
// TODO(mberhault): just like GRANT/REVOKE privileges, we fetch the list of all users.
// This is wasteful when we have a LOT of users compared to the number of users being operated on.
users, err := p.GetAllUsersAndRoles(ctx)
if err != nil {
return nil, err
}
// Check roles: these have to be roles.
for _, r := range revoke.Roles {
if isRole, ok := users[string(r)]; !ok || !isRole {
return nil, pgerror.Newf(pgerror.CodeUndefinedObjectError, "role %s does not exist", r)
}
}
// Check members: these can be users or roles.
for _, m := range revoke.Members {
if _, ok := users[string(m)]; !ok {
return nil, pgerror.Newf(pgerror.CodeUndefinedObjectError, "user or role %s does not exist", m)
}
}
var memberStmt string
if revoke.AdminOption {
// ADMIN OPTION FOR is specified, we don't remove memberships just remove the admin option.
memberStmt = `UPDATE system.role_members SET "isAdmin" = false WHERE "role" = $1 AND "member" = $2`
} else {
// Admin option not specified: remove membership if it exists.
memberStmt = `DELETE FROM system.role_members WHERE "role" = $1 AND "member" = $2`
}
var rowsAffected int
for _, r := range revoke.Roles {
for _, m := range revoke.Members {
if string(r) == sqlbase.AdminRole && string(m) == security.RootUser {
// We use CodeObjectInUseError which is what happens if you tried to delete the current user in pg.
return nil, pgerror.Newf(pgerror.CodeObjectInUseError,
"user %s cannot be removed from role %s or lose the ADMIN OPTION",
security.RootUser, sqlbase.AdminRole)
}
affected, err := p.ExecCfg().InternalExecutor.Exec(
ctx, "revoke-role", p.Txn(),
memberStmt,
r, m,
)
if err != nil {
return nil, err
}
rowsAffected += affected
}
}
// We need to bump the table version to trigger a refresh if anything changed.
if rowsAffected > 0 {
if err := p.BumpRoleMembershipTableVersion(ctx); err != nil {
return nil, err
}
}
return sql.NewZeroNode(nil /* columns */), nil
}
func init() {
sql.AddWrappedPlanHook(createRolePlanHook)
sql.AddWrappedPlanHook(dropRolePlanHook)
sql.AddWrappedPlanHook(grantRolePlanHook)
sql.AddWrappedPlanHook(revokeRolePlanHook)
}