Skip to content

feat(cloudformation): apply AssociatedRoles on RDS clusters and instances - #5656

Merged
pgermosen merged 4 commits into
floci-io:mainfrom
avison9:feat/5638-cfn-rds-associated-roles
Oct 15, 2026
Merged

pgermosen merged 4 commits into
floci-io:mainfrom
avison9:feat/5638-cfn-rds-associated-roles

Conversation

@avison9

@avison9 avison9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #5638.

AWS::RDS::DBCluster and AWS::RDS::DBInstance ignored AssociatedRoles. A CDK DatabaseCluster with s3ImportBuckets / s3ExportBuckets deployed against Floci, but DescribeDBClusters then reported no roles. #5367 added the RDS side and named this as a follow-up.

# CDK DatabaseCluster (aurora-postgresql 16.3) with an import and an export bucket, deployed to Floci
aws --endpoint-url http://localhost:4566 rds describe-db-clusters --db-cluster-identifier "$CLUSTER" \
    --query 'DBClusters[0].AssociatedRoles[].[FeatureName,Status]' --output text
# before: (nothing)
# after:
# s3Import  ACTIVE
# s3Export  ACTIVE

What changed (RdsCfnProvisioner):

  • Reading the property. It is resolved before it is read, so Fn::GetAtt and Ref work, Fn::If may select the whole list or a single entry, and AWS::NoValue leaves it out. A value that is not a list of roles is a ValidationError, so a malformed property never reads as "no roles".
  • Create. The roles are associated with the new cluster or instance through the RDS service, so its rules apply: a missing RoleArn, and an instance role without FeatureName. A cluster's list is checked first and taken in one step through addRolesToNewDbCluster, so a role named twice refuses it. An instance takes its roles one at a time, and, as AWS's AWS::RDS::DBInstance handler ignores DBInstanceRoleAlreadyExists, a role or feature the list repeats is skipped rather than failing the resource. If RDS refuses, the resource fails and the new database is deleted, so a failed stack leaves nothing behind. A renamed instance's predecessor is deleted only after the new instance has its roles, so a refused list costs only the new one.
  • Update (No interruption, as the schema says). A cluster's list is checked first, as on a create. Then the roles the template gained are added, and the roles the previous template declared and this one drops are removed. A role associated outside the stack (AddRoleToDBCluster by hand) is kept while the template leaves it out. A role the template newly declares is always added, so one attached by hand first fails the update with DBClusterRoleAlreadyExists, as on AWS. Instances skip repeats on an update as on a create. If any step is refused, the update removes the roles it added and puts back the ones it removed before reporting the error, so the database matches the record the stack restores. The declared list is recorded on the resource as the internal __FlociAssociatedRoles attribute, beside the existing __FlociDbClusterCreateOnly. A replaced cluster goes through the create path, and if a later resource rolls that replacement back, the prior record is restored with the prior cluster, the same way the create-only record is.
  • Aurora members. The schema documents AssociatedRoles on an AWS::RDS::DBInstance as "Not applicable. The associated roles are managed by the DB cluster." An instance with DBClusterIdentifier and its own AssociatedRoles therefore fails with a ValidationError that points at the cluster's property, instead of attaching roles to the member.
  • RDS. addRolesToNewDbCluster now refuses a role named twice in its own list, the check CreateDBCluster already ran before calling it. CloudFormation calls it directly.
  • Docs. A "RDS Associated Roles" section in docs/services/cloudformation.md.

When #5637 merges, the feature-name check it adds applies here too with no change, since both paths call the same service methods.

Deliberately not in this PR:

  • Role quota faults.
  • Checking that the IAM role exists, which the RDS actions don't do either.
  • Undoing in-place role changes when a later resource fails the stack update. An update that fails on its own roles does undo them, but the provisioner doesn't undo the other in-place changes it makes (password, IAM authentication) when another resource fails either.

Type of change

  • Bug fix (fix:)
  • New feature (feat:)
  • Breaking change (feat!: or fix!:)
  • Docs / chore

AWS Compatibility

