mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): validate WAFv2 WebACL and Config rule required properties (#3576)
Fail fast on missing required properties, read WebACL tags via ListTagsForResource, delete Config rules by name through CfnDeletes and keep absent ComplianceResourceTypes unset. * Fix #2711 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
569f9f55e4
commit
36c8396f25
5 files changed
+284
-27
No files matched your search
+114
@@ -2,11 +2,14 @@ package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* End-to-end check that CloudFormation registers real {@code Fn::GetAtt} attributes for
|
||||
@@ -160,4 +163,115 @@ class CloudFormationWafV2ConfigGetAttIntegrationTest {
|
||||
.statusCode(200)
|
||||
.body("ConfigRules[0].ConfigRuleName", org.hamcrest.Matchers.equalTo(ruleName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStackWithoutWebAclVisibilityConfigFailsWithRequiredPropertyReason() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-wafv2-required-stack-" + suffix;
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Acl": {
|
||||
"Type": "AWS::WAFv2::WebACL",
|
||||
"Properties": {
|
||||
"Name": "cfn-acl-required-%s",
|
||||
"Scope": "REGIONAL",
|
||||
"DefaultAction": {"Allow": {}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(suffix);
|
||||
|
||||
createStack(stackName, template);
|
||||
|
||||
describeStackEvents(stackName)
|
||||
.body(containsString("<ResourceStatus>CREATE_FAILED</ResourceStatus>"))
|
||||
.body(containsString(
|
||||
"<ResourceStatusReason>AWS::WAFv2::WebACL requires VisibilityConfig</ResourceStatusReason>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStackWithoutConfigRuleSourceFailsWithRequiredPropertyReason() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-config-required-stack-" + suffix;
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Rule": {
|
||||
"Type": "AWS::Config::ConfigRule",
|
||||
"Properties": {
|
||||
"ConfigRuleName": "cfn-rule-required-%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(suffix);
|
||||
|
||||
createStack(stackName, template);
|
||||
|
||||
describeStackEvents(stackName)
|
||||
.body(containsString("<ResourceStatus>CREATE_FAILED</ResourceStatus>"))
|
||||
.body(containsString(
|
||||
"<ResourceStatusReason>AWS::Config::ConfigRule requires Source</ResourceStatusReason>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void configRuleTagScopeOmitsComplianceResourceTypes() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String ruleName = "cfn-rule-tagscope-" + suffix;
|
||||
String stackName = "cfn-config-tagscope-stack-" + suffix;
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Rule": {
|
||||
"Type": "AWS::Config::ConfigRule",
|
||||
"Properties": {
|
||||
"ConfigRuleName": "%s",
|
||||
"Scope": {"TagKey": "env", "TagValue": "prod"},
|
||||
"Source": {"Owner": "AWS", "SourceIdentifier": "REQUIRED_TAGS"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(ruleName);
|
||||
|
||||
createStack(stackName, template);
|
||||
|
||||
given()
|
||||
.contentType(CONFIG_JSON)
|
||||
.header("X-Amz-Target", CONFIG_TARGET_PREFIX + "DescribeConfigRules")
|
||||
.body("{\"ConfigRuleNames\":[\"" + ruleName + "\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConfigRules[0].Scope.TagKey", org.hamcrest.Matchers.equalTo("env"))
|
||||
.body("ConfigRules[0].Scope", not(hasKey("ComplianceResourceTypes")));
|
||||
}
|
||||
|
||||
private void createStack(String stackName, String template) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private ValidatableResponse describeStackEvents(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
+61
-3
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.configservice.AwsConfigService;
|
||||
@@ -14,10 +15,17 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
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.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ConfigCfnProvisionerTest {
|
||||
@@ -104,12 +112,62 @@ class ConfigCfnProvisionerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteIsIdempotent() {
|
||||
when(config.describeConfigRules("us-east-1", List.of())).thenReturn(List.of());
|
||||
void configRuleWithoutSourceFailsBeforeCallingConfig() {
|
||||
ObjectNode props = mapper.createObjectNode().put("ConfigRuleName", "tenant-isolation");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource(), props, context()));
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
assertEquals("AWS::Config::ConfigRule requires Source", failure.getMessage());
|
||||
verifyNoInteractions(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopeWithoutComplianceResourceTypesLeavesThemUnset() {
|
||||
ObjectNode props = mapper.createObjectNode().put("ConfigRuleName", "tagged-only");
|
||||
props.set("Scope", mapper.createObjectNode().put("TagKey", "env").put("TagValue", "prod"));
|
||||
props.set("Source", mapper.createObjectNode().put("Owner", "AWS")
|
||||
.put("SourceIdentifier", "REQUIRED_TAGS"));
|
||||
ConfigRule stored = new ConfigRule("tagged-only", "arn:rule", "config-rule-789",
|
||||
null, null, null, null, null, "ACTIVE", null, List.of());
|
||||
when(config.putConfigRule(eq("us-east-1"), any())).thenReturn(stored);
|
||||
|
||||
provisioner.provision(resource(), props, context());
|
||||
|
||||
ArgumentCaptor<ConfigRule> desired = ArgumentCaptor.forClass(ConfigRule.class);
|
||||
verify(config).putConfigRule(eq("us-east-1"), desired.capture());
|
||||
assertEquals("env", desired.getValue().scope().tagKey());
|
||||
assertNull(desired.getValue().scope().complianceResourceTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRemovesRuleByNameWithoutListingEveryRule() {
|
||||
provisioner.delete("AWS::Config::ConfigRule", "tenant-isolation", "us-east-1");
|
||||
|
||||
verify(config).deleteConfigRule("us-east-1", "tenant-isolation");
|
||||
verify(config, never()).describeConfigRules(anyString(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesRuleAlreadyGone() {
|
||||
doThrow(new AwsException("NoSuchConfigRuleException", "missing", 400))
|
||||
.when(config).deleteConfigRule("us-east-1", "missing");
|
||||
|
||||
provisioner.delete("AWS::Config::ConfigRule", "missing", "us-east-1");
|
||||
|
||||
verify(config).describeConfigRules("us-east-1", List.of());
|
||||
verify(config).deleteConfigRule("us-east-1", "missing");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePropagatesRealFailure() {
|
||||
doThrow(new AwsException("ResourceInUseException", "remediation attached", 400))
|
||||
.when(config).deleteConfigRule("us-east-1", "in-use");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.delete("AWS::Config::ConfigRule", "in-use", "us-east-1"));
|
||||
|
||||
assertEquals("ResourceInUseException", failure.getErrorCode());
|
||||
}
|
||||
|
||||
private ProvisionContext context() {
|
||||
|
||||
+84
-12
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.wafv2.WafV2Service;
|
||||
@@ -12,12 +13,17 @@ import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class WafV2CfnProvisionerTest {
|
||||
@@ -28,12 +34,7 @@ class WafV2CfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void webAclCreatesBackingResourceAndSetsAwsReferences() {
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("Name", "portal")
|
||||
.put("Scope", "REGIONAL")
|
||||
.put("Description", "edge policy");
|
||||
props.set("DefaultAction", mapper.createObjectNode().set("Allow", mapper.createObjectNode()));
|
||||
props.set("VisibilityConfig", mapper.createObjectNode().put("MetricName", "portal"));
|
||||
ObjectNode props = requiredProps("portal").put("Description", "edge policy");
|
||||
props.set("Rules", mapper.createArrayNode().add(mapper.createObjectNode().put("Name", "managed")));
|
||||
props.set("Tags", mapper.createArrayNode()
|
||||
.add(mapper.createObjectNode().put("Key", "Owner").put("Value", "Platform")));
|
||||
@@ -67,14 +68,34 @@ class WafV2CfnProvisionerTest {
|
||||
when(wafV2.getWebAcl("REGIONAL", "acl-123", "portal")).thenReturn(updated);
|
||||
StackResource resource = resource();
|
||||
resource.setPhysicalId("portal|acl-123|REGIONAL");
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "portal").put("Scope", "REGIONAL");
|
||||
|
||||
provisioner.provision(resource, props, context());
|
||||
provisioner.provision(resource, requiredProps("portal"), context());
|
||||
|
||||
verify(wafV2).updateWebAcl(any(), eq("REGIONAL"), eq("acl-123"), eq("portal"), eq("lock-1"));
|
||||
assertEquals("portal|acl-123|REGIONAL", resource.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void inPlaceUpdateReconcilesTagsReadFromListTagsForResource() {
|
||||
WebAcl existing = acl("portal", "acl-123", "REGIONAL", "lock-1");
|
||||
WebAcl fetched = acl("portal", "acl-123", "REGIONAL", "lock-2");
|
||||
fetched.setArn("arn:portal");
|
||||
when(wafV2.listWebAcls("REGIONAL")).thenReturn(List.of(existing));
|
||||
when(wafV2.getWebAcl("REGIONAL", "acl-123", "portal")).thenReturn(fetched);
|
||||
when(wafV2.listTagsForResource("arn:portal"))
|
||||
.thenReturn(Map.of("Owner", "Security", "Stale", "yes"));
|
||||
StackResource resource = resource();
|
||||
resource.setPhysicalId("portal|acl-123|REGIONAL");
|
||||
ObjectNode props = requiredProps("portal");
|
||||
props.set("Tags", mapper.createArrayNode()
|
||||
.add(mapper.createObjectNode().put("Key", "Owner").put("Value", "Platform")));
|
||||
|
||||
provisioner.provision(resource, props, context());
|
||||
|
||||
verify(wafV2).untagResource("arn:portal", List.of("Stale"));
|
||||
verify(wafV2).tagResource("arn:portal", Map.of("Owner", "Platform"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacementTracksNewAclEvenWhenOldAclDeleteFails() {
|
||||
WebAcl existing = acl("portal", "acl-old", "REGIONAL", "lock-1");
|
||||
@@ -87,9 +108,8 @@ class WafV2CfnProvisionerTest {
|
||||
.when(wafV2).deleteWebAcl("REGIONAL", "acl-old", "portal", "lock-1");
|
||||
StackResource resource = resource();
|
||||
resource.setPhysicalId("portal|acl-old|REGIONAL");
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "portal-renamed").put("Scope", "REGIONAL");
|
||||
|
||||
provisioner.provision(resource, props, context());
|
||||
provisioner.provision(resource, requiredProps("portal-renamed"), context());
|
||||
|
||||
assertEquals("portal-renamed|acl-new|REGIONAL", resource.getPhysicalId());
|
||||
assertEquals("arn:new", resource.getAttributes().get("Arn"));
|
||||
@@ -97,9 +117,9 @@ class WafV2CfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void intrinsicResolvedRuleFieldIsNotDoubleEncoded() {
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "portal").put("Scope", "REGIONAL");
|
||||
ObjectNode props = requiredProps("portal");
|
||||
props.set("DefaultAction", mapper.createObjectNode());
|
||||
ObjectNode resolved = mapper.createObjectNode().put("Name", "portal").put("Scope", "REGIONAL");
|
||||
ObjectNode resolved = requiredProps("portal");
|
||||
resolved.set("DefaultAction",
|
||||
com.fasterxml.jackson.databind.node.TextNode.valueOf("{\"Allow\":{}}"));
|
||||
WebAcl created = acl("portal", "acl-1", "REGIONAL", "lock-1");
|
||||
@@ -116,6 +136,58 @@ class WafV2CfnProvisionerTest {
|
||||
assertEquals("{\"Allow\":{}}", desired.getValue().getDefaultAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
void webAclWithoutScopeFailsBeforeCallingWafV2() {
|
||||
assertMissingRequiredPropertyFails("Scope");
|
||||
}
|
||||
|
||||
@Test
|
||||
void webAclWithoutDefaultActionFailsBeforeCallingWafV2() {
|
||||
assertMissingRequiredPropertyFails("DefaultAction");
|
||||
}
|
||||
|
||||
@Test
|
||||
void webAclWithoutVisibilityConfigFailsBeforeCallingWafV2() {
|
||||
assertMissingRequiredPropertyFails("VisibilityConfig");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateWithoutScopeFailsInsteadOfReplacingTheWebAcl() {
|
||||
WebAcl existing = acl("portal", "acl-123", "REGIONAL", "lock-1");
|
||||
when(wafV2.listWebAcls("REGIONAL")).thenReturn(List.of(existing));
|
||||
StackResource resource = resource();
|
||||
resource.setPhysicalId("portal|acl-123|REGIONAL");
|
||||
ObjectNode props = requiredProps("portal");
|
||||
props.remove("Scope");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource, props, context()));
|
||||
|
||||
assertEquals("AWS::WAFv2::WebACL requires Scope", failure.getMessage());
|
||||
verify(wafV2, never()).createWebAcl(any(), any(), anyString(), anyString());
|
||||
verify(wafV2, never()).deleteWebAcl(any(), anyString(), anyString(), anyString());
|
||||
assertEquals("portal|acl-123|REGIONAL", resource.getPhysicalId());
|
||||
}
|
||||
|
||||
private void assertMissingRequiredPropertyFails(String property) {
|
||||
ObjectNode props = requiredProps("portal");
|
||||
props.remove(property);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource(), props, context()));
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
assertEquals("AWS::WAFv2::WebACL requires " + property, failure.getMessage());
|
||||
verifyNoInteractions(wafV2);
|
||||
}
|
||||
|
||||
private ObjectNode requiredProps(String name) {
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", name).put("Scope", "REGIONAL");
|
||||
props.set("DefaultAction", mapper.createObjectNode().set("Allow", mapper.createObjectNode()));
|
||||
props.set("VisibilityConfig", mapper.createObjectNode().put("MetricName", name));
|
||||
return props;
|
||||
}
|
||||
|
||||
private ProvisionContext context() {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolveNode(any(JsonNode.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
Reference in new issue
Block a user