mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): provision SES event destinations (#8620)
* feat(cloudformation): provision SES event destinations * fix(cloudformation): validate SES event destination replacements * fix(cloudformation): apply SES event destination review feedback * test(cloudformation): use shared stack waits for SES event destinations
This commit is contained in:
1 parent
aef8416c4b
commit
5f77b841a2
22 files changed
+1264
-104
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -174,6 +175,37 @@ class CfnCreateOnlyPropertiesTest {
|
||||
assertTrue(unknownProps.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sesEventDestinationNameUsesConditionalReplacementMetadata() throws IOException {
|
||||
String type = "AWS::SES::ConfigurationSetEventDestination";
|
||||
assertEquals(Set.of("ConfigurationSetName"), CfnCreateOnlyProperties.getCreateOnlyProperties(type));
|
||||
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "EventDestination"));
|
||||
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Name"));
|
||||
assertEquals(Set.of("ConfigurationSetName", "EventDestination/Name"),
|
||||
CfnCreateOnlyProperties.replacementPointers(type));
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode previous = mapper.readTree("""
|
||||
{"ConfigurationSetName":"set","EventDestination":{"Name":"old","Enabled":true}}
|
||||
""");
|
||||
JsonNode renamed = mapper.readTree("""
|
||||
{"ConfigurationSetName":"set","EventDestination":{"Name":"new","Enabled":true}}
|
||||
""");
|
||||
JsonNode updated = mapper.readTree("""
|
||||
{"ConfigurationSetName":"set","EventDestination":{"Name":"old","Enabled":false}}
|
||||
""");
|
||||
assertTrue(CfnCreateOnlyProperties.createOnlyValueChanged(type, previous, renamed));
|
||||
assertFalse(CfnCreateOnlyProperties.createOnlyValueChanged(type, previous, updated));
|
||||
assertEquals(Set.of(previous.path("ConfigurationSetName"), previous.path("EventDestination").path("Name")),
|
||||
Set.copyOf(CfnCreateOnlyProperties.createOnlySubtrees(type, previous)));
|
||||
|
||||
try (InputStream in = getClass().getClassLoader()
|
||||
.getResourceAsStream("cloudformation/conditional-create-only-properties.json")) {
|
||||
assertEquals(Map.of(type, Set.of("EventDestination/Name")),
|
||||
mapper.readValue(in, new TypeReference<Map<String, Set<String>>>() {}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every type floci provisions has an entry in create-only-properties.json, taken from its
|
||||
* registry schema, so the name heuristic in {@code isCreateOnly} is never what answers for a
|
||||
|
||||
@@ -114,6 +114,7 @@ import io.floci.aws.services.cloudformation.provisioners.SchedulerScheduleGroupC
|
||||
import io.floci.aws.services.cloudformation.provisioners.SecretTargetAttachmentCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SecretsManagerCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SesCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SesEventDestinationCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SesReceiptRuleCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SignerCfnProvisioner;
|
||||
import io.floci.aws.services.cloudformation.provisioners.SnsCfnProvisioner;
|
||||
@@ -173,6 +174,7 @@ import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3vectors.S3VectorsService;
|
||||
import io.floci.aws.services.scheduler.SchedulerService;
|
||||
import io.floci.aws.services.secretsmanager.SecretsManagerService;
|
||||
import io.floci.aws.services.ses.SesConfigurationSetService;
|
||||
import io.floci.aws.services.ses.SesIdentityService;
|
||||
import io.floci.aws.services.ses.SesReceiptRuleService;
|
||||
import io.floci.aws.services.ses.SesService;
|
||||
@@ -228,6 +230,7 @@ final class CfnProvisionerFixture {
|
||||
private S3VectorsService s3VectorsService;
|
||||
private SnsService snsService;
|
||||
private SesService sesService;
|
||||
private SesConfigurationSetService sesConfigurationSetService;
|
||||
private SesReceiptRuleService sesReceiptRuleService;
|
||||
private SesIdentityService sesIdentityService;
|
||||
private DynamoDbService dynamoDbService;
|
||||
@@ -361,6 +364,9 @@ final class CfnProvisionerFixture {
|
||||
if (sesService != null && sesIdentityService != null) {
|
||||
discovered.add(new SesCfnProvisioner(sesService, sesIdentityService));
|
||||
}
|
||||
if (sesConfigurationSetService != null) {
|
||||
discovered.add(new SesEventDestinationCfnProvisioner(sesConfigurationSetService));
|
||||
}
|
||||
if (sesReceiptRuleService != null && sesIdentityService != null) {
|
||||
discovered.add(new SesReceiptRuleCfnProvisioner(sesReceiptRuleService, sesIdentityService));
|
||||
}
|
||||
@@ -605,6 +611,11 @@ final class CfnProvisionerFixture {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sesConfigurationSets(SesConfigurationSetService v) {
|
||||
this.sesConfigurationSetService = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sesIdentity(SesIdentityService v) {
|
||||
this.sesIdentityService = v;
|
||||
return this;
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.floci.aws.services.cloudformation.provisioners.ProvisionContext;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.scheduler.SchedulerService;
|
||||
import io.floci.aws.services.ses.SesConfigurationSetService;
|
||||
import io.floci.aws.services.sns.SnsService;
|
||||
import io.floci.aws.testing.MainSources;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -73,6 +74,14 @@ class CfnProvisionerFixtureTest {
|
||||
assertTrue(serves(fixture, "AWS::Scheduler::ScheduleGroup"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sesConfigurationSetsWireEventDestinations() {
|
||||
CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder()
|
||||
.sesConfigurationSets(mock(SesConfigurationSetService.class));
|
||||
|
||||
assertTrue(serves(fixture, "AWS::SES::ConfigurationSetEventDestination"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedServiceIsNotWired() {
|
||||
CfnProvisionerFixture.Builder fixture = CfnProvisionerFixture.builder()
|
||||
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.services.ses.SesConfigurationSetService;
|
||||
import io.floci.aws.services.ses.model.EventDestination;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.CfnStackWaits;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
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.sns.SnsClient;
|
||||
import software.amazon.awssdk.services.ssm.SsmClient;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationSesEventDestinationIntegrationTest {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private CloudFormationClient cfn;
|
||||
private String stackName;
|
||||
private boolean deleted;
|
||||
|
||||
@Inject
|
||||
SesConfigurationSetService configurationSets;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
cfn = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
|
||||
stackName = "cfn-ses-ed-" + Long.toString(System.nanoTime(), 36);
|
||||
deleted = false;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
try {
|
||||
if (!deleted) {
|
||||
CfnStackWaits.deleteStack(cfn, stackName);
|
||||
}
|
||||
} finally {
|
||||
cfn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventDestinationFollowsConfigurationSetCreateUpdateAndRemoval() {
|
||||
String setName = stackName + "-set";
|
||||
String destinationName = stackName + "-events";
|
||||
CfnStackWaits.createStack(cfn, stackName,
|
||||
template(setName, destinationName, "SEND", "initial"), "CREATE_COMPLETE");
|
||||
assertEquals(destinationName, cfn.describeStacks(request -> request.stackName(stackName))
|
||||
.stacks().getFirst().outputs().stream()
|
||||
.filter(output -> "DestinationId".equals(output.outputKey()))
|
||||
.findFirst().orElseThrow().outputValue());
|
||||
|
||||
List<EventDestination> destinations = configurationSets.getEventDestinations(setName, "us-east-1");
|
||||
assertEquals(1, destinations.size());
|
||||
assertEquals(destinationName, destinations.getFirst().getName());
|
||||
assertEquals(List.of("SEND"), destinations.getFirst().getMatchingEventTypes());
|
||||
assertEquals("initial", destinations.getFirst().getCloudWatchDestination()
|
||||
.getDimensionConfigurations().getFirst().getDefaultDimensionValue());
|
||||
|
||||
CfnStackWaits.updateStack(cfn, stackName,
|
||||
template(setName, destinationName, "BOUNCE", "updated"), "UPDATE_COMPLETE");
|
||||
|
||||
destinations = configurationSets.getEventDestinations(setName, "us-east-1");
|
||||
assertEquals(1, destinations.size());
|
||||
assertEquals(destinationName, destinations.getFirst().getName());
|
||||
assertEquals(List.of("BOUNCE"), destinations.getFirst().getMatchingEventTypes());
|
||||
assertEquals("updated", destinations.getFirst().getCloudWatchDestination()
|
||||
.getDimensionConfigurations().getFirst().getDefaultDimensionValue());
|
||||
|
||||
String replacementSet = stackName + "-new-set";
|
||||
CfnStackWaits.updateStack(cfn, stackName,
|
||||
template(replacementSet, destinationName, "BOUNCE", "updated"), "UPDATE_COMPLETE");
|
||||
assertTrue(configurationSets.find(setName, "us-east-1").isEmpty());
|
||||
assertEquals(destinationName, configurationSets.getEventDestinations(replacementSet, "us-east-1")
|
||||
.getFirst().getName());
|
||||
|
||||
CfnStackWaits.updateStack(cfn, stackName, """
|
||||
{"Resources": {"ConfigurationSet": {
|
||||
"Type": "AWS::SES::ConfigurationSet", "Properties": {"Name": "%s"}
|
||||
}}}
|
||||
""".formatted(replacementSet), "UPDATE_COMPLETE");
|
||||
assertTrue(configurationSets.getEventDestinations(replacementSet, "us-east-1").isEmpty());
|
||||
|
||||
CfnStackWaits.deleteStack(cfn, stackName);
|
||||
deleted = true;
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingTheDestinationNamePreviewsAndPerformsReplacement() {
|
||||
String setName = stackName + "-set";
|
||||
String oldName = stackName + "-old";
|
||||
String newName = stackName + "-new";
|
||||
CfnStackWaits.createStack(cfn, stackName,
|
||||
template(setName, oldName, "SEND", "initial"), "CREATE_COMPLETE");
|
||||
|
||||
CfnStackTestSupport.createUpdateChangeSet(cfn, stackName, "rename",
|
||||
template(setName, newName, "SEND", "initial"));
|
||||
assertEquals(List.of("True"), CfnStackTestSupport.replacement(
|
||||
cfn, stackName, "rename", "EventDestination"));
|
||||
|
||||
CfnStackTestSupport.executeAndAwaitUpdate(cfn, stackName, "rename");
|
||||
assertEquals(List.of(newName), configurationSets.getEventDestinations(setName, "us-east-1")
|
||||
.stream().map(EventDestination::getName).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedDynamicDestinationNamePreviewsReplacement() {
|
||||
String setName = stackName + "-set";
|
||||
String oldName = stackName + "-old";
|
||||
String newName = stackName + "-new";
|
||||
String parameter = "/ses/event-destination/" + stackName;
|
||||
String reference = "{{resolve:ssm:" + parameter + "}}";
|
||||
try (SsmClient ssm = AwsTestClients.create(SsmClient.builder(), "test", Region.US_EAST_1)) {
|
||||
try {
|
||||
ssm.putParameter(request -> request.name(parameter).type("String").value(oldName));
|
||||
CfnStackWaits.createStack(cfn, stackName,
|
||||
template(setName, reference, "SEND", "initial"), "CREATE_COMPLETE");
|
||||
|
||||
ssm.putParameter(request -> request.name(parameter).type("String").value(newName)
|
||||
.overwrite(true));
|
||||
CfnStackTestSupport.createUpdateChangeSet(cfn, stackName, "rename-dynamic",
|
||||
template(setName, reference, "BOUNCE", "initial"));
|
||||
assertEquals(List.of("True"), CfnStackTestSupport.replacement(
|
||||
cfn, stackName, "rename-dynamic", "EventDestination"));
|
||||
|
||||
CfnStackTestSupport.executeAndAwaitUpdate(cfn, stackName, "rename-dynamic");
|
||||
assertEquals(List.of(newName), configurationSets.getEventDestinations(setName, "us-east-1")
|
||||
.stream().map(EventDestination::getName).toList());
|
||||
} catch (RuntimeException | Error failure) {
|
||||
try {
|
||||
ssm.deleteParameter(request -> request.name(parameter));
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
ssm.deleteParameter(request -> request.name(parameter));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void dependentFailureRestoresAnInPlaceUpdate() throws JsonProcessingException {
|
||||
String setName = stackName + "-set";
|
||||
String destinationName = stackName + "-events";
|
||||
CfnStackWaits.createStack(cfn, stackName,
|
||||
template(setName, destinationName, "SEND", "initial"), "CREATE_COMPLETE");
|
||||
|
||||
String update = withBadDependent(template(setName, destinationName, "BOUNCE", "updated"));
|
||||
CfnStackWaits.updateStack(cfn, stackName, update, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
List<EventDestination> destinations = configurationSets.getEventDestinations(setName, "us-east-1");
|
||||
assertEquals(1, destinations.size());
|
||||
assertEquals(destinationName, destinations.getFirst().getName());
|
||||
assertEquals(List.of("SEND"), destinations.getFirst().getMatchingEventTypes());
|
||||
assertEquals("initial", destinations.getFirst().getCloudWatchDestination()
|
||||
.getDimensionConfigurations().getFirst().getDefaultDimensionValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dependentFailureRemovesAReplacementAndKeepsThePriorDestination() throws JsonProcessingException {
|
||||
String setName = stackName + "-set";
|
||||
String destinationName = stackName + "-events";
|
||||
CfnStackWaits.createStack(cfn, stackName,
|
||||
template(setName, destinationName, "SEND", "initial"), "CREATE_COMPLETE");
|
||||
|
||||
String update = withBadDependent(template(setName, destinationName + "-new", "BOUNCE", "updated"));
|
||||
CfnStackWaits.updateStack(cfn, stackName, update, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
List<EventDestination> destinations = configurationSets.getEventDestinations(setName, "us-east-1");
|
||||
assertEquals(1, destinations.size());
|
||||
assertEquals(destinationName, destinations.getFirst().getName());
|
||||
assertEquals(List.of("SEND"), destinations.getFirst().getMatchingEventTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloudFormationArnPropertiesMapToSesDestinationsAndEnabledDefaultsToFalse() {
|
||||
String setName = stackName + "-set";
|
||||
String roleArn = "arn:aws:iam::000000000000:role/ses-firehose";
|
||||
String streamArn = "arn:aws:firehose:us-east-1:000000000000:deliverystream/ses-events";
|
||||
try (SnsClient sns = AwsTestClients.create(SnsClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String topicArn = sns.createTopic(request -> request.name(stackName + "-topic")).topicArn();
|
||||
try {
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"ConfigurationSet": {
|
||||
"Type": "AWS::SES::ConfigurationSet", "Properties": {"Name": "%s"}
|
||||
},
|
||||
"SnsEvents": {
|
||||
"Type": "AWS::SES::ConfigurationSetEventDestination",
|
||||
"Properties": {
|
||||
"ConfigurationSetName": {"Ref": "ConfigurationSet"},
|
||||
"EventDestination": {
|
||||
"Name": "sns-events",
|
||||
"MatchingEventTypes": ["SEND"],
|
||||
"SnsDestination": {"TopicARN": "%s"}
|
||||
}
|
||||
}
|
||||
},
|
||||
"FirehoseEvents": {
|
||||
"Type": "AWS::SES::ConfigurationSetEventDestination",
|
||||
"Properties": {
|
||||
"ConfigurationSetName": {"Ref": "ConfigurationSet"},
|
||||
"EventDestination": {
|
||||
"Name": "firehose-events",
|
||||
"MatchingEventTypes": ["SEND"],
|
||||
"KinesisFirehoseDestination": {
|
||||
"IAMRoleARN": "%s", "DeliveryStreamARN": "%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(setName, topicArn, roleArn, streamArn);
|
||||
CfnStackWaits.createStack(cfn, stackName, template, "CREATE_COMPLETE");
|
||||
|
||||
List<EventDestination> destinations = configurationSets.getEventDestinations(setName, "us-east-1");
|
||||
assertEquals(2, destinations.size());
|
||||
EventDestination snsDestination = destinations.stream()
|
||||
.filter(destination -> "sns-events".equals(destination.getName()))
|
||||
.findFirst().orElseThrow();
|
||||
EventDestination firehoseDestination = destinations.stream()
|
||||
.filter(destination -> "firehose-events".equals(destination.getName()))
|
||||
.findFirst().orElseThrow();
|
||||
assertFalse(snsDestination.isEnabled());
|
||||
assertEquals(topicArn, snsDestination.getSnsDestination().getTopicArn());
|
||||
assertEquals(roleArn, firehoseDestination.getKinesisFirehoseDestination().getIamRoleArn());
|
||||
assertEquals(streamArn, firehoseDestination.getKinesisFirehoseDestination().getDeliveryStreamArn());
|
||||
} finally {
|
||||
sns.deleteTopic(request -> request.topicArn(topicArn));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String withBadDependent(String template) throws JsonProcessingException {
|
||||
ObjectNode root = (ObjectNode) MAPPER.readTree(template);
|
||||
ObjectNode badSecret = ((ObjectNode) root.path("Resources")).putObject("BadSecret");
|
||||
badSecret.put("Type", "AWS::SecretsManager::Secret");
|
||||
badSecret.put("DependsOn", "EventDestination");
|
||||
ObjectNode properties = badSecret.putObject("Properties");
|
||||
properties.put("SecretString", "conflicting");
|
||||
properties.putObject("GenerateSecretString").put("PasswordLength", 8);
|
||||
return root.toString();
|
||||
}
|
||||
|
||||
private String template(String setName, String destinationName, String eventType, String defaultValue) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"ConfigurationSet": {
|
||||
"Type": "AWS::SES::ConfigurationSet",
|
||||
"Properties": {"Name": "%s"}
|
||||
},
|
||||
"EventDestination": {
|
||||
"Type": "AWS::SES::ConfigurationSetEventDestination",
|
||||
"Properties": {
|
||||
"ConfigurationSetName": {"Ref": "ConfigurationSet"},
|
||||
"EventDestination": {
|
||||
"Name": "%s",
|
||||
"Enabled": true,
|
||||
"MatchingEventTypes": ["%s"],
|
||||
"CloudWatchDestination": {
|
||||
"DimensionConfigurations": [{
|
||||
"DimensionName": "campaign",
|
||||
"DimensionValueSource": "messageTag",
|
||||
"DefaultDimensionValue": "%s"
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"DestinationId": {"Value": {"Fn::GetAtt": ["EventDestination", "Id"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(setName, destinationName, eventType, defaultValue);
|
||||
}
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.ses.SesConfigurationSetService;
|
||||
import io.floci.aws.services.ses.model.EventDestination;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
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.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class SesEventDestinationCfnProvisionerTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final SesConfigurationSetService configurationSets = mock(SesConfigurationSetService.class);
|
||||
private final SesEventDestinationCfnProvisioner provisioner =
|
||||
new SesEventDestinationCfnProvisioner(configurationSets);
|
||||
|
||||
@Test
|
||||
void failedReplacementRollbackRetriesBeforeTheNextUpdateUsesTheRestoredPhysicalId() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "old", false), context(null));
|
||||
provisioner.provision(resource, properties("second", "new", true), context("old"));
|
||||
resource.setStatus("UPDATE_FAILED");
|
||||
|
||||
doThrow(new AwsException("InternalFailure", "retry deletion", 500)).doNothing()
|
||||
.when(configurationSets).deleteEventDestination("second", "new", REGION);
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(resource));
|
||||
assertTrue(provisioner.requiresUpdateRecovery(resource));
|
||||
assertEquals("new", resource.getPhysicalId());
|
||||
|
||||
EventDestination previous = new EventDestination();
|
||||
previous.setName("old");
|
||||
previous.setMatchingEventTypes(List.of("SEND"));
|
||||
when(configurationSets.getEventDestinations("first", REGION)).thenReturn(List.of(previous));
|
||||
|
||||
// The stack can still pass the failed replacement's id as the prior id on this call.
|
||||
provisioner.provision(resource, properties("first", "old", true), context("new"));
|
||||
|
||||
assertEquals("old", resource.getPhysicalId());
|
||||
assertEquals("old", resource.getAttributes().get("Id"));
|
||||
verify(configurationSets, times(2)).deleteEventDestination("second", "new", REGION);
|
||||
verify(configurationSets).updateEventDestination(eq("first"), eq("old"), any(EventDestination.class),
|
||||
eq(ACCOUNT), eq(REGION), eq(true));
|
||||
verify(configurationSets, times(1)).createEventDestination(eq("first"), eq("old"),
|
||||
any(EventDestination.class), eq(ACCOUNT), eq(REGION), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void retainedReplacementWithTheSameNameKeepsTheOldConfigurationSetDestination() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "shared", false), context(null));
|
||||
resource.setUpdateReplacePolicy("Retain");
|
||||
|
||||
provisioner.provision(resource, properties("second", "shared", true), context("shared"));
|
||||
|
||||
assertTrue(provisioner.hasReplacementUpdate(resource));
|
||||
assertNull(provisioner.updateCleanupPhysicalId(resource));
|
||||
assertTrue(provisioner.completeUpdate(resource).complete());
|
||||
assertFalse(provisioner.hasReplacementUpdate(resource));
|
||||
provisioner.clearUpdate(resource);
|
||||
provisioner.delete(resource, REGION);
|
||||
|
||||
verify(configurationSets).deleteEventDestination("second", "shared", REGION);
|
||||
verify(configurationSets, never()).deleteEventDestination("first", "shared", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedNestedDestinationProperties() {
|
||||
ObjectNode props = properties("first", "events", true);
|
||||
props.withObject("EventDestination").putObject("SnsDestination")
|
||||
.put("TopicARN", "arn:aws:sns:us-east-1:000000000000:events")
|
||||
.put("Unexpected", "ignored");
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(resource(), props, context(null)));
|
||||
verify(configurationSets, never()).createEventDestination(eq("first"), eq("events"),
|
||||
any(EventDestination.class), eq(ACCOUNT), eq(REGION), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingAndBlankSnsTopicArnBeforeCallingSes() {
|
||||
ObjectNode missing = properties("first", "events", true);
|
||||
missing.withObject("EventDestination").putObject("SnsDestination");
|
||||
assertSchemaInvalidWithoutSesCall(missing);
|
||||
|
||||
ObjectNode blank = properties("first", "events", true);
|
||||
blank.withObject("EventDestination").putObject("SnsDestination").put("TopicARN", " ");
|
||||
assertInvalidWithoutSesCall(blank);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingAndBlankFirehoseArnsBeforeCallingSes() {
|
||||
ObjectNode missing = properties("first", "events", true);
|
||||
missing.withObject("EventDestination").putObject("KinesisFirehoseDestination")
|
||||
.put("IAMRoleARN", "arn:aws:iam::000000000000:role/ses");
|
||||
assertSchemaInvalidWithoutSesCall(missing);
|
||||
|
||||
ObjectNode blank = properties("first", "events", true);
|
||||
blank.withObject("EventDestination").putObject("KinesisFirehoseDestination")
|
||||
.put("IAMRoleARN", " ")
|
||||
.put("DeliveryStreamARN", "arn:aws:firehose:us-east-1:000000000000:deliverystream/events");
|
||||
assertInvalidWithoutSesCall(blank);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingAndBlankEventBusArnBeforeCallingSes() {
|
||||
ObjectNode missing = properties("first", "events", true);
|
||||
missing.withObject("EventDestination").putObject("EventBridgeDestination");
|
||||
assertSchemaInvalidWithoutSesCall(missing);
|
||||
|
||||
ObjectNode blank = properties("first", "events", true);
|
||||
blank.withObject("EventDestination").putObject("EventBridgeDestination").put("EventBusArn", " ");
|
||||
assertInvalidWithoutSesCall(blank);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingConfigurationSetNameAndNonObjectDestinationsBeforeCallingSes() {
|
||||
ObjectNode missingSet = properties("first", "events", true);
|
||||
missingSet.remove("ConfigurationSetName");
|
||||
assertSchemaInvalidWithoutSesCall(missingSet);
|
||||
|
||||
ObjectNode blankSet = properties("first", "events", true).put("ConfigurationSetName", " ");
|
||||
assertInvalidWithoutSesCall(blankSet);
|
||||
|
||||
ObjectNode nonObjectDestination = properties("first", "events", true)
|
||||
.put("EventDestination", "events");
|
||||
assertSchemaInvalidWithoutSesCall(nonObjectDestination);
|
||||
|
||||
ObjectNode nonObjectBlock = properties("first", "events", true);
|
||||
nonObjectBlock.withObject("EventDestination").put("SnsDestination", "topic");
|
||||
assertSchemaInvalidWithoutSesCall(nonObjectBlock);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedCloudWatchDimensionsBeforeCallingSes() {
|
||||
ObjectNode nonArray = properties("first", "events", true);
|
||||
nonArray.withObject("EventDestination").putObject("CloudWatchDestination")
|
||||
.put("DimensionConfigurations", "dimension");
|
||||
assertSchemaInvalidWithoutSesCall(nonArray);
|
||||
|
||||
ObjectNode nonObjectEntry = properties("first", "events", true);
|
||||
nonObjectEntry.withObject("EventDestination").putObject("CloudWatchDestination")
|
||||
.putArray("DimensionConfigurations").add("dimension");
|
||||
assertSchemaInvalidWithoutSesCall(nonObjectEntry);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSchemaRequiredDestinationMembersBeforeCallingSes() {
|
||||
ObjectNode missingEvents = properties("first", "events", true);
|
||||
missingEvents.withObject("EventDestination").remove("MatchingEventTypes");
|
||||
assertSchemaInvalidWithoutSesCall(missingEvents);
|
||||
|
||||
ObjectNode missingDimensionName = properties("first", "events", true);
|
||||
missingDimensionName.withObject("EventDestination").putObject("CloudWatchDestination")
|
||||
.putArray("DimensionConfigurations").addObject()
|
||||
.put("DimensionValueSource", "messageTag")
|
||||
.put("DefaultDimensionValue", "unknown");
|
||||
assertSchemaInvalidWithoutSesCall(missingDimensionName);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsNullRequiredMembersAndDimensionEntriesBeforeCallingSes() {
|
||||
ObjectNode nullEvents = properties("first", "events", true);
|
||||
nullEvents.withObject("EventDestination").putNull("MatchingEventTypes");
|
||||
assertInvalidWithoutSesCall(nullEvents);
|
||||
|
||||
ObjectNode nullTopic = properties("first", "events", true);
|
||||
nullTopic.withObject("EventDestination").putObject("SnsDestination").putNull("TopicARN");
|
||||
assertInvalidWithoutSesCall(nullTopic);
|
||||
|
||||
ObjectNode nullDimensionName = properties("first", "events", true);
|
||||
nullDimensionName.withObject("EventDestination").putObject("CloudWatchDestination")
|
||||
.putArray("DimensionConfigurations").addObject()
|
||||
.put("DimensionValueSource", "messageTag")
|
||||
.put("DefaultDimensionValue", "unknown")
|
||||
.putNull("DimensionName");
|
||||
assertInvalidWithoutSesCall(nullDimensionName);
|
||||
|
||||
ObjectNode nullDimensionEntry = properties("first", "events", true);
|
||||
nullDimensionEntry.withObject("EventDestination").putObject("CloudWatchDestination")
|
||||
.putArray("DimensionConfigurations").addNull();
|
||||
assertInvalidWithoutSesCall(nullDimensionEntry);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsReadOnlySchemaIdBeforeCallingSes() {
|
||||
ObjectNode props = properties("first", "events", true).put("Id", "read-only");
|
||||
assertInvalidWithoutSesCall(props);
|
||||
}
|
||||
|
||||
private void assertSchemaInvalidWithoutSesCall(ObjectNode props) {
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource(), props, context(null)));
|
||||
assertEquals("InvalidRequest", failure.getErrorCode());
|
||||
assertEquals(400, failure.getHttpStatus());
|
||||
verifyNoInteractions(configurationSets);
|
||||
}
|
||||
|
||||
private void assertInvalidWithoutSesCall(ObjectNode props) {
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource(), props, context(null)));
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
assertEquals(400, failure.getHttpStatus());
|
||||
verifyNoInteractions(configurationSets);
|
||||
}
|
||||
|
||||
private ObjectNode properties(String set, String name, boolean enabled) {
|
||||
ObjectNode props = mapper.createObjectNode().put("ConfigurationSetName", set);
|
||||
ObjectNode destination = props.putObject("EventDestination");
|
||||
destination.put("Name", name);
|
||||
destination.put("Enabled", enabled);
|
||||
destination.putArray("MatchingEventTypes").add("SEND");
|
||||
return props;
|
||||
}
|
||||
|
||||
private static StackResource resource() {
|
||||
StackResource resource = new StackResource();
|
||||
resource.setResourceType("AWS::SES::ConfigurationSetEventDestination");
|
||||
resource.setLogicalId("EventDestination");
|
||||
return resource;
|
||||
}
|
||||
|
||||
private ProvisionContext context(String prior) {
|
||||
CloudFormationTemplateEngine engine = CloudFormationTemplateEngine.standalone(ACCOUNT, REGION,
|
||||
"stack", mapper, UnaryOperator.identity());
|
||||
return new ProvisionContext(engine, REGION, ACCOUNT, "stack", prior);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user