Verified against a local build in real (non-mock) RDS mode:

  • AWS CDK (aws-cdk-lib 2.264.0, CLI 2.1135.1). A DatabaseCluster (Aurora PostgreSQL 16.3) with an S3 import and an S3 export bucket was synthesized and deployed with aws cloudformation deploy:
    • The stack reached CREATE_COMPLETE, and the cluster reported both roles ACTIVE with features s3Import and s3Export.
    • Redeploying without the export bucket gave UPDATE_COMPLETE, and only s3Import remained.
    • Deleting the stack removed the cluster.
  • Property names and required fields follow the AWS::RDS::DBCluster / DBClusterRole and AWS::RDS::DBInstance / DBInstanceRole schemas in the CloudFormation template reference.

Tests

  • CloudFormationRdsAssociatedRolesIntegrationTest (8, mock RDS, no Docker):
    • Create and update. A stack with an AWS::IAM::Role referenced through Fn::GetAtt, a cluster and an instance. After an update that swaps the cluster's role, the new role is present, the dropped one is gone, and a role added by hand outside the stack is kept.
    • Fn::If. Fn::If around the whole list and around single entries, including one that selects AWS::NoValue.
    • Refused lists.
      • A cluster role list RDS refuses rolls the stack back and leaves no cluster.
      • An update carrying a non-list value, or a role named twice, rolls back with the roles unchanged.
    • Repeated roles.
      • A repeated instance role or feature is skipped, as AWS does.
      • Newly declaring a cluster role that was attached by hand fails the update with DBClusterRoleAlreadyExists.
    • Undo. An update that removes one role and adds another, then is refused, leaves the cluster as it was, and a retry goes through.
    • Aurora members. A member carrying AssociatedRoles fails with the ValidationError.
  • RdsCfnProvisionerTest:
    • deletesANewDbInstanceWhoseAssociatedRolesAreRefused: the instance rollback.
    • renamedDbInstanceKeepsItsPredecessorWhenTheNewRolesAreRefused.
    • rolledBackDbClusterReplacementPutsTheRolesRecordBack.
    • failedDbInstanceRoleUpdateUndoesItsOwnChanges.
  • Mutation checks. Each part was checked by disabling it, and a test fails each time:
    • associating on create;
    • resolving the property;
    • refusing a non-list;
    • checking a cluster's list before an update;
    • removing dropped roles;
    • keeping roles added outside the stack;
    • adding a newly declared role even when it is already attached;
    • skipping a repeated instance role;
    • deleting a refused cluster;
    • deleting a refused instance;
    • the rename order;
    • restoring the record on rollback;
    • undoing a failed update's own changes;
    • the Aurora member check.
  • Regression subset (every RDS and CloudFormation test class, plus the AwsQuery* routing tests): 4,586 tests in 317 classes pass. One is skipped: AppSyncCfnIntegrationTest needs the AppSync sidecar image, which is not available locally.
  • ./mvnw checkstyle:check, make partition-check and make docs-check pass.

Checklist

  • ./mvnw test passes locally (the subset above; the full suite was not run)
  • New or updated integration test added
  • Commit messages follow Conventional Commits
  • I have no more than 2 open, non-draft pull requests and no more than 4 open pull requests in total (drafts included) in this repository (maintainers and dependency bots are exempt).

…nces

AWS::RDS::DBCluster and AWS::RDS::DBInstance ignored AssociatedRoles,
so a CDK DatabaseCluster with s3ImportBuckets deployed with no role on
the cluster.

The roles are now associated when the resource is created, through the
same checks as AddRoleToDBCluster and AddRoleToDBInstance; a list RDS
refuses fails the resource and deletes the new database. An update adds
the roles the template gained and removes the ones the previous
template declared and this one drops, so a role associated outside the
stack is kept. An Aurora cluster member carrying its own
AssociatedRoles is a ValidationError, as the schema calls the property
not applicable there. addRolesToNewDbCluster now also refuses a role
named twice in its own list.

Closes floci-io#5638
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Apply CloudFormation AssociatedRoles to RDS clusters and instances

✨ Enhancement 🧪 Tests 📝 Documentation 🕐 40+ Minutes

Grey Divider

AI Description

• Apply template-declared RDS roles on creation and reconcile them on stack updates.
Diagram

graph TD
  T["CFN Template"] --> P["RDS Provisioner"] --> D{"New database?"} -->|Create| S["RDS Role APIs"] --> B["DB Roles"] --> N["Updated Role Record"]
  D -->|Update| R["Previous Role Record"] --> S
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Associate roles within RDS create methods
  • ➕ Could make database creation and initial role association a single operation.
  • ➖ Requires broader RDS service API changes and separate handling of subsequent stack updates.

