feat(cloudtrail): implement AddTags, RemoveTags and ListTags (#2934)

* feat(cloudtrail): implement AddTags, RemoveTags and ListTags

CloudTrailJsonHandler dispatched none of the three tag actions, so any
trail lookup went through the unsupported-action path — including the
Terraform AWS provider's transparent-tagging ListTags call on every read
of aws_cloudtrail, which broke refresh/plan/destroy for any state
containing a trail regardless of whether it was tagged.

Tags are tracked per-trail in CloudTrailEntry (not the wire-serialized
Trail record, which AWS never includes tags on) and are resolved by ARN
only, matching AWS's actual request shape for these three actions. An
ARN that isn't a well-formed CloudTrail trail ARN raises
CloudTrailARNInvalidException; a well-formed ARN with no matching trail
raises ResourceNotFoundException, so a stale Terraform state entry
surfaces as a real error instead of an empty tag list.

Fixes floci-io/floci#2297

* fix(cloudtrail): omit Value from ListTags response for valueless tags

AddTags only requires Key, so a tag can legitimately have no Value.
listTags() was serializing that as an explicit "Value":null, but AWS
omits absent optionals from the response entirely.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-02 17:29:03 -05:00
1 parent 65e1d506cc
commit 671628b525
5 files changed
+245 -6

No files matched your search

@@ -14,7 +14,9 @@ import java.util.zip.GZIPInputStream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -292,6 +294,120 @@ class CloudTrailIntegrationTest {
.body(containsString(newBucket));
}
@Test
void tagLifecycleAddsListsAndRemovesTags() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String trailName = "tag-" + suffix;
String destBucket = "tag-logs-" + suffix;
createBucket(destBucket);
String trailArn = invokeCloudTrail("CreateTrail", String.format("""
{"Name":"%s","S3BucketName":"%s"}
""", trailName, destBucket))
.then().statusCode(200)
.extract().path("TrailARN");
// A trail with no tags yet returns an entry with an empty TagsList, not an
// omitted entry.
invokeCloudTrail("ListTags", String.format("""
{"ResourceIdList":["%s"]}
""", trailArn))
.then().statusCode(200)
.body("ResourceTagList[0].ResourceId", equalTo(trailArn))
.body("ResourceTagList[0].TagsList", hasSize(0));
invokeCloudTrail("AddTags", String.format("""
{"ResourceId":"%s","TagsList":[{"Key":"env","Value":"prod"},{"Key":"team","Value":"sec"}]}
""", trailArn))
.then().statusCode(200);
invokeCloudTrail("ListTags", String.format("""
{"ResourceIdList":["%s"]}
""", trailArn))
.then().statusCode(200)
.body("ResourceTagList[0].TagsList", hasSize(2));
// AddTags overwrites an existing key's value rather than duplicating it.
invokeCloudTrail("AddTags", String.format("""
{"ResourceId":"%s","TagsList":[{"Key":"env","Value":"staging"}]}
""", trailArn))
.then().statusCode(200);
invokeCloudTrail("ListTags", String.format("""
{"ResourceIdList":["%s"]}
""", trailArn))
.then().statusCode(200)
.body("ResourceTagList[0].TagsList", hasSize(2))
.body("ResourceTagList[0].TagsList.find { it.Key == 'env' }.Value", equalTo("staging"));
invokeCloudTrail("RemoveTags", String.format("""
{"ResourceId":"%s","TagsList":[{"Key":"env"}]}
""", trailArn))
.then().statusCode(200);
invokeCloudTrail("ListTags", String.format("""
{"ResourceIdList":["%s"]}
""", trailArn))
.then().statusCode(200)
.body("ResourceTagList[0].TagsList", hasSize(1))
.body("ResourceTagList[0].TagsList[0].Key", equalTo("team"));
}
@Test
void listTagsOmitsValueForTagWithNoValue() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String trailName = "novalue-" + suffix;
String destBucket = "novalue-logs-" + suffix;
createBucket(destBucket);
String trailArn = invokeCloudTrail("CreateTrail", String.format("""
{"Name":"%s","S3BucketName":"%s"}
""", trailName, destBucket))
.then().statusCode(200)
.extract().path("TrailARN");
invokeCloudTrail("AddTags", String.format("""
{"ResourceId":"%s","TagsList":[{"Key":"novalue"}]}
""", trailArn))
.then().statusCode(200);
// AWS omits absent optionals: a valueless tag must come back as
// {"Key":"novalue"} with no "Value" member at all, not "Value":null.
invokeCloudTrail("ListTags", String.format("""
{"ResourceIdList":["%s"]}
""", trailArn))
.then().statusCode(200)
.body(containsString("{\"Key\":\"novalue\"}"))
.body(not(containsString("\"Value\":null")));
}
@Test
void listTagsForUnknownTrailArnReturnsResourceNotFound() {
invokeCloudTrail("ListTags", """
{"ResourceIdList":["arn:aws:cloudtrail:us-east-1:000000000000:trail/does-not-exist"]}
""")
.then().statusCode(400)
.body(containsString("ResourceNotFoundException"));
}
@Test
void addTagsWithMalformedArnReturnsCloudTrailArnInvalid() {
invokeCloudTrail("AddTags", """
{"ResourceId":"not-an-arn","TagsList":[{"Key":"env","Value":"prod"}]}
""")
.then().statusCode(400)
.body(containsString("CloudTrailARNInvalidException"));
}
@Test
void addTagsWithNonTrailArnReturnsCloudTrailArnInvalid() {
invokeCloudTrail("AddTags", """
{"ResourceId":"arn:aws:s3:::some-bucket","TagsList":[{"Key":"env","Value":"prod"}]}
""")
.then().statusCode(400)
.body(containsString("CloudTrailARNInvalidException"));
}
// --- Helpers ---
private static io.restassured.response.Response invokeCloudTrail(String action, String body) {