mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cloudwatch): validate manual state reason lengths (#6470)
Check modeled reason lengths before state mutation, preserving required StateReason validation and both regression groups from current main. Use the Unicode-aware Smithy formatter. Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
3d2e17bbbd
commit
75d7923290
4 files changed
+89
No files matched your search
@@ -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
|
||||
|
||||
+10
@@ -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;
|
||||
|
||||
+34
@@ -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);
|
||||
|
||||
+41
@@ -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");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user