Recommendation: Keep the provisioner-level reconciliation: it reuses RDS role validation, tracks only stack-declared roles, and limits changes to the CloudFormation integration. The create-method alternative has a wider impact for limited additional benefit.

Files changed (5) +354 / -0

Enhancement (1) +152 / -0
RdsCfnProvisioner.javaReconcile declared roles for RDS resources +152/-0

Reconcile declared roles for RDS resources

• Associates cluster and instance roles through RDS service methods, removes only previously declared roles on updates, and records the declared list on the stack resource. It deletes newly created databases when role association fails and rejects roles declared on Aurora members.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java

Bug fix (1) +1 / -0
RdsService.javaValidate batch cluster role associations +1/-0

Validate batch cluster role associations

• Runs existing cluster-role list validation before associating roles with a newly created cluster, rejecting duplicate role ARNs and missing required values.

src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java

Tests (2) +192 / -0
CloudFormationRdsAssociatedRolesIntegrationTest.javaTest stack-level RDS role behavior +168/-0

Test stack-level RDS role behavior

• Covers creation, update reconciliation, preservation of manually associated roles, cleanup after a refused cluster role list, and rejection of roles on Aurora members.

src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRdsAssociatedRolesIntegrationTest.java

RdsCfnProvisionerTest.javaTest instance cleanup after role failure +24/-0

Test instance cleanup after role failure

• Verifies that a newly created DB instance is deleted and its CloudFormation resource fails when a later role association is refused.

src/test/java/io/github/hectorvent/floci/services/cloudformation/RdsCfnProvisionerTest.java

Documentation (1) +9 / -0
cloudformation.mdDocument CloudFormation RDS role behavior +9/-0

Document CloudFormation RDS role behavior

• Explains role association on create, stack-managed reconciliation on update, and why Aurora cluster members cannot declare their own roles.

docs/services/cloudformation.md

@qodo-code-review

qodo-code-review Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (1) 📎 Requirement gaps (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. A failed rename deletes both databases ✓ Resolved
Description
provisionDbInstance deletes the old instance through deleteRenamedResource before the new
reconcileInstanceRoles call can reject the replacement's roles. If a rename update supplies an
invalid association, role cleanup deletes the new instance too, while stack rollback restores
metadata pointing to the already-deleted old instance.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[360]

+        instance = reconcileInstanceRoles(r, instance, roles, created, region);
Relevance

●●● Strong

Recent precedents accept fixes preventing irreversible replacement cleanup before later
reconciliation failures.

PR-#4511
PR-#5008

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The rename path deletes the prior physical ID before reconciliation. A role error then deletes the
newly created ID, and failed-update handling restores the previous metadata without recreating its
database.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[335-360]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[523-535]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java[1736-1763]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
A failed role association during an instance rename deletes both the old and new databases.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[351-360]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[523-535]
### Recommended Fix
Keep the old instance until all new-instance provisioning and role associations succeed; on failure, delete only the new instance and retain the old one.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Bad role metadata hides update state 📘 Rule violation ☼ Reliability
Description
readAssociatedRoles catches Exception, logs only the raw value, and returns an empty list. When
the internal record is malformed, the next update treats all previously managed associations as
absent, so dropped roles remain and current roles may be re-added or conflict.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R596-598]

+        } catch (Exception e) {
+            LOG.warnv("Ignoring unreadable AssociatedRoles record: {0}", raw);
+        }
Relevance

●●● Strong

Recent precedent accepted propagating malformed state instead of silently converting parsing
failures into empty state.

PR-#3699
PR-#5218

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The changed parser catches Exception, logs a warning, and returns an empty list instead of
propagating the parsing failure. This violates the requirement to suppress only known benign errors
and to let non-benign failures reach the caller.

Rule 3865600: Do not swallow exceptions broadly; suppress only known benign errors
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[586-599]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The role-reconciliation state parser catches every exception and continues with an empty list, allowing corrupted internal state to cause incomplete or conflicting role updates.

## Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[591-599]

## Recommended Fix
Catch only the expected parsing exception, and propagate a clear failure when the persisted role record is malformed so reconciliation cannot silently proceed with missing prior associations. Add a regression test covering malformed `__FlociAssociatedRoles` state and asserting that the update fails without applying an incomplete reconciliation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗



