diff --git a/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java b/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java index 16ec0eaeee..84e50908f9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java @@ -274,11 +274,8 @@ public class WafV2Service { // ──────────────────────────── Association ──────────────────────────── public void associateWebAcl(String webAclArn, String resourceArn) { - boolean known = webAclStore.scan(k -> true).stream() - .anyMatch(a -> a.getArn().equals(webAclArn)); - if (!known) { - throw new AwsException("WAFNonexistentItemException", - "AWS WAF couldn't perform the operation because your resource doesn't exist.", 400); + if (scanArn(webAclStore, webAclArn) == null) { + throw nonexistentItem(); } associationStore.put(resourceArn, webAclArn); } @@ -310,6 +307,9 @@ public class WafV2Service { // ──────────────────────────── Logging ──────────────────────────── public String putLoggingConfiguration(String resourceArn, String loggingConfigJson) { + if (scanArn(webAclStore, resourceArn) == null) { + throw nonexistentItem(); + } loggingStore.put(resourceArn, loggingConfigJson); return loggingConfigJson; } @@ -438,16 +438,18 @@ public class WafV2Service { if (name == null || name.isBlank()) { throw new AwsException("WAFInvalidParameterException", "Name is required.", 400); } - V resource = store.get(key(scope, id)).orElseThrow(() -> new AwsException( - "WAFNonexistentItemException", - "AWS WAF couldn't perform the operation because your resource doesn't exist.", 400)); + V resource = store.get(key(scope, id)).orElseThrow(WafV2Service::nonexistentItem); if (!name.equals(nameOf(resource))) { - throw new AwsException("WAFNonexistentItemException", - "AWS WAF couldn't perform the operation because your resource doesn't exist.", 400); + throw nonexistentItem(); } return resource; } + private static AwsException nonexistentItem() { + return new AwsException("WAFNonexistentItemException", + "AWS WAF couldn't perform the operation because your resource doesn't exist.", 400); + } + private V findByName(StorageBackend store, String scope, String name) { return store.scan(k -> true).stream() .filter(v -> scope.equals(scopeOf(v)) && name.equals(nameOf(v))) 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 5deaaca377..a6a6839a3d 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 @@ -9,6 +9,8 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.util.UUID; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -28,6 +30,8 @@ class WafV2IntegrationTest { private static final String PREFIX = "AWSWAF_20190729."; private static final String API_RESOURCE_ARN = "arn:aws:apigateway:us-east-1::/restapis/floci-waf-test/stages/prod"; + private static final String LOG_DESTINATION_ARN = + "arn:aws:firehose:us-east-1:000000000000:deliverystream/aws-waf-logs-test"; private static String ipSetId; private static String ipSetArn; @@ -419,6 +423,24 @@ class WafV2IntegrationTest { @Test @Order(19) + void putLoggingConfigurationForExistingWebAclSucceeds() { + call("PutLoggingConfiguration", + "{\"LoggingConfiguration\":{\"ResourceArn\":\"" + webAclArn + "\"," + + "\"LogDestinationConfigs\":[\"" + LOG_DESTINATION_ARN + "\"]}}") + .then().statusCode(200) + .body("LoggingConfiguration.ResourceArn", equalTo(webAclArn)) + .body("LoggingConfiguration.LogDestinationConfigs[0]", equalTo(LOG_DESTINATION_ARN)); + + call("GetLoggingConfiguration", "{\"ResourceArn\":\"" + webAclArn + "\"}") + .then().statusCode(200) + .body("LoggingConfiguration.ResourceArn", equalTo(webAclArn)); + + call("DeleteLoggingConfiguration", "{\"ResourceArn\":\"" + webAclArn + "\"}") + .then().statusCode(200); + } + + @Test + @Order(20) void teardown() { call("DisassociateWebACL", "{\"ResourceArn\":\"" + API_RESOURCE_ARN + "\"}") .then().statusCode(200); @@ -440,7 +462,7 @@ class WafV2IntegrationTest { } @Test - @Order(20) + @Order(21) void getMissingWebAclReturnsNonexistentItem() { call("GetWebACL", "{\"Name\":\"nope\",\"Scope\":\"REGIONAL\",\"Id\":\"00000000-0000-0000-0000-000000000000\"}") @@ -449,7 +471,7 @@ class WafV2IntegrationTest { } @Test - @Order(21) + @Order(22) void arnLookupsOnMissingResourcesReturnNonexistentItemAs400() { String missingArn = "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/nope/" + "00000000-0000-0000-0000-000000000000"; @@ -471,4 +493,20 @@ class WafV2IntegrationTest { .then().statusCode(400) .body("__type", equalTo("WAFNonexistentItemException")); } + + @Test + @Order(23) + void putLoggingConfigurationForMissingWebAclDoesNotPersist() { + String missingArn = "arn:aws:wafv2:us-east-1:000000000000:regional/webacl/nope/" + UUID.randomUUID(); + + call("PutLoggingConfiguration", + "{\"LoggingConfiguration\":{\"ResourceArn\":\"" + missingArn + "\"," + + "\"LogDestinationConfigs\":[\"" + LOG_DESTINATION_ARN + "\"]}}") + .then().statusCode(400) + .body("__type", equalTo("WAFNonexistentItemException")); + + call("GetLoggingConfiguration", "{\"ResourceArn\":\"" + missingArn + "\"}") + .then().statusCode(400) + .body("__type", equalTo("WAFNonexistentItemException")); + } }