-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathrevoke_role.go
177 lines (151 loc) · 5.31 KB
/
revoke_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
// Copyright 2020 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 sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
)
// RevokeRoleNode removes entries from the system.role_members table.
// This is called from REVOKE <ROLE>
type RevokeRoleNode struct {
roles []security.SQLUsername
members []security.SQLUsername
adminOption bool
run revokeRoleRun
}
type revokeRoleRun struct {
rowsAffected int
}
// RevokeRole represents a GRANT ROLE statement.
func (p *planner) RevokeRole(ctx context.Context, n *tree.RevokeRole) (planNode, error) {
return p.RevokeRoleNode(ctx, n)
}
func (p *planner) RevokeRoleNode(ctx context.Context, n *tree.RevokeRole) (*RevokeRoleNode, error) {
sqltelemetry.IncIAMRevokeCounter(n.AdminOption)
ctx, span := tracing.ChildSpan(ctx, n.StatementTag())
defer span.Finish()
hasAdminRole, err := p.HasAdminRole(ctx)
if err != nil {
return nil, err
}
// check permissions on each role.
allRoles, err := p.MemberOfWithAdminOption(ctx, p.User())
if err != nil {
return nil, err
}
inputRoles := make([]security.SQLUsername, len(n.Roles))
inputMembers := make([]security.SQLUsername, len(n.Members))
for i, role := range n.Roles {
normalizedRole, err := security.MakeSQLUsernameFromUserInput(string(role), security.UsernameValidation)
if err != nil {
return nil, err
}
inputRoles[i] = normalizedRole
}
for i, member := range n.Members {
normalizedMember, err := security.MakeSQLUsernameFromUserInput(string(member), security.UsernameValidation)
if err != nil {
return nil, err
}
inputMembers[i] = normalizedMember
}
for _, r := range inputRoles {
// If the user is an admin, don't check if the user is allowed to add/drop
// roles in the role. However, if the role being modified is the admin role, then
// make sure the user is an admin with the admin option.
if hasAdminRole && !r.IsAdminRole() {
continue
}
if isAdmin, ok := allRoles[r]; !ok || !isAdmin {
if r.IsAdminRole() {
return nil, pgerror.Newf(pgcode.InsufficientPrivilege,
"%s is not a role admin for role %s", p.User(), r)
}
return nil, pgerror.Newf(pgcode.InsufficientPrivilege,
"%s is not a superuser or role admin for role %s", p.User(), r)
}
}
// Check that roles exist.
// TODO(mberhault): just like GRANT/REVOKE privileges, we fetch the list of all roles.
// This is wasteful when we have a LOT of roles compared to the number of roles being operated on.
roles, err := p.GetAllRoles(ctx)
if err != nil {
return nil, err
}
for _, r := range inputRoles {
if _, ok := roles[r]; !ok {
return nil, pgerror.Newf(pgcode.UndefinedObject, "role/user %s does not exist", r)
}
}
for _, m := range inputMembers {
if _, ok := roles[m]; !ok {
return nil, pgerror.Newf(pgcode.UndefinedObject, "role/user %s does not exist", m)
}
}
return &RevokeRoleNode{
roles: inputRoles,
members: inputMembers,
adminOption: n.AdminOption,
}, nil
}
func (n *RevokeRoleNode) startExec(params runParams) error {
opName := "revoke-role"
var memberStmt string
if n.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 n.roles {
for _, m := range n.members {
if r.IsAdminRole() && m.IsRootUser() {
// We use CodeObjectInUseError which is what happens if you tried to delete the current user in pg.
return pgerror.Newf(pgcode.ObjectInUse,
"role/user %s cannot be removed from role %s or lose the ADMIN OPTION",
security.RootUser, security.AdminRole)
}
affected, err := params.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx(
params.ctx,
opName,
params.p.txn,
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
memberStmt,
r.Normalized(), m.Normalized(),
)
if err != nil {
return err
}
rowsAffected += affected
}
}
// We need to bump the table version to trigger a refresh if anything changed.
if rowsAffected > 0 {
if err := params.p.BumpRoleMembershipTableVersion(params.ctx); err != nil {
return err
}
}
n.run.rowsAffected += rowsAffected
return nil
}
// Next implements the planNode interface.
func (*RevokeRoleNode) Next(runParams) (bool, error) { return false, nil }
// Values implements the planNode interface.
func (*RevokeRoleNode) Values() tree.Datums { return tree.Datums{} }
// Close implements the planNode interface.
func (*RevokeRoleNode) Close(context.Context) {}