Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cloudwatch): validate manual state reason lengths (#6470)

Andre Kurait75d79232902f
diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md
index a090463a92..7fe0e7fee2 100644
--- a/docs/services/cloudwatch.md
+++ b/docs/services/cloudwatch.md
@@ -415,6 +415,10 @@ Tags supplied to `PutMetricAlarm` apply only when creating an alarm; use `TagRes
`UntagResource` to change existing tags. `SetAlarmState` requires `StateReason` and rejects
missing or unknown state values before changing the alarm.
+`SetAlarmState` enforces the modeled maximum lengths of 1023 characters for
+`StateReason` and 4000 characters for `StateReasonData` before changing alarm state.
+Length checks count Unicode code points and preserve empty and whitespace values.
+
### SNS alarm actions
State transitions publish to same-account standard SNS topics in `AlarmActions`, `OKActions`, or
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 1b86a39740..7874fad726 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
@@ -562,6 +562,16 @@ public class CloudWatchMetricsService {
if (stateReason == null) {
throw SmithyValidationErrors.validationError(SmithyValidationErrors.notNull("stateReason"));
}
+ String reasonViolation = stateReason == null ? null
+ : SmithyValidationErrors.lengthOutOfRange("stateReason", stateReason, 0, 1023);
+ if (reasonViolation != null) {
+ throw SmithyValidationErrors.validationError(reasonViolation);
+ }
+ String dataViolation = stateReasonData == null ? null
+ : SmithyValidationErrors.lengthOutOfRange("stateReasonData", stateReasonData, 0, 4000);
+ if (dataViolation != null) {
+ throw SmithyValidationErrors.validationError(dataViolation);
+ }
List<AlarmSnsNotifier.Notification> 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 a7a0f162eb..f2fd940f7c 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
@@ -112,6 +112,40 @@ class CloudWatchMetricsServiceTest {
assertEquals(reason, alarm.getStateReason());
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void manualReasonLengthLimitsPreserveRejectedState(boolean data) {
+ MetricAlarm alarm = new MetricAlarm();
+ alarm.setAlarmName("reason-length");
+ service.putMetricAlarm(alarm, REGION);
+ int maximum = data ? 4000 : 1023;
+ String path = data ? "stateReasonData" : "stateReason";
+ for (String unit : List.of("x", " ", "\uD83D\uDE00")) {
+ String boundary = unit.repeat(maximum);
+ service.setAlarmState("reason-length", "OK", data ? "accepted" : boundary,
+ data ? boundary : "{}", REGION);
+ long timestamp = alarm.getStateUpdatedTimestamp();
+ String rejected = boundary + unit;
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.setAlarmState("reason-length", "ALARM", data ? "rejected" : rejected,
+ data ? rejected : "changed", REGION));
+ assertEquals("ValidationError", error.getErrorCode());
+ assertEquals("1 validation error detected: Value '" + rejected + "' at '" + path
+ + "' failed to satisfy constraint: Member must have length less than or equal to " + maximum,
+ error.getMessage());
+ assertEquals("OK", alarm.getStateValue());
+ assertEquals(data ? "accepted" : boundary, alarm.getStateReason());
+ assertEquals(data ? boundary : "{}", alarm.getStateReasonData());
+ assertEquals(timestamp, alarm.getStateUpdatedTimestamp());
+ }
+ service.setAlarmState("reason-length", "OK", "", "", REGION);
+ assertEquals("", alarm.getStateReason());
+ assertEquals("", alarm.getStateReasonData());
+ service.setAlarmState("reason-length", "OK", " ", null, REGION);
+ assertEquals(" ", alarm.getStateReason());
+ assertNull(alarm.getStateReasonData());
+ }
+
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 20c6b776c9..71af54d875 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
@@ -167,6 +167,47 @@ class CompositeAlarmIntegrationTest {
"DescribeAlarmsResponse.DescribeAlarmsResult.CompositeAlarms.member.StateValue"));
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void reasonLengthRejectionPreservesSourceAndDependentState(boolean json) throws Exception {
+ String source = "reason-length-" + Long.toString(System.nanoTime(), 36);
+ String dependent = source + "-dependent";
+ call(json, "PutCompositeAlarm", Map.of("AlarmName", source, "AlarmRule", "FALSE"))
+ .then().statusCode(200);
+ call(json, "PutCompositeAlarm", Map.of("AlarmName", dependent, "AlarmRule", "ALARM(\"" + source + "\")"))
+ .then().statusCode(200);
+ for (boolean data : List.of(false, true)) {
+ int maximum = data ? 4000 : 1023;
+ String field = data ? "StateReasonData" : "StateReason";
+ String path = data ? "stateReasonData" : "stateReason";
+ String boundary = data ? "{\"text\":\"" + "x".repeat(maximum - 11) + "\"}" : "x".repeat(maximum);
+ Map<String, Object> request = new LinkedHashMap<>(Map.of("AlarmName", source, "StateValue", "OK",
+ "StateReason", "accepted", "StateReasonData", "{}"));
+ request.put(field, boundary);
+ call(json, "SetAlarmState", request).then().statusCode(200);
+ Response before = call(json, "DescribeAlarms", Map.of("AlarmNames", List.of(source, dependent),
+ "AlarmTypes", List.of("CompositeAlarm")));
+ String rejected = boundary + " ";
+ request.put(field, rejected);
+ request.put("StateValue", "ALARM");
+ Response error = call(json, "SetAlarmState", request);
+ error.then().statusCode(400);
+ assertEquals("1 validation error detected: Value '" + rejected + "' at '" + path
+ + "' failed to satisfy constraint: Member must have length less than or equal to " + maximum,
+ errorMessage(json, error));
+ Response after = call(json, "DescribeAlarms", Map.of("AlarmNames", List.of(source, dependent),
+ "AlarmTypes", List.of("CompositeAlarm")));
+ before.then().statusCode(200);
+ after.then().statusCode(200);
+ if (json) {
+ assertEquals(before.jsonPath().getList("CompositeAlarms"), after.jsonPath().getList("CompositeAlarms"));
+ } else {
+ assertEquals(before.xmlPath().getString("DescribeAlarmsResponse.DescribeAlarmsResult"),
+ after.xmlPath().getString("DescribeAlarmsResponse.DescribeAlarmsResult"));
+ }
+ }
+ }
+
private String errorMessage(boolean json, Response response) {
return json ? response.jsonPath().getString("message") : response.xmlPath().getString("ErrorResponse.Error.Message");
}