Skip to content

Commit

Permalink
sql: dropping user with default privilege's error message is more clear
Browse files Browse the repository at this point in the history
Release note (sql change): When dropping a user that has default privileges,
the error message now includes which database and schema the default privileges
are defined in.
  • Loading branch information
RichardJCai committed Feb 24, 2022
1 parent 920464f commit dbade07
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 57 deletions.
48 changes: 39 additions & 9 deletions pkg/sql/drop_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (n *DropRoleNode) startExec(params runParams) error {
break
}
}
return accumulateDependentDefaultPrivileges(db.GetDefaultPrivilegeDescriptor(), userNames)
return accumulateDependentDefaultPrivileges(db.GetDefaultPrivilegeDescriptor(), userNames, db.GetName(), "" /* schemaName */)
}); err != nil {
return err
}
Expand Down Expand Up @@ -208,7 +208,12 @@ func (n *DropRoleNode) startExec(params runParams) error {
})
}

if err := accumulateDependentDefaultPrivileges(schemaDesc.GetDefaultPrivilegeDescriptor(), userNames); err != nil {
dbDesc, err := lCtx.getDatabaseByID(schemaDesc.GetParentID())
if err != nil {
return err
}

if err := accumulateDependentDefaultPrivileges(schemaDesc.GetDefaultPrivilegeDescriptor(), userNames, dbDesc.GetName(), schemaDesc.GetName()); err != nil {
return err
}
}
Expand Down Expand Up @@ -251,6 +256,19 @@ func (n *DropRoleNode) startExec(params runParams) error {
for _, name := range n.roleNames {
// Did the user own any objects?
dependentObjects := userNames[name]

// Sort the slice so we're guaranteed the same ordering on errors.
sort.SliceStable(dependentObjects, func(i int, j int) bool {
if dependentObjects[i].ObjectType != dependentObjects[j].ObjectType {
return dependentObjects[i].ObjectType < dependentObjects[j].ObjectType
}

if dependentObjects[i].ObjectName != dependentObjects[j].ObjectName {
return dependentObjects[i].ObjectName < dependentObjects[j].ObjectName
}

return dependentObjects[i].ErrorMessage.Error() < dependentObjects[j].ErrorMessage.Error()
})
if len(dependentObjects) > 0 {
objectsMsg := tree.NewFmtCtx(tree.FmtSimple)
for _, obj := range dependentObjects {
Expand Down Expand Up @@ -422,6 +440,8 @@ func (*DropRoleNode) Close(context.Context) {}
func accumulateDependentDefaultPrivileges(
defaultPrivilegeDescriptor catalog.DefaultPrivilegeDescriptor,
userNames map[security.SQLUsername][]objectAndType,
dbName string,
schemaName string,
) error {
// The func we pass into ForEachDefaultPrivilegeForRole will never
// err so no err will be returned.
Expand All @@ -434,7 +454,7 @@ func accumulateDependentDefaultPrivileges(
role.ForAllRoles = true
}
for object, defaultPrivs := range defaultPrivilegesForRole.DefaultPrivilegesPerObject {
addDependentPrivileges(object, defaultPrivs, role, userNames)
addDependentPrivileges(object, defaultPrivs, role, userNames, dbName, schemaName)
}
return nil
})
Expand All @@ -445,6 +465,8 @@ func addDependentPrivileges(
defaultPrivs catpb.PrivilegeDescriptor,
role catpb.DefaultPrivilegesRole,
userNames map[security.SQLUsername][]objectAndType,
dbName string,
schemaName string,
) {
var objectType string
switch object {
Expand All @@ -458,15 +480,23 @@ func addDependentPrivileges(
objectType = "schemas"
}

wrapErrorMsgWithSchemaName := func(errorMsg string) string {
if schemaName != "" {
return errorMsg + fmt.Sprintf(" in schema %s", schemaName)
}

return errorMsg
}

for _, privs := range defaultPrivs.Users {
if !role.ForAllRoles {
if _, ok := userNames[role.Role]; ok {
userNames[role.Role] = append(userNames[role.Role],
objectAndType{
ObjectType: defaultPrivilege,
ErrorMessage: errors.Newf(
"owner of default privileges on new %s belonging to role %s",
objectType, role.Role,
wrapErrorMsgWithSchemaName("owner of default privileges on new %s belonging to role %s in database %s"),
objectType, role.Role, dbName,
),
})
}
Expand All @@ -476,13 +506,13 @@ func addDependentPrivileges(
var err error
if role.ForAllRoles {
err = errors.Newf(
"privileges for default privileges on new %s for all roles",
objectType,
wrapErrorMsgWithSchemaName("privileges for default privileges on new %s for all roles in database %s"),
objectType, dbName,
)
} else {
err = errors.Newf(
"privileges for default privileges on new %s belonging to role %s",
objectType, role.Role,
wrapErrorMsgWithSchemaName("privileges for default privileges on new %s belonging to role %s in database %s"),
objectType, role.Role, dbName,
)
}
userNames[grantee] = append(userNames[grantee],
Expand Down
161 changes: 113 additions & 48 deletions pkg/sql/logictest/testdata/logic_test/drop_role_with_default_privileges
Original file line number Diff line number Diff line change
@@ -1,85 +1,150 @@
statement ok
CREATE USER test1;
CREATE USER test2;
GRANT test1 TO ROOT;
CREATE USER testuser1;
CREATE USER testuser2;
GRANT testuser1 TO ROOT;

statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE test1 GRANT SELECT ON TABLES TO test2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 GRANT SELECT ON TABLES TO testuser2;

statement error pq: role test1 cannot be dropped because some objects depend on it\nowner of default privileges on new relations belonging to role test1
DROP ROLE test1
statement error pq: role testuser1 cannot be dropped because some objects depend on it\nowner of default privileges on new relations belonging to role testuser1 in database test
DROP ROLE testuser1

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new relations belonging to role test1
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new relations belonging to role testuser1 in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE SELECT ON TABLES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 GRANT USAGE ON SCHEMAS TO test2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE SELECT ON TABLES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 GRANT USAGE ON SCHEMAS TO testuser2;

statement error pq: role test1 cannot be dropped because some objects depend on it\nowner of default privileges on new schemas belonging to role test1
DROP ROLE test1
statement error pq: role testuser1 cannot be dropped because some objects depend on it\nowner of default privileges on new schemas belonging to role testuser1 in database test
DROP ROLE testuser1

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new schemas belonging to role test1
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new schemas belonging to role testuser1 in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE USAGE ON SCHEMAS FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 GRANT USAGE ON TYPES TO test2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE USAGE ON SCHEMAS FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 GRANT USAGE ON TYPES TO testuser2;

statement error pq: role test1 cannot be dropped because some objects depend on it\nowner of default privileges on new types belonging to role test1
DROP ROLE test1
statement error pq: role testuser1 cannot be dropped because some objects depend on it\nowner of default privileges on new types belonging to role testuser1 in database test
DROP ROLE testuser1

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new types belonging to role test1
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new types belonging to role testuser1 in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE USAGE ON TYPES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 GRANT SELECT ON SEQUENCES TO test2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE USAGE ON TYPES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 GRANT SELECT ON SEQUENCES TO testuser2;

statement error pq: role test1 cannot be dropped because some objects depend on it\nowner of default privileges on new sequences belonging to role test1
DROP ROLE test1
statement error pq: role testuser1 cannot be dropped because some objects depend on it\nowner of default privileges on new sequences belonging to role testuser1 in database test
DROP ROLE testuser1

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new sequences belonging to role test1
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new sequences belonging to role testuser1 in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE SELECT ON TABLES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE USAGE ON SCHEMAS FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE USAGE ON TYPES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE test1 REVOKE SELECT ON SEQUENCES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE SELECT ON TABLES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE USAGE ON SCHEMAS FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE USAGE ON TYPES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser1 REVOKE SELECT ON SEQUENCES FROM testuser2;

statement ok
DROP ROLE test1;
DROP ROLE testuser1;

statement ok
DROP ROLE test2;
DROP ROLE testuser2;

statement ok
CREATE USER test2
CREATE USER testuser2

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON TABLES TO test2
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON TABLES TO testuser2

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new relations for all roles
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new relations for all roles in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE SELECT ON TABLES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT USAGE ON SCHEMAS TO test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE SELECT ON TABLES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT USAGE ON SCHEMAS TO testuser2;

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new schemas for all roles
DROP ROLE test2;
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new schemas for all roles in database test
DROP ROLE testuser2;

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE USAGE ON SCHEMAS FROM test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT USAGE ON TYPES TO test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE USAGE ON SCHEMAS FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT USAGE ON TYPES TO testuser2;

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new types for all roles
DROP ROLE test2
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new types for all roles in database test
DROP ROLE testuser2

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE USAGE ON TYPES FROM test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON SEQUENCES TO test2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE USAGE ON TYPES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON SEQUENCES TO testuser2;

statement error pq: role test2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new sequences for all roles
DROP ROLE test2
statement error pq: role testuser2 cannot be dropped because some objects depend on it\nprivileges for default privileges on new sequences for all roles in database test
DROP ROLE testuser2

# Grant default privileges to testuser2 in a second database.
statement ok
CREATE ROLE testuser3;
GRANT testuser2 TO root;
GRANT testuser3 TO root;
CREATE DATABASE testdb2;
USE testdb2;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON SEQUENCES TO testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser3 GRANT SELECT ON SEQUENCES TO testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser2 GRANT SELECT ON SEQUENCES TO testuser3;

statement error pq: role testuser2 cannot be dropped because some objects depend on it\nowner of default privileges on new sequences belonging to role testuser2 in database testdb2\nprivileges for default privileges on new sequences belonging to role testuser3 in database testdb2\nprivileges for default privileges on new sequences for all roles in database test\nprivileges for default privileges on new sequences for all roles in database testdb2
DROP ROLE testuser2

# Tests where default privileges are defined on the schema.
statement ok
CREATE ROLE testuser4;
GRANT testuser4 TO root;

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES IN SCHEMA PUBLIC GRANT SELECT ON SEQUENCES TO testuser4;

statement error pq: role testuser4 cannot be dropped because some objects depend on it\nprivileges for default privileges on new sequences for all roles in database testdb2 in schema public
DROP ROLE testuser4

statement ok
CREATE SCHEMA s;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES IN SCHEMA s GRANT SELECT ON SEQUENCES TO testuser4;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser3 IN SCHEMA s GRANT SELECT ON SEQUENCES TO testuser4;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser4 IN SCHEMA s GRANT SELECT ON SEQUENCES TO testuser3;

statement error pq: role testuser4 cannot be dropped because some objects depend on it\nowner of default privileges on new sequences belonging to role testuser4 in database testdb2 in schema s\nprivileges for default privileges on new sequences belonging to role testuser3 in database testdb2 in schema s\nprivileges for default privileges on new sequences for all roles in database testdb2 in schema public\nprivileges for default privileges on new sequences for all roles in database testdb2 in schema s
DROP ROLE testuser4

# Now let's ensure we can revoke all the privileges and drop testuser2/3/4.

# Prepare to drop testuser2.
statement ok
USE test;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE ALL ON SEQUENCES FROM testuser2;

statement ok
USE testdb2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser3 REVOKE ALL ON SEQUENCES FROM testuser2;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser2 REVOKE ALL ON SEQUENCES FROM testuser3;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES REVOKE ALL ON SEQUENCES FROM testuser2;

statement ok
DROP ROLE testuser2;

# Prepare to drop testuser3.
statement ok
ALTER DEFAULT PRIVILEGES FOR ROLE testuser3 IN SCHEMA s REVOKE ALL ON SEQUENCES FROM testuser4;
ALTER DEFAULT PRIVILEGES FOR ROLE testuser4 IN SCHEMA s REVOKE ALL ON SEQUENCES FROM testuser3;

statement ok
DROP ROLE testuser3;

statement ok
ALTER DEFAULT PRIVILEGES FOR ALL ROLES IN SCHEMA s REVOKE ALL ON SEQUENCES FROM testuser4;
ALTER DEFAULT PRIVILEGES FOR ALL ROLES IN SCHEMA public REVOKE ALL ON SEQUENCES FROM testuser4;

statement ok
DROP ROLE testuser4;

0 comments on commit dbade07

Please sign in to comment.