mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): provision SES receipt rules (#7040)
This commit is contained in:
1 parent
83ec0b82c3
commit
f48c3a9c01
8 files changed
+795
-1
No files matched your search
+11
@@ -109,6 +109,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.Scheduler
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SecretTargetAttachmentCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SecretsManagerCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SesCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SesReceiptRuleCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SnsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SqsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SsmCfnProvisioner;
|
||||
@@ -162,6 +163,7 @@ import io.github.hectorvent.floci.services.s3vectors.S3VectorsService;
|
||||
import io.github.hectorvent.floci.services.scheduler.SchedulerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.ses.SesIdentityService;
|
||||
import io.github.hectorvent.floci.services.ses.SesReceiptRuleService;
|
||||
import io.github.hectorvent.floci.services.ses.SesService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
@@ -203,6 +205,7 @@ final class CfnProvisionerFixture {
|
||||
private S3VectorsService s3VectorsService;
|
||||
private SnsService snsService;
|
||||
private SesService sesService;
|
||||
private SesReceiptRuleService sesReceiptRuleService;
|
||||
private SesIdentityService sesIdentityService;
|
||||
private DynamoDbService dynamoDbService;
|
||||
private LambdaService lambdaService;
|
||||
@@ -329,6 +332,9 @@ final class CfnProvisionerFixture {
|
||||
if (sesService != null && sesIdentityService != null) {
|
||||
discovered.add(new SesCfnProvisioner(sesService, sesIdentityService));
|
||||
}
|
||||
if (sesReceiptRuleService != null && sesIdentityService != null) {
|
||||
discovered.add(new SesReceiptRuleCfnProvisioner(sesReceiptRuleService, sesIdentityService));
|
||||
}
|
||||
if (dynamoDbService != null) {
|
||||
NativeDynamoDbBackend backend = new NativeDynamoDbBackend(null, null, dynamoDbService, null, null, objectMapper);
|
||||
discovered.add(new DynamoDbCfnProvisioner(new DynamoDbFacade(backend, backend,
|
||||
@@ -555,6 +561,11 @@ final class CfnProvisionerFixture {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sesReceiptRules(SesReceiptRuleService v) {
|
||||
this.sesReceiptRuleService = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder dynamoDb(DynamoDbService v) {
|
||||
this.dynamoDbService = v;
|
||||
return this;
|
||||
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.ses.SesReceiptRuleService;
|
||||
import io.github.hectorvent.floci.services.ses.model.ReceiptRule;
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
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 org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class CloudFormationSesReceiptRuleIntegrationTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private CloudFormationClient cfn;
|
||||
@Inject
|
||||
SesReceiptRuleService ses;
|
||||
private String stackName;
|
||||
private String ruleSet;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
cfn = AwsTestClients.create(CloudFormationClient.builder(), "test", Region.US_EAST_1);
|
||||
stackName = "cfn-receipt-" + Long.toString(System.nanoTime(), 36);
|
||||
ruleSet = stackName + "-set";
|
||||
ses.createReceiptRuleSet(ruleSet, "us-east-1");
|
||||
ses.createReceiptRuleSet(ruleSet + "-next", "us-east-1");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
try {
|
||||
cfn.deleteStack(r -> r.stackName(stackName));
|
||||
await().atMost(Duration.ofSeconds(15)).until(() -> {
|
||||
try {
|
||||
return cfn.describeStacks(r -> r.stackName(stackName)).stacks().isEmpty();
|
||||
} catch (CloudFormationException expected) {
|
||||
return expected.statusCode() == 400;
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
ses.deleteReceiptRuleSet(ruleSet, "us-east-1");
|
||||
ses.deleteReceiptRuleSet(ruleSet + "-next", "us-east-1");
|
||||
cfn.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void sameNamedRuleInAnotherSetReplacesTheOldEntity(boolean retain) throws Exception {
|
||||
ObjectNode initial = (ObjectNode) mapper.readTree(template("store", true));
|
||||
if (retain) {
|
||||
initial.withObject("/Resources/Rule").put("UpdateReplacePolicy", "Retain");
|
||||
}
|
||||
cfn.createStack(r -> r.stackName(stackName).templateBody(initial.toString()));
|
||||
awaitStatus("CREATE_COMPLETE");
|
||||
ObjectNode updated = (ObjectNode) mapper.readTree(template("store", false));
|
||||
updated.withObject("/Resources/Rule/Properties").put("RuleSetName", ruleSet + "-next");
|
||||
if (retain) {
|
||||
updated.withObject("/Resources/Rule").put("UpdateReplacePolicy", "Retain");
|
||||
}
|
||||
cfn.updateStack(r -> r.stackName(stackName).templateBody(updated.toString()));
|
||||
awaitStatus("UPDATE_COMPLETE");
|
||||
assertFalse(ses.describeReceiptRule(ruleSet + "-next", "store", "us-east-1").isEnabled());
|
||||
if (retain) {
|
||||
assertTrue(ses.describeReceiptRule(ruleSet, "store", "us-east-1").isEnabled());
|
||||
} else {
|
||||
assertEquals("RuleDoesNotExist", assertThrows(AwsException.class,
|
||||
() -> ses.describeReceiptRule(ruleSet, "store", "us-east-1")).getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedPositionChangeRestoresRuleContentsAndOriginalPosition() throws Exception {
|
||||
ReceiptRule anchor = new ReceiptRule();
|
||||
anchor.setName("anchor");
|
||||
ses.createReceiptRule(ruleSet, anchor, null, "us-east-1", sender -> false);
|
||||
ObjectNode initial = (ObjectNode) mapper.readTree(template("store", true));
|
||||
initial.withObject("/Resources/Rule/Properties").put("After", "anchor");
|
||||
cfn.createStack(r -> r.stackName(stackName).templateBody(initial.toString()));
|
||||
awaitStatus("CREATE_COMPLETE");
|
||||
ObjectNode updated = (ObjectNode) mapper.readTree(template("store", false));
|
||||
updated.withObject("/Resources/Rule/Properties").put("After", "missing");
|
||||
cfn.updateStack(r -> r.stackName(stackName).templateBody(updated.toString()));
|
||||
awaitStatus("UPDATE_ROLLBACK_COMPLETE");
|
||||
assertTrue(ses.describeReceiptRule(ruleSet, "store", "us-east-1").isEnabled());
|
||||
assertEquals(List.of("anchor", "store"), ses.describeReceiptRuleSet(ruleSet, "us-east-1")
|
||||
.getRules().stream().map(ReceiptRule::getName).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsTheIssueTemplateWithAnS3TargetResolvedFromRef() throws Exception {
|
||||
ObjectNode template = (ObjectNode) mapper.readTree(template("store", true));
|
||||
template.withObject("/Resources").putObject("Bucket").put("Type", "AWS::S3::Bucket");
|
||||
template.withObject("/Resources/Rule/Properties/Rule").putArray("Actions")
|
||||
.addObject().putObject("S3Action").putObject("BucketName").put("Ref", "Bucket");
|
||||
template.withObject("/Outputs").putObject("BucketName").putObject("Value").put("Ref", "Bucket");
|
||||
cfn.createStack(r -> r.stackName(stackName).templateBody(template.toString()));
|
||||
Stack created = awaitStatus("CREATE_COMPLETE");
|
||||
String bucket = created.outputs().stream().filter(o -> "BucketName".equals(o.outputKey()))
|
||||
.findFirst().orElseThrow().outputValue();
|
||||
ReceiptRule rule = ses.describeReceiptRule(ruleSet, "store", "us-east-1");
|
||||
assertEquals("S3Action", rule.getActions().getFirst().getType());
|
||||
assertEquals(bucket, rule.getActions().getFirst().property("BucketName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsUpdatesAndDeletesRealReceiptRules() throws Exception {
|
||||
String initial = template("store", true);
|
||||
cfn.createStack(r -> r.stackName(stackName).templateBody(initial));
|
||||
Stack created = awaitStatus("CREATE_COMPLETE");
|
||||
assertTrue(ses.describeReceiptRule(ruleSet, "store", "us-east-1").isEnabled());
|
||||
assertTrue(created.outputs().stream().allMatch(o -> "store".equals(o.outputValue())));
|
||||
|
||||
String updated = template("store", false);
|
||||
cfn.updateStack(r -> r.stackName(stackName).templateBody(updated));
|
||||
awaitStatus("UPDATE_COMPLETE");
|
||||
assertFalse(ses.describeReceiptRule(ruleSet, "store", "us-east-1").isEnabled());
|
||||
|
||||
cfn.deleteStack(r -> r.stackName(stackName));
|
||||
await().atMost(Duration.ofSeconds(15)).untilAsserted(() ->
|
||||
assertEquals("RuleDoesNotExist", assertThrows(AwsException.class,
|
||||
() -> ses.describeReceiptRule(ruleSet, "store", "us-east-1")).getErrorCode()));
|
||||
}
|
||||
|
||||
private Stack awaitStatus(String expected) {
|
||||
return await().atMost(Duration.ofSeconds(15)).until(
|
||||
() -> cfn.describeStacks(r -> r.stackName(stackName)).stacks().getFirst(),
|
||||
s -> expected.equals(s.stackStatusAsString()));
|
||||
}
|
||||
|
||||
private String template(String name, boolean enabled) throws Exception {
|
||||
return mapper.writeValueAsString(Map.of("Resources", Map.of("Rule", Map.of(
|
||||
"Type", "AWS::SES::ReceiptRule", "Properties", Map.of("RuleSetName", ruleSet,
|
||||
"Rule", Map.of("Name", name, "Enabled", enabled, "Recipients", List.of("example.com"),
|
||||
"Actions", List.of(Map.of("AddHeaderAction",
|
||||
Map.of("HeaderName", "X-Test", "HeaderValue", "yes"))))))),
|
||||
"Outputs", Map.of("RuleRef", Map.of("Value", Map.of("Ref", "Rule")),
|
||||
"RuleName", Map.of("Value", Map.of("Fn::GetAtt", List.of("Rule", "RuleName"))))));
|
||||
}
|
||||
}
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.ses.SesIdentityService;
|
||||
import io.github.hectorvent.floci.services.ses.SesReceiptRuleService;
|
||||
import io.github.hectorvent.floci.services.ses.model.ReceiptFilter;
|
||||
import io.github.hectorvent.floci.services.ses.model.ReceiptRule;
|
||||
import io.github.hectorvent.floci.services.ses.model.ReceiptRuleSet;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class SesReceiptRuleCfnProvisionerTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private SesReceiptRuleService rules;
|
||||
private SesReceiptRuleCfnProvisioner provisioner;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.<ReceiptRuleSet>create(eq("ses"), eq("ses-receipt-rule-sets.json"), any()))
|
||||
.thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT));
|
||||
when(factory.<ReceiptFilter>create(eq("ses"), eq("ses-receipt-filters.json"), any()))
|
||||
.thenReturn(AccountAwareStorageBackend.inMemory(ACCOUNT));
|
||||
rules = spy(new SesReceiptRuleService(factory, mock(S3Service.class), mock(SnsService.class),
|
||||
mock(LambdaService.class), mock(ServiceCallAuthorizer.class), new RegionResolver(REGION, ACCOUNT),
|
||||
Clock.systemUTC()));
|
||||
rules.createReceiptRuleSet("first", REGION);
|
||||
rules.createReceiptRuleSet("second", REGION);
|
||||
provisioner = new SesReceiptRuleCfnProvisioner(rules, mock(SesIdentityService.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedNameStaysStableAndExplicitNameRemovalCreatesReplacement() {
|
||||
StackResource generated = resource();
|
||||
ObjectNode props = properties("first", null);
|
||||
provisioner.provision(generated, props, context(null));
|
||||
String name = generated.getPhysicalId();
|
||||
assertTrue(name.length() <= 64);
|
||||
props.withObject("/Rule").put("Enabled", true);
|
||||
provisioner.provision(generated, props, context(name));
|
||||
assertEquals(name, generated.getPhysicalId());
|
||||
assertTrue(rules.describeReceiptRule("first", name, REGION).isEnabled());
|
||||
provisioner.clearUpdate(generated);
|
||||
|
||||
StackResource named = resource();
|
||||
provisioner.provision(named, properties("second", "named"), context(null));
|
||||
provisioner.provision(named, properties("second", null), context("named"));
|
||||
assertNotEquals("named", named.getPhysicalId());
|
||||
assertTrue(provisioner.completeUpdate(named).complete());
|
||||
assertMissing("second", "named");
|
||||
}
|
||||
|
||||
@Test
|
||||
void retainedSameNameReplacementKeepsBothRules() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "store"), context(null));
|
||||
resource.setUpdateReplacePolicy("Retain");
|
||||
provisioner.provision(resource, properties("second", "store"), context("store"));
|
||||
assertTrue(provisioner.hasReplacementUpdate(resource));
|
||||
assertNull(provisioner.updateCleanupPhysicalId(resource));
|
||||
assertTrue(provisioner.completeUpdate(resource).complete());
|
||||
provisioner.clearUpdate(resource);
|
||||
provisioner.delete(resource, REGION);
|
||||
assertEquals("store", rules.describeReceiptRule("first", "store", REGION).getName());
|
||||
assertMissing("second", "store");
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacementRollbackCanRetryFailedDeletionWithoutLosingEitherAddress() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "store"), context(null));
|
||||
provisioner.provision(resource, properties("second", "store"), context("store"));
|
||||
resource.setStatus("UPDATE_FAILED");
|
||||
doThrow(new AwsException("InternalFailure", "retry", 500)).doCallRealMethod()
|
||||
.when(rules).deleteReceiptRule("second", "store", REGION);
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(resource));
|
||||
assertTrue(provisioner.requiresUpdateRecovery(resource));
|
||||
assertEquals("store", resource.getPhysicalId());
|
||||
assertTrue(provisioner.rollbackUpdate(resource));
|
||||
assertFalse(provisioner.requiresUpdateRecovery(resource));
|
||||
assertEquals("store", rules.describeReceiptRule("first", "store", REGION).getName());
|
||||
assertMissing("second", "store");
|
||||
provisioner.delete(resource, REGION);
|
||||
assertMissing("first", "store");
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedStubMigrationRemovesOnlyTheNewlyCreatedRule() {
|
||||
StackResource resource = resource();
|
||||
resource.setPhysicalId("Receipt-1234abcd");
|
||||
resource.getAttributes().put("Arn", "arn:aws:stub:::Receipt");
|
||||
provisioner.provision(resource, properties("first", "store"), context("Receipt-1234abcd"));
|
||||
assertEquals("store", rules.describeReceiptRule("first", "store", REGION).getName());
|
||||
assertTrue(provisioner.rollbackUpdate(resource));
|
||||
assertEquals("Receipt-1234abcd", resource.getPhysicalId());
|
||||
assertEquals("arn:aws:stub:::Receipt", resource.getAttributes().get("Arn"));
|
||||
assertMissing("first", "store");
|
||||
}
|
||||
|
||||
@Test
|
||||
void newUpdateRecoversAnUnfinishedUndoBeforeReplacingItsSnapshot() {
|
||||
StackResource resource = resource();
|
||||
ObjectNode original = properties("first", "store");
|
||||
original.withObject("/Rule").put("Enabled", true);
|
||||
provisioner.provision(resource, original, context(null));
|
||||
provisioner.provision(resource, properties("first", "store"), context("store"));
|
||||
doThrow(new AwsException("InternalFailure", "retry undo", 500)).doCallRealMethod()
|
||||
.when(rules).updateReceiptRule(eq("first"), argThat(ReceiptRule::isEnabled), eq(REGION), any());
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(resource));
|
||||
// The dispatcher passes a fresh resource object, so the previous UPDATE_FAILED status is absent.
|
||||
resource.setStatus("CREATE_IN_PROGRESS");
|
||||
provisioner.provision(resource, properties("first", "store"), context("store"));
|
||||
assertTrue(provisioner.rollbackUpdate(resource));
|
||||
assertTrue(rules.describeReceiptRule("first", "store", REGION).isEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanupFailureRemainsAddressableForRetryAndStackDelete() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "old"), context(null));
|
||||
provisioner.provision(resource, properties("second", "new"), context("old"));
|
||||
doThrow(new AwsException("InternalFailure", "retry", 500)).doCallRealMethod()
|
||||
.when(rules).deleteReceiptRule("first", "old", REGION);
|
||||
assertFalse(provisioner.completeUpdate(resource).complete());
|
||||
provisioner.clearUpdate(resource);
|
||||
assertEquals("old", provisioner.updateCleanupPhysicalId(resource));
|
||||
provisioner.delete(resource, REGION);
|
||||
assertMissing("first", "old");
|
||||
assertMissing("second", "new");
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateReplacementNeverAdoptsOrDeletesAnExternalRule() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "store"), context(null));
|
||||
ReceiptRule external = new ReceiptRule();
|
||||
external.setName("store");
|
||||
rules.createReceiptRule("second", external, null, REGION, sender -> false);
|
||||
assertEquals("AlreadyExists", assertThrows(AwsException.class, () ->
|
||||
provisioner.provision(resource, properties("second", "store"), context("store"))).getErrorCode());
|
||||
provisioner.delete(resource, REGION);
|
||||
assertEquals("store", rules.describeReceiptRule("second", "store", REGION).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stackDeleteToleratesMissingSetButPropagatesRealFailures() {
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, properties("first", "store"), context(null));
|
||||
doThrow(new AwsException("InternalFailure", "unavailable", 500)).doCallRealMethod()
|
||||
.when(rules).deleteReceiptRule("first", "store", REGION);
|
||||
assertThrows(AwsException.class, () -> provisioner.delete(resource, REGION));
|
||||
rules.deleteReceiptRuleSet("first", REGION);
|
||||
assertDoesNotThrow(() -> provisioner.delete(resource, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsEmptyActionsAsAllowedByTheCloudFormationSchema() {
|
||||
ObjectNode props = properties("first", "store");
|
||||
props.withObject("/Rule").putArray("Actions").addObject();
|
||||
provisioner.provision(resource(), props, context(null));
|
||||
assertNull(rules.describeReceiptRule("first", "store", REGION).getActions().getFirst().getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertsNestedActionsAndResolvesIntrinsics() throws Exception {
|
||||
ObjectNode props = properties("first", "store");
|
||||
props.withObject("/Rule").set("Actions", mapper.readTree("""
|
||||
[{"AddHeaderAction":{"HeaderName":"X-Test","HeaderValue":{"Fn::Join":["",["a","b"]]}}},
|
||||
{"StopAction":{"Scope":"RuleSet"}}]
|
||||
"""));
|
||||
props.withObject("/Rule").putArray("Recipients").add("example.com");
|
||||
StackResource resource = resource();
|
||||
provisioner.provision(resource, props, context(null));
|
||||
ReceiptRule actual = rules.describeReceiptRule("first", "store", REGION);
|
||||
assertEquals(List.of("example.com"), actual.getRecipients());
|
||||
assertEquals("ab", actual.getActions().getFirst().property("HeaderValue"));
|
||||
assertEquals("StopAction", actual.getActions().get(1).getType());
|
||||
assertEquals("store", resource.getAttributes().get("RuleName"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{}", "{\"RuleSetName\":\"first\"}",
|
||||
"{\"RuleSetName\":\"first\",\"Rule\":{\"Name\":7}}",
|
||||
"{\"RuleSetName\":\"first\",\"Rule\":{\"Enabled\":\"yes\"}}",
|
||||
"{\"RuleSetName\":\"first\",\"Rule\":{\"Actions\":[{\"StopAction\":{},\"S3Action\":{}}]}}",
|
||||
"{\"RuleSetName\":\"first\",\"Rule\":{\"Actions\":[{\"BogusAction\":{}}]}}"})
|
||||
void rejectsInvalidPropertiesBeforeCreatingAnything(String json) throws Exception {
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(resource(), mapper.readTree(json), context(null)));
|
||||
assertTrue(rules.describeReceiptRuleSet("first", REGION).getRules().isEmpty());
|
||||
}
|
||||
|
||||
private void assertMissing(String set, String name) {
|
||||
assertEquals("RuleDoesNotExist", assertThrows(AwsException.class,
|
||||
() -> rules.describeReceiptRule(set, name, REGION)).getErrorCode());
|
||||
}
|
||||
|
||||
private ObjectNode properties(String set, String name) {
|
||||
ObjectNode props = mapper.createObjectNode().put("RuleSetName", set);
|
||||
ObjectNode rule = props.putObject("Rule");
|
||||
if (name != null) {
|
||||
rule.put("Name", name);
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
private StackResource resource() {
|
||||
StackResource resource = new StackResource();
|
||||
resource.setResourceType("AWS::SES::ReceiptRule");
|
||||
resource.setLogicalId("Receipt");
|
||||
return resource;
|
||||
}
|
||||
|
||||
private ProvisionContext context(String prior) {
|
||||
return new ProvisionContext(CloudFormationTemplateEngine.standalone(ACCOUNT, REGION, "stack", mapper,
|
||||
UnaryOperator.identity()), REGION, ACCOUNT, "stack", prior);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user