refactor(cloudformation): reuse IAM target helpers (#8761)

* refactor(cloudformation): reuse IAM target helpers
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-17 12:56:19 -04:00
1 parent d1d03a4ad8
commit 123ec6d347
2 files changed
+15 -27

No files matched your search

@@ -179,9 +179,9 @@ public class IamManagedPolicyCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put("ManagedPolicyRoleTargets", String.join("\n", roleNames));
r.getAttributes().put("ManagedPolicyUserTargets", String.join("\n", userNames));
r.getAttributes().put("ManagedPolicyGroupTargets", String.join("\n", groupNames));
putTargetIds(r, "ManagedPolicyRoleTargetIds", roleTargetIds);
putTargetIds(r, "ManagedPolicyUserTargetIds", userTargetIds);
putTargetIds(r, "ManagedPolicyGroupTargetIds", groupTargetIds);
setTargetAttribute(r, "ManagedPolicyRoleTargetIds", roleTargetIds);
setTargetAttribute(r, "ManagedPolicyUserTargetIds", userTargetIds);
setTargetAttribute(r, "ManagedPolicyGroupTargetIds", groupTargetIds);
String policyArn = policy.getArn();
// On adopt, attachments from the previous pass are not this attempt's to undo.
@@ -291,12 +291,12 @@ public class IamManagedPolicyCfnProvisioner implements CfnResourceProvisioner {
if (cleanupSucceeded && createdPolicy) {
r.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR);
} else if (cleanupSucceeded) {
restoreTargetAttribute(r, "ManagedPolicyRoleTargets", previousRoleTargets);
restoreTargetAttribute(r, "ManagedPolicyUserTargets", previousUserTargets);
restoreTargetAttribute(r, "ManagedPolicyGroupTargets", previousGroupTargets);
restoreTargetAttribute(r, "ManagedPolicyRoleTargetIds", previousRoleTargetIds);
restoreTargetAttribute(r, "ManagedPolicyUserTargetIds", previousUserTargetIds);
restoreTargetAttribute(r, "ManagedPolicyGroupTargetIds", previousGroupTargetIds);
setTargetAttribute(r, "ManagedPolicyRoleTargets", previousRoleTargets);
setTargetAttribute(r, "ManagedPolicyUserTargets", previousUserTargets);
setTargetAttribute(r, "ManagedPolicyGroupTargets", previousGroupTargets);
setTargetAttribute(r, "ManagedPolicyRoleTargetIds", previousRoleTargetIds);
setTargetAttribute(r, "ManagedPolicyUserTargetIds", previousUserTargetIds);
setTargetAttribute(r, "ManagedPolicyGroupTargetIds", previousGroupTargetIds);
if (previousDefaultVersionId != null) {
r.getAttributes().put("DefaultVersionId", previousDefaultVersionId);
}
@@ -312,11 +312,11 @@ public class IamManagedPolicyCfnProvisioner implements CfnResourceProvisioner {
}
}
private static void restoreTargetAttribute(StackResource resource, String key, String previousValue) {
if (previousValue == null) {
private static void setTargetAttribute(StackResource resource, String key, String value) {
if (value == null) {
resource.getAttributes().remove(key);
} else {
resource.getAttributes().put(key, previousValue);
resource.getAttributes().put(key, value);
}
}
@@ -328,14 +328,6 @@ public class IamManagedPolicyCfnProvisioner implements CfnResourceProvisioner {
return entities.stream().map(idGetter).collect(Collectors.joining("\n"));
}
private static void putTargetIds(StackResource resource, String key, String ids) {
if (ids == null) {
resource.getAttributes().remove(key);
} else {
resource.getAttributes().put(key, ids);
}
}
private static boolean attemptPrincipalCleanup(RuntimeException failure, String policyArn, String type,
Collection<String> names, Consumer<String> action,
String operation) {
@@ -37,15 +37,11 @@ final class IamManagedPolicyDeletes {
static List<String> roleTargets(IamService iamService, StackResource resource) {
String policyArn = resource.getPhysicalId();
String targets = resource.getAttributes().get("ManagedPolicyRoleTargets");
String ids = resource.getAttributes().get("ManagedPolicyRoleTargetIds");
if (targets != null) {
List<String> recordedNames = splitTargets(targets);
if (recordedNames.isEmpty() || ids == null || ids.isBlank()) {
return recordedNames;
}
return recordedTargets(targets, ids,
iamService.listRoles("/"), IamRole::getRoleId, IamRole::getRoleName);
return principalTargets(resource, "ManagedPolicyRoleTargets", "ManagedPolicyRoleTargetIds",
() -> iamService.listRoles("/"), IamRole::getRoleId, IamRole::getRoleName);
}
String ids = resource.getAttributes().get("ManagedPolicyRoleTargetIds");
// Stacks persisted before target metadata was introduced still need to be deletable.
// The policy is stack-owned, so discover only roles that currently reference this ARN.
List<IamRole> attachedRoles = iamService.listRoles("/");