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

* feat(cloudformation): apply AssociatedRoles on RDS clusters and instances

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 #5638

* fix(cloudformation): follow AWS on repeated roles, and check before changing 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.

* fix(cloudformation): undo a failed role update's own changes

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.
This commit is contained in:
David authored and GitHub committed 2026-10-15 06:59:49 -04:00
1 parent f08a443ec9
commit ad1b225591
5 files changed
+727

No files matched your search

+10
View File
@@ -365,6 +365,16 @@ references in `MasterUsername` and `MasterUserPassword` during resource creation
Dynamic-reference expansion is currently scoped to these RDS credential properties.
## RDS Associated Roles
`AWS::RDS::DBCluster` and `AWS::RDS::DBInstance` apply `AssociatedRoles` through the same checks as
`AddRoleToDBCluster` / `AddRoleToDBInstance` (see [RDS IAM role associations](rds.md#iam-role-associations)):
- `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`.
- On create, the roles are associated with the new cluster or instance. A list RDS refuses (a missing `RoleArn`, an instance role without `FeatureName`, or a cluster role named twice) fails the resource, and the new database is deleted again. As in AWS, an instance role or feature the list repeats is skipped, not refused. A renamed instance keeps its predecessor until the new one has its roles.
- On `UpdateStack` (no interruption, as in AWS), a cluster's list is checked first, then the roles the template gained are added, and the roles the previous template declared and the new one drops are removed. A role associated outside the stack is kept while the template leaves it out; declaring it fails the update with `DBClusterRoleAlreadyExists`, as in AWS. If any step of an update is refused, the roles it already added are removed and the ones it removed are put back, so the database matches what the stack still records and a retry starts from there. A rolled-back cluster replacement restores the record of which roles the stack declared.
- An Aurora cluster member (`AWS::RDS::DBInstance` with `DBClusterIdentifier`) cannot carry `AssociatedRoles`: the schema documents the property as not applicable there, since the cluster manages its roles. The resource fails with a `ValidationError` pointing at the cluster's `AssociatedRoles`.
## Account-Aware Provisioning
Resources provisioned by `CreateStack` / `UpdateStack` land in the **caller's account** namespace
@@ -1,7 +1,9 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
@@ -14,6 +16,7 @@ import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
import io.github.hectorvent.floci.services.rds.model.DbProxy;
import io.github.hectorvent.floci.services.rds.model.DbProxyAuth;
import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;
import io.github.hectorvent.floci.services.rds.model.DbRoleAssociation;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -85,6 +88,18 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
*/
static final String DB_CLUSTER_CREATE_ONLY_PRIOR_ATTR = "__FlociDbClusterCreateOnlyPrior";
/**
* The {@code AssociatedRoles} the template declared at the last successful provision, a JSON
* array of {@code {RoleArn, FeatureName}}. An update removes only roles this record names and
* the template no longer does, so a role associated outside the stack is left in place.
*/
static final String ASSOCIATED_ROLES_ATTR = "__FlociAssociatedRoles";
/**
* The roles record an update overwrote, kept until the update commits so a rollback that puts
* the prior cluster back puts its record back too; an empty value means there was none.
*/
static final String ASSOCIATED_ROLES_PRIOR_ATTR = "__FlociAssociatedRolesPrior";
private final RdsService rdsService;
private final CfnDynamicReferences dynamicReferences;
@@ -163,6 +178,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
@Override
public void clearUpdate(StackResource resource) {
resource.getAttributes().remove(DB_CLUSTER_CREATE_ONLY_PRIOR_ATTR);
resource.getAttributes().remove(ASSOCIATED_ROLES_PRIOR_ATTR);
ReplacementCleanup.clear(resource);
}
@@ -184,6 +200,14 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
resource.getAttributes().put(DB_CLUSTER_CREATE_ONLY_ATTR, priorRecord);
}
}
String priorRoles = resource.getAttributes().remove(ASSOCIATED_ROLES_PRIOR_ATTR);
if (priorRoles != null) {
if (priorRoles.isEmpty()) {
resource.getAttributes().remove(ASSOCIATED_ROLES_ATTR);
} else {
resource.getAttributes().put(ASSOCIATED_ROLES_ATTR, priorRoles);
}
}
return replaced;
}
@@ -315,7 +339,16 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
// threading them through an UpdateStack is a separate change: DBInstanceClass and
// AllocatedStorage are update-in-place on the AWS::RDS::DBInstance schema while Engine is
// create-only, so passing them needs the replacement handling the DB cluster arm has.
List<DbRoleAssociation> roles = parseAssociatedRoles(props, engine);
String clusterMember = resolveOptional(props, "DBClusterIdentifier", engine);
if (!roles.isEmpty() && clusterMember != null && !clusterMember.isBlank()) {
// The schema: "Amazon Aurora: Not applicable. The associated roles are managed by the DB cluster."
throw new AwsException("ValidationError", "AssociatedRoles is not applicable to DB instance " + id
+ ", a member of DB cluster " + clusterMember
+ "; set AssociatedRoles on the AWS::RDS::DBCluster instead.", 400);
}
DbInstance instance = sameNameExistingResource(priorPhysicalId, id, rdsService::getDbInstance);
boolean created = instance == null;
if (instance != null) {
instance = rdsService.modifyDbInstance(
id,
@@ -337,6 +370,11 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
resolveOptional(props, "DBSubnetGroupName", engine),
resolveOptional(props, "DBClusterIdentifier", engine),
null, false, false, null, Map.of(), region);
}
// The roles go on before a renamed instance's predecessor is deleted, so a refused list
// deletes only the new instance and the stack still has the old one to roll back to.
instance = reconcileInstanceRoles(r, instance, roles, created, ctx.isUpdate(), region);
if (created) {
deleteRenamedResource(priorPhysicalId, id, rdsService::deleteDbInstance, "DB instance");
}
r.setPhysicalId(instance.getDbInstanceIdentifier());
@@ -400,6 +438,11 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
LOG.infov("Replacing DB cluster {0} with {1}: {2}", priorPhysicalId, id, replacementReason);
cluster = null;
}
List<DbRoleAssociation> roles = parseAssociatedRoles(props, engine);
if (!roles.isEmpty()) {
rdsService.validateClusterRoleAssociations(id, resolveOptional(props, "Engine", engine), roles);
}
boolean created = cluster == null;
if (cluster != null) {
cluster = rdsService.modifyDbCluster(
id,
@@ -426,6 +469,7 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
serverlessV2MinCapacity, serverlessV2MaxCapacity, serverlessV2SecondsUntilAutoPause,
false, null, engineMode, storageEncrypted);
}
cluster = reconcileClusterRoles(r, cluster, roles, created, ctx.isUpdate(), region);
r.setPhysicalId(cluster.getDbClusterIdentifier());
r.getAttributes().put("DBClusterIdentifier", cluster.getDbClusterIdentifier());
if (cluster.getEndpoint() != null) {
@@ -447,6 +491,241 @@ public class RdsCfnProvisioner implements CfnResourceProvisioner {
ReplacementCleanup.record(r, ctx, attributesBefore);
}
/**
* The template's {@code AssociatedRoles}, in order, with {@code Fn::If} resolved around the list
* and its entries. An absent property, or one that resolves to {@code AWS::NoValue}, is an empty
* list; any other value that is not a list of objects is a {@code ValidationError}, so a
* malformed property never reads as "no roles" and removes the ones the stack declared.
*/
private List<DbRoleAssociation> parseAssociatedRoles(JsonNode props, CloudFormationTemplateEngine engine) {
List<DbRoleAssociation> roles = new ArrayList<>();
if (props == null || !props.has("AssociatedRoles")) {
return roles;
}
JsonNode list = engine.resolveNodeOmittingNoValue(props.get("AssociatedRoles"));
if (list == null || list.isMissingNode()) {
return roles;
}
if (!list.isArray()) {
throw new AwsException("ValidationError", "AssociatedRoles must be a list of roles.", 400);
}
for (JsonNode role : list) {
if (role == null || role.isMissingNode()) {
continue;
}
if (!role.isObject()) {
throw new AwsException("ValidationError",
"Each AssociatedRoles entry must be an object with a RoleArn.", 400);
}
roles.add(new DbRoleAssociation(textOrNull(role.get("RoleArn")), textOrNull(role.get("FeatureName"))));
}
return roles;
}
private static String textOrNull(JsonNode node) {
return node == null || node.isNull() || node.isMissingNode() ? null : node.asText();
}
/**
* Brings a cluster's roles in line with the template. A new cluster takes them all in one step,
* and is deleted again if they are refused, so a failed resource leaves no cluster behind. On an
* update the roles the last template declared and this one does not are removed, and the ones
* this template adds are associated.
*/
private DbCluster reconcileClusterRoles(StackResource r, DbCluster cluster, List<DbRoleAssociation> roles,
boolean created, boolean update, String region) {
String id = cluster.getDbClusterIdentifier();
if (created) {
if (!roles.isEmpty()) {
try {
cluster = rdsService.addRolesToNewDbCluster(region, id, roles);
} catch (AwsException e) {
CfnDeletes.safeDelete("DB cluster", id, () -> rdsService.deleteDbCluster(id, region),
"DBClusterNotFoundFault");
throw e;
}
}
} else {
List<DbRoleAssociation> removed = new ArrayList<>();
List<DbRoleAssociation> added = new ArrayList<>();
try {
List<DbRoleAssociation> current = cluster.getAssociatedRoles();
for (DbRoleAssociation dropped : droppedRoles(r, roles)) {
if (containsRole(current, dropped)) {
cluster = rdsService.removeRoleFromDbCluster(region, id, dropped.getRoleArn(),
dropped.getFeatureName());
removed.add(dropped);
}
}
// A role the last template already declared is left as it is. One the template newly
// declares is always added, so a role attached by hand first fails the update with
// DBClusterRoleAlreadyExists, as it does on AWS.
List<DbRoleAssociation> declared = readAssociatedRoles(r.getAttributes().get(ASSOCIATED_ROLES_ATTR));
for (DbRoleAssociation role : roles) {
if (!containsRole(declared, role) || !containsRole(cluster.getAssociatedRoles(), role)) {
cluster = rdsService.addRoleToDbCluster(region, id, role.getRoleArn(), role.getFeatureName());
added.add(role);
}
}
} catch (AwsException e) {
undoClusterRoleChanges(id, region, added, removed);
throw e;
}
}
recordAssociatedRoles(r, roles, update);
return cluster;
}
/** As {@link #reconcileClusterRoles}, for an instance; each role is added on its own. */
private DbInstance reconcileInstanceRoles(StackResource r, DbInstance instance, List<DbRoleAssociation> roles,
boolean created, boolean update, String region) {
String id = instance.getDbInstanceIdentifier();
if (created) {
try {
for (DbRoleAssociation role : roles) {
instance = addInstanceRole(instance, id, role, region);
}
} catch (AwsException e) {
rdsService.deleteDbInstance(id, region);
throw e;
}
} else {
List<DbRoleAssociation> removed = new ArrayList<>();
List<DbRoleAssociation> added = new ArrayList<>();
try {
List<DbRoleAssociation> current = instance.getAssociatedRoles();
for (DbRoleAssociation dropped : droppedRoles(r, roles)) {
if (containsRole(current, dropped)) {
instance = rdsService.removeRoleFromDbInstance(region, id, dropped.getRoleArn(),
dropped.getFeatureName());
removed.add(dropped);
}
}
for (DbRoleAssociation role : roles) {
if (!containsRole(instance.getAssociatedRoles(), role)) {
instance = addInstanceRole(instance, id, role, region);
if (containsRole(instance.getAssociatedRoles(), role)) {
added.add(role);
}
}
}
} catch (AwsException e) {
undoInstanceRoleChanges(id, region, added, removed);
throw e;
}
}
recordAssociatedRoles(r, roles, update);
return instance;
}
/**
* Puts a cluster's roles back as they were before a failed update: the roles it added are
* removed and the ones it removed are added again, so the cluster matches the record the stack
* restores and a retry starts from there. A step that fails here is logged and the rest go on,
* so the original refusal is what the stack reports.
*/
private void undoClusterRoleChanges(String id, String region, List<DbRoleAssociation> added,
List<DbRoleAssociation> removed) {
for (DbRoleAssociation role : added) {
undoStep(() -> rdsService.removeRoleFromDbCluster(region, id, role.getRoleArn(), role.getFeatureName()),
"remove", role, "DB cluster", id);
}
for (DbRoleAssociation role : removed) {
undoStep(() -> rdsService.addRoleToDbCluster(region, id, role.getRoleArn(), role.getFeatureName()),
"restore", role, "DB cluster", id);
}
}
/** As {@link #undoClusterRoleChanges}, for an instance. */
private void undoInstanceRoleChanges(String id, String region, List<DbRoleAssociation> added,
List<DbRoleAssociation> removed) {
for (DbRoleAssociation role : added) {
undoStep(() -> rdsService.removeRoleFromDbInstance(region, id, role.getRoleArn(), role.getFeatureName()),
"remove", role, "DB instance", id);
}
for (DbRoleAssociation role : removed) {
undoStep(() -> rdsService.addRoleToDbInstance(region, id, role.getRoleArn(), role.getFeatureName()),
"restore", role, "DB instance", id);
}
}
private static void undoStep(Runnable step, String verb, DbRoleAssociation role, String kind, String id) {
try {
step.run();
} catch (AwsException e) {
LOG.warnv("Could not {0} role {1} on {2} {3} while undoing a failed update: {4}",
verb, role.getRoleArn(), kind, id, e.getMessage());
}
}
/**
* Associates one instance role. AWS's {@code AWS::RDS::DBInstance} handler ignores
* {@code DBInstanceRoleAlreadyExists}, so a role or feature the list repeats, or one already on
* the instance, is skipped rather than failing the resource.
*/
private DbInstance addInstanceRole(DbInstance instance, String id, DbRoleAssociation role, String region) {
try {
return rdsService.addRoleToDbInstance(region, id, role.getRoleArn(), role.getFeatureName());
} catch (AwsException e) {
if ("DBInstanceRoleAlreadyExists".equals(e.getErrorCode())) {
return instance;
}
throw e;
}
}
/** The roles the last template declared that this one does not. */
private static List<DbRoleAssociation> droppedRoles(StackResource r, List<DbRoleAssociation> roles) {
List<DbRoleAssociation> dropped = new ArrayList<>();
for (DbRoleAssociation previous : readAssociatedRoles(r.getAttributes().get(ASSOCIATED_ROLES_ATTR))) {
if (!containsRole(roles, previous)) {
dropped.add(previous);
}
}
return dropped;
}
/** Whether {@code roles} holds {@code role}: the same RoleArn for the same feature, or both without one. */
private static boolean containsRole(List<DbRoleAssociation> roles, DbRoleAssociation role) {
return roles.stream().anyMatch(candidate -> Objects.equals(candidate.getRoleArn(), role.getRoleArn())
&& Objects.equals(blankToNull(candidate.getFeatureName()), blankToNull(role.getFeatureName())));
}
private static String blankToNull(String value) {
return value == null || value.isBlank() ? null : value;
}
private static void recordAssociatedRoles(StackResource r, List<DbRoleAssociation> roles, boolean update) {
String priorRecord = r.getAttributes().get(ASSOCIATED_ROLES_ATTR);
if (update && !r.getAttributes().containsKey(ASSOCIATED_ROLES_PRIOR_ATTR)) {
r.getAttributes().put(ASSOCIATED_ROLES_PRIOR_ATTR, priorRecord == null ? "" : priorRecord);
}
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());
}
private static List<DbRoleAssociation> readAssociatedRoles(String raw) {
List<DbRoleAssociation> roles = new ArrayList<>();
if (raw == null || raw.isBlank()) {
return roles;
}
try {
for (JsonNode entry : MAPPER.readTree(raw)) {
roles.add(new DbRoleAssociation(entry.path("RoleArn").asText(null),
entry.path("FeatureName").asText(null)));
}
} catch (JsonProcessingException e) {
LOG.warnv("Ignoring unreadable AssociatedRoles record: {0}", raw);
}
return roles;
}
/**
* Why the template's createOnly properties call for a replacement of the cluster on file, or
* null when they describe it. {@code EngineMode}, {@code StorageEncrypted} and
@@ -9782,6 +9782,8 @@ public class RdsService implements Resettable, ResourceProvider {
*/
public synchronized DbCluster addRolesToNewDbCluster(String region, String clusterId,
List<DbRoleAssociation> roles) {
// The features are checked below against the cluster's own engine.
validateClusterRoleAssociations(clusterId, null, roles);
String effectiveRegion = effectiveRegion(region);
DbCluster cluster = getDbCluster(clusterId, effectiveRegion);
requireAvailableCluster(cluster, clusterId);
@@ -0,0 +1,325 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.RdsMockProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.path.xml.XmlPath;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code AssociatedRoles} on {@code AWS::RDS::DBCluster} and {@code AWS::RDS::DBInstance}: the
* roles are associated on create, an update adds the ones the template gained and removes the
* ones it dropped (leaving roles associated outside the stack alone), and a list RDS refuses fails
* the resource without leaving the database behind.
*/
@QuarkusTest
@TestProfile(RdsMockProfile.class)
class CloudFormationRdsAssociatedRolesIntegrationTest {
private static final String CFN_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261014/us-east-1/cloudformation/aws4_request";
private static final String RDS_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261014/us-east-1/rds/aws4_request, "
+ "SignedHeaders=content-type;host, Signature=test";
private static final String EXPORT_ROLE = "arn:aws:iam::000000000000:role/cfn-rds-export";
private static final String LAMBDA_ROLE = "arn:aws:iam::000000000000:role/cfn-rds-lambda";
private static final String MANUAL_ROLE = "arn:aws:iam::000000000000:role/cfn-rds-manual";
@Test
void rolesFollowTheTemplateAcrossCreateAndUpdate() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-" + suffix;
String clusterId = "cfn-roles-cluster-" + suffix;
String instanceId = "cfn-roles-db-" + suffix;
try {
stack("CreateStack", stackName, template(clusterId, instanceId, """
[{"RoleArn": {"Fn::GetAtt": ["ImportRole", "Arn"]}, "FeatureName": "s3Import"}]
"""));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String importRole = clusterRoles(clusterId).get(0);
assertTrue(importRole.endsWith(":role/cfn-rds-import-" + suffix), importRole);
assertEquals(List.of(LAMBDA_ROLE), instanceRoles(instanceId));
// A role associated outside the stack is not the stack's to remove.
rds("AddRoleToDBCluster").formParam("DBClusterIdentifier", clusterId)
.formParam("RoleArn", MANUAL_ROLE)
.when().post("/").then().statusCode(200);
stack("UpdateStack", stackName, template(clusterId, instanceId, """
[{"RoleArn": "%s", "FeatureName": "s3Export"}]
""".formatted(EXPORT_ROLE)));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertThat(clusterRoles(clusterId), containsInAnyOrder(MANUAL_ROLE, EXPORT_ROLE));
assertEquals(List.of(LAMBDA_ROLE), instanceRoles(instanceId));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void aRefusedRoleListLeavesNoClusterBehind() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-twice-" + suffix;
String clusterId = "cfn-roles-twice-" + suffix;
try {
stack("CreateStack", stackName, """
{"Resources": {"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": [{"RoleArn": "%s"}, {"RoleArn": "%s"}]}}}}
""".formatted(clusterId, EXPORT_ROLE, EXPORT_ROLE));
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
assertTrue(state.status().startsWith("ROLLBACK") || state.status().equals("CREATE_FAILED"),
state.status());
rds("DescribeDBClusters").formParam("DBClusterIdentifier", clusterId)
.when().post("/").then().statusCode(404);
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void anAuroraMemberCannotCarryItsOwnRoles() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-member-" + suffix;
String clusterId = "cfn-roles-member-cluster-" + suffix;
try {
stack("CreateStack", stackName, """
{"Resources": {
"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123"}},
"Member": {"Type": "AWS::RDS::DBInstance", "Properties": {
"DBClusterIdentifier": {"Ref": "Cluster"}, "Engine": "aurora-postgresql",
"DBInstanceClass": "db.r6g.large",
"AssociatedRoles": [{"RoleArn": "%s", "FeatureName": "s3Import"}]}}}}
""".formatted(clusterId, EXPORT_ROLE));
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stackName);
assertTrue(state.status().startsWith("ROLLBACK") || state.status().equals("CREATE_FAILED"),
state.status());
String events = stack("DescribeStackEvents", stackName, null);
assertTrue(events.contains("AssociatedRoles is not applicable"), events);
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void conditionsAroundTheListAndItsEntriesAreResolved() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-if-" + suffix;
String listCluster = "cfn-roles-if-list-" + suffix;
String entryCluster = "cfn-roles-if-entry-" + suffix;
try {
stack("CreateStack", stackName, """
{"Conditions": {"Yes": {"Fn::Equals": ["a", "a"]}, "No": {"Fn::Equals": ["a", "b"]}},
"Resources": {
"ListCluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": {"Fn::If": ["Yes", [{"RoleArn": "%s", "FeatureName": "s3Export"}],
{"Ref": "AWS::NoValue"}]}}},
"EntryCluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": [
{"Fn::If": ["Yes", {"RoleArn": "%s", "FeatureName": "Lambda"}, {"Ref": "AWS::NoValue"}]},
{"Fn::If": ["No", {"RoleArn": "%s"}, {"Ref": "AWS::NoValue"}]}]}}}}
""".formatted(listCluster, EXPORT_ROLE, entryCluster, LAMBDA_ROLE, MANUAL_ROLE));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(List.of(EXPORT_ROLE), clusterRoles(listCluster));
assertEquals(List.of(LAMBDA_ROLE), clusterRoles(entryCluster));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void anUpdateWithAMalformedOrRepeatedListKeepsTheRoles() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-bad-update-" + suffix;
String clusterId = "cfn-roles-bad-update-" + suffix;
String cluster = """
{"Resources": {"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": %s}}}}
""";
try {
stack("CreateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\", \"FeatureName\": \"s3Export\"}]".formatted(EXPORT_ROLE)));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
// A value that is not a list is refused, not read as "no roles".
stack("UpdateStack", stackName, cluster.formatted(clusterId, "\"%s\"".formatted(LAMBDA_ROLE)));
assertTrue(CfnStackWaits.awaitTerminal(stackName).status().startsWith("UPDATE_ROLLBACK"));
assertEquals(List.of(EXPORT_ROLE), clusterRoles(clusterId));
// A role named twice is refused on an update as on a create, before anything changes.
stack("UpdateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}, {\"RoleArn\": \"%s\"}]".formatted(LAMBDA_ROLE, LAMBDA_ROLE)));
assertTrue(CfnStackWaits.awaitTerminal(stackName).status().startsWith("UPDATE_ROLLBACK"));
assertEquals(List.of(EXPORT_ROLE), clusterRoles(clusterId));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void aRepeatedInstanceRoleIsSkippedAsAwsDoes() {
// AWS's DBInstance handler ignores DBInstanceRoleAlreadyExists, so a repeated role or
// feature does not fail the resource.
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-instance-twice-" + suffix;
String instanceId = "cfn-roles-instance-twice-" + suffix;
try {
stack("CreateStack", stackName, """
{"Resources": {"Database": {"Type": "AWS::RDS::DBInstance", "Properties": {
"DBInstanceIdentifier": "%s", "Engine": "postgres", "DBInstanceClass": "db.t3.micro",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123", "AllocatedStorage": 20,
"AssociatedRoles": [{"RoleArn": "%s", "FeatureName": "Lambda"},
{"RoleArn": "%s", "FeatureName": "Lambda"},
{"RoleArn": "%s", "FeatureName": "Lambda"}]}}}}
""".formatted(instanceId, LAMBDA_ROLE, LAMBDA_ROLE, EXPORT_ROLE));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(List.of(LAMBDA_ROLE), instanceRoles(instanceId));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void declaringARoleAttachedByHandFailsTheClusterUpdate() {
// On AWS a cluster update that newly declares a role already attached outside the stack
// fails with DBClusterRoleAlreadyExists.
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-hand-" + suffix;
String clusterId = "cfn-roles-hand-" + suffix;
String cluster = """
{"Resources": {"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": %s}}}}
""";
try {
stack("CreateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}]".formatted(EXPORT_ROLE)));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
rds("AddRoleToDBCluster").formParam("DBClusterIdentifier", clusterId)
.formParam("RoleArn", MANUAL_ROLE)
.when().post("/").then().statusCode(200);
stack("UpdateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}, {\"RoleArn\": \"%s\"}]".formatted(EXPORT_ROLE, MANUAL_ROLE)));
assertTrue(CfnStackWaits.awaitTerminal(stackName).status().startsWith("UPDATE_ROLLBACK"));
String events = stack("DescribeStackEvents", stackName, null);
assertTrue(events.contains("already associated"), events);
assertThat(clusterRoles(clusterId), containsInAnyOrder(EXPORT_ROLE, MANUAL_ROLE));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
@Test
void aFailedUpdateUndoesItsOwnRoleChanges() {
// The update removes EXPORT_ROLE and adds LAMBDA_ROLE before MANUAL_ROLE, attached by hand,
// is refused. Both changes are undone, so the cluster matches the record the stack keeps
// and a retry without MANUAL_ROLE goes through.
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-rds-roles-undo-" + suffix;
String clusterId = "cfn-roles-undo-" + suffix;
String cluster = """
{"Resources": {"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": %s}}}}
""";
try {
stack("CreateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}]".formatted(EXPORT_ROLE)));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
rds("AddRoleToDBCluster").formParam("DBClusterIdentifier", clusterId)
.formParam("RoleArn", MANUAL_ROLE)
.when().post("/").then().statusCode(200);
stack("UpdateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}, {\"RoleArn\": \"%s\"}]".formatted(LAMBDA_ROLE, MANUAL_ROLE)));
assertTrue(CfnStackWaits.awaitTerminal(stackName).status().startsWith("UPDATE_ROLLBACK"));
assertThat(clusterRoles(clusterId), containsInAnyOrder(EXPORT_ROLE, MANUAL_ROLE));
stack("UpdateStack", stackName, cluster.formatted(clusterId,
"[{\"RoleArn\": \"%s\"}, {\"RoleArn\": \"%s\"}]".formatted(EXPORT_ROLE, LAMBDA_ROLE)));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertThat(clusterRoles(clusterId), containsInAnyOrder(EXPORT_ROLE, LAMBDA_ROLE, MANUAL_ROLE));
} finally {
stack("DeleteStack", stackName, null);
CfnStackWaits.awaitStackDeleted(stackName);
}
}
private static String template(String clusterId, String instanceId, String clusterRoles) {
return """
{"Resources": {
"ImportRole": {"Type": "AWS::IAM::Role", "Properties": {
"RoleName": "cfn-rds-import-%s",
"AssumeRolePolicyDocument": {"Version": "2012-10-17", "Statement": [{"Effect": "Allow",
"Principal": {"Service": "rds.amazonaws.com"}, "Action": "sts:AssumeRole"}]}}},
"Cluster": {"Type": "AWS::RDS::DBCluster", "Properties": {
"DBClusterIdentifier": "%s", "Engine": "aurora-postgresql",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123",
"AssociatedRoles": %s}},
"Database": {"Type": "AWS::RDS::DBInstance", "Properties": {
"DBInstanceIdentifier": "%s", "Engine": "postgres", "DBInstanceClass": "db.t3.micro",
"MasterUsername": "dbadmin", "MasterUserPassword": "password123", "AllocatedStorage": 20,
"AssociatedRoles": [{"RoleArn": "%s", "FeatureName": "Lambda"}]}}}}
""".formatted(clusterId.substring(clusterId.lastIndexOf('-') + 1), clusterId, clusterRoles.strip(),
instanceId, LAMBDA_ROLE);
}
private static List<String> clusterRoles(String clusterId) {
XmlPath xml = rds("DescribeDBClusters").formParam("DBClusterIdentifier", clusterId)
.when().post("/").then().statusCode(200).extract().xmlPath();
return xml.getList("**.findAll { it.name() == 'DBClusterRole' }.RoleArn", String.class);
}
private static List<String> instanceRoles(String instanceId) {
XmlPath xml = rds("DescribeDBInstances").formParam("DBInstanceIdentifier", instanceId)
.when().post("/").then().statusCode(200).extract().xmlPath();
return xml.getList("**.findAll { it.name() == 'DBInstanceRole' }.RoleArn", String.class);
}
private static RequestSpecification rds(String action) {
return given().header("Authorization", RDS_AUTH)
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("Version", "2014-10-31");
}
private static String stack(String action, String stackName, String template) {
RequestSpecification request = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", action)
.formParam("StackName", stackName);
if (template != null) {
request.formParam("TemplateBody", template);
}
return request.when().post("/").then().statusCode(200).extract().asString();
}
}
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
import io.github.hectorvent.floci.services.rds.model.DbProxy;
import io.github.hectorvent.floci.services.rds.model.DbProxyAuth;
import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;
import io.github.hectorvent.floci.services.rds.model.DbRoleAssociation;
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
@@ -157,6 +158,30 @@ class RdsCfnProvisionerTest {
null, false, false, null, Map.of(), "us-east-1");
}
@Test
void deletesANewDbInstanceWhoseAssociatedRolesAreRefused() {
DbInstance instance = mock(DbInstance.class);
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
when(rdsService.createDbInstance(any(), any(), any(), any(), any(), any(), any(),
anyInt(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), anyBoolean(),
any(), anyMap(), nullable(String.class))).thenReturn(instance);
when(rdsService.addRoleToDbInstance("us-east-1", "mydb", "arn:aws:iam::000000000000:role/a", "s3Import"))
.thenReturn(instance);
when(rdsService.addRoleToDbInstance("us-east-1", "mydb", "arn:aws:iam::000000000000:role/b", "s3Import"))
.thenThrow(new AwsException("InvalidParameterValue", "s3Import is not supported here", 400));
StackResource r = provision("Db", "AWS::RDS::DBInstance", """
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
"MasterUserPassword":"secret","AssociatedRoles":[
{"RoleArn":"arn:aws:iam::000000000000:role/a","FeatureName":"s3Import"},
{"RoleArn":"arn:aws:iam::000000000000:role/b","FeatureName":"s3Import"}]}
""");
assertEquals("CREATE_FAILED", r.getStatus());
assertTrue(r.getStatusReason().contains("s3Import is not supported here"), r.getStatusReason());
verify(rdsService).deleteDbInstance("mydb", "us-east-1");
}
@Test
void provisionsDbInstanceInStackRegion() {
DbInstance instance = mock(DbInstance.class);
@@ -1452,6 +1477,92 @@ class RdsCfnProvisionerTest {
assertFalse(provisioner.rollbackUpdate(r), "there is no replacement to undo");
}
@Test
void rolledBackDbClusterReplacementPutsTheRolesRecordBack() {
// The replacement records its template's roles; rolling it back to the prior cluster must
// bring back the prior record too, or the next update removes roles by the wrong list.
priorUnnamedCluster("provisioned", false);
createDbClusterEchoesRequestedId();
when(rdsService.addRolesToNewDbCluster(any(), any(), anyList())).thenAnswer(invocation -> {
DbCluster withRoles = mock(DbCluster.class);
doReturn(invocation.getArgument(1)).when(withRoles).getDbClusterIdentifier();
return withRoles;
});
String priorRecord = "[{\"RoleArn\":\"arn:aws:iam::000000000000:role/prior\"}]";
StackResource r = provisionExisting("Cluster", "AWS::RDS::DBCluster", """
{"Engine":"aurora-postgresql","EngineMode":"serverless",
"AssociatedRoles":[{"RoleArn":"arn:aws:iam::000000000000:role/replacement"}]}
""", "us-east-1", PRIOR_CLUSTER,
Map.of("DBClusterIdentifier", PRIOR_CLUSTER, "__FlociAssociatedRoles", priorRecord));
assertNotEquals(PRIOR_CLUSTER, r.getPhysicalId());
assertTrue(r.getAttributes().get("__FlociAssociatedRoles").contains("role/replacement"));
assertTrue(provisioner.rollbackUpdate(r));
assertEquals(priorRecord, r.getAttributes().get("__FlociAssociatedRoles"));
}
@Test
void failedDbInstanceRoleUpdateUndoesItsOwnChanges() {
// The update removes the declared s3Import role, adds a Lambda role, then the next add is
// refused: the Lambda role comes off again and the s3Import role goes back on.
String old = "arn:aws:iam::000000000000:role/old";
String lambda = "arn:aws:iam::000000000000:role/lambda";
String refused = "arn:aws:iam::000000000000:role/refused";
DbInstance before = mock(DbInstance.class);
when(before.getDbInstanceIdentifier()).thenReturn("mydb");
when(before.getAssociatedRoles()).thenReturn(List.of(new DbRoleAssociation(old, "s3Import")));
DbInstance withoutOld = mock(DbInstance.class);
when(withoutOld.getDbInstanceIdentifier()).thenReturn("mydb");
DbInstance withLambda = mock(DbInstance.class);
when(withLambda.getDbInstanceIdentifier()).thenReturn("mydb");
when(withLambda.getAssociatedRoles()).thenReturn(List.of(new DbRoleAssociation(lambda, "Lambda")));
when(rdsService.getDbInstance("mydb")).thenReturn(before);
when(rdsService.modifyDbInstance(eq("mydb"), any(), any(), any())).thenReturn(before);
when(rdsService.removeRoleFromDbInstance("us-east-1", "mydb", old, "s3Import")).thenReturn(withoutOld);
when(rdsService.addRoleToDbInstance("us-east-1", "mydb", lambda, "Lambda")).thenReturn(withLambda);
when(rdsService.addRoleToDbInstance("us-east-1", "mydb", refused, "s3Export"))
.thenThrow(new AwsException("InvalidDBInstanceState", "not available", 400));
StackResource r = provisionExisting("Db", "AWS::RDS::DBInstance", """
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
"MasterUserPassword":"secret","AssociatedRoles":[
{"RoleArn":"arn:aws:iam::000000000000:role/lambda","FeatureName":"Lambda"},
{"RoleArn":"arn:aws:iam::000000000000:role/refused","FeatureName":"s3Export"}]}
""", "us-east-1", "mydb", Map.of("DBInstanceIdentifier", "mydb",
"__FlociAssociatedRoles", "[{\"RoleArn\":\"" + old + "\",\"FeatureName\":\"s3Import\"}]"));
assertEquals("CREATE_FAILED", r.getStatus());
InOrder undo = inOrder(rdsService);
undo.verify(rdsService).removeRoleFromDbInstance("us-east-1", "mydb", lambda, "Lambda");
undo.verify(rdsService).addRoleToDbInstance("us-east-1", "mydb", old, "s3Import");
verify(rdsService, never()).deleteDbInstance(any(), any());
}
@Test
void renamedDbInstanceKeepsItsPredecessorWhenTheNewRolesAreRefused() {
// The roles go on before the renamed instance's predecessor is deleted, so a refused list
// costs only the new instance.
DbInstance renamed = mock(DbInstance.class);
when(renamed.getDbInstanceIdentifier()).thenReturn("newdb");
when(rdsService.createDbInstance(any(), any(), any(), any(), any(), any(), any(),
anyInt(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), anyBoolean(),
any(), anyMap(), nullable(String.class))).thenReturn(renamed);
when(rdsService.addRoleToDbInstance(any(), eq("newdb"), any(), any()))
.thenThrow(new AwsException("InvalidParameterValue", "feature not supported", 400));
StackResource r = provisionExisting("Db", "AWS::RDS::DBInstance", """
{"DBInstanceIdentifier":"newdb","Engine":"postgres","MasterUsername":"admin",
"MasterUserPassword":"secret",
"AssociatedRoles":[{"RoleArn":"arn:aws:iam::000000000000:role/a","FeatureName":"s3Import"}]}
""", "us-east-1", "olddb", Map.of("DBInstanceIdentifier", "olddb"));
assertEquals("CREATE_FAILED", r.getStatus());
verify(rdsService).deleteDbInstance("newdb", "us-east-1");
verify(rdsService, never()).deleteDbInstance("olddb");
verify(rdsService, never()).deleteDbInstance("olddb", "us-east-1");
}
@Test
void failedLaterUpdateRollsBackDbClusterReplacementToThePriorCluster() {
// A later resource failing the stack update rolls the replacement back: the resource names