Remediation recommended

3. Malformed role lists erase associations ✓ Resolved
Description
parseAssociatedRoles returns an empty list for every non-array AssociatedRoles value rather than
rejecting a malformed value. On an update with a scalar property, reconciliation treats all
previously declared roles as dropped, removes them from RDS, and records an empty ownership list
while the stack update succeeds.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R475-477]

+        JsonNode list = props != null ? props.get("AssociatedRoles") : null;
+        if (list == null || !list.isArray()) {
+            return roles;
Relevance

●●● Strong

Recent validation precedents reject invalid resolved shapes instead of silently treating them as
absent.

PR-#5218
PR-#4970

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The non-array early return supplies an empty desired list. The update path removes roles found in
the stored declaration but absent from that list, then persists the empty list.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[472-484]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-520]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[554-584]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
A present but malformed AssociatedRoles value is interpreted as an empty list and can remove existing associations.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[472-484]
### Recommended Fix
After resolving structured intrinsics, treat only an absent property or a selected AWS::NoValue as absent. Reject other non-array values with a validation error before reconciliation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


4. Rollback can misalign later role updates ✓ Resolved
Description
recordAssociatedRoles overwrites the cluster's prior __FlociAssociatedRoles record with the
replacement template's roles, while rollbackUpdate restores the old physical cluster without
restoring that record. If another resource subsequently fails the stack update and triggers
replacement rollback, a later update calculates role removals from the discarded replacement's roles
rather than the restored cluster's original template, leaving associations out of sync or attempting
incorrect changes.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R575-583]

+    private static void recordAssociatedRoles(StackResource r, List<DbRoleAssociation> roles) {
+        ArrayNode record = MAPPER.createArrayNode();
+        for (DbRoleAssociation role : roles) {
+            ObjectNode entry = record.addObject().put("RoleArn", role.getRoleArn());
+            if (blankToNull(role.getFeatureName()) != null) {
+                entry.put("FeatureName", role.getFeatureName());
+            }
+        }
+        r.getAttributes().put(ASSOCIATED_ROLES_ATTR, record.toString());
Relevance

●●● Strong

Recent precedent accepted restoring prior exposed metadata when replacement rollback deletes the new
resource.

PR-#5373
PR-#4731

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The reconciliation path records the current template's roles in __FlociAssociatedRoles, while
replacement cleanup excludes internal __Floci attributes from its prior-attribute snapshot and RDS
rollback restores only the create-only cluster record. The subsequent update path reads the retained
associated-role record to compute dropped roles, demonstrating how a restored cluster can be
reconciled using metadata from the failed replacement.

Reconcile AssociatedRoles on updates without interruption
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[575-583]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[180-196]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ReplacementCleanup.java[111-124]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ReplacementCleanup.java[73-79]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ReplacementCleanup.java[108-121]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[186-199]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[554-584]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A replacement update records the new template's associated roles, but rollback restores the prior cluster without restoring its prior `__FlociAssociatedRoles` record. Subsequent updates therefore use metadata from the discarded replacement when determining role additions and removals.

## Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[575-584]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[180-199]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ReplacementCleanup.java[111-124]

## Recommended Fix
Save the prior `__FlociAssociatedRoles` record before replacement and restore it when rollback restores the old cluster, either by including it in the replacement rollback snapshot or by restoring it explicitly in `rollbackUpdate`. Clear any saved record when the update commits. Add a regression test that fails a replacement update, rolls it back, then performs another update and verifies that role additions and removals are calculated from the restored cluster's original template.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


5. Updates accept repeated role entries ✓ Resolved
Description
reconcileClusterRoles and reconcileInstanceRoles skip an identical second entry once the first
entry has been added or was already present, without validating the desired list. An update
containing a repeated role can therefore succeed even though the new-cluster validator rejects
repeated ARNs and the instance association API refuses repeated roles.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R513-516]

