feat(cloudformation): provision AWS::WAFv2::IPSet (#5950)

* feat(cloudformation): provision AWS::WAFv2::IPSet

Serve AWS::WAFv2::IPSet from WafV2CfnProvisioner instead of the
dispatcher stub, so stacks with IP sets no longer fail in strict mode
and Web ACL rules can resolve a real IP set ARN.

Name and Scope are create-only: a change replaces the set with a new
name|id|scope physical id (Ref) and deletes the prior one the same way
a Web ACL replacement does, tolerating a cleanup failure so the new set
is still tracked. Addresses, Description and Tags update in place
through UpdateIPSet; IPAddressVersion is never passed to the update,
matching the real API, so a template that flips it fails address
validation and reports UPDATE_FAILED. Fn::GetAtt resolves Arn and Id
from the backing set, and DeleteStack removes it, tolerating a set
already gone.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(cloudformation): defer IPSet replacement cleanup and honor its optional name

Review follow-ups on the AWS::WAFv2::IPSet provisioner:

- A renamed or re-scoped set now records the displaced one with
  ReplacementCleanup, so a failing delete of the prior set is reported
  by completeUpdate instead of being tolerated, and rollbackUpdate
  restores the prior set and deletes the replacement, the same contract
  the S3 bucket replacement follows.
- IPAddressVersion is validated against IPV4/IPV6 on every provision.
  An in-place update never sends it to UpdateIPSet, so an unknown value
  would otherwise be accepted without ever reaching WAFv2.
- Name is optional in the CloudFormation schema, matching what the Web
  ACL arm in the same provisioner already does: an omitted Name
  generates one on a create and reuses the existing set's name on an
  update.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

* fix(cloudformation): mark AWS::WAFv2::IPSet Name and Scope create-only

Name and Scope are create-only in the registry schema, so without an entry
in create-only-properties.json a Scope change fell to the name-based
default and showed Replacement: False in the change set. A resource that
reads the set's Ref or Arn was then skipped by the update and kept the ARN
of the set that replacement cleanup deletes.

Also share the IPV4/IPV6 rule between the provisioner and
WafV2Service.validateAddresses, which repeated it, so an unknown
IPAddressVersion is rejected in one place.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>

---------

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-18 20:32:42 -04:00
1 parent 06bd8c2207
commit a57c20943a
8 files changed
+666 -23

No files matched your search

@@ -51,6 +51,17 @@ class CfnCreateOnlyPropertiesTest {
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Tags"));
}
@Test
void isCreateOnly_ipSetReplacesOnlyForNameAndScope() {
String type = "AWS::WAFv2::IPSet";
assertTrue(CfnCreateOnlyProperties.isCreateOnly(type, "Name"));
assertTrue(CfnCreateOnlyProperties.isCreateOnly(type, "Scope"));
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Addresses"));
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Description"));
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "IPAddressVersion"));
assertFalse(CfnCreateOnlyProperties.isCreateOnly(type, "Tags"));
}
@Test
void isCreateOnly_treatsEmptySchemaListAsAuthoritative() {
assertFalse(CfnCreateOnlyProperties.isCreateOnly("AWS::ApiGateway::RestApi", "Name"));
@@ -0,0 +1,240 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* AWS::WAFv2::IPSet through a stack: Ref is {@code name|id|scope}, {@code Fn::GetAtt} resolves the
* real {@code Arn} and {@code Id} rather than literal placeholders, an {@code Addresses} change
* updates in place keeping the id, a rename replaces the set, an omitted {@code Name} still gets a
* generated one, and deleting the stack deletes it. Every assertion reads WAFv2 back, not only the
* stack.
*/
@QuarkusTest
class CloudFormationWafV2IpSetIntegrationTest {
private static final String CFN_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
private static final String WAF_JSON = "application/x-amz-json-1.1";
private static final String WAF_TARGET_PREFIX = "AWSWAF_20190729.";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void ipSetFollowsTheTemplateAcrossCreateUpdateAndStackDelete() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-wafv2-ipset-" + suffix;
String nameA = "cfn-ipset-a-" + suffix;
String nameB = "cfn-ipset-b-" + suffix;
cfn("CreateStack", stackName, template(nameA, "\"192.0.2.1/32\""));
String created = describeStack(stackName)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
.extract().asString();
String id = output(created, "IpSetId");
String arn = output(created, "IpSetArn");
assertFalse(id.contains("IpSet."), "Fn::GetAtt Id resolved to a literal placeholder");
assertFalse(arn.contains("IpSet."), "Fn::GetAtt Arn resolved to a literal placeholder");
assertEquals(nameA + "|" + id + "|REGIONAL", output(created, "IpSetRef"));
ipSet(nameA, id)
.body("IPSet.ARN", equalTo(arn))
.body("IPSet.Addresses", contains("192.0.2.1/32"));
cfn("UpdateStack", stackName, template(nameA, "\"198.51.100.0/24\""));
String addressesUpdated = describeStack(stackName)
.body(containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"))
.extract().asString();
assertEquals(id, output(addressesUpdated, "IpSetId"));
assertEquals(arn, output(addressesUpdated, "IpSetArn"));
ipSet(nameA, id).body("IPSet.Addresses", contains("198.51.100.0/24"));
cfn("UpdateStack", stackName, template(nameB, "\"198.51.100.0/24\""));
String replaced = describeStack(stackName)
.body(containsString("<StackStatus>UPDATE_COMPLETE</StackStatus>"))
.body(not(containsString("ROLLBACK")))
.extract().asString();
String replacementId = output(replaced, "IpSetId");
String replacementArn = output(replaced, "IpSetArn");
assertFalse(replacementId.equals(id), "a renamed IP set must be replaced, not updated in place");
assertEquals(nameB + "|" + replacementId + "|REGIONAL", output(replaced, "IpSetRef"));
ipSet(nameB, replacementId).body("IPSet.ARN", equalTo(replacementArn));
listIpSets()
.body("IPSets.ARN", not(hasItem(arn)))
.body("IPSets.ARN", hasItem(replacementArn));
cfn("DeleteStack", stackName, null);
listIpSets().body("IPSets.ARN", not(hasItem(replacementArn)));
}
@Test
void createStackWithoutIpSetAddressesFailsWithRequiredPropertyReason() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-wafv2-ipset-required-" + suffix;
String template = """
{
"Resources": {
"IpSet": {
"Type": "AWS::WAFv2::IPSet",
"Properties": {
"Name": "cfn-ipset-required-%s",
"Scope": "REGIONAL",
"IPAddressVersion": "IPV4"
}
}
}
}
""".formatted(suffix);
cfn("CreateStack", stackName, template);
describeStackEvents(stackName)
.body(containsString("<ResourceStatus>CREATE_FAILED</ResourceStatus>"))
.body(containsString(
"<ResourceStatusReason>AWS::WAFv2::IPSet requires Addresses</ResourceStatusReason>"));
}
@Test
void createStackWithoutIpSetNameStillCreatesTheSet() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-wafv2-ipset-anon-" + suffix;
String template = """
{
"Resources": {
"IpSet": {
"Type": "AWS::WAFv2::IPSet",
"Properties": {
"Scope": "REGIONAL",
"IPAddressVersion": "IPV4",
"Addresses": ["203.0.113.10/32"]
}
}
},
"Outputs": {
"IpSetRef": {"Value": {"Ref": "IpSet"}},
"IpSetArn": {"Value": {"Fn::GetAtt": ["IpSet", "Arn"]}},
"IpSetId": {"Value": {"Fn::GetAtt": ["IpSet", "Id"]}}
}
}
""";
cfn("CreateStack", stackName, template);
String created = describeStack(stackName)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
.extract().asString();
String ref = output(created, "IpSetRef");
String generatedName = ref.substring(0, ref.indexOf('|'));
assertFalse(generatedName.isBlank(), "an IP set without Name must still get a generated one");
ipSet(generatedName, output(created, "IpSetId"))
.body("IPSet.ARN", equalTo(output(created, "IpSetArn")));
cfn("DeleteStack", stackName, null);
listIpSets().body("IPSets.ARN", not(hasItem(output(created, "IpSetArn"))));
}
private static String template(String name, String addresses) {
return """
{
"Resources": {
"IpSet": {
"Type": "AWS::WAFv2::IPSet",
"Properties": {
"Name": "%1$s",
"Scope": "REGIONAL",
"IPAddressVersion": "IPV4",
"Addresses": [%2$s],
"Description": "cfn ip set"
}
}
},
"Outputs": {
"IpSetRef": {"Value": {"Ref": "IpSet"}},
"IpSetArn": {"Value": {"Fn::GetAtt": ["IpSet", "Arn"]}},
"IpSetId": {"Value": {"Fn::GetAtt": ["IpSet", "Id"]}}
}
}
""".formatted(name, addresses);
}
private static String output(String describeXml, String key) {
return XmlPath.from(describeXml).getString("DescribeStacksResponse.DescribeStacksResult.Stacks.member"
+ ".Outputs.member.find { it.OutputKey == '" + key + "' }.OutputValue");
}
private void cfn(String action, String stackName, String template) {
RequestSpecification request = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", action)
.formParam("StackName", stackName);
if (template != null) {
request.formParam("TemplateBody", template);
}
request.when().post("/").then().statusCode(200);
}
private ValidatableResponse describeStack(String stackName) {
return given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.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);
}
private ValidatableResponse ipSet(String name, String id) {
return waf("GetIPSet", "{\"Name\":\"" + name + "\",\"Scope\":\"REGIONAL\",\"Id\":\"" + id + "\"}")
.statusCode(200);
}
private ValidatableResponse listIpSets() {
return waf("ListIPSets", "{\"Scope\":\"REGIONAL\"}").statusCode(200);
}
private ValidatableResponse waf(String action, String body) {
return given()
.contentType(WAF_JSON)
.header("X-Amz-Target", WAF_TARGET_PREFIX + action)
.body(body)
.when()
.post("/")
.then();
}
}
@@ -7,6 +7,7 @@ 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;
import io.github.hectorvent.floci.services.wafv2.model.IpSet;
import io.github.hectorvent.floci.services.wafv2.model.WebAcl;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -21,8 +22,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
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.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -33,6 +36,7 @@ import static org.mockito.Mockito.when;
class WafV2CfnProvisionerTest {
private static final String ASSOCIATION = "AWS::WAFv2::WebACLAssociation";
private static final String IP_SET_TYPE = "AWS::WAFv2::IPSet";
private static final String POOL = "arn:aws:cognito-idp:us-east-1:111122223333:userpool/us-east-1_pool";
private static final String POOL_2 = "arn:aws:cognito-idp:us-east-1:111122223333:userpool/us-east-1_pool2";
private static final String ACL_A = "arn:aws:wafv2:us-east-1:111122223333:regional/webacl/a/acl-a";
@@ -114,7 +118,7 @@ class WafV2CfnProvisionerTest {
when(wafV2.listWebAcls("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.createWebAcl(any(), eq("REGIONAL"), eq("portal-renamed"), eq("us-east-1")))
.thenReturn(created);
org.mockito.Mockito.doThrow(new RuntimeException("WAFAssociatedItemException"))
doThrow(new RuntimeException("WAFAssociatedItemException"))
.when(wafV2).deleteWebAcl("REGIONAL", "acl-old", "portal", "lock-1");
StackResource resource = resource();
resource.setPhysicalId("portal|acl-old|REGIONAL");
@@ -334,6 +338,280 @@ class WafV2CfnProvisionerTest {
assertFalse(provisioner.rollbackUpdate(resource));
}
@Test
void ipSetCreatesBackingResourceAndSetsAwsReferences() {
ObjectNode props = ipSetProps("blocklist");
props.put("Description", "deny list");
props.set("Tags", mapper.createArrayNode()
.add(mapper.createObjectNode().put("Key", "Owner").put("Value", "Security")));
IpSet created = ipSet("blocklist", "ip-123", "REGIONAL", "lock-1");
created.setArn("arn:aws:wafv2:us-east-1:111122223333:regional/ipset/blocklist/ip-123");
when(wafV2.createIpSet(any(), eq("REGIONAL"), eq("blocklist"), eq("us-east-1")))
.thenReturn(created);
StackResource resource = ipSetResource();
provisioner.provision(resource, props, context());
ArgumentCaptor<IpSet> desired = ArgumentCaptor.forClass(IpSet.class);
verify(wafV2).createIpSet(desired.capture(), eq("REGIONAL"), eq("blocklist"), eq("us-east-1"));
assertEquals("deny list", desired.getValue().getDescription());
assertEquals("IPV4", desired.getValue().getIpAddressVersion());
assertEquals(List.of("192.0.2.1/32"), desired.getValue().getAddresses());
assertEquals("Security", desired.getValue().getTags().get("Owner"));
assertEquals("blocklist|ip-123|REGIONAL", resource.getPhysicalId());
assertEquals(created.getArn(), resource.getAttributes().get("Arn"));
assertEquals("ip-123", resource.getAttributes().get("Id"));
}
@Test
void sameNamedIpSetUpdatesInPlaceWithoutPassingTheIpAddressVersion() {
IpSet existing = ipSet("blocklist", "ip-123", "REGIONAL", "lock-1");
IpSet updated = ipSet("blocklist", "ip-123", "REGIONAL", "lock-2");
updated.setArn("arn:updated");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.getIpSet("REGIONAL", "ip-123", "blocklist")).thenReturn(updated);
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-123|REGIONAL");
ObjectNode props = ipSetProps("blocklist");
props.put("Description", "rotated");
props.set("Addresses", mapper.createArrayNode().add("198.51.100.7/32"));
provisioner.provision(resource, props, context());
verify(wafV2).updateIpSet(eq("REGIONAL"), eq("ip-123"), eq("rotated"),
eq(List.of("198.51.100.7/32")), eq("blocklist"), eq("lock-1"));
verify(wafV2, never()).createIpSet(any(), any(), anyString(), anyString());
assertEquals("blocklist|ip-123|REGIONAL", resource.getPhysicalId());
assertEquals("arn:updated", resource.getAttributes().get("Arn"));
assertEquals("ip-123", resource.getAttributes().get("Id"));
}
@Test
void ipSetInPlaceUpdateReconcilesTags() {
IpSet existing = ipSet("blocklist", "ip-123", "REGIONAL", "lock-1");
IpSet fetched = ipSet("blocklist", "ip-123", "REGIONAL", "lock-2");
fetched.setArn("arn:blocklist");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.getIpSet("REGIONAL", "ip-123", "blocklist")).thenReturn(fetched);
when(wafV2.listTagsForResource("arn:blocklist"))
.thenReturn(Map.of("Owner", "Security", "Stale", "yes"));
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-123|REGIONAL");
ObjectNode props = ipSetProps("blocklist");
props.set("Tags", mapper.createArrayNode()
.add(mapper.createObjectNode().put("Key", "Owner").put("Value", "Platform")));
provisioner.provision(resource, props, context());
verify(wafV2).untagResource("arn:blocklist", List.of("Stale"));
verify(wafV2).tagResource("arn:blocklist", Map.of("Owner", "Platform"));
}
@Test
void renamedIpSetRecordsThePriorSetAndCommittedCleanupDeletesIt() {
IpSet existing = ipSet("blocklist", "ip-old", "REGIONAL", "lock-1");
IpSet created = ipSet("blocklist-v2", "ip-new", "REGIONAL", "lock-2");
created.setArn("arn:new");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.createIpSet(any(), eq("REGIONAL"), eq("blocklist-v2"), eq("us-east-1")))
.thenReturn(created);
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-old|REGIONAL");
provisioner.provision(resource, ipSetProps("blocklist-v2"),
updateContext("blocklist|ip-old|REGIONAL"));
assertEquals("blocklist-v2|ip-new|REGIONAL", resource.getPhysicalId());
assertEquals("arn:new", resource.getAttributes().get("Arn"));
assertTrue(provisioner.hasReplacementUpdate(resource));
assertEquals("blocklist|ip-old|REGIONAL", provisioner.updateCleanupPhysicalId(resource));
verify(wafV2, never()).deleteIpSet(any(), anyString(), anyString(), anyString());
UpdateCleanupResult result = provisioner.completeUpdate(resource);
assertTrue(result.complete());
verify(wafV2).deleteIpSet("REGIONAL", "ip-old", "blocklist", "lock-1");
assertFalse(provisioner.hasReplacementUpdate(resource));
}
@Test
void committedCleanupOfAnIpSetStillReportsThePriorSetWhenItsDeleteFails() {
IpSet existing = ipSet("blocklist", "ip-old", "REGIONAL", "lock-1");
IpSet created = ipSet("blocklist-v2", "ip-new", "REGIONAL", "lock-2");
created.setArn("arn:new");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.createIpSet(any(), eq("REGIONAL"), eq("blocklist-v2"), eq("us-east-1")))
.thenReturn(created);
doThrow(new RuntimeException("WAFAssociatedItemException"))
.when(wafV2).deleteIpSet("REGIONAL", "ip-old", "blocklist", "lock-1");
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-old|REGIONAL");
provisioner.provision(resource, ipSetProps("blocklist-v2"),
updateContext("blocklist|ip-old|REGIONAL"));
UpdateCleanupResult result = provisioner.completeUpdate(resource);
assertTrue(result.applicable());
assertFalse(result.complete());
assertEquals("blocklist|ip-old|REGIONAL", result.previousPhysicalId());
assertEquals(1, result.attempts());
assertEquals("WAFAssociatedItemException", result.failureReason());
assertEquals("blocklist-v2|ip-new|REGIONAL", resource.getPhysicalId());
assertTrue(provisioner.hasReplacementUpdate(resource));
}
@Test
void rollbackOfAnIpSetReplacementRestoresThePriorSetAndDeletesTheReplacement() {
IpSet existing = ipSet("blocklist", "ip-old", "REGIONAL", "lock-1");
IpSet created = ipSet("blocklist-v2", "ip-new", "REGIONAL", "lock-2");
created.setArn("arn:new");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing, created));
when(wafV2.createIpSet(any(), eq("REGIONAL"), eq("blocklist-v2"), eq("us-east-1")))
.thenReturn(created);
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-old|REGIONAL");
provisioner.provision(resource, ipSetProps("blocklist-v2"),
updateContext("blocklist|ip-old|REGIONAL"));
assertTrue(provisioner.rollbackUpdate(resource));
assertEquals("blocklist|ip-old|REGIONAL", resource.getPhysicalId());
assertFalse(provisioner.hasReplacementUpdate(resource));
verify(wafV2).deleteIpSet("REGIONAL", "ip-new", "blocklist-v2", "lock-2");
}
@Test
void ipSetWithoutNameGeneratesOneOnCreate() {
ObjectNode props = ipSetProps("blocklist");
props.remove("Name");
when(wafV2.createIpSet(any(), eq("REGIONAL"), anyString(), eq("us-east-1")))
.thenAnswer(invocation -> {
IpSet created = ipSet(invocation.getArgument(2), "ip-123", "REGIONAL", "lock-1");
created.setArn("arn:generated");
return created;
});
StackResource resource = ipSetResource();
provisioner.provision(resource, props, context());
ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class);
verify(wafV2).createIpSet(any(), eq("REGIONAL"), name.capture(), eq("us-east-1"));
assertFalse(name.getValue().isBlank());
assertEquals(name.getValue() + "|ip-123|REGIONAL", resource.getPhysicalId());
assertEquals("arn:generated", resource.getAttributes().get("Arn"));
}
@Test
void ipSetWithoutNameReusesTheExistingNameOnUpdate() {
IpSet existing = ipSet("blocklist", "ip-123", "REGIONAL", "lock-1");
IpSet updated = ipSet("blocklist", "ip-123", "REGIONAL", "lock-2");
updated.setArn("arn:updated");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
when(wafV2.getIpSet("REGIONAL", "ip-123", "blocklist")).thenReturn(updated);
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-123|REGIONAL");
ObjectNode props = ipSetProps("blocklist");
props.remove("Name");
provisioner.provision(resource, props, context());
verify(wafV2).updateIpSet(eq("REGIONAL"), eq("ip-123"), any(), anyList(),
eq("blocklist"), eq("lock-1"));
verify(wafV2, never()).createIpSet(any(), any(), anyString(), anyString());
}
@Test
void ipSetWithUnknownIpAddressVersionFailsBeforeCallingWafV2() {
ObjectNode props = ipSetProps("blocklist");
props.put("IPAddressVersion", "IPV5");
AwsException failure = assertThrows(AwsException.class,
() -> provisioner.provision(ipSetResource(), props, context()));
assertEquals("WAFInvalidParameterException", failure.getErrorCode());
verifyNoInteractions(wafV2);
}
@Test
void ipSetWithoutScopeFailsBeforeCallingWafV2() {
assertMissingIpSetPropertyFails("Scope");
}
@Test
void ipSetWithoutIpAddressVersionFailsBeforeCallingWafV2() {
assertMissingIpSetPropertyFails("IPAddressVersion");
}
@Test
void ipSetWithoutAddressesFailsBeforeCallingWafV2() {
assertMissingIpSetPropertyFails("Addresses");
}
@Test
void ipSetDeleteRemovesTheBackingResource() {
IpSet existing = ipSet("blocklist", "ip-123", "REGIONAL", "lock-1");
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of(existing));
provisioner.delete(IP_SET_TYPE, "blocklist|ip-123|REGIONAL", "us-east-1");
verify(wafV2).deleteIpSet("REGIONAL", "ip-123", "blocklist", "lock-1");
}
@Test
void ipSetDeleteIsNoOpWhenAlreadyGone() {
when(wafV2.listIpSets("REGIONAL")).thenReturn(List.of());
provisioner.delete(IP_SET_TYPE, "blocklist|ip-123|REGIONAL", "us-east-1");
verify(wafV2, never()).deleteIpSet(any(), anyString(), anyString(), anyString());
}
@Test
void ipSetRollbackUpdateKeepsTheDefault() {
StackResource resource = ipSetResource();
resource.setPhysicalId("blocklist|ip-123|REGIONAL");
assertFalse(provisioner.rollbackUpdate(resource));
}
private void assertMissingIpSetPropertyFails(String property) {
ObjectNode props = ipSetProps("blocklist");
props.remove(property);
AwsException failure = assertThrows(AwsException.class,
() -> provisioner.provision(ipSetResource(), props, context()));
assertEquals("ValidationError", failure.getErrorCode());
assertEquals("AWS::WAFv2::IPSet requires " + property, failure.getMessage());
verifyNoInteractions(wafV2);
}
private ObjectNode ipSetProps(String name) {
ObjectNode props = mapper.createObjectNode()
.put("Name", name)
.put("Scope", "REGIONAL")
.put("IPAddressVersion", "IPV4");
props.set("Addresses", mapper.createArrayNode().add("192.0.2.1/32"));
return props;
}
private StackResource ipSetResource() {
StackResource resource = new StackResource();
resource.setLogicalId("Blocklist");
resource.setResourceType(IP_SET_TYPE);
resource.setAttributes(new HashMap<>());
return resource;
}
private IpSet ipSet(String name, String id, String scope, String lockToken) {
IpSet ipSet = new IpSet();
ipSet.setName(name);
ipSet.setId(id);
ipSet.setScope(scope);
ipSet.setLockToken(lockToken);
return ipSet;
}
private ObjectNode associationProps(String webAclArn) {
return associationProps(POOL, webAclArn);
}