-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgrant_revoke.go
432 lines (393 loc) · 14.5 KB
/
grant_revoke.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2015 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"
"fmt"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/errors"
)
// Grant adds privileges to users.
// TODO(marc): open questions:
// - should we have root always allowed and not present in the permissions list?
// Privileges: GRANT on database/table/view.
// Notes: postgres requires the object owner.
// mysql requires the "grant option" and the same privileges, and sometimes superuser.
func (p *planner) Grant(ctx context.Context, n *tree.Grant) (planNode, error) {
grantOn := getGrantOnObject(n.Targets, sqltelemetry.IncIAMGrantPrivilegesCounter)
if err := privilege.ValidatePrivileges(n.Privileges, grantOn); err != nil {
return nil, err
}
grantees, err := n.Grantees.ToSQLUsernames(p.SessionData(), security.UsernameValidation)
if err != nil {
return nil, err
}
return &changePrivilegesNode{
isGrant: true,
targets: n.Targets,
grantees: grantees,
desiredprivs: n.Privileges,
changePrivilege: func(privDesc *descpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) {
privDesc.Grant(grantee, privileges, n.WithGrantOption)
},
grantOn: grantOn,
granteesNameList: n.Grantees,
}, nil
}
// Revoke removes privileges from users.
// TODO(marc): open questions:
// - should we have root always allowed and not present in the permissions list?
// Privileges: GRANT on database/table/view.
// Notes: postgres requires the object owner.
// mysql requires the "grant option" and the same privileges, and sometimes superuser.
func (p *planner) Revoke(ctx context.Context, n *tree.Revoke) (planNode, error) {
grantOn := getGrantOnObject(n.Targets, sqltelemetry.IncIAMRevokePrivilegesCounter)
if err := privilege.ValidatePrivileges(n.Privileges, grantOn); err != nil {
return nil, err
}
grantees, err := n.Grantees.ToSQLUsernames(p.SessionData(), security.UsernameValidation)
if err != nil {
return nil, err
}
return &changePrivilegesNode{
isGrant: false,
targets: n.Targets,
grantees: grantees,
desiredprivs: n.Privileges,
changePrivilege: func(privDesc *descpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) {
privDesc.Revoke(grantee, privileges, grantOn, n.GrantOptionFor)
},
grantOn: grantOn,
granteesNameList: n.Grantees,
}, nil
}
type changePrivilegesNode struct {
isGrant bool
targets tree.TargetList
grantees []security.SQLUsername
desiredprivs privilege.List
changePrivilege func(*descpb.PrivilegeDescriptor, privilege.List, security.SQLUsername)
grantOn privilege.ObjectType
// granteesNameList is used for creating an AST node for alter default
// privileges inside changePrivilegesNode's startExec.
// This is required for getting the pre-normalized name to construct the AST.
granteesNameList tree.RoleSpecList
}
// ReadingOwnWrites implements the planNodeReadingOwnWrites interface.
// This is because GRANT/REVOKE performs multiple KV operations on descriptors
// and expects to see its own writes.
func (n *changePrivilegesNode) ReadingOwnWrites() {}
func (n *changePrivilegesNode) startExec(params runParams) error {
ctx := params.ctx
p := params.p
if err := p.validateRoles(ctx, n.grantees, true /* isPublicValid */); err != nil {
return err
}
var err error
var descriptors []catalog.Descriptor
// DDL statements avoid the cache to avoid leases, and can view non-public descriptors.
// TODO(vivek): check if the cache can be used.
p.runWithOptions(resolveFlags{skipCache: true}, func() {
descriptors, err = getDescriptorsFromTargetListForPrivilegeChange(ctx, p, n.targets)
})
if err != nil {
return err
}
if len(descriptors) == 0 {
return nil
}
if p.ExecCfg().Settings.Version.IsActive(ctx, clusterversion.DefaultPrivileges) {
if n.grantOn == privilege.Database {
compatiblePrivileges, err := convertPGIncompatibleDatabasePrivilegesToDefaultPrivileges(ctx, p, n, params)
if err != nil {
return err
}
// When granting, we exclude the incompatible privileges.
// Note: we can't do this when revoking in case the privilege was granted
// before 21.2 where this conversion does not happen.
if n.isGrant {
n.desiredprivs = compatiblePrivileges
}
}
}
var events []eventLogEntry
// First, update the descriptors. We want to catch all errors before
// we update them in KV below.
b := p.txn.NewBatch()
for _, descriptor := range descriptors {
// Disallow privilege changes on system objects. For more context, see #43842.
op := "REVOKE"
if n.isGrant {
op = "GRANT"
}
if descriptor.GetID() < keys.MinUserDescID {
return pgerror.Newf(pgcode.InsufficientPrivilege, "cannot %s on system object", op)
}
if err := p.CheckPrivilege(ctx, descriptor, privilege.GRANT); err != nil {
return err
}
if len(n.desiredprivs) > 0 {
// Only allow granting/revoking privileges that the requesting
// user themselves have on the descriptor.
for _, priv := range n.desiredprivs {
if err := p.CheckPrivilege(ctx, descriptor, priv); err != nil {
return err
}
}
privileges := descriptor.GetPrivileges()
if p.ExecCfg().Settings.Version.IsActive(ctx, clusterversion.ValidateGrantOption) {
err := privileges.ValidateGrantPrivileges(p.User(), n.desiredprivs)
if err != nil {
return err
}
}
for _, grantee := range n.grantees {
n.changePrivilege(privileges, n.desiredprivs, grantee)
}
// Ensure superusers have exactly the allowed privilege set.
// Postgres does not actually enforce this, instead of checking that
// superusers have all the privileges, Postgres allows superusers to
// bypass privilege checks.
err = catprivilege.ValidateSuperuserPrivileges(*privileges, descriptor, n.grantOn)
if err != nil {
return err
}
// Validate privilege descriptors directly as the db/table level Validate
// may fix up the descriptor.
err = catprivilege.Validate(*privileges, descriptor, n.grantOn)
if err != nil {
return err
}
}
eventDetails := eventpb.CommonSQLPrivilegeEventDetails{}
if n.isGrant {
eventDetails.GrantedPrivileges = n.desiredprivs.SortedNames()
} else {
eventDetails.RevokedPrivileges = n.desiredprivs.SortedNames()
}
switch d := descriptor.(type) {
case *dbdesc.Mutable:
if err := p.writeDatabaseChangeToBatch(ctx, d, b); err != nil {
return err
}
if err := p.createNonDropDatabaseChangeJob(ctx, d.ID,
fmt.Sprintf("updating privileges for database %d", d.ID)); err != nil {
return err
}
for _, grantee := range n.grantees {
privs := eventDetails // copy the granted/revoked privilege list.
privs.Grantee = grantee.Normalized()
events = append(events, eventLogEntry{
targetID: int32(d.ID),
event: &eventpb.ChangeDatabasePrivilege{
CommonSQLPrivilegeEventDetails: privs,
DatabaseName: (*tree.Name)(&d.Name).String(),
}})
}
case *tabledesc.Mutable:
// TODO (lucy): This should probably have a single consolidated job like
// DROP DATABASE.
if err := p.createOrUpdateSchemaChangeJob(
ctx, d,
fmt.Sprintf("updating privileges for table %d", d.ID),
descpb.InvalidMutationID,
); err != nil {
return err
}
if !d.Dropped() {
if err := p.writeSchemaChangeToBatch(ctx, d, b); err != nil {
return err
}
}
for _, grantee := range n.grantees {
privs := eventDetails // copy the granted/revoked privilege list.
privs.Grantee = grantee.Normalized()
events = append(events, eventLogEntry{
targetID: int32(d.ID),
event: &eventpb.ChangeTablePrivilege{
CommonSQLPrivilegeEventDetails: privs,
TableName: d.Name, // FIXME
}})
}
case *typedesc.Mutable:
err := p.writeTypeSchemaChange(ctx, d, fmt.Sprintf("updating privileges for type %d", d.ID))
if err != nil {
return err
}
for _, grantee := range n.grantees {
privs := eventDetails // copy the granted/revoked privilege list.
privs.Grantee = grantee.Normalized()
events = append(events, eventLogEntry{
targetID: int32(d.ID),
event: &eventpb.ChangeTypePrivilege{
CommonSQLPrivilegeEventDetails: privs,
TypeName: d.Name, // FIXME
}})
}
case *schemadesc.Mutable:
if err := p.writeSchemaDescChange(
ctx,
d,
fmt.Sprintf("updating privileges for schema %d", d.ID),
); err != nil {
return err
}
for _, grantee := range n.grantees {
privs := eventDetails // copy the granted/revoked privilege list.
privs.Grantee = grantee.Normalized()
events = append(events, eventLogEntry{
targetID: int32(d.ID),
event: &eventpb.ChangeSchemaPrivilege{
CommonSQLPrivilegeEventDetails: privs,
SchemaName: d.Name, // FIXME
}})
}
}
}
// Now update the descriptors transactionally.
if err := p.txn.Run(ctx, b); err != nil {
return err
}
// Record the privilege changes in the event log. This is an
// auditable log event and is recorded in the same transaction as
// the table descriptor update.
if err := params.p.logEvents(params.ctx, events...); err != nil {
return err
}
return nil
}
func (*changePrivilegesNode) Next(runParams) (bool, error) { return false, nil }
func (*changePrivilegesNode) Values() tree.Datums { return tree.Datums{} }
func (*changePrivilegesNode) Close(context.Context) {}
// getGrantOnObject returns the type of object being granted on based on the TargetList.
// getGrantOnObject also calls incIAMFunc with the object type name.
func getGrantOnObject(targets tree.TargetList, incIAMFunc func(on string)) privilege.ObjectType {
switch {
case targets.Databases != nil:
incIAMFunc(sqltelemetry.OnDatabase)
return privilege.Database
case targets.AllTablesInSchema:
incIAMFunc(sqltelemetry.OnAllTablesInSchema)
return privilege.Table
case targets.Schemas != nil:
incIAMFunc(sqltelemetry.OnSchema)
return privilege.Schema
case targets.Types != nil:
incIAMFunc(sqltelemetry.OnType)
return privilege.Type
default:
incIAMFunc(sqltelemetry.OnTable)
return privilege.Table
}
}
// validateRoles checks that all the roles are valid users.
// isPublicValid determines whether or not Public is a valid role.
func (p *planner) validateRoles(
ctx context.Context, roles []security.SQLUsername, isPublicValid bool,
) error {
users, err := p.GetAllRoles(ctx)
if err != nil {
return err
}
if isPublicValid {
users[security.PublicRoleName()] = true // isRole
}
for i, grantee := range roles {
if _, ok := users[grantee]; !ok {
sqlName := tree.Name(roles[i].Normalized())
return errors.Errorf("user or role %s does not exist", &sqlName)
}
}
return nil
}
// convertPGIncompatibleDatabasePrivilegesToDefaultPrivileges takes the
// incompatible database privileges in the grant statement and creates
// a alter default privileges AST and plan node and executes the plan node.
func convertPGIncompatibleDatabasePrivilegesToDefaultPrivileges(
ctx context.Context, p *planner, n *changePrivilegesNode, params runParams,
) (compatiblePrivs privilege.List, err error) {
databaseTargets := n.targets.Databases
// Convert PGIncompatibleDatabase privileges to default privileges instead.
var incompatiblePrivs privilege.List
for _, priv := range n.desiredprivs {
if privilege.PGIncompatibleDBPrivileges.Contains(priv) {
incompatiblePrivs = append(incompatiblePrivs, priv)
} else {
compatiblePrivs = append(compatiblePrivs, priv)
}
}
// The incompatible privileges are added as default privileges for all roles
// on the databases.
for _, database := range databaseTargets {
// If n.targets.Database has elements, all descriptors must be database
// descriptors.
// The objectType is always Tables since the incompatible pg privileges
// are (SELECT, INSERT, UPDATE, DELETE) which were previously allowed
// for the sake of inheriting privileges from the DB to the table.
objectType := tree.Tables
var translatedStatement string
if len(incompatiblePrivs) > 0 {
alterDefaultPrivilegesASTNode := tree.AlterDefaultPrivileges{
ForAllRoles: true,
IsGrant: n.isGrant,
Database: &database,
}
if n.isGrant {
alterDefaultPrivilegesASTNode.Grant = tree.AbbreviatedGrant{
Grantees: n.granteesNameList,
Privileges: incompatiblePrivs,
Target: objectType,
}
} else if !n.isGrant {
// If revoke is specified, we need to revoke both existing privileges
// on the database and also default privileges.
alterDefaultPrivilegesASTNode.Revoke = tree.AbbreviatedRevoke{
Grantees: n.granteesNameList,
Privileges: incompatiblePrivs,
Target: objectType,
}
}
alterDefaultPrivilegesNode, err := p.alterDefaultPrivileges(
ctx, &alterDefaultPrivilegesASTNode,
)
if err != nil {
return nil, err
}
translatedStatement = fmt.Sprintf("USE %s; %s;", database.Normalize(),
alterDefaultPrivilegesASTNode.String())
if err := alterDefaultPrivilegesNode.startExec(params); err != nil {
return nil, err
}
p.BufferClientNotice(ctx, pgnotice.Newf(
"GRANT %s ON DATABASE is deprecated.\n"+
"This statement was automatically converted to %s\n"+
"Please use ALTER DEFAULT PRIVILEGES going forward",
incompatiblePrivs, translatedStatement,
))
}
}
return compatiblePrivs, nil
}