From 65db87d627da6bea64891c1e2c898afbf33d58bf Mon Sep 17 00:00:00 2001 From: lex00 <121451605+lex00@users.noreply.github.com> Date: Mon, 7 Sep 2026 20:11:12 -0600 Subject: [PATCH] fix(cloudwatch): wrap TagResource and UntagResource in their declared Result element (#3211) TagResourceOutput and UntagResourceOutput are empty structures with a declared resultWrapper, so the Query responses must carry the TagResourceResult and UntagResourceResult elements. The AWS Go SDK v2 unmarshaler behind the Terraform AWS provider fails on a response without them even though the tag write succeeded. Ported from lex00/floci#89. --- .../CloudWatchMetricsQueryHandler.java | 7 +- ...atchMetricsTagResourceIntegrationTest.java | 95 +++++++++++++++++++ .../metrics/CloudWatchMetricsTagsTest.java | 43 +++++++++ 3 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagResourceIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java index 41b8dfefa0..9f6067389f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsQueryHandler.java @@ -285,7 +285,10 @@ public class CloudWatchMetricsQueryHandler { } else { metricsService.tagResource(arn, tags, region); } - return Response.ok(AwsQueryResponse.envelopeNoResult("TagResource", null)).build(); + // TagResourceOutput is an empty structure with a declared resultWrapper, so the + // wrapper element must be present. The AWS Go SDK v2 unmarshaler, behind the + // Terraform AWS provider, fails on a response without it. + return Response.ok(AwsQueryResponse.envelopeEmptyResult("TagResource", null)).build(); } private Response handleUntagResource(MultivaluedMap params, String region) { @@ -301,7 +304,7 @@ public class CloudWatchMetricsQueryHandler { } else { metricsService.untagResource(arn, keys, region); } - return Response.ok(AwsQueryResponse.envelopeNoResult("UntagResource", null)).build(); + return Response.ok(AwsQueryResponse.envelopeEmptyResult("UntagResource", null)).build(); } // ──────────────────────────── Dashboards ──────────────────────────── diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagResourceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagResourceIntegrationTest.java new file mode 100644 index 0000000000..b0375edfa8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagResourceIntegrationTest.java @@ -0,0 +1,95 @@ +package io.github.hectorvent.floci.services.cloudwatch.metrics; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; + +/** + * TagResource and UntagResource have an empty output structure in the CloudWatch API model, + * so their Query responses still carry the Result wrapper element. The AWS Go SDK v2 + * unmarshaler behind the Terraform AWS provider fails on a response without it. + */ +@QuarkusTest +class CloudWatchMetricsTagResourceIntegrationTest { + + private static final String CW_SCOPE = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/monitoring/aws4_request"; + private static final String ALARM_NAME = "tag-wrapper-alarm"; + private static final String ALARM_ARN = + "arn:aws:cloudwatch:us-east-1:000000000000:alarm:" + ALARM_NAME; + + @Test + void tagAndUntagResponsesCarryTheirResultElements() { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CW_SCOPE) + .formParam("Action", "PutMetricAlarm") + .formParam("AlarmName", ALARM_NAME) + .formParam("MetricName", "CPUUtilization") + .formParam("Namespace", "AWS/EC2") + .formParam("ComparisonOperator", "GreaterThanThreshold") + .formParam("EvaluationPeriods", "1") + .formParam("Period", "60") + .formParam("Statistic", "Average") + .formParam("Threshold", "80") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CW_SCOPE) + .formParam("Action", "TagResource") + .formParam("ResourceARN", ALARM_ARN) + .formParam("Tags.member.1.Key", "env") + .formParam("Tags.member.1.Value", "prod") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body(containsString("env"))); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagsTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagsTest.java index 98b7528e40..e5c585e115 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagsTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsTagsTest.java @@ -117,6 +117,49 @@ class CloudWatchMetricsTagsTest { assertTrue(xml.contains("prod")); } + @Test + void tagResourceQueryResponseCarriesTheResultWrapper() { + // TagResourceOutput is an empty structure, so the Query response still declares a + // TagResourceResult element. The AWS Go SDK v2 unmarshaler rejects a response without it. + MetricAlarm alarm = new MetricAlarm(); + alarm.setAlarmName("test-alarm"); + alarm.setAlarmArn("arn:aws:cloudwatch:us-east-1:000000000000:alarm:test-alarm"); + service.putMetricAlarm(alarm, REGION); + + MultivaluedMap params = new MultivaluedHashMap<>(); + params.add("ResourceARN", alarm.getAlarmArn()); + params.add("Tags.member.1.Key", "env"); + params.add("Tags.member.1.Value", "prod"); + + Response response = queryHandler.handle("TagResource", params, REGION); + String xml = (String) response.getEntity(); + + assertTrue(xml.contains(""), xml); + assertTrue(xml.contains(""), xml); + assertEquals("prod", service.listTagsForResource(alarm.getAlarmArn(), REGION).get("env")); + } + + @Test + void untagResourceQueryResponseCarriesTheResultWrapper() { + MetricAlarm alarm = new MetricAlarm(); + alarm.setAlarmName("test-alarm"); + alarm.setAlarmArn("arn:aws:cloudwatch:us-east-1:000000000000:alarm:test-alarm"); + service.putMetricAlarm(alarm, REGION); + service.tagResource(alarm.getAlarmArn(), Map.of("env", "prod"), REGION); + + MultivaluedMap params = new MultivaluedHashMap<>(); + params.add("ResourceARN", alarm.getAlarmArn()); + params.add("TagKeys.member.1", "env"); + + Response response = queryHandler.handle("UntagResource", params, REGION); + String xml = (String) response.getEntity(); + + assertTrue(xml.contains(""), xml); + assertTrue(xml.contains("