Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(wafv2): persist tags on TagResource/UntagResource (#3915)

Matheus Oliveiradf9535d7db0f
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 3f2eece250..669c0d42a3 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
@@ -333,58 +333,67 @@ public class WafV2Service {
// ──────────────────────────── Tags ────────────────────────────
public Map<String, String> listTagsForResource(String resourceArn) {
- return taggable(resourceArn).getTags();
+ return tagsOf(taggable(resourceArn));
}
public void tagResource(String resourceArn, Map<String, String> tags) {
Object resource = taggable(resourceArn);
- applyTagged(resource, r -> r.getTags().putAll(tags));
+ tagsOf(resource).putAll(tags);
+ persistTagged(resource);
}
public void untagResource(String resourceArn, List<String> keys) {
Object resource = taggable(resourceArn);
- applyTagged(resource, r -> keys.forEach(r.getTags()::remove));
+ keys.forEach(tagsOf(resource)::remove);
+ persistTagged(resource);
}
// ──────────────────────────── Helpers ────────────────────────────
- private interface Tagged { Map<String, String> getTags(); }
-
- private Tagged taggable(String resourceArn) {
+ private Object taggable(String resourceArn) {
if (resourceArn == null) {
throw new AwsException("WAFInvalidParameterException", "ResourceARN is required.", 400);
}
WebAcl acl = scanArn(webAclStore, resourceArn);
if (acl != null) {
- return acl::getTags;
+ return acl;
}
IpSet ip = scanArn(ipSetStore, resourceArn);
if (ip != null) {
- return ip::getTags;
+ return ip;
}
RegexPatternSet rx = scanArn(regexStore, resourceArn);
if (rx != null) {
- return rx::getTags;
+ return rx;
}
RuleGroup rg = scanArn(ruleGroupStore, resourceArn);
if (rg != null) {
- return rg::getTags;
+ return rg;
}
throw new AwsException("WAFNonexistentItemException", "Resource not found: " + resourceArn, 404);
}
- private void applyTagged(Object resource, java.util.function.Consumer<Tagged> mutation) {
+ private Map<String, String> tagsOf(Object resource) {
+ if (resource instanceof WebAcl a) {
+ return a.getTags();
+ } else if (resource instanceof IpSet i) {
+ return i.getTags();
+ } else if (resource instanceof RegexPatternSet r) {
+ return r.getTags();
+ } else if (resource instanceof RuleGroup g) {
+ return g.getTags();
+ }
+ throw new AwsException("WAFNonexistentItemException", "Resource not found.", 404);
+ }
+
+ private void persistTagged(Object resource) {
if (resource instanceof WebAcl a) {
- mutation.accept(a::getTags);
webAclStore.put(key(a.getScope(), a.getId()), a);
} else if (resource instanceof IpSet i) {
- mutation.accept(i::getTags);
ipSetStore.put(key(i.getScope(), i.getId()), i);
} else if (resource instanceof RegexPatternSet r) {
- mutation.accept(r::getTags);
regexStore.put(key(r.getScope(), r.getId()), r);
} else if (resource instanceof RuleGroup g) {
- mutation.accept(g::getTags);
ruleGroupStore.put(key(g.getScope(), g.getId()), g);
}
}
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 d350e9f478..6a63dd6129 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
@@ -304,6 +304,60 @@ class WafV2IntegrationTest {
@Test
@Order(15)
+ void tagResourcePersistsTagsForIpSet() {
+ call("TagResource",
+ "{\"ResourceARN\":\"" + ipSetArn + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"test\"},"
+ + "{\"Key\":\"team\",\"Value\":\"waf\"}]}")
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + ipSetArn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.ResourceARN", equalTo(ipSetArn))
+ .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\":\"" + ipSetArn + "\",\"TagKeys\":[\"team\"]}")
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + ipSetArn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(1))
+ .body("TagInfoForResource.TagList[0].Key", equalTo("env"));
+ }
+
+ @Test
+ @Order(16)
+ void tagResourcePersistsTagsForWebAcl() {
+ call("TagResource",
+ "{\"ResourceARN\":\"" + webAclArn + "\",\"Tags\":[{\"Key\":\"owner\",\"Value\":\"floci\"}]}")
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + webAclArn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(1))
+ .body("TagInfoForResource.TagList[0].Key", equalTo("owner"))
+ .body("TagInfoForResource.TagList[0].Value", equalTo("floci"));
+
+ call("TagResource",
+ "{\"ResourceARN\":\"" + webAclArn + "\",\"Tags\":[{\"Key\":\"owner\",\"Value\":\"floci-team\"}]}")
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + webAclArn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(1))
+ .body("TagInfoForResource.TagList[0].Value", equalTo("floci-team"));
+
+ call("UntagResource", "{\"ResourceARN\":\"" + webAclArn + "\",\"TagKeys\":[\"owner\"]}")
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + webAclArn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(0));
+ }
+
+ @Test
+ @Order(17)
void teardown() {
call("DisassociateWebACL", "{\"ResourceArn\":\"" + API_RESOURCE_ARN + "\"}")
.then().statusCode(200);
@@ -325,7 +379,7 @@ class WafV2IntegrationTest {
}
@Test
- @Order(16)
+ @Order(18)
void getMissingWebAclReturnsNotFound() {
call("GetWebACL",
"{\"Name\":\"nope\",\"Scope\":\"REGIONAL\",\"Id\":\"00000000-0000-0000-0000-000000000000\"}")