fix(cloudformation): apply AssumeRolePolicyDocument when adopting an existing IAM role on update (#2143)

provisionIamRole's adoption path (triggered by EntityAlreadyExists
when a same-stack UpdateStack retries against a role that already
exists) fetched the existing role via getRole() but never applied
the template's current AssumeRolePolicyDocument. A changed trust
policy was silently dropped while the stack still reported
UPDATE_COMPLETE.

Routes the adoption path through the existing IamService.updateAssumeRolePolicy
helper, mirroring how createRole() already applies the document on
first create.

If a later step in the same attempt fails (e.g. a bad
ManagedPolicyArns entry), the whole resource update fails and
CloudFormation reports UPDATE_ROLLBACK_COMPLETE - the trust policy
must actually roll back too, not stay on the new value. Captures the
role's prior trust policy before applying the update and restores it
in the existing failure-cleanup path alongside policy detachment.

Does not re-fetch the role after applying the trust policy: nothing
downstream reads anything affected by that update (Arn, RoleId,
AttachedPolicyArns are unchanged by it), and a failing re-fetch there
would escape before the restoration catch block, leaving the new
trust policy active despite a rolled-back update.

Adds an ID-verified updateAssumeRolePolicy(name, doc, expectedRoleId)
overload in IamService: a role deleted and recreated under the same
name between the earlier identity check and this write would
otherwise silently receive an update meant for the role this stack
actually owns. Both the primary write and the failure-triggered
restore use this overload - restoring a trust policy is just as
consequential as applying one, so both directions get the same
identity guarantee.

Fixes #2084.
This commit is contained in:
Olamide Kosile authored and GitHub committed 2026-08-09 19:40:15 -04:00
1 parent a2b20cc085
commit 4a5fae4dcd
5 files changed
+285

No files matched your search

@@ -70,6 +70,13 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
IamRole role;
boolean createdRole = false;
// Set only on the adoption path, to the role's trust policy (and verified RoleId) as they
// were before this attempt touched them. Lets a later failure (e.g. bad ManagedPolicyArns)
// restore the trust policy below, so a rolled-back update doesn't leave it changed despite
// UPDATE_ROLLBACK_COMPLETE - and restore it onto the *same verified role*, not onto a
// replacement that took the name in the meantime.
String priorAssumeRolePolicyDocument = null;
String priorAssumeRoleId = null;
try {
role = iamService.createRole(resolvedRoleName, path, assumeDoc, description, 3600, Map.of());
createdRole = true;
@@ -89,6 +96,22 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR);
throw e;
}
// createRole() only runs on first create; adopting an existing role on update must
// still apply this template's current AssumeRolePolicyDocument, or a changed trust
// policy is silently dropped while the stack still reports UPDATE_COMPLETE.
//
// Deliberately not re-fetching the role afterward: everything read from `role` below
// (Arn, RoleId, AttachedPolicyArns) is unaffected by a trust-policy update, and a
// failing re-fetch here would escape before the failure-cleanup block below can
// restore the trust policy, leaving the new document active despite a rolled-back
// update.
// Pass existingRoleId through so IamService re-verifies identity atomically with the
// write, closing the gap between the check above and this call: a role deleted and
// recreated under the same name in between would otherwise silently receive an update
// meant for the role this stack actually owns.
priorAssumeRolePolicyDocument = role.getAssumeRolePolicyDocument();
priorAssumeRoleId = existingRoleId;
iamService.updateAssumeRolePolicy(resolvedRoleName, assumeDoc, existingRoleId);
}
r.setPhysicalId(resolvedRoleName);
@@ -110,6 +133,8 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
// name this attempt introduced there is no prior value and the policy is removed instead.
Map<String, String> originalInlinePolicies = new HashMap<>(role.getInlinePolicies());
LinkedHashSet<String> inlineWrittenByThisAttempt = new LinkedHashSet<>();
final String documentToRestore = priorAssumeRolePolicyDocument;
final String roleIdToRestore = priorAssumeRoleId;
try {
for (String policyArn : managedPolicyArns) {
iamService.attachRolePolicy(resolvedRoleName, policyArn);
@@ -215,6 +240,16 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
cleanupSucceeded = false;
}
}
if (documentToRestore != null) {
// ID-verified, same as the primary write above: if the role was deleted and
// recreated under this name in between, this must not write the restored document
// onto the replacement.
if (!CfnRollback.attemptIamCleanup(failure, "restore prior trust policy on role " + resolvedRoleName,
() -> iamService.updateAssumeRolePolicy(
resolvedRoleName, documentToRestore, roleIdToRestore))) {
cleanupSucceeded = false;
}
}
if (createdRole) {
if (!CfnRollback.attemptIamCleanup(failure, "delete role " + resolvedRoleName,
() -> iamService.deleteRole(resolvedRoleName))) {
@@ -572,6 +572,24 @@ public class IamService implements SessionAccountLookup {
roles.put(roleName, role);
}
/**
* Same as {@link #updateAssumeRolePolicy(String, String)}, but verifies {@code expectedRoleId}
* against the resolved role's immutable ID before applying the update, atomically with the
* name-based lookup. For callers (e.g. CloudFormation role adoption) that already verified role
* identity by ID earlier: without this, a role deleted and recreated under the same name between
* that check and this call would silently receive the update meant for the original role.
*/
public void updateAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId) {
IamRole role = getRole(roleName);
if (expectedRoleId != null && !expectedRoleId.equals(role.getRoleId())) {
throw new AwsException("EntityAlreadyExists",
"Role " + roleName + " was replaced by a different role of the same name; "
+ "refusing to apply an update meant for the original role.", 409);
}
role.setAssumeRolePolicyDocument(policyDocument);
roles.put(roleName, role);
}
public void tagRole(String roleName, Map<String, String> newTags) {
IamRole role = getRole(roleName);
role.getTags().putAll(newTags);