Repository navigation
feat(cloudformation): apply AssociatedRoles on RDS clusters and instances - #5656
Conversation
…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
PR Summary by QodoApply CloudFormation AssociatedRoles to RDS clusters and instances
AI Description
Diagram
High-Level Assessment
Files changed (5)
|
Code Review by Qodo
1.
|
| } catch (Exception e) { | ||
| LOG.warnv("Ignoring unreadable AssociatedRoles record: {0}", raw); | ||
| } |
There was a problem hiding this comment.
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
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
left a comment
There was a problem hiding this comment.
@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.
…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.
|
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. Repeated roles, following AWS's handlers.
Replacement rollback. An update keeps the previous record as Reverting each change fails its test. The RDS and CloudFormation packages and the |
pgermosen
left a comment
There was a problem hiding this comment.
@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.
* 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.
|
Both done in 5665a45, after merging 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.
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
The RDS and CloudFormation packages and the |
Summary
Closes #5638.
AWS::RDS::DBClusterandAWS::RDS::DBInstanceignoredAssociatedRoles. A CDKDatabaseClusterwiths3ImportBuckets/s3ExportBucketsdeployed against Floci, butDescribeDBClustersthen reported no roles. #5367 added the RDS side and named this as a follow-up.What changed (
RdsCfnProvisioner):Fn::GetAttandRefwork,Fn::Ifmay select the whole list or a single entry, andAWS::NoValueleaves it out. A value that is not a list of roles is aValidationError, so a malformed property never reads as "no roles".RoleArn, and an instance role withoutFeatureName. A cluster's list is checked first and taken in one step throughaddRolesToNewDbCluster, so a role named twice refuses it. An instance takes its roles one at a time, and, as AWS'sAWS::RDS::DBInstancehandler ignoresDBInstanceRoleAlreadyExists, 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.AddRoleToDBClusterby 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 withDBClusterRoleAlreadyExists, 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__FlociAssociatedRolesattribute, 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.AssociatedRoleson anAWS::RDS::DBInstanceas "Not applicable. The associated roles are managed by the DB cluster." An instance withDBClusterIdentifierand its ownAssociatedRolestherefore fails with aValidationErrorthat points at the cluster's property, instead of attaching roles to the member.addRolesToNewDbClusternow refuses a role named twice in its own list, the checkCreateDBClusteralready ran before calling it. CloudFormation calls it directly.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:
Type of change
fix:)feat:)feat!:orfix!:)AWS Compatibility
Verified against a local build in real (non-mock) RDS mode:
DatabaseCluster(Aurora PostgreSQL 16.3) with an S3 import and an S3 export bucket was synthesized and deployed withaws cloudformation deploy:CREATE_COMPLETE, and the cluster reported both rolesACTIVEwith featuress3Importands3Export.UPDATE_COMPLETE, and onlys3Importremained.AWS::RDS::DBCluster/DBClusterRoleandAWS::RDS::DBInstance/DBInstanceRoleschemas in the CloudFormation template reference.Tests
CloudFormationRdsAssociatedRolesIntegrationTest(8, mock RDS, no Docker):AWS::IAM::Rolereferenced throughFn::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::Ifaround the whole list and around single entries, including one that selectsAWS::NoValue.DBClusterRoleAlreadyExists.AssociatedRolesfails with theValidationError.RdsCfnProvisionerTest:deletesANewDbInstanceWhoseAssociatedRolesAreRefused: the instance rollback.renamedDbInstanceKeepsItsPredecessorWhenTheNewRolesAreRefused.rolledBackDbClusterReplacementPutsTheRolesRecordBack.failedDbInstanceRoleUpdateUndoesItsOwnChanges.AwsQuery*routing tests): 4,586 tests in 317 classes pass. One is skipped:AppSyncCfnIntegrationTestneeds the AppSync sidecar image, which is not available locally../mvnw checkstyle:check,make partition-checkandmake docs-checkpass.Checklist
./mvnw testpasses locally (the subset above; the full suite was not run)