diff --git a/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2IntegrationTest.java index e96347ed80..5deaaca377 100644 --- a/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2IntegrationTest.java @@ -358,6 +358,67 @@ class WafV2IntegrationTest { @Test @Order(17) + void tagResourcePersistsTagsForRegexPatternSet() { + Response created = call("CreateRegexPatternSet", + "{\"Name\":\"floci-tagged-regex\",\"Scope\":\"REGIONAL\",\"RegularExpressionList\":[]}"); + created.then().statusCode(200); + String arn = created.jsonPath().getString("Summary.ARN"); + String id = created.jsonPath().getString("Summary.Id"); + + assertTagAndUntagRoundTrip(arn); + + String lockToken = call("GetRegexPatternSet", + "{\"Name\":\"floci-tagged-regex\",\"Scope\":\"REGIONAL\",\"Id\":\"" + id + "\"}") + .then().statusCode(200).extract().jsonPath().getString("LockToken"); + call("DeleteRegexPatternSet", + "{\"Name\":\"floci-tagged-regex\",\"Scope\":\"REGIONAL\",\"Id\":\"" + id + + "\",\"LockToken\":\"" + lockToken + "\"}") + .then().statusCode(200); + } + + @Test + @Order(18) + void tagResourcePersistsTagsForRuleGroup() { + Response created = call("CreateRuleGroup", + "{\"Name\":\"floci-tagged-rules\",\"Scope\":\"REGIONAL\",\"Capacity\":1,\"Rules\":[]}"); + created.then().statusCode(200); + String arn = created.jsonPath().getString("Summary.ARN"); + String id = created.jsonPath().getString("Summary.Id"); + + assertTagAndUntagRoundTrip(arn); + + String lockToken = call("GetRuleGroup", + "{\"Name\":\"floci-tagged-rules\",\"Scope\":\"REGIONAL\",\"Id\":\"" + id + "\"}") + .then().statusCode(200).extract().jsonPath().getString("LockToken"); + call("DeleteRuleGroup", + "{\"Name\":\"floci-tagged-rules\",\"Scope\":\"REGIONAL\",\"Id\":\"" + id + + "\",\"LockToken\":\"" + lockToken + "\"}") + .then().statusCode(200); + } + + private static void assertTagAndUntagRoundTrip(String arn) { + call("TagResource", "{\"ResourceARN\":\"" + arn + + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"test\"},{\"Key\":\"team\",\"Value\":\"waf\"}]}") + .then().statusCode(200); + + call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}") + .then().statusCode(200) + .body("TagInfoForResource.ResourceARN", equalTo(arn)) + .body("TagInfoForResource.TagList", hasSize(2)) + .body("TagInfoForResource.TagList.find { it.Key == 'env' }.Value", equalTo("test")) + .body("TagInfoForResource.TagList.find { it.Key == 'team' }.Value", equalTo("waf")); + + call("UntagResource", "{\"ResourceARN\":\"" + arn + "\",\"TagKeys\":[\"team\"]}") + .then().statusCode(200); + + call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}") + .then().statusCode(200) + .body("TagInfoForResource.TagList", hasSize(1)) + .body("TagInfoForResource.TagList[0].Key", equalTo("env")); + } + + @Test + @Order(19) void teardown() { call("DisassociateWebACL", "{\"ResourceArn\":\"" + API_RESOURCE_ARN + "\"}") .then().statusCode(200); @@ -379,7 +440,7 @@ class WafV2IntegrationTest { } @Test - @Order(18) + @Order(20) void getMissingWebAclReturnsNonexistentItem() { call("GetWebACL", "{\"Name\":\"nope\",\"Scope\":\"REGIONAL\",\"Id\":\"00000000-0000-0000-0000-000000000000\"}") @@ -388,7 +449,7 @@ class WafV2IntegrationTest { } @Test - @Order(19) + @Order(21) void arnLookupsOnMissingResourcesReturnNonexistentItemAs400() { String missingArn = "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/nope/" + "00000000-0000-0000-0000-000000000000"; diff --git a/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagPersistenceTest.java new file mode 100644 index 0000000000..33ad93bea9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagPersistenceTest.java @@ -0,0 +1,67 @@ +package io.github.hectorvent.floci.services.wafv2; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.wafv2.model.RegexPatternSet; +import io.github.hectorvent.floci.services.wafv2.model.RuleGroup; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class WafV2TagPersistenceTest { + + @Test + void regexPatternSetAndRuleGroupTagChangesWriteBackToTheirStores() { + AccountAwareStorageBackend regexStore = spy(AccountAwareStorageBackend.inMemory("000000000000")); + AccountAwareStorageBackend ruleGroupStore = spy(AccountAwareStorageBackend.inMemory("000000000000")); + StorageFactory storageFactory = mock(StorageFactory.class); + when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(invocation -> + switch (invocation.getArgument(1, String.class)) { + case "wafv2-regex-sets.json" -> regexStore; + case "wafv2-rule-groups.json" -> ruleGroupStore; + default -> AccountAwareStorageBackend.inMemory("000000000000"); + }); + WafV2Service service = new WafV2Service(storageFactory, mock(RegionResolver.class), new ObjectMapper()); + + RegexPatternSet regex = new RegexPatternSet(); + regex.setId("regex-id"); + regex.setScope("REGIONAL"); + regex.setArn("arn:aws:wafv2:us-east-1:000000000000:regional/regexpatternset/regex/regex-id"); + regexStore.put("REGIONAL:regex-id", regex); + assertTagWrites(service, regexStore, "REGIONAL:regex-id", regex, regex.getArn()); + + RuleGroup ruleGroup = new RuleGroup(); + ruleGroup.setId("rule-group-id"); + ruleGroup.setScope("REGIONAL"); + ruleGroup.setArn("arn:aws:wafv2:us-east-1:000000000000:regional/rulegroup/rules/rule-group-id"); + ruleGroupStore.put("REGIONAL:rule-group-id", ruleGroup); + assertTagWrites(service, ruleGroupStore, "REGIONAL:rule-group-id", ruleGroup, ruleGroup.getArn()); + } + + private static void assertTagWrites(WafV2Service service, StorageBackend store, + String key, T resource, String arn) { + clearInvocations(store); + service.tagResource(arn, Map.of("team", "waf")); + assertEquals(Map.of("team", "waf"), service.listTagsForResource(arn)); + verify(store).put(key, resource); + + clearInvocations(store); + service.untagResource(arn, List.of("team")); + assertTrue(service.listTagsForResource(arn).isEmpty()); + verify(store).put(key, resource); + } +}