feat(iam): enforce DeleteUser prerequisites and carry access keys and group membership through a rename (#3918)

DeleteUser now returns DeleteConflict while the user still has inline policies or access keys, matching AWS. UpdateUser rename moves the user's access keys and group membership to the new name, so a renamed user keeps them and a user recreated under the old name does not inherit them.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-19 19:54:55 -05:00
1 parent 224b56bd8c
commit eef69445cc
5 files changed
+352 -5

No files matched your search

@@ -411,6 +411,14 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
throw new AwsException("DeleteConflict",
"Cannot delete entity, must delete login profile first.", 409);
}
if (!user.getInlinePolicies().isEmpty()) {
throw new AwsException("DeleteConflict",
"Cannot delete entity, must delete policies first.", 409);
}
if (!userAccessKeys(userName).isEmpty()) {
throw new AwsException("DeleteConflict",
"Cannot delete entity, must delete access keys first.", 409);
}
users.delete(userName);
LOG.infov("Deleted IAM user: {0}", userName);
}
@@ -443,6 +451,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
throw new AwsException("EntityAlreadyExists",
"User with name " + newUserName + " already exists.", 409);
}
List<AccessKey> keysToMove = userAccessKeys(userName);
users.delete(userName);
user.setUserName(newUserName);
if (newPath != null) user.setPath(normalizePath(newPath));
@@ -453,6 +462,17 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
profile.setUserName(newUserName);
loginProfiles.put(newUserName, profile);
});
for (AccessKey key : keysToMove) {
key.setUserName(newUserName);
accessKeys.put(key.getAccessKeyId(), key);
}
for (String groupName : user.getGroupNames()) {
groups.get(groupName).ifPresent(group -> {
group.getUserNames().remove(userName);
group.getUserNames().add(newUserName);
groups.put(groupName, group);
});
}
} else {
if (newPath != null) {
user.setPath(normalizePath(newPath));
@@ -1363,6 +1383,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
public List<AccessKey> listAccessKeys(String userName) {
getUser(userName); // validates existence
return userAccessKeys(userName);
}
private List<AccessKey> userAccessKeys(String userName) {
return accessKeys.scan(k -> true).stream()
.filter(ak -> userName.equals(ak.getUserName()))
.toList();