mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
f08a443ec9
commit
ad1b225591
5 files changed
+727
No files matched your search
+325
@@ -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();
|
||||
}
|
||||
}
|
||||
+111
@@ -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
|
||||
|
||||
Reference in new issue
Block a user