mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudwatch): reject PutMetricData datums without a MetricName (#6072)
* fix(cloudwatch): reject PutMetricData datums without a MetricName MetricName is the one required member of MetricDatum. The JSON handler stored a nameless datum under an empty name, and the Query handler treated a member without MetricName as the end of the list, silently dropping it and every later datum while still storing the earlier ones. Both handlers now reject a missing or empty MetricName in top-level MetricData and in EntityMetricData[].MetricData with MissingParameter (400), the error PutMetricData declares for a missing required input, naming the offending member. A whitespace name is still accepted, since AWS constrains MetricName only to 1 to 255 characters. Every datum is validated before any is stored, so a rejected request leaves no state behind. On Query, a member index with no keys at all still ends that MetricData list. * fix(cloudwatch): return MissingRequiredParameterException on JSON and CBOR CloudWatch is awsQueryCompatible. MissingParameter is the awsQueryError code of the MissingRequiredParameterException shape, so it belongs in the Query <Code> and the x-amzn-query-error header, while a JSON 1.0 or CBOR body's __type names the shape. With the code in __type the AWS SDK v2 raised a generic CloudWatchException instead of the modelled one. A package-private CloudWatchMetricsException keeps the Query code as its error code and overrides jsonType() with the shape name, the same split SwfException makes. The shared query-code table in AwsException is left alone because SQS reports MissingParameter as its JSON type.
This commit is contained in:
1 parent
1f96bf42f1
commit
b8d87df054
8 files changed
+260
-7
No files matched your search
+34
@@ -0,0 +1,34 @@
|
||||
package io.github.hectorvent.floci.services.cloudwatch.metrics;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/**
|
||||
* A CloudWatch error whose Query code and JSON shape name differ.
|
||||
*
|
||||
* <p>CloudWatch is awsQueryCompatible: the Query {@code <Code>} and the
|
||||
* {@code x-amzn-query-error} header carry the {@code awsQueryError} code
|
||||
* ({@code MissingParameter}), while a JSON 1.0 or CBOR body's {@code __type} names the
|
||||
* model shape ({@code MissingRequiredParameterException}). The AWS SDK v2 picks the typed
|
||||
* exception from {@code __type}, so the code alone yields a generic {@code CloudWatchException}.
|
||||
* Keeping {@link #getErrorCode()} as the Query code and overriding {@link #jsonType()}
|
||||
* reproduces both surfaces.
|
||||
*/
|
||||
class CloudWatchMetricsException extends AwsException {
|
||||
|
||||
private final String jsonType;
|
||||
|
||||
private CloudWatchMetricsException(String errorCode, String jsonType, String message, int httpStatus) {
|
||||
super(errorCode, message, httpStatus);
|
||||
this.jsonType = jsonType;
|
||||
}
|
||||
|
||||
static CloudWatchMetricsException missingParameter(String parameter) {
|
||||
return new CloudWatchMetricsException("MissingParameter", "MissingRequiredParameterException",
|
||||
"The parameter " + parameter + " is required.", 400);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonType() {
|
||||
return jsonType;
|
||||
}
|
||||
}
|
||||
+13
-4
@@ -83,9 +83,12 @@ public class CloudWatchMetricsJsonHandler {
|
||||
|
||||
private Response handlePutMetricData(JsonNode request, String region) {
|
||||
String namespace = request.path("Namespace").asText();
|
||||
List<MetricDatum> datums = parseMetricDataJson(request.path("MetricData"));
|
||||
List<MetricDatum> datums = parseMetricDataJson(request.path("MetricData"), "MetricData");
|
||||
int entityIndex = 0;
|
||||
for (JsonNode entityMetricData : request.path("EntityMetricData")) {
|
||||
datums.addAll(parseMetricDataJson(entityMetricData.path("MetricData")));
|
||||
entityIndex++;
|
||||
datums.addAll(parseMetricDataJson(entityMetricData.path("MetricData"),
|
||||
"EntityMetricData.member." + entityIndex + ".MetricData"));
|
||||
}
|
||||
LOG.debugv("JSON PutMetricData parsed {0} datums for namespace {1}", datums.size(), namespace);
|
||||
metricsService.putMetricData(namespace, datums, region);
|
||||
@@ -609,12 +612,18 @@ public class CloudWatchMetricsJsonHandler {
|
||||
return configurations;
|
||||
}
|
||||
|
||||
private List<MetricDatum> parseMetricDataJson(JsonNode node) {
|
||||
private List<MetricDatum> parseMetricDataJson(JsonNode node, String listPath) {
|
||||
List<MetricDatum> datums = new ArrayList<>();
|
||||
if (!node.isArray()) return datums;
|
||||
int index = 0;
|
||||
for (JsonNode item : node) {
|
||||
index++;
|
||||
JsonNode metricName = item.path("MetricName");
|
||||
if (!metricName.isTextual() || metricName.asText().isEmpty()) {
|
||||
throw CloudWatchMetricsException.missingParameter(listPath + ".member." + index + ".MetricName");
|
||||
}
|
||||
MetricDatum datum = new MetricDatum();
|
||||
datum.setMetricName(item.path("MetricName").asText());
|
||||
datum.setMetricName(metricName.asText());
|
||||
datum.setValue(item.path("Value").asDouble(0));
|
||||
datum.setUnit(item.path("Unit").asText(null));
|
||||
JsonNode ts = item.path("Timestamp");
|
||||
|
||||
+7
-3
@@ -617,9 +617,13 @@ public class CloudWatchMetricsQueryHandler {
|
||||
private List<MetricDatum> parseMetricData(MultivaluedMap<String, String> params, String listPrefix) {
|
||||
List<MetricDatum> datums = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String metricName = params.getFirst(listPrefix + ".member." + i + ".MetricName");
|
||||
if (metricName == null) {
|
||||
break;
|
||||
String memberPrefix = listPrefix + ".member." + i + ".";
|
||||
String metricName = params.getFirst(memberPrefix + "MetricName");
|
||||
if (metricName == null || metricName.isEmpty()) {
|
||||
if (metricName == null && params.keySet().stream().noneMatch(key -> key.startsWith(memberPrefix))) {
|
||||
break;
|
||||
}
|
||||
throw CloudWatchMetricsException.missingParameter(memberPrefix + "MetricName");
|
||||
}
|
||||
MetricDatum datum = new MetricDatum();
|
||||
datum.setMetricName(metricName);
|
||||
|
||||
Reference in new issue
Block a user