mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudwatch): accept the CloudWatchVersion20130116 JSON target (#6586)
* fix(cloudwatch): accept the CloudWatchVersion20130116 JSON target The CloudWatch console's API sends JSON 1.0 requests to the monitoring endpoint with X-Amz-Target CloudWatchVersion20130116.<Operation>, signed for the monitoring service. Floci knew only the GraniteServiceVersion20100801 prefix the SDKs send, so every such request got HTTP 404 UnknownOperationException. Register the prefix on the monitoring descriptor as a JSON-only target, the mechanism the DynamoDB_20111205 prefix already uses. A request naming it now reaches the CloudWatch JSON handler and the same metrics and alarms, after the same signature validation, unsigned-call refusal and cloudwatch:<Operation> IAM check as the current prefix. CBOR and Smithy RPC v2 requests that name it stay unknown, as do other CloudWatchVersion dates. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudwatch): serve only shared operations on the console JSON target The CloudWatch console's API names its operations with the CloudWatchVersion20130116 target, and several of them take shapes the current API does not: its GetMetricData takes Defaults and Metrics instead of MetricDataQueries, and its PutMetricAlarm and DescribeAlarms describe metric queries with Alias, Namespace and MetricName items instead of Id and MetricStat. The CloudWatch JSON handler ignores members it does not know, so through that target a console GetMetricData answered HTTP 200 with no results, and a console PutMetricAlarm stored an alarm without its metric. Route the target through a handleLegacy entry, as DynamoDB_20111205 is routed, and serve there only the operations whose shapes the two APIs share: ListMetrics, DescribeAlarms, GetMetricStatistics and DeleteAlarms. Every other operation, and a request that sets a member only the console's API defines (CountOnlyRequest, SegmentId, TotalSegments, ExcludeAutoscalingAlarms, ExcludeManagedAlarms), gets HTTP 400 UnsupportedOperation, the error Floci gives an operation it does not implement. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudwatch): refuse unshared members and math alarms on the console target Two gaps were left on the CloudWatchVersion20130116 target. Its DescribeAlarms listed a metric math alarm's Metrics as the current API's Id and MetricStat items, which the console's API does not define, so a client parsing the answer with that API's model sees metrics with no namespace, name or statistic. And only the five console-only members known today were refused, so a member the console's API adds later would again be ignored. Refuse a DescribeAlarms answer that would list a metric math alarm, and check request members against the ones the current API defines for each served operation, as botocore's cloudwatch 2010-08-01 model lists them, instead of against a list of console-only names. A null or false value still counts as unset. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudwatch): serve only honoured members on the console target The console target accepted every request member botocore's model defines for its four operations, but the CloudWatch JSON handler ignores several of them: StateValue, AlarmTypes, ActionPrefix, ChildrenOfAlarmName and ParentsOfAlarmName on DescribeAlarms, ExtendedStatistics on GetMetricStatistics, and RecentlyActive, OwningAccount and IncludeLinkedAccounts on ListMetrics. A request using one got HTTP 200 as if the member were unset. A null member also reached the handler, which read it as the string "null". Accept on the console target only the members the handler honours, plus NextToken and MaxRecords, which change nothing because every result comes back at once. Refuse any other member that is set with HTTP 400 UnsupportedOperation, and drop null members before handling. The four operations are served under these member checks and the DescribeAlarms check, which now names an alarm defined with Metrics rather than a metric math alarm, since it also covers a single metric query. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudwatch): cover presigned console-target calls and align naming Call the CloudWatchVersion20130116 target the console target in the tests, as the docs and comments do, and say why handleLegacy keeps the name of DynamoDB's JSON-only entry point. Cover presigned requests on both prefixes, and keep the DescribeAlarms rows within what AWS accepts by sending AlarmNames and AlarmNamePrefix apart. State in the docs that only the bare prefix over application/x-amz-json-1.0 is accepted, and in the comments that only top-level null members count as unset. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudwatch): pin the console target's other wire forms as unknown The docs say only the bare CloudWatchVersion20130116 prefix over application/x-amz-json-1.0 reaches CloudWatch. Pin that: the bare prefix over application/json or JSON 1.1, and the fully qualified com.amazonaws.cloudwatch.v2013_01_16 form, answer HTTP 404 UnknownOperationException. Name the presigned test for what it checks, a wrong secret, and wrap the new comments at 100 columns. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudwatch): share the signed IAM user setup between signature tests CloudWatchJsonTargetSignatureIntegrationTest and CloudWatchQueryGetSignatureIntegrationTest each carried their own copy of a helper that signs IAM Query calls with the root test key, and of the setup that creates a user with an access key and an inline policy and then removes them. Move both into SignedIamUsers under the testing package and call it from the two classes. It uses real SigV4 signatures, which the classes need because signature validation is on; EnforcementFixtures' placeholder signatures would be refused. The returned user is AutoCloseable, so each test holds it in a try-with-resources block; if creating the key or the policy fails, the parts already made are removed. Test behaviour is unchanged: both classes run the same 13 tests and pass before and after. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
8b57546159
commit
bc75edb4c4
10 files changed
+610
-52
No files matched your search
@@ -176,7 +176,9 @@ public class AwsJsonController {
|
||||
case "sns" -> snsJsonHandler.handle(action, request, region);
|
||||
case "states" -> sfnJsonHandler.handle(action, request, region);
|
||||
case "swf" -> swfJsonHandler.handle(action, request, region);
|
||||
case "monitoring" -> cloudWatchMetricsJsonHandler.handle(action, request, region);
|
||||
case "monitoring" -> targetMatch.jsonOnly()
|
||||
? cloudWatchMetricsJsonHandler.handleLegacy(action, request, region)
|
||||
: cloudWatchMetricsJsonHandler.handle(action, request, region);
|
||||
case "cloudcontrol" -> cloudControlJsonHandler.handle(
|
||||
action, request, region, regionResolver.getAccountId());
|
||||
case "network-firewall" -> networkFirewallJsonHandler.handle(
|
||||
|
||||
@@ -269,7 +269,7 @@ public class ResolvedServiceCatalog {
|
||||
config.storage().services().cloudwatchmetrics().flushIntervalMs(), AwsNamespaces.CW, ServiceProtocol.QUERY,
|
||||
protocols(ServiceProtocol.QUERY, ServiceProtocol.JSON, ServiceProtocol.CBOR),
|
||||
Set.of("GraniteServiceVersion20100801."), Set.of("monitoring"),
|
||||
Set.of("GraniteServiceVersion20100801"), Set.of()),
|
||||
Set.of("GraniteServiceVersion20100801"), Set.of(), "CloudWatchVersion20130116."),
|
||||
descriptor("secretsmanager", "secretsmanager", config.services().secretsmanager().enabled(), true,
|
||||
"secretsmanager", storageMode(config.storage().services().secretsmanager().mode(), config.storage().mode()),
|
||||
config.storage().services().secretsmanager().flushIntervalMs(), null, ServiceProtocol.JSON,
|
||||
|
||||
+66
@@ -29,6 +29,7 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Handles CloudWatch Metrics requests via the JSON 1.0 protocol.
|
||||
@@ -38,6 +39,20 @@ import java.util.Map;
|
||||
public class CloudWatchMetricsJsonHandler {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CloudWatchMetricsJsonHandler.class);
|
||||
|
||||
/**
|
||||
* The operations served through the console's {@code CloudWatchVersion20130116.} target, each
|
||||
* with the request members accepted there: the ones this handler honours, plus
|
||||
* {@code NextToken} and {@code MaxRecords}, which change nothing because every result comes
|
||||
* back at once.
|
||||
*/
|
||||
private static final Map<String, Set<String>> CONSOLE_TARGET_OPERATIONS = Map.of(
|
||||
"ListMetrics", Set.of("Namespace", "MetricName", "Dimensions", "NextToken"),
|
||||
"DescribeAlarms", Set.of("AlarmNames", "AlarmNamePrefix", "MaxRecords", "NextToken"),
|
||||
"GetMetricStatistics", Set.of("Namespace", "MetricName", "Dimensions", "StartTime", "EndTime",
|
||||
"Period", "Statistics", "Unit"),
|
||||
"DeleteAlarms", Set.of("AlarmNames"));
|
||||
|
||||
private final CloudWatchMetricsService metricsService;
|
||||
private final CloudWatchDashboardsService dashboardsService;
|
||||
private final CloudWatchMetricStreamsService metricStreamsService;
|
||||
@@ -85,6 +100,57 @@ public class CloudWatchMetricsJsonHandler {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles an operation named with the {@code CloudWatchVersion20130116.} target of the
|
||||
* CloudWatch console's API. The console's model gives many operations and members shapes of
|
||||
* their own, which this handler would silently misread or ignore, such as {@code GetMetricData}
|
||||
* with {@code Defaults} and {@code Metrics}, or {@code PutMetricAlarm} metric items with
|
||||
* {@code Alias} and {@code MetricName}. So only the operations and top-level members in
|
||||
* {@code CONSOLE_TARGET_OPERATIONS} are served: any other operation, and any other member set
|
||||
* to a value other than {@code null} or {@code false}, is refused rather than answered as if it
|
||||
* were unset, and a top-level {@code null} member is dropped. Members nested inside the served
|
||||
* ones are passed on unchecked, nulls included. A {@code DescribeAlarms} answer that would list
|
||||
* an alarm defined with {@code Metrics} is refused too, since the console's API gives those
|
||||
* items its own shape, with {@code Alias} instead of {@code Id} and {@code MetricStat}. The
|
||||
* name matches {@code DynamoDbJsonHandler.handleLegacy}, the entry point of the other JSON-only
|
||||
* target.
|
||||
*/
|
||||
public Response handleLegacy(String action, JsonNode request, String region) {
|
||||
Set<String> servedMembers = CONSOLE_TARGET_OPERATIONS.get(action);
|
||||
if (servedMembers == null) {
|
||||
return unsupportedWithConsoleTarget("Operation " + action);
|
||||
}
|
||||
ObjectNode served = objectMapper.createObjectNode();
|
||||
for (Map.Entry<String, JsonNode> member : request.properties()) {
|
||||
JsonNode value = member.getValue();
|
||||
if (value.isNull()) {
|
||||
continue;
|
||||
}
|
||||
if (servedMembers.contains(member.getKey())) {
|
||||
served.set(member.getKey(), value);
|
||||
} else if (!value.isBoolean() || value.booleanValue()) {
|
||||
return unsupportedWithConsoleTarget("Parameter " + member.getKey() + " of " + action);
|
||||
}
|
||||
}
|
||||
Response response = handle(action, served, region);
|
||||
if ("DescribeAlarms".equals(action) && response.getEntity() instanceof JsonNode body) {
|
||||
for (JsonNode alarm : body.path("MetricAlarms")) {
|
||||
if (alarm.has("Metrics")) {
|
||||
return unsupportedWithConsoleTarget("DescribeAlarms of alarm "
|
||||
+ alarm.path("AlarmName").asText() + ", which is defined with Metrics,");
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private static Response unsupportedWithConsoleTarget(String subject) {
|
||||
return Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnsupportedOperation", subject
|
||||
+ " is not supported by CloudWatch JSON with the CloudWatchVersion20130116 target."))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response handlePutMetricData(JsonNode request, String region) {
|
||||
String namespace = request.path("Namespace").asText();
|
||||
List<MetricDatum> datums = parseMetricDataJson(request.path("MetricData"), "MetricData");
|
||||
|
||||
Reference in new issue
Block a user