+            for (DbRoleAssociation role : roles) {
+                if (!containsRole(cluster.getAssociatedRoles(), role)) {
+                    cluster = rdsService.addRoleToDbCluster(region, id, role.getRoleArn(), role.getFeatureName());
+                }
Relevance

●●● Strong

Duplicate desired entries bypassing service validation is a clear correctness gap with a
deterministic validation fix.

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The update loops use presence in the current associations to decide whether to call the API, making
an exact duplicate a no-op. Creation and direct instance additions enforce uniqueness instead.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-517]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[536-547]
src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java[9653-9666]
src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java[9697-9707]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
Update reconciliation silently accepts duplicate role entries that the RDS association rules reject on creation.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-517]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[536-547]
### Recommended Fix
Validate the entire desired list before any update mutation, applying cluster ARN uniqueness and instance ARN and feature uniqueness rules. Then reconcile the validated list.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


View medium (1)
6. Conditional roles are lost or rejected ✓ Resolved
Description
parseAssociatedRoles examines the raw property and its raw array entries instead of resolving
structured intrinsic expressions. A property-level Fn::If selecting a role list is treated as an
empty list, while an entry-level Fn::If produces a role with no ARN and fails association even
when its selected branch supplies a valid role.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R475-480]

+        JsonNode list = props != null ? props.get("AssociatedRoles") : null;
+        if (list == null || !list.isArray()) {
+            return roles;
+        }
+        for (JsonNode role : list) {
+            roles.add(new DbRoleAssociation(resolveOptional(role, "RoleArn", engine),
Relevance

●●● Strong

Recent precedents accept resolving structured conditionals before parsing nested resource
properties.

PR-#5012
PR-#5218
PR-#5407

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The parser returns immediately for a raw non-array and reads fields directly from each raw entry.
The template engine has structured resolution for array-valued conditional branches and omission of
selected NoValue expressions, but neither is called here.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[472-484]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java[187-202]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java[228-253]
src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java[9697-9707]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
Conditional expressions around the role list or its entries are parsed before their selected branches are resolved.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[472-484]
### Recommended Fix
Resolve the structured AssociatedRoles value and its entries with the template engine, omitting selected AWS::NoValue entries, before checking the array shape and reading role fields.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Informational

7. Role updates make stack rollback fail 🐞 Bug ☼ Reliability
Description
reconcileClusterRoles and reconcileInstanceRoles apply successful in-place role changes, but
rollbackUpdate has no corresponding role restoration. When a later resource fails the stack
update, CloudFormation asks this provisioner to roll back; it returns false for an in-place update,
reports rollback as unimplemented, and leaves the new associations applied.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R519-520]

+        recordAssociatedRoles(r, roles);
+        return cluster;
Relevance

● Weak

Recent precedents rejected adding stack-level rollback for successful in-place mutations outside
provisioner-local failures.

PR-#5266
PR-#5493

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
Successful reconciliation records only the new roles. RDS rollback handles replacement and
create-only metadata but no role associations; the stack rollback walker treats a false result for
an updated resource as an unimplemented rollback.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[186-199]
src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-551]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java[2200-2228]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
A successful in-place role update cannot be reversed if a later resource fails the stack update.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[186-199]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-551]
### Recommended Fix
Retain the previous role configuration for an update and reconcile live associations back to it during rollback. Preserve the previous ownership record until rollback or update completion.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


8. Failed updates leave role changes behind ✓ Resolved
Description
reconcileClusterRoles and reconcileInstanceRoles mutate associations one at a time without
undoing earlier calls when a later call fails. On a failed update, CloudFormation restores the
previous resource record and skips its physical rollback, so successfully added or removed roles
remain on the database despite the failed template.
Code

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[R513-516]

+            for (DbRoleAssociation role : roles) {
+                if (!containsRole(cluster.getAssociatedRoles(), role)) {
+                    cluster = rdsService.addRoleToDbCluster(region, id, role.getRoleArn(), role.getFeatureName());
+                }
Relevance

● Weak

Recent precedents rejected broad rollback of multiple in-place mutations after later stack-resource
failures.

PR-#5266
PR-#5493

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
Both update loops issue separate service calls, whereas failed-update handling restores old metadata
and marks the resource as already restored. The rollback walker then skips physical rollback for
that resource.

src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-551]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java[1736-1763]
src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java[2200-2228]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
An update that fails partway through role reconciliation leaves earlier role changes applied while CloudFormation restores old metadata.
### Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[505-518]
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[536-549]
### Recommended Fix
Snapshot the pre-update associations and compensate for completed additions and removals when a later role operation fails. Propagate a rollback failure if compensation cannot restore the prior state.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
✅ Compliance rules (platform): 8 rules
✅ Cross-repo context — repo relationships
Review mode: Auto: ⚖️ Balanced: Downgraded extended -> standard: change is below the extended eligibility bar (hunks 12/18, lines 354/200; both must reach the floor). Router rationale: Multiple create/update paths, cleanup semantics, and role-state tracking invite defects.

