Files
floci/src/main/java
Alex BannaandAlexy Grabov 264b909e87 fix(cloudformation): model AWS::IAM::Policy as an inline policy (Fixes #1531) (#1800)
* fix(cloudformation): model AWS::IAM::Policy as an inline policy (Fixes #1531)

AWS::IAM::Policy is an inline policy embedded in the roles/users/groups it
names (PutRolePolicy/PutUserPolicy/PutGroupPolicy), not a standalone
managed policy. Floci provisioned it via iamService.createPolicy, giving
it an account-global name. When two stacks reused the same construct
sub-tree — as CDK does, emitting the same auto-generated PolicyName
(IamTaskRoleDefaultPolicy<hash>) on different roles — the second stack
failed with "Policy ... already exists" and rolled back, even though real
AWS scopes an inline policy's name to its owning principal.

Provision AWS::IAM::Policy as an inline policy attached to each named
principal, and remember the targets so a resource/stack delete detaches it
cleanly (an inline policy has no ARN and is removed with its principal).
AWS::IAM::ManagedPolicy keeps its standalone managed-policy behaviour and
now honours ManagedPolicyName. Two stacks reusing the same inline policy
name on different roles both deploy.

* fix(cloudformation): address review — log inline-detach failures, safe delimiter, test delete

Review feedback on the AWS::IAM::Policy inline-policy handling:

- detachInline no longer swallows exceptions silently (AGENTS.md forbids
  empty catch blocks); it logs at debug like the sibling *Safe helpers.
- Inline-policy target principals are joined/split on a newline instead of
  a comma. IAM friendly names permit commas ([\w+=,.@-]+) but never
  newlines, so a role/user/group name containing a comma no longer
  corrupts the target list and orphans the inline policy on delete.
- Added a test that deletes the stack and asserts the inline policy is
  detached (GetRolePolicy -> NoSuchEntity) from a role created outside the
  stack, so the detach path (deleteInlinePolicySafe/detachInline) is
  exercised rather than a cascade role delete.

* fix(cloudformation): surface IAM attachment failures

* fix(cloudformation): roll back IAM resources safely

* fix(cloudformation): roll back partial inline policies

* fix(cloudformation): detach removed IAM targets

* fix(cloudformation): verify IAM policy target updates

* fix(cloudformation): preserve inline policy update state

* fix(cloudformation): migrate legacy IAM policies

* fix(cloudformation): roll back legacy policy migration

---------

Co-authored-by: Alexy Grabov <Alexy.Grabov@cyberark.com>
2026-07-27 22:24:46 -05:00
..