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: fix excess privileges being created from default privileges. #72323

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
23 changes: 16 additions & 7 deletions pkg/sql/catalog/catprivilege/default_privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,31 +145,40 @@ func (d *immutable) CreatePrivilegesFromDefaultPrivileges(
}

newPrivs := descpb.NewDefaultPrivilegeDescriptor(user)
// If default privileges are not defined for the creator role, we handle
// it as the default case where the user has all privileges.
role := descpb.DefaultPrivilegesRole{Role: user}
if _, found := d.GetDefaultPrivilegesForRole(role); !found {
if defaultPrivilegesForRole, found := d.GetDefaultPrivilegesForRole(role); !found {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the comment above need adjusting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this is still accurate but it might be more fitting to put this into the if !found clause? Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i'm in favor of putting it inside of if !found and then one more comment inside of the else

// If default privileges are not defined for the creator role, we handle
// it as the case where the user has all privileges.
defaultPrivilegesForCreatorRole := descpb.InitDefaultPrivilegesForRole(role)
for _, user := range GetUserPrivilegesForObject(defaultPrivilegesForCreatorRole, targetObject) {
newPrivs.Grant(
user.UserProto.Decode(),
privilege.ListFromBitField(user.Privileges, targetObject.ToPrivilegeObjectType()),
)
}
} else {
// If default privileges were defined for the role, we create privileges
// using the default privileges.
for _, user := range GetUserPrivilegesForObject(*defaultPrivilegesForRole, targetObject) {
newPrivs.Grant(
user.UserProto.Decode(),
privilege.ListFromBitField(user.Privileges, targetObject.ToPrivilegeObjectType()),
)
}
}

// The privileges for the object are the union of the default privileges
// defined for the object for the object creator and the default privileges
// defined for all roles.
_ = d.ForEachDefaultPrivilegeForRole(func(defaultPrivilegesForRole descpb.DefaultPrivilegesForRole) error {
for _, user := range GetUserPrivilegesForObject(defaultPrivilegesForRole, targetObject) {
defaultPrivilegesForAllRoles, found := d.GetDefaultPrivilegesForRole(descpb.DefaultPrivilegesRole{ForAllRoles: true})
if found {
for _, user := range GetUserPrivilegesForObject(*defaultPrivilegesForAllRoles, targetObject) {
newPrivs.Grant(
user.UserProto.Decode(),
privilege.ListFromBitField(user.Privileges, targetObject.ToPrivilegeObjectType()),
)
}
return nil
})
}
newPrivs.SetOwner(user)
newPrivs.Version = descpb.Version21_2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,20 @@ database_name schema_name grantee privilege_type
d s5 admin ALL
d s5 root ALL
d s5 testuser CREATE

statement ok
ALTER DEFAULT PRIVILEGES GRANT ALL ON SCHEMAS TO testuser, testuser2

user root

statement ok
CREATE SCHEMA s_72322

# When root creates the table, testuser and testuser2 should not get privileges.
query TTTT colnames
SHOW GRANTS ON SCHEMA s_72322
----
database_name schema_name grantee privilege_type
d s_72322 admin ALL
d s_72322 root ALL
d s_72322 testuser CREATE