Grey Divider

Tip of the day
💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment on lines +596 to +598
} catch (Exception e) {
LOG.warnv("Ignoring unreadable AssociatedRoles record: {0}", raw);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Bad role metadata hides update state 📘 Rule violation ☼ Reliability

readAssociatedRoles catches Exception, logs only the raw value, and returns an empty list. When
the internal record is malformed, the next update treats all previously managed associations as
absent, so dropped roles remain and current roles may be re-added or conflict.
Agent Prompt
## Issue description
The role-reconciliation state parser catches every exception and continues with an empty list, allowing corrupted internal state to cause incomplete or conflicting role updates.

## Fix Focus Areas
- src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/RdsCfnProvisioner.java[591-599]

## Recommended Fix
Catch only the expected parsing exception, and propagate a clear failure when the persisted role record is malformed so reconciliation cannot silently proceed with missing prior associations. Add a regression test covering malformed `__FlociAssociatedRoles` state and asserting that the update fails without applying an incomplete reconciliation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

avison9 added a commit to avison9/floci that referenced this pull request Oct 14, 2026
Callers that check an AssociatedRoles list without knowing the engine,
such as the CloudFormation provisioner in floci-io#5656, keep the signature they
compile against; it runs the same checks without the feature check.
@pgermosen pgermosen added cloudformation AWS CloudFormation enhancement New feature or request labels Oct 14, 2026

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avison9 thanks, a few things. A DBInstanceIdentifier rename deletes the old instance before the roles are applied, so a refused list loses both; apply the roles first. AWS's DBInstance handler ignores DBInstanceRoleAlreadyExists, so a repeated role still succeeds there, while here the instance gets deleted (clusters are the opposite: duplicates and hand-attached roles fail on update). Fn::If and AWS::NoValue aren't resolved; engine.resolveNodeOmittingNoValue covers that, and a non-array result should be refused. And a replacement rollback leaves __FlociAssociatedRoles on the new roles; restore it like __FlociDbClusterCreateOnlyPrior.

@pgermosen pgermosen added the waiting-author Review posted; waiting on the PR author to respond label Oct 14, 2026
…hanging a database

A renamed instance's predecessor was deleted before the new instance
took its roles, so a refused list left neither; the roles now go on
first. The property is resolved before it is read, so Fn::If around the
list or an entry works and AWS::NoValue leaves it out, and any other
value that is not a list is a ValidationError rather than "no roles".

Repeated roles follow AWS's handlers: an instance skips a role or
feature already associated (AWS ignores DBInstanceRoleAlreadyExists),
while a cluster's list is checked first on an update too, and a role
the template newly declares is always added, so one attached by hand
fails the update with DBClusterRoleAlreadyExists. A rolled back cluster
replacement also restores the record of which roles the stack declared.
@avison9

avison9 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

All four are addressed in d810912.

Rename. A renamed instance gets its roles before its predecessor is deleted, so a refused list deletes only the new instance. RdsCfnProvisionerTest.renamedDbInstanceKeepsItsPredecessorWhenTheNewRolesAreRefused covers it.

Repeated roles, following AWS's handlers.

  • Instances. A DBInstanceRoleAlreadyExists is ignored, so a role or feature the list repeats is skipped and the resource succeeds (aRepeatedInstanceRoleIsSkippedAsAwsDoes). Other refusals still fail it and delete a new instance.
  • Clusters. The list is checked before any change on an update as well as on a create, so a role named twice fails the update with the roles unchanged (anUpdateWithAMalformedOrRepeatedListKeepsTheRoles). A role the template newly declares is always added, so one attached by hand first fails the update with DBClusterRoleAlreadyExists (declaringARoleAttachedByHandFailsTheClusterUpdate). A hand-attached role the template leaves out is still kept.

Fn::If / AWS::NoValue. AssociatedRoles goes through engine.resolveNodeOmittingNoValue before it is read. A conditional list and conditional entries both resolve, and an entry that selects AWS::NoValue is left out (conditionsAroundTheListAndItsEntriesAreResolved). Any other non-array result is a ValidationError, and an update carrying one rolls back with the roles unchanged.

Replacement rollback. An update keeps the previous record as __FlociAssociatedRolesPrior. rollbackUpdate puts it back and clearUpdate drops it, as with __FlociDbClusterCreateOnlyPrior (rolledBackDbClusterReplacementPutsTheRolesRecordBack).

Reverting each change fails its test. The RDS and CloudFormation packages and the AwsQuery* routing tests pass: 4,311 tests in 312 classes, with one skip (AppSyncCfnIntegrationTest, which needs the AppSync sidecar image locally).

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avison9 thanks, all four are in: the rename keeps its predecessor, repeated instance roles are skipped, Fn::If and AWS::NoValue resolve, and a replacement rollback restores the record.

One gap the new cluster rule exposes: roles change one at a time, so if an update adds B and then C is refused, B stays attached while the stack restores the old record. Retrying then fails on B with DBClusterRoleAlreadyExists, where AWS's rollback would remove B. Could the update check every role it adds before changing anything, or undo its own changes when one fails?

Smaller: readAssociatedRoles catches Exception where only the JSON parse error is expected. The shard 1 CodePipeline failure is a race on main.

pgermosen pushed a commit that referenced this pull request Oct 14, 2026
* feat(rds): refuse role feature names the engine does not support

AddRoleToDBInstance, AddRoleToDBCluster and CreateDBCluster
AssociatedRoles stored any FeatureName, so Lambda on a MySQL instance
was accepted although MySQL supports no role features.

A FeatureName now has to be one the engine lists in
SupportedFeatureNames, matched without regard to case, or the call is
InvalidParameterValue; CreateDBCluster refuses before any cluster
exists. A cluster role with no feature, and an engine the catalogue
does not list, are left alone. Aurora PostgreSQL also lists Bedrock,
which the Aurora user guide offers next to Comprehend and SageMaker.

Closes #5631

* refactor(rds): keep the two-argument cluster role check

Callers that check an AssociatedRoles list without knowing the engine,
such as the CloudFormation provisioner in #5656, keep the signature they
compile against; it runs the same checks without the feature check.

* fix(rds): match feature names without regard to case in every role check

The supported-feature check matched without regard to case, but the
instance duplicate check and both removal lookups compared exactly, so
an instance could take s3Import and then s3import as two roles. All of
them now match the same way. The two-argument cluster role check, which
nothing here calls, is removed.
An update changed roles one at a time, so when a later step was
refused the roles it had already added stayed on the database while
the stack restored its old record, and a retry failed on them with
DBClusterRoleAlreadyExists. A refused step now removes the roles the
update added and puts back the ones it removed before the error is
reported. The cluster check also takes the template's engine, now that
it checks feature names, and the roles record reader catches only the
JSON parse error.
@avison9

avison9 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Both done in 5665a45, after merging upstream/main with #5637 in (no force push).

A failed update undoes its own changes. The update records each role it removes and adds. If a later step is refused, it removes the roles it added and puts back the ones it removed, then reports the original error, so the database matches the record the stack restores. This covers clusters and instances, and any refusal, including state, feature and a role attached by hand.

  • aFailedUpdateUndoesItsOwnRoleChanges: an update that drops one role and adds another, then hits a hand-attached role, leaves the cluster as it was. A retry without the hand-attached role then goes through.
  • RdsCfnProvisionerTest.failedDbInstanceRoleUpdateUndoesItsOwnChanges: the same for an instance, in order.

Without the undo, both fail. Undoing rather than pre-checking keeps one place that covers every RDS rule. The cluster check now gets the template's Engine too, through #5637's three-argument validateClusterRoleAssociations, so a feature the engine doesn't support is refused before anything changes.

readAssociatedRoles now catches only JsonProcessingException.

The RDS and CloudFormation packages and the AwsQuery* routing tests pass: 4,586 tests in 317 classes, with one skip (AppSyncCfnIntegrationTest, which needs the AppSync sidecar image locally). Checkstyle, make partition-check and make docs-check are clean.

@pgermosen pgermosen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avison9 thanks, a refused role update now puts back what it changed, so a retry starts clean, and readAssociatedRoles only catches the parse error. Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cloudformation AWS CloudFormation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] cloudformation: apply AssociatedRoles on AWS::RDS::DBCluster and AWS::RDS::DBInstance

2 participants