Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: refactor pg_builtin to use actual grant options #74172

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ccl/importccl/import_table_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (so *importSequenceOperators) HasPrivilege(
ctx context.Context,
specifier tree.HasPrivilegeSpecifier,
user security.SQLUsername,
kind privilege.Kind,
priv privilege.Privilege,
) (bool, error) {
return false, errors.WithStack(errSequenceOperators)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/faketreeeval/evalctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (so *DummySequenceOperators) HasPrivilege(
ctx context.Context,
specifier tree.HasPrivilegeSpecifier,
user security.SQLUsername,
kind privilege.Kind,
priv privilege.Privilege,
) (bool, error) {
return false, errors.WithStack(errEvalPlanner)
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (ep *DummyEvalPlanner) HasPrivilege(
ctx context.Context,
specifier tree.HasPrivilegeSpecifier,
user security.SQLUsername,
kind privilege.Kind,
priv privilege.Privilege,
) (bool, error) {
return false, errors.WithStack(errEvalPlanner)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/privilege/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const (
RULE Kind = 12
)

// Privilege represents a privilege parsed from an Access Privilege Inquiry
// Function's privilege string argument.
type Privilege struct {
Kind Kind
// Each privilege Kind has an optional "grant option" flag associated with
// it. A role can only grant a privilege on an object to others if it is the
// owner of the object or if it itself holds that privilege WITH GRANT OPTION
// on the object. This replaces the CockroachDB-specific GRANT privilege.
GrantOption bool
}

// ObjectType represents objects that can have privileges.
type ObjectType string

Expand Down
22 changes: 16 additions & 6 deletions pkg/sql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (p *planner) HasPrivilege(
ctx context.Context,
specifier tree.HasPrivilegeSpecifier,
user security.SQLUsername,
kind privilege.Kind,
priv privilege.Privilege,
) (bool, error) {
desc, err := p.ResolveDescriptorForPrivilegeSpecifier(
ctx,
Expand All @@ -315,32 +315,42 @@ func (p *planner) HasPrivilege(
}

// hasPrivilegeFunc checks whether any role has the given privilege.
hasPrivilegeFunc := func(priv privilege.Kind) (bool, error) {
err := p.CheckPrivilegeForUser(ctx, desc, priv, user)
hasPrivilegeFunc := func(priv privilege.Privilege) (bool, error) {
err := p.CheckPrivilegeForUser(ctx, desc, priv.Kind, user)
if err == nil {
if priv.GrantOption {
if !p.ExecCfg().Settings.Version.IsActive(ctx, clusterversion.ValidateGrantOption) {
err = p.CheckPrivilegeForUser(ctx, desc, privilege.GRANT, user)
} else {
err = p.CheckGrantOptionsForUser(ctx, desc, []privilege.Kind{priv.Kind}, true /* isGrant */)
}
}
}
if err != nil {
if pgerror.GetPGCode(err) == pgcode.InsufficientPrivilege {
return false, nil
}
return false, err
}

return true, nil
}

if kind == privilege.RULE {
if priv.Kind == privilege.RULE {
// RULE was only added for compatibility with Postgres, and Postgres
// never allows RULE to be granted, even if the user has ALL privileges.
// See https://www.postgresql.org/docs/8.1/sql-grant.html
// and https://www.postgresql.org/docs/release/8.2.0/.
return false, nil
}
hasPrivilege, err := hasPrivilegeFunc(privilege.ALL)
hasPrivilege, err := hasPrivilegeFunc(privilege.Privilege{Kind: privilege.ALL})
if err != nil {
return false, err
}
if hasPrivilege {
return true, nil
}
return hasPrivilegeFunc(kind)
return hasPrivilegeFunc(priv)
}

// ResolveDescriptorForPrivilegeSpecifier resolves a tree.HasPrivilegeSpecifier
Expand Down
Loading