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

executor: fix drop role failed after revoke role from current user #29814

Merged
merged 4 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ func (e *SimpleExec) executeRevokeRole(ctx context.Context, s *ast.RevokeRoleStm
return errors.Trace(err)
}
sql := new(strings.Builder)
// when an active role of current user is revoked,
// it should be removed from activeRoles
unconsolable marked this conversation as resolved.
Show resolved Hide resolved
activeRoles, currentUser := e.ctx.GetSessionVars().ActiveRoles, ""
if e.ctx.GetSessionVars().User != nil {
currentUser = e.ctx.GetSessionVars().User.Username
}
for _, user := range s.Users {
exists, err := userExists(ctx, e.ctx, user.Username, user.Hostname)
if err != nil {
Expand Down Expand Up @@ -693,11 +699,29 @@ func (e *SimpleExec) executeRevokeRole(ctx context.Context, s *ast.RevokeRoleStm
}
return ErrCannotUser.GenWithStackByArgs("REVOKE ROLE", role.String())
}

// delete from activeRoles
if currentUser == user.Username {
for i := 0; i < len(activeRoles); i++ {
if activeRoles[i].Username == role.Username && activeRoles[i].Hostname == role.Hostname {
activeRoles = append(activeRoles[:i], activeRoles[i+1:]...)
break
}
}
}
}
}
if _, err := sqlExecutor.ExecuteInternal(context.TODO(), "commit"); err != nil {
return err
}
checker := privilege.GetPrivilegeManager(e.ctx)
if checker == nil {
return errors.New("miss privilege checker")
}
if ok, roleName := checker.ActiveRoles(e.ctx, activeRoles); !ok {
u := e.ctx.GetSessionVars().User
return ErrRoleNotGranted.GenWithStackByArgs(roleName, u.String())
}
return domain.GetDomain(e.ctx).NotifyUpdatePrivilege()
}

Expand Down
14 changes: 14 additions & 0 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,17 @@ func (s *testSuite3) TestShowGrantsAfterDropRole(c *C) {
tk.MustExec("DROP ROLE r29473")
tk.MustQuery("SHOW GRANTS").Check(testkit.Rows("GRANT CREATE USER ON *.* TO 'u29473'@'%'"))
}

func (s *testSuite3) TestDropRoleAfterRevoke(c *C) {
// issue 29781
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost"}, nil, nil)

tk.MustExec("create role r1, r2, r3;")
defer tk.MustExec("drop role r2, r3;")
unconsolable marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("grant r1,r2,r3 to current_user();")
tk.MustExec("set role all;")
tk.MustExec("revoke r1, r3 from root;")
unconsolable marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("drop role r1;")
}