mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): give a DB instance its security groups and encryption (#7867)
An AWS::RDS::DBInstance created through CloudFormation never received its VPCSecurityGroups or StorageEncrypted, so describe-db-instances showed neither. Both now reach the instance on create. A stack update that changes the groups moves the instance to them in place, and one that drops them puts a standalone instance on its VPC's default group, as AWS does; a cluster member keeps its groups. A rollback puts the previous groups back. StorageEncrypted can only be set at create, so changing or dropping it replaces the instance: the old one is deleted once the update commits and kept when it rolls back. A custom-named instance refuses the change, as on AWS. Part of #7603 The rollback test of a security group change now starts from two groups and from none, as well as from one, so a saved list that kept only its last group, or saved nothing for an instance that had none, fails it. The line that saves the list now creates the array first, which reads more plainly. The docs and the encryption record's Javadoc now say that an instance created before StorageEncrypted was applied, whose template sets it through a parameter, does not see a change made only through that parameter on its first update, as Ec2Tags.ownedKeys already notes for tags. The DB instance and DB cluster arms refuse a custom-named replacement through CfnExceptions.customNamedReplacement, and the private copy is gone. The DB cluster's createOnly record and the AssociatedRoles record are CreateOnlyInputs constants now, beside the DB instance's encryption record, so all three are read, written, put back on a rollback and cleared on commit through it. The attribute names are unchanged, so a stack created before keeps its records, and an update that leaves the cluster record as it was still saves no copy of it. One thing changes with it: the roles record an update overwrites is now saved every time, where it used to be saved only when no saved copy was there. A committed in-place update used to leave that copy on the resource, so a stack from then could have its roles record rolled back to an older one by the first update that fails; a test covers it. The encryption record's Javadoc and the docs now say that the previous template resolves on the update, so a change made only through a parameter, or anything else the update changes, is not seen. An update whose template changed a cluster member's VPCSecurityGroups moved the member to the new groups. On AWS the handler sends that change to ModifyDBInstance and RDS refuses it for a member, whose groups are its cluster's, so the update fails. It now fails the same way, with InvalidParameterCombination and RDS's message, before anything on the instance changes. The change is read from the template, as AWS's handler reads it, so a member whose list stays the same still updates, including one a stack created before Floci applied the groups. Refusing the property outright would fail a list that matches the cluster's, which the schema allows.
This commit is contained in:
1 parent
3270066923
commit
3d3e94dcc6
10 files changed
+888
-168
No files matched your search
+20
-32
@@ -383,38 +383,29 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
String stackName = "cfn-ec2-lt-profile-" + Long.toString(System.nanoTime(), 36);
|
||||
try (CloudFormationClient cloudFormation = AwsTestClients.create(CloudFormationClient.builder(), "test",
|
||||
Region.of(REGION))) {
|
||||
cloudFormation.createStack(request -> request.stackName(stackName)
|
||||
.templateBody(launchTemplateProfileStack("First", false)));
|
||||
try {
|
||||
Stack created = CfnStackWaits.awaitStatus(cloudFormation, stackName, "CREATE_COMPLETE");
|
||||
String instanceId = CfnStackWaits.output(created, "InstanceId");
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(created, "FirstArn"));
|
||||
Stack created = CfnStackWaits.createStack(cloudFormation, stackName,
|
||||
launchTemplateProfileStack("First", false), "CREATE_COMPLETE");
|
||||
String instanceId = CfnStackWaits.output(created, "InstanceId");
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(created, "FirstArn"));
|
||||
|
||||
// A template version naming another profile gives the same instance that profile, a
|
||||
// failed update puts it back, and a version naming none leaves the instance without one.
|
||||
Stack changed = updateStack(cloudFormation, stackName, launchTemplateProfileStack("Second", false),
|
||||
"UPDATE_COMPLETE");
|
||||
assertEquals(instanceId, CfnStackWaits.output(changed, "InstanceId"));
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(changed, "SecondArn"));
|
||||
Stack rolledBack = updateStack(cloudFormation, stackName, launchTemplateProfileStack("First", true),
|
||||
"UPDATE_ROLLBACK_COMPLETE");
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(rolledBack, "SecondArn"));
|
||||
Stack cleared = updateStack(cloudFormation, stackName, launchTemplateProfileStack(null, false),
|
||||
"UPDATE_COMPLETE");
|
||||
assertEquals(instanceId, CfnStackWaits.output(cleared, "InstanceId"));
|
||||
assertInstanceProfile(instanceId, null);
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stackName, CFN_AUTH);
|
||||
}
|
||||
// A template version naming another profile gives the same instance that profile, a
|
||||
// failed update puts it back, and a version naming none leaves the instance without one.
|
||||
Stack changed = CfnStackWaits.updateStack(cloudFormation, stackName,
|
||||
launchTemplateProfileStack("Second", false), "UPDATE_COMPLETE");
|
||||
assertEquals(instanceId, CfnStackWaits.output(changed, "InstanceId"));
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(changed, "SecondArn"));
|
||||
Stack rolledBack = CfnStackWaits.updateStack(cloudFormation, stackName,
|
||||
launchTemplateProfileStack("First", true), "UPDATE_ROLLBACK_COMPLETE");
|
||||
assertInstanceProfile(instanceId, CfnStackWaits.output(rolledBack, "SecondArn"));
|
||||
Stack cleared = CfnStackWaits.updateStack(cloudFormation, stackName,
|
||||
launchTemplateProfileStack(null, false), "UPDATE_COMPLETE");
|
||||
assertEquals(instanceId, CfnStackWaits.output(cleared, "InstanceId"));
|
||||
assertInstanceProfile(instanceId, null);
|
||||
} finally {
|
||||
CfnStackWaits.deleteStack(stackName, CFN_AUTH);
|
||||
}
|
||||
}
|
||||
|
||||
private static Stack updateStack(CloudFormationClient cloudFormation, String stackName, String template,
|
||||
String expectedStatus) {
|
||||
cloudFormation.updateStack(request -> request.stackName(stackName).templateBody(template));
|
||||
return CfnStackWaits.awaitStatus(cloudFormation, stackName, expectedStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* An instance launched from a launch template whose IamInstanceProfile names the stack's profile
|
||||
* {@code profile}, First or Second, or no profile when it is null. With {@code laterFails}, a
|
||||
@@ -423,10 +414,7 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
private static String launchTemplateProfileStack(String profile, boolean laterFails) {
|
||||
String member = profile == null ? "" : """
|
||||
, "IamInstanceProfile": {"Name": {"Ref": "%s"}}""".formatted(profile);
|
||||
String later = !laterFails ? "" : """
|
||||
,
|
||||
"Later": {"Type": "AWS::EC2::Instance", "DependsOn": "Server", "Properties": {"LaunchTemplate": {
|
||||
"LaunchTemplateId": "lt-0123456789abcdef0", "Version": "1"}}}""";
|
||||
String later = laterFails ? CfnStackWaits.failingResourceAfter("Server") : "";
|
||||
return """
|
||||
{"Resources": {
|
||||
"Role": {"Type": "AWS::IAM::Role", "Properties": {"AssumeRolePolicyDocument": {
|
||||
|
||||
+2
-9
@@ -320,24 +320,17 @@ class CloudFormationEc2MetadataOptionsIntegrationTest {
|
||||
"Outputs": {"InstanceId": {"Value": {"Ref": "Inst"}}}
|
||||
}
|
||||
""".formatted(createdOptions);
|
||||
// The second instance names a launch template that does not exist, so it fails after Inst
|
||||
// has been updated, which it depends on.
|
||||
String failing = """
|
||||
{
|
||||
"Resources": {
|
||||
"Inst": {
|
||||
"Type": "AWS::EC2::Instance",
|
||||
"Properties": {"ImageId": "ami-12345678", "MetadataOptions": {"HttpTokens": "%s"}}
|
||||
},
|
||||
"Later": {
|
||||
"Type": "AWS::EC2::Instance",
|
||||
"DependsOn": "Inst",
|
||||
"Properties": {"LaunchTemplate": {"LaunchTemplateId": "lt-0123456789abcdef0", "Version": "1"}}
|
||||
}
|
||||
}%s
|
||||
},
|
||||
"Outputs": {"InstanceId": {"Value": {"Ref": "Inst"}}}
|
||||
}
|
||||
""".formatted(updatedTokens);
|
||||
""".formatted(updatedTokens, CfnStackWaits.failingResourceAfter("Inst"));
|
||||
try {
|
||||
cfn("CreateStack", stackName, created);
|
||||
String instanceId = output(describeStack(stackName, "CREATE_COMPLETE"), "InstanceId");
|
||||
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.CfnStackWaits;
|
||||
import io.floci.aws.testing.RdsMockProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.rds.RdsClient;
|
||||
import software.amazon.awssdk.services.rds.model.DBInstance;
|
||||
import software.amazon.awssdk.services.rds.model.VpcSecurityGroupMembership;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.floci.aws.testing.CfnStackWaits.output;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@code VPCSecurityGroups} and {@code StorageEncrypted} on {@code AWS::RDS::DBInstance}, read back
|
||||
* through the AWS SDK: the groups follow the template in place, through a failed update and when the
|
||||
* template drops them, and a change of {@code StorageEncrypted} either way replaces the instance.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(RdsMockProfile.class)
|
||||
class CloudFormationRdsDbInstanceIntegrationTest {
|
||||
|
||||
private final String stackName = "cfn-rds-db-" + Long.toString(System.nanoTime(), 36);
|
||||
private CloudFormationClient cloudFormation;
|
||||
private RdsClient rds;
|
||||
|
||||
@BeforeEach
|
||||
void clients() {
|
||||
cloudFormation = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
|
||||
rds = AwsTestClients.create(RdsClient.builder(), "test", Region.US_EAST_1);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteStack() {
|
||||
try {
|
||||
CfnStackWaits.deleteStack(stackName);
|
||||
} finally {
|
||||
cloudFormation.close();
|
||||
rds.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityGroupsFollowTheTemplateThroughChangeRollbackAndRemoval() {
|
||||
Stack created = create(template("First", false, false));
|
||||
String id = output(created, "DbId");
|
||||
assertEquals(List.of(output(created, "FirstId")), groups(id));
|
||||
|
||||
Stack changed = update(template("Second", false, false), "UPDATE_COMPLETE");
|
||||
assertEquals(id, output(changed, "DbId"));
|
||||
assertEquals(List.of(output(changed, "SecondId")), groups(id));
|
||||
|
||||
update(template("First", false, true), "UPDATE_ROLLBACK_COMPLETE");
|
||||
assertEquals(List.of(output(changed, "SecondId")), groups(id));
|
||||
|
||||
// As AWS's handler does, a template that drops the groups puts the instance on its VPC's default group.
|
||||
Stack cleared = update(template(null, false, false), "UPDATE_COMPLETE");
|
||||
assertEquals(id, output(cleared, "DbId"));
|
||||
assertEquals(List.of(defaultGroupOf(instance(id).dbSubnetGroup().vpcId())), groups(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStorageEncryptedChangeReplacesTheInstanceAndAFailedOneKeepsIt() {
|
||||
String id = output(create(template(null, true, false)), "DbId");
|
||||
assertTrue(instance(id).storageEncrypted());
|
||||
|
||||
// Dropping StorageEncrypted asks for an unencrypted instance, which only a new one can be.
|
||||
update(template(null, false, true), "UPDATE_ROLLBACK_COMPLETE");
|
||||
assertEquals(List.of(id), stackInstanceIds(), "the replacement is deleted again");
|
||||
assertTrue(instance(id).storageEncrypted());
|
||||
|
||||
String unencrypted = output(update(template(null, false, false), "UPDATE_COMPLETE"), "DbId");
|
||||
assertNotEquals(id, unencrypted);
|
||||
assertFalse(instance(unencrypted).storageEncrypted());
|
||||
assertEquals(List.of(unencrypted), stackInstanceIds(), "the prior instance is deleted once the update commits");
|
||||
|
||||
String encrypted = output(update(template(null, true, false), "UPDATE_COMPLETE"), "DbId");
|
||||
assertNotEquals(unencrypted, encrypted);
|
||||
assertTrue(instance(encrypted).storageEncrypted());
|
||||
assertEquals(List.of(encrypted), stackInstanceIds());
|
||||
}
|
||||
|
||||
private Stack create(String template) {
|
||||
return CfnStackWaits.createStack(cloudFormation, stackName, template, "CREATE_COMPLETE");
|
||||
}
|
||||
|
||||
private Stack update(String template, String expectedStatus) {
|
||||
return CfnStackWaits.updateStack(cloudFormation, stackName, template, expectedStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Two security groups and a DB instance on {@code group}, First or Second, or on none when it is
|
||||
* null; with {@code StorageEncrypted: true} when {@code encrypted}, and with a resource that fails
|
||||
* after the instance when {@code laterFails}.
|
||||
*/
|
||||
private static String template(String group, boolean encrypted, boolean laterFails) {
|
||||
String groups = group == null ? "" : """
|
||||
, "VPCSecurityGroups": [{"Fn::GetAtt": ["%s", "GroupId"]}]""".formatted(group);
|
||||
return """
|
||||
{"Resources": {
|
||||
"First": {"Type": "AWS::EC2::SecurityGroup", "Properties": {"GroupDescription": "first"}},
|
||||
"Second": {"Type": "AWS::EC2::SecurityGroup", "Properties": {"GroupDescription": "second"}},
|
||||
"Db": {"Type": "AWS::RDS::DBInstance", "Properties": {"Engine": "postgres",
|
||||
"DBInstanceClass": "db.t3.micro", "AllocatedStorage": 20, "MasterUsername": "dbadmin",
|
||||
"MasterUserPassword": "password123"%s%s}}%s
|
||||
}, "Outputs": {
|
||||
"DbId": {"Value": {"Ref": "Db"}},
|
||||
"FirstId": {"Value": {"Fn::GetAtt": ["First", "GroupId"]}},
|
||||
"SecondId": {"Value": {"Fn::GetAtt": ["Second", "GroupId"]}}
|
||||
}}
|
||||
""".formatted(groups, encrypted ? ", \"StorageEncrypted\": true" : "",
|
||||
laterFails ? CfnStackWaits.failingResourceAfter("Db") : "");
|
||||
}
|
||||
|
||||
private DBInstance instance(String id) {
|
||||
return rds.describeDBInstances(request -> request.dbInstanceIdentifier(id)).dbInstances().getFirst();
|
||||
}
|
||||
|
||||
private List<String> groups(String id) {
|
||||
return instance(id).vpcSecurityGroups().stream().map(VpcSecurityGroupMembership::vpcSecurityGroupId).toList();
|
||||
}
|
||||
|
||||
/** The instances this stack's {@code Db} resource has, by the name CloudFormation generates for it. */
|
||||
private List<String> stackInstanceIds() {
|
||||
return rds.describeDBInstances().dbInstances().stream().map(DBInstance::dbInstanceIdentifier)
|
||||
.filter(id -> id.startsWith(stackName + "-db-")).toList();
|
||||
}
|
||||
|
||||
private static String defaultGroupOf(String vpcId) {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
return ec2.describeSecurityGroups(request -> request.filters(
|
||||
filter -> filter.name("vpc-id").values(vpcId),
|
||||
filter -> filter.name("group-name").values("default")))
|
||||
.securityGroups().getFirst().groupId();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,14 +34,17 @@ import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
@@ -135,20 +138,45 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
/** Any {@code createDbInstance} call returns {@code instance}. */
|
||||
private void stubCreateDbInstance(DbInstance instance) {
|
||||
stubCreateDbInstance(invocation -> instance);
|
||||
}
|
||||
|
||||
private void stubCreateDbInstance(Answer<DbInstance> answer) {
|
||||
when(rdsService.createDbInstance(any(), any(), any(), any(), any(), any(), any(),
|
||||
anyInt(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), anyBoolean(),
|
||||
any(), anyMap(), anyList(), nullable(String.class), nullable(String.class),
|
||||
anyBoolean(), any(DbInstanceSettings.class))).thenReturn(instance);
|
||||
anyBoolean(), any(DbInstanceSettings.class))).thenAnswer(answer);
|
||||
}
|
||||
|
||||
/** Any {@code createDbInstance} call returns an instance named {@code id}, which this returns. */
|
||||
private DbInstance stubCreatedInstance(String id) {
|
||||
DbInstance created = dbInstance(id);
|
||||
stubCreateDbInstance(created);
|
||||
return created;
|
||||
}
|
||||
|
||||
/** RdsService has an instance named {@code id} on file and hands it back from an in-place modify. */
|
||||
private DbInstance stubInstanceOnFile(String id) {
|
||||
DbInstance onFile = dbInstance(id);
|
||||
when(rdsService.getDbInstance(id)).thenReturn(onFile);
|
||||
when(rdsService.modifyDbInstance(eq(id), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(DbInstanceSettings.class))).thenReturn(onFile);
|
||||
return onFile;
|
||||
}
|
||||
|
||||
/** A DB instance named {@code id}; stubbed with doReturn so it can be built inside an Answer. */
|
||||
private static DbInstance dbInstance(String id) {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
doReturn(id).when(instance).getDbInstanceIdentifier();
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsDbInstanceWithMappedArgsAndEndpointAttributes() {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
DbInstance instance = stubCreatedInstance("mydb");
|
||||
when(instance.getEndpoint()).thenReturn(new DbEndpoint("mydb.local", 5432));
|
||||
when(instance.getDbInstanceArn()).thenReturn("arn:aws:rds:us-east-1:000000000000:db:mydb");
|
||||
when(instance.getDbiResourceId()).thenReturn("db-ABCDEFGHIJKLMNOP");
|
||||
stubCreateDbInstance(instance);
|
||||
|
||||
StackResource r = provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","EngineVersion":"16",
|
||||
@@ -173,9 +201,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void aDbInstanceTakesItsCaCertificateAndExposesItsDetails() {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
stubCreateDbInstance(instance);
|
||||
DbInstance instance = stubCreatedInstance("mydb");
|
||||
when(rdsService.instanceCertificate(instance)).thenReturn(
|
||||
new RdsService.InstanceCertificate("rds-ca-ecc384-g1", Instant.parse("2121-05-25T23:41:55Z")));
|
||||
|
||||
@@ -195,9 +221,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void deletesANewDbInstanceWhoseAssociatedRolesAreRefused() {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
stubCreateDbInstance(instance);
|
||||
DbInstance instance = stubCreatedInstance("mydb");
|
||||
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"))
|
||||
@@ -217,9 +241,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void provisionsDbInstanceInStackRegion() {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
stubCreateDbInstance(instance);
|
||||
stubCreatedInstance("mydb");
|
||||
|
||||
provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
|
||||
@@ -437,9 +459,7 @@ class RdsCfnProvisionerTest {
|
||||
"{\"username\":\"resolved-user\",\"password\":\"resolved-secret\"}");
|
||||
when(secretsManagerService.getSecretValue(any(), any(), any(), any())).thenReturn(version);
|
||||
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
stubCreateDbInstance(instance);
|
||||
stubCreatedInstance("mydb");
|
||||
|
||||
provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres",
|
||||
@@ -722,9 +742,7 @@ class RdsCfnProvisionerTest {
|
||||
Parameter param = new Parameter("/db/password", "resolved-secure", "SecureString");
|
||||
when(ssmService.getParameter("/db/password", "us-east-1")).thenReturn(param);
|
||||
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
stubCreateDbInstance(instance);
|
||||
stubCreatedInstance("mydb");
|
||||
|
||||
StackResource resource = provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres",
|
||||
@@ -1408,12 +1426,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void updateStackReconcilesExistingDbInstanceInsteadOfRecreating() {
|
||||
DbInstance existing = mock(DbInstance.class);
|
||||
when(rdsService.getDbInstance("mydb")).thenReturn(existing);
|
||||
DbInstance reconciled = mock(DbInstance.class);
|
||||
when(reconciled.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
when(rdsService.modifyDbInstance(eq("mydb"), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(DbInstanceSettings.class))).thenReturn(reconciled);
|
||||
stubInstanceOnFile("mydb");
|
||||
|
||||
StackResource r = provisionUpdate("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
|
||||
@@ -1431,11 +1444,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void updateStackChangesAnExistingDbInstancesCaCertificate() {
|
||||
when(rdsService.getDbInstance("mydb")).thenReturn(mock(DbInstance.class));
|
||||
DbInstance reconciled = mock(DbInstance.class);
|
||||
when(reconciled.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
when(rdsService.modifyDbInstance(eq("mydb"), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(DbInstanceSettings.class))).thenReturn(reconciled);
|
||||
stubInstanceOnFile("mydb");
|
||||
|
||||
provisionUpdate("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
|
||||
@@ -1614,17 +1623,11 @@ class RdsCfnProvisionerTest {
|
||||
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");
|
||||
DbInstance before = stubInstanceOnFile("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");
|
||||
DbInstance withoutOld = dbInstance("mydb");
|
||||
DbInstance withLambda = dbInstance("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(), any(), any(), any(), any(),
|
||||
any(DbInstanceSettings.class))).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"))
|
||||
@@ -1632,7 +1635,7 @@ class RdsCfnProvisionerTest {
|
||||
|
||||
StackResource r = provisionExisting("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","MasterUsername":"admin",
|
||||
"MasterUserPassword":"secret","AssociatedRoles":[
|
||||
"MasterUserPassword":"secret","VPCSecurityGroups":["sg-new"],"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",
|
||||
@@ -1643,15 +1646,16 @@ class RdsCfnProvisionerTest {
|
||||
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());
|
||||
// The groups change last, so the engine, which restores this resource without calling
|
||||
// rollbackUpdate, is right that the instance still has the groups it had.
|
||||
verify(rdsService, never()).setDbInstanceVpcSecurityGroups(any(), 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");
|
||||
stubCreateDbInstance(renamed);
|
||||
stubCreatedInstance("newdb");
|
||||
when(rdsService.addRoleToDbInstance(any(), eq("newdb"), any(), any()))
|
||||
.thenThrow(new AwsException("InvalidParameterValue", "feature not supported", 400));
|
||||
|
||||
@@ -1716,12 +1720,16 @@ class RdsCfnProvisionerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void dbClusterCleanupTreatsAnAlreadyGoneClusterAsDeleted() {
|
||||
void replacementCleanupTreatsAnAlreadyGoneClusterOrInstanceAsDeleted() {
|
||||
doThrow(new AwsException("DBClusterNotFoundFault", "DB cluster gone not found.", 404))
|
||||
.when(rdsService).deleteDbCluster("gone", "us-east-1");
|
||||
doThrow(new AwsException("DBInstanceNotFound", "DB instance gone not found.", 404))
|
||||
.when(rdsService).deleteDbInstance("gone", "us-east-1");
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete("AWS::RDS::DBCluster", "gone", "us-east-1"));
|
||||
assertDoesNotThrow(() -> provisioner.delete("AWS::RDS::DBInstance", "gone", "us-east-1"));
|
||||
verify(rdsService).deleteDbCluster("gone", "us-east-1");
|
||||
verify(rdsService).deleteDbInstance("gone", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -2057,6 +2065,309 @@ class RdsCfnProvisionerTest {
|
||||
verify(rdsService, never()).modifyDbCluster(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
// ── VPCSecurityGroups and StorageEncrypted of AWS::RDS::DBInstance ──
|
||||
|
||||
private static final String ENCRYPTION_ATTR = "__FlociDbInstanceStorageEncrypted";
|
||||
private static final String IN_PLACE_ATTR = "__FlociDbInstancePriorInPlaceUpdate";
|
||||
private static final String PLAIN = "{\"Engine\":\"postgres\"}";
|
||||
private static final String SG_FIRST = "{\"Engine\":\"postgres\",\"VPCSecurityGroups\":[\"sg-first\"]}";
|
||||
private static final String SG_SECOND = "{\"Engine\":\"postgres\",\"VPCSecurityGroups\":[\"sg-second\"]}";
|
||||
private static final String ENCRYPTED = "{\"Engine\":\"postgres\",\"StorageEncrypted\":true}";
|
||||
private static final String MEMBER = "{\"DBClusterIdentifier\":\"cluster\"";
|
||||
|
||||
/** What one {@code createDbInstance} call asked for, of what these tests read. */
|
||||
private record CreateRequest(String id, List<String> groups, DbInstanceSettings settings) { }
|
||||
|
||||
/**
|
||||
* A DB instance a CreateStack of {@code template} left on file as {@code onFile}, and every
|
||||
* {@code createDbInstance} call made since, that one first.
|
||||
*/
|
||||
private record Created(StackResource resource, String id, String template, DbInstance onFile,
|
||||
List<CreateRequest> requests) { }
|
||||
|
||||
/**
|
||||
* A CreateStack of the unnamed instance {@code json}. RdsService then has it on file with
|
||||
* {@code groupsOnFile}, and modifies it in place without changing anything else.
|
||||
*/
|
||||
private Created createInstance(String json, String... groupsOnFile) {
|
||||
List<CreateRequest> requests = new ArrayList<>();
|
||||
stubCreateDbInstance(invocation -> {
|
||||
requests.add(new CreateRequest(invocation.getArgument(0), invocation.getArgument(17),
|
||||
invocation.getArgument(21)));
|
||||
return dbInstance(invocation.getArgument(0));
|
||||
});
|
||||
StackResource created = provision("Db", "AWS::RDS::DBInstance", json);
|
||||
assertEquals("CREATE_COMPLETE", created.getStatus(), created.getStatusReason());
|
||||
DbInstance onFile = stubInstanceOnFile(created.getPhysicalId());
|
||||
when(onFile.getVpcSecurityGroupIds()).thenReturn(List.of(groupsOnFile));
|
||||
return new Created(created, created.getPhysicalId(), json, onFile, requests);
|
||||
}
|
||||
|
||||
/** An UpdateStack from the template the instance was created with to {@code json}. */
|
||||
private StackResource updateInstance(Created created, String json) {
|
||||
return provisioner.provision("Db", "AWS::RDS::DBInstance", props(json), engine(), "us-east-1",
|
||||
"000000000000", "my-stack", created.id(), created.resource().getAttributes(), event -> { },
|
||||
props(created.template()));
|
||||
}
|
||||
|
||||
/** An instance created on {@code sg-first}, then updated to {@code json}; it is kept, under the same id. */
|
||||
private StackResource updateFromFirstGroup(String json) {
|
||||
return updateFromFirstGroup(json, created -> { });
|
||||
}
|
||||
|
||||
/** As {@link #updateFromFirstGroup(String)}, with {@code beforeUpdate} given the created instance first. */
|
||||
private StackResource updateFromFirstGroup(String json, Consumer<Created> beforeUpdate) {
|
||||
Created created = createInstance(SG_FIRST, "sg-first");
|
||||
beforeUpdate.accept(created);
|
||||
return updateInstance(created, json);
|
||||
}
|
||||
|
||||
/** An instance created encrypted, then replaced by an update that drops StorageEncrypted. */
|
||||
private Created replaceEncryptedInstance() {
|
||||
Created created = createInstance(ENCRYPTED);
|
||||
StackResource replaced = updateInstance(created, PLAIN);
|
||||
assertNotEquals(created.id(), replaced.getPhysicalId());
|
||||
return new Created(replaced, created.id(), PLAIN, created.onFile(), created.requests());
|
||||
}
|
||||
|
||||
/** How many times the provisioner asked RdsService to modify an instance, whatever the arguments. */
|
||||
private long modifyCalls() {
|
||||
return Mockito.mockingDetails(rdsService).getInvocations().stream()
|
||||
.filter(invocation -> invocation.getMethod().getName().equals("modifyDbInstance"))
|
||||
.count();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDbInstanceIsCreatedWithItsVpcSecurityGroupsAndStorageEncrypted() {
|
||||
Created created = createInstance("""
|
||||
{"Engine":"postgres","VPCSecurityGroups":["sg-first","sg-second"],"StorageEncrypted":true}
|
||||
""");
|
||||
|
||||
assertEquals(List.of(new CreateRequest(created.id(), List.of("sg-first", "sg-second"),
|
||||
new DbInstanceSettings(true, null, null, null, null, null))), created.requests());
|
||||
assertEquals("true", created.resource().getAttributes().get(ENCRYPTION_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedStorageEncryptedFailsInsteadOfLeavingTheInstanceUnencrypted() {
|
||||
stubCreateDbInstance(mock(DbInstance.class));
|
||||
|
||||
StackResource r = provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"Engine":"postgres","StorageEncrypted":"yes"}
|
||||
""");
|
||||
|
||||
assertEquals("CREATE_FAILED", r.getStatus());
|
||||
assertTrue(r.getStatusReason().contains("StorageEncrypted must be true or false"), r.getStatusReason());
|
||||
}
|
||||
|
||||
/** Every group the instance had comes back, however many, and an instance that had none gets none. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"sg-first", "sg-first,sg-other", ""})
|
||||
void anUpdateMovesTheInstanceToTheNewGroupsLastAndARollbackPutsTheOldOnesBack(String groupsOnFile) {
|
||||
String[] before = groupsOnFile.isEmpty() ? new String[0] : groupsOnFile.split(",");
|
||||
StackResource r = updateInstance(createInstance(PLAIN, before), SG_SECOND);
|
||||
|
||||
assertEquals("CREATE_COMPLETE", r.getStatus(), r.getStatusReason());
|
||||
InOrder order = inOrder(rdsService);
|
||||
order.verify(rdsService).modifyDbInstance(eq(r.getPhysicalId()), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(DbInstanceSettings.class));
|
||||
order.verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-second"));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r), "the groups were all the update changed");
|
||||
|
||||
verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of(before));
|
||||
assertFalse(r.getAttributes().containsKey(IN_PLACE_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppedGroupsPutTheInstanceOnItsVpcsDefaultGroupAndARollbackPutsThemBack() {
|
||||
when(rdsService.defaultVpcSecurityGroupIds(eq("us-east-1"), any())).thenReturn(List.of("sg-default"));
|
||||
|
||||
StackResource r = updateFromFirstGroup(PLAIN);
|
||||
|
||||
verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-default"));
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-first"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
PLAIN + " | " + PLAIN + " | sg-default",
|
||||
MEMBER + ",\"VPCSecurityGroups\":[\"sg-first\"]} | " + MEMBER + "} | sg-default",
|
||||
SG_FIRST + " | " + PLAIN + " |"
|
||||
})
|
||||
void theGroupsStayWhenTheTemplateNeverDeclaredThemAMemberDropsThemOrTheVpcHasNoDefault(
|
||||
String template, String updated, String defaultGroup) {
|
||||
// AWS's handler gives the VPC's default group only to a standalone instance whose previous
|
||||
// template declared groups, and only when there is such a group; a member's are its cluster's.
|
||||
when(rdsService.defaultVpcSecurityGroupIds(eq("us-east-1"), any()))
|
||||
.thenReturn(defaultGroup == null ? List.of() : List.of(defaultGroup));
|
||||
Created created = createInstance(template, "sg-manual");
|
||||
|
||||
StackResource r = updateInstance(created, updated);
|
||||
|
||||
assertEquals("CREATE_COMPLETE", r.getStatus(), r.getStatusReason());
|
||||
verify(rdsService, never()).setDbInstanceVpcSecurityGroups(any(), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
MEMBER + ",\"VPCSecurityGroups\":[\"sg-first\"]} | " + MEMBER + ",\"VPCSecurityGroups\":[\"sg-second\"]}",
|
||||
MEMBER + "} | " + MEMBER + ",\"VPCSecurityGroups\":[\"sg-cluster\"]}"
|
||||
})
|
||||
void aClusterMemberWhoseTemplateChangesItsGroupsFailsBeforeAnythingChanges(String template, String updated) {
|
||||
// AWS's handler sends the change to ModifyDBInstance, and RDS refuses it for a member, even one
|
||||
// that names the groups the member already has.
|
||||
StackResource r = updateInstance(createInstance(template, "sg-cluster"), updated);
|
||||
|
||||
assertEquals("CREATE_FAILED", r.getStatus());
|
||||
assertTrue(r.getStatusReason().contains("Modify vpc security group for the DB Cluster"), r.getStatusReason());
|
||||
assertEquals(0, modifyCalls());
|
||||
verify(rdsService, never()).setDbInstanceVpcSecurityGroups(any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterMemberWhoseTemplateKeepsItsGroupsIsGivenThemAndNotRefused() {
|
||||
// A member a stack created before Floci applied VPCSecurityGroups has none of them on file, but
|
||||
// its template did not change them, so AWS's handler would send RDS nothing to refuse.
|
||||
String template = MEMBER + ",\"VPCSecurityGroups\":[\"sg-first\"]}";
|
||||
StackResource r = updateInstance(createInstance(template), template);
|
||||
|
||||
assertEquals("CREATE_COMPLETE", r.getStatus(), r.getStatusReason());
|
||||
verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-first"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackOfAnUpdateThatAlsoChangedOtherSettingsPutsTheGroupsBackButIsNotComplete() {
|
||||
// The instance reports IAM authentication off when the update starts and on once modified.
|
||||
StackResource r = updateFromFirstGroup("""
|
||||
{"Engine":"postgres","VPCSecurityGroups":["sg-second"],"EnableIAMDatabaseAuthentication":true}
|
||||
""", created -> when(created.onFile().isIamDatabaseAuthenticationEnabled()).thenReturn(false, true));
|
||||
|
||||
assertFalse(provisioner.rollbackUpdate(r), "IAM authentication has no rollback here");
|
||||
verify(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-first"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackThatFindsTheInstanceGoneFailsAndKeepsWhatItOwes() {
|
||||
StackResource r = updateFromFirstGroup(SG_SECOND);
|
||||
doThrow(new AwsException("DBInstanceNotFound", "DB instance not found.", 404))
|
||||
.when(rdsService).setDbInstanceVpcSecurityGroups("us-east-1", r.getPhysicalId(), List.of("sg-first"));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
|
||||
assertEquals("DBInstanceNotFound", failure.getErrorCode());
|
||||
assertTrue(r.getAttributes().containsKey(IN_PLACE_ATTR), "a retried rollback tries again");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommittedInPlaceUpdateDropsWhatARollbackWouldHavePutBack() {
|
||||
// An in-place update owes no cleanup, so the engine calls completeUpdate and not clearUpdate;
|
||||
// a record left here would be put back by the rollback of the next update.
|
||||
StackResource r = updateFromFirstGroup(SG_SECOND);
|
||||
assertTrue(r.getAttributes().containsKey("__FlociAssociatedRolesPrior"));
|
||||
|
||||
assertFalse(provisioner.completeUpdate(r).applicable());
|
||||
|
||||
assertFalse(r.getAttributes().containsKey("__FlociAssociatedRolesPrior"));
|
||||
assertFalse(r.getAttributes().containsKey(ENCRYPTION_ATTR + "Prior"));
|
||||
assertFalse(r.getAttributes().containsKey(IN_PLACE_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackPutsBackTheRolesRecordTheUpdateFoundAndNotOneAnOlderFlociLeftBehind() {
|
||||
// Before completeUpdate dropped it, a committed in-place update left the roles record it
|
||||
// overwrote on the resource, and a stack can still carry one from then. The update found one
|
||||
// record, writes another, and the rollback has to put back the one it found.
|
||||
String found = "[{\"RoleArn\":\"arn:aws:iam::000000000000:role/declared\"}]";
|
||||
StackResource r = updateFromFirstGroup(SG_SECOND, created -> created.resource().getAttributes().putAll(Map.of(
|
||||
"__FlociAssociatedRoles", found,
|
||||
"__FlociAssociatedRolesPrior", "[{\"RoleArn\":\"arn:aws:iam::000000000000:role/old\"}]")));
|
||||
assertEquals("[]", r.getAttributes().get("__FlociAssociatedRoles"));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
assertEquals(found, r.getAttributes().get("__FlociAssociatedRoles"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStorageEncryptedChangeCreatesTheReplacementAndDeletesTheOldInstanceOnlyOnCommit() {
|
||||
// Dropping StorageEncrypted: true is the change to an unencrypted instance, which AWS can
|
||||
// only make by creating another one.
|
||||
Created replaced = replaceEncryptedInstance();
|
||||
|
||||
String replacement = replaced.resource().getPhysicalId();
|
||||
assertEquals(new CreateRequest(replacement, List.of(), DbInstanceSettings.defaults()),
|
||||
replaced.requests().get(1));
|
||||
assertEquals(0, modifyCalls());
|
||||
verify(rdsService, never()).deleteDbInstance(any(), any());
|
||||
verify(rdsService, never()).deleteDbInstance(any());
|
||||
verify(rdsService, never()).setDbInstanceVpcSecurityGroups(any(), any(), any());
|
||||
assertEquals(replaced.id(), provisioner.updateCleanupPhysicalId(replaced.resource()));
|
||||
|
||||
assertTrue(provisioner.completeUpdate(replaced.resource()).complete());
|
||||
|
||||
verify(rdsService).deleteDbInstance(replaced.id(), "us-east-1");
|
||||
assertEquals("false", replaced.resource().getAttributes().get(ENCRYPTION_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRolledBackReplacementIsDeletedAndTheRecordFollowsTheOldInstanceBack() {
|
||||
Created replaced = replaceEncryptedInstance();
|
||||
String replacement = replaced.resource().getPhysicalId();
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(replaced.resource()));
|
||||
|
||||
assertEquals(replaced.id(), replaced.resource().getPhysicalId());
|
||||
verify(rdsService).deleteDbInstance(replacement, "us-east-1");
|
||||
verify(rdsService, never()).deleteDbInstance(replaced.id(), "us-east-1");
|
||||
assertEquals("true", replaced.resource().getAttributes().get(ENCRYPTION_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReplacementWhoseDeleteFailsStillLeavesTheRecordWithTheOldInstance() {
|
||||
// The resource names the old instance again before the delete is tried, so a rollback the
|
||||
// stack later skips, rather than retries, must not leave the replacement's record behind.
|
||||
Created replaced = replaceEncryptedInstance();
|
||||
doThrow(new AwsException("InvalidDBInstanceState", "DB instance is busy.", 400))
|
||||
.when(rdsService).deleteDbInstance(replaced.resource().getPhysicalId(), "us-east-1");
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(replaced.resource()));
|
||||
|
||||
assertEquals(replaced.id(), replaced.resource().getPhysicalId());
|
||||
assertEquals("true", replaced.resource().getAttributes().get(ENCRYPTION_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCustomNamedInstanceRefusesAStorageEncryptedChange() {
|
||||
Created created = createInstance("{\"DBInstanceIdentifier\":\"mydb\",\"Engine\":\"postgres\"}");
|
||||
|
||||
StackResource r = updateInstance(created, """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","StorageEncrypted":true}
|
||||
""");
|
||||
|
||||
assertEquals("CREATE_FAILED", r.getStatus());
|
||||
assertTrue(r.getStatusReason().contains(
|
||||
"custom-named resource requires replacing. Rename mydb and update the stack again."));
|
||||
assertEquals(1, created.requests().size());
|
||||
assertEquals(0, modifyCalls());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {ENCRYPTED + " | false", PLAIN + " | true"})
|
||||
void anInstanceFromBeforeStorageEncryptedWasRecordedIsComparedWithThePreviousTemplate(
|
||||
String updated, boolean replaced) {
|
||||
// Its template said true, but nothing records that, so the update reads the previous template:
|
||||
// keeping true keeps the instance, dropping it replaces the instance.
|
||||
Created created = createInstance(ENCRYPTED);
|
||||
created.resource().getAttributes().remove(ENCRYPTION_ATTR);
|
||||
|
||||
StackResource r = updateInstance(created, updated);
|
||||
|
||||
assertEquals(replaced, !created.id().equals(r.getPhysicalId()));
|
||||
assertEquals(String.valueOf(!replaced), r.getAttributes().get(ENCRYPTION_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDelegatesToRdsServiceForEachRdsType() {
|
||||
// Stack deletion tears down RDS resources via the physical id set at provision time.
|
||||
|
||||
@@ -1562,7 +1562,7 @@ class RdsServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndModifyDbInstancePersistVpcSecurityGroups() {
|
||||
void vpcSecurityGroupsPersistThroughCreateModifyAndCloudFormationsExactSet() {
|
||||
DbInstance instance = rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null, null, false, false, null,
|
||||
@@ -1575,6 +1575,17 @@ class RdsServiceTest {
|
||||
|
||||
assertEquals(List.of("sg-updated-a", "sg-updated-b"), modified.getVpcSecurityGroupIds());
|
||||
assertEquals(List.of("sg-updated-a", "sg-updated-b"), rdsService.getDbInstance("mydb").getVpcSecurityGroupIds());
|
||||
|
||||
// ModifyDBInstance cannot leave an instance without groups; CloudFormation's rollback can.
|
||||
assertEquals(List.of(), rdsService.setDbInstanceVpcSecurityGroups("us-east-1", "mydb", List.of())
|
||||
.getVpcSecurityGroupIds());
|
||||
assertEquals(List.of(), rdsService.getDbInstance("mydb").getVpcSecurityGroupIds());
|
||||
|
||||
SecurityGroup defaultGroup = new SecurityGroup();
|
||||
defaultGroup.setGroupId("sg-default");
|
||||
when(ec2Service.defaultSecurityGroupOf("us-east-1", rdsService.getDbInstance("mydb").getVpcId()))
|
||||
.thenReturn(Optional.of(defaultGroup));
|
||||
assertEquals(List.of("sg-default"), rdsService.defaultVpcSecurityGroupIds("us-east-1", "mydb"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -139,6 +139,32 @@ public final class CfnStackWaits {
|
||||
stack.stackStatusReason() == null ? "" : stack.stackStatusReason())));
|
||||
}
|
||||
|
||||
/** Creates {@code stackName} from {@code template} through the SDK and waits for {@code expectedStatus}. */
|
||||
public static Stack createStack(CloudFormationClient client, String stackName, String template,
|
||||
String expectedStatus) {
|
||||
client.createStack(request -> request.stackName(stackName).templateBody(template));
|
||||
return awaitStatus(client, stackName, expectedStatus);
|
||||
}
|
||||
|
||||
/** Updates {@code stackName} to {@code template} through the SDK and waits for {@code expectedStatus}. */
|
||||
public static Stack updateStack(CloudFormationClient client, String stackName, String template,
|
||||
String expectedStatus) {
|
||||
client.updateStack(request -> request.stackName(stackName).templateBody(template));
|
||||
return awaitStatus(client, stackName, expectedStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* A resource to append to a template's {@code Resources}, after the one named {@code logicalId}:
|
||||
* an instance that names a launch template that does not exist, so it fails once that resource
|
||||
* is done, and an update that changed it rolls back.
|
||||
*/
|
||||
public static String failingResourceAfter(String logicalId) {
|
||||
return """
|
||||
,
|
||||
"Later": {"Type": "AWS::EC2::Instance", "DependsOn": "%s", "Properties": {"LaunchTemplate": {
|
||||
"LaunchTemplateId": "lt-0123456789abcdef0", "Version": "1"}}}""".formatted(logicalId);
|
||||
}
|
||||
|
||||
/** The value of {@code stack}'s output {@code key}; fails when the stack has no such output. */
|
||||
public static String output(Stack stack, String key) {
|
||||
return stack.outputs().stream().filter(output -> key.equals(output.outputKey())).findFirst()
|
||||
|
||||
Reference in new issue
Block a user