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

taggable() erased the resolved resource behind a private Tagged
lambda, so applyTagged()'s instanceof checks never matched and the
tag mutation/store.put were dead code. Dispatch on the resource's
concrete type directly instead.

* Fix #3910

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-09-19 10:14:29 -04:00
1 parent 8ca748355d
commit df9535d7db
2 files changed
+79 -16

No files matched your search

@@ -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);
}
}
@@ -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\"}")