From 87cd8ead733b3a5e0d752a8d17c638b0b2a4fc2c Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Sat, 24 Oct 2026 20:56:48 -0500 Subject: [PATCH] fix: require StateReason before changing alarm state (#6466) --- docs/services/cloudwatch.md | 4 +- .../metrics/CloudWatchMetricsService.java | 3 ++ .../metrics/CloudWatchMetricsServiceTest.java | 30 +++++++++++++ .../CompositeAlarmIntegrationTest.java | 43 +++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md index 0bb885089b..a090463a92 100644 --- a/docs/services/cloudwatch.md +++ b/docs/services/cloudwatch.md @@ -412,8 +412,8 @@ rejected with `ValidationError`. Updating a metric alarm preserves its current state, state reason, state timestamp and tags. Tags supplied to `PutMetricAlarm` apply only when creating an alarm; use `TagResource` and -`UntagResource` to change existing tags. `SetAlarmState` rejects missing or unknown state -values before changing the alarm. +`UntagResource` to change existing tags. `SetAlarmState` requires `StateReason` and rejects +missing or unknown state values before changing the alarm. ### SNS alarm actions diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java index 2be55674d0..1b86a39740 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java @@ -559,6 +559,9 @@ public class CloudWatchMetricsService { throw SmithyValidationErrors.validationError(stateValue == null ? SmithyValidationErrors.notNull("stateValue") : SmithyValidationErrors.notInEnum(stateValue, "stateValue", states)); } + if (stateReason == null) { + throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNull("stateReason")); + } List notifications = List.of(); synchronized (this) { String key = region + "::" + alarmName; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java index 2fd9d942ff..a7a0f162eb 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java @@ -82,6 +82,36 @@ class CloudWatchMetricsServiceTest { assertEquals(timestamp, alarm.getStateUpdatedTimestamp()); } + @Test + void missingManualStateReasonPreservesAlarm() { + MetricAlarm alarm = new MetricAlarm(); + alarm.setAlarmName("reason-required"); + service.putMetricAlarm(alarm, REGION); + service.setAlarmState(alarm.getAlarmName(), "OK", "preserved", "{}", REGION); + long timestamp = alarm.getStateUpdatedTimestamp(); + AwsException error = assertThrows(AwsException.class, + () -> service.setAlarmState(alarm.getAlarmName(), "ALARM", null, "changed", REGION)); + assertEquals("ValidationError", error.getErrorCode()); + assertEquals("1 validation error detected: Value null at 'stateReason' failed to satisfy constraint: " + + "Member must not be null", error.getMessage()); + MetricAlarm stored = service.describeAlarms(List.of(alarm.getAlarmName()), null, REGION).getFirst(); + assertEquals("OK", stored.getStateValue()); + assertEquals("preserved", stored.getStateReason()); + assertEquals("{}", stored.getStateReasonData()); + assertEquals(timestamp, stored.getStateUpdatedTimestamp()); + } + + @ParameterizedTest + @ValueSource(strings = {"", " ", "manual reason"}) + void suppliedManualStateReasonIsPreserved(String reason) { + MetricAlarm alarm = new MetricAlarm(); + alarm.setAlarmName("reason-supplied"); + service.putMetricAlarm(alarm, REGION); + service.setAlarmState(alarm.getAlarmName(), "ALARM", reason, null, REGION); + assertEquals("ALARM", alarm.getStateValue()); + assertEquals(reason, alarm.getStateReason()); + } + private MetricDatum datum(String name, double value) { MetricDatum d = new MetricDatum(); d.setMetricName(name); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java index 7ed97dae6e..20c6b776c9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CompositeAlarmIntegrationTest.java @@ -124,6 +124,49 @@ class CompositeAlarmIntegrationTest { } } + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void missingStateReasonPreservesAlarmAndDependents(boolean json) throws Exception { + String name = "reason-required-" + Long.toString(System.nanoTime(), 36); + String dependent = name + "-dependent"; + call(json, "PutCompositeAlarm", Map.of("AlarmName", name, "AlarmRule", "FALSE")) + .then().statusCode(200); + call(json, "PutCompositeAlarm", Map.of("AlarmName", dependent, "AlarmRule", "ALARM(\"" + name + "\")")) + .then().statusCode(200); + call(json, "SetAlarmState", Map.of("AlarmName", name, "StateValue", "OK", + "StateReason", "preserved", "StateReasonData", "{}")) + .then().statusCode(200); + Map describe = Map.of("AlarmNames", List.of(name, dependent), + "AlarmTypes", List.of("CompositeAlarm")); + Response before = call(json, "DescribeAlarms", describe).then().statusCode(200).extract().response(); + Map request = new LinkedHashMap<>(Map.of("AlarmName", name, "StateValue", "ALARM", + "StateReasonData", "changed")); + Response error = call(json, "SetAlarmState", request); + error.then().statusCode(400); + assertEquals("1 validation error detected: Value null at 'stateReason' failed to satisfy constraint: " + + "Member must not be null", errorMessage(json, error)); + if (json) { + request.put("StateReason", null); + Response explicitNull = call(true, "SetAlarmState", request); + explicitNull.then().statusCode(400); + assertEquals(errorMessage(true, error), errorMessage(true, explicitNull)); + } + Response after = call(json, "DescribeAlarms", describe).then().statusCode(200).extract().response(); + if (json) { + assertEquals(before.jsonPath().getList("CompositeAlarms"), after.jsonPath().getList("CompositeAlarms")); + } else { + assertEquals(before.xmlPath().getString("DescribeAlarmsResponse.DescribeAlarmsResult.CompositeAlarms"), + after.xmlPath().getString("DescribeAlarmsResponse.DescribeAlarmsResult.CompositeAlarms")); + } + request.put("StateReason", "accepted"); + call(json, "SetAlarmState", request).then().statusCode(200); + Response changed = call(json, "DescribeAlarms", Map.of("AlarmNames", List.of(dependent), + "AlarmTypes", List.of("CompositeAlarm"))).then().statusCode(200).extract().response(); + assertEquals("ALARM", json ? changed.jsonPath().getString("CompositeAlarms[0].StateValue") + : changed.xmlPath().getString( + "DescribeAlarmsResponse.DescribeAlarmsResult.CompositeAlarms.member.StateValue")); + } + private String errorMessage(boolean json, Response response) { return json ? response.jsonPath().getString("message") : response.xmlPath().getString("ErrorResponse.Error.Message"); }