Skip to content

fix(aws-cloudwatch): validate metric Period on GetMetricData and GetMetricStatistics - #1703

Merged
NitinKumar004 merged 1 commit into
developmentfrom
fix/cloudwatch-period-validation
Oct 23, 2026
Merged

NitinKumar004 merged 1 commit into
developmentfrom
fix/cloudwatch-period-validation

Conversation

@NitinKumar004

Copy link
Copy Markdown
Collaborator

GetMetricData never validated the Period of a MetricStat or MetricDataQuery, and GetMetricStatistics never validated its Period. A huge Period overflowed time.Duration(p) * time.Second. A zero step panicked with a divide by zero, and a negative step produced garbage buckets. For example, a MetricStat Period of 540431955284459520 is a multiple of 60 that wraps to a zero step.

Changes:

  • The provider now has ValidatePeriod, which applies the AWS rule from the MetricStat, MetricDataQuery and GetMetricStatistics references. Period is at least 1 and fits an Integer. Valid values are 1, 5, 10, 20, 30, 60 or any multiple of 60. An invalid value returns InvalidArgument with an AWS-style message that names the parameter, for example MetricDataQueries.member.1.MetricStat.Period.
  • Provider GetMetricData rejects any non-zero invalid Period. Zero still means the one-minute default for library callers. Buckets are counted in int64 seconds, and a non-positive step returns no points.
  • The wire layer checks every query's MetricStat Period and its optional Period through that capability. GetMetricStatistics requires a valid Period. Both map the error to 400 InvalidParameterValue.
  • PutMetricAlarm: the EvaluationPeriods * Period lookback check now divides instead of multiplying, so it cannot overflow. It also applies to the MetricStat periods of a metric math alarm, so a huge Period cannot reach the evaluator.

Verified with:

  • Regression tests at the provider and wire levels: huge, zero, negative and non-multiple-of-60 periods, plus a client timeout on every request. On the old code, these tests hit the divide-by-zero panic.
  • AWS CLI against cloudemu serve: the huge, zero, negative and 61 periods return InvalidParameterValue, and a valid query returns its data.
  • Terraform: aws_cloudwatch_metric_alarm (single-metric and metric math) applies, plans with no changes and destroys cleanly.

Closes #1701

…etricStatistics

An invalid or huge Period overflowed the bucket step and crashed the server with a divide by zero. Period now follows the AWS rule and returns InvalidParameterValue, buckets are counted in int64 seconds, and the math alarm lookback check no longer overflows.
@NitinKumar004
NitinKumar004 merged commit 2eccc87 into development Oct 23, 2026
23 checks passed
Satyam-Trivedi-ZS added a commit to Satyam-Trivedi-ZS/cloudemu that referenced this pull request Oct 24, 2026
…verflow-safe

A SEARCH period argument past the time.Duration range made the FILL grid
step wrap negative (endless loop) or to exactly zero (divide by zero).

- SEARCH's period argument now follows the MetricStat Period rule
  (1, 5, 10, 20, 30 or a multiple of 60, up to the Integer maximum),
  checked on the float before the int conversion. The rule moves to
  metricmath.ValidPeriod and the provider's ValidatePeriod from stackshy#1703
  uses it, so there is a single definition.
- GetMetricData rejects such an expression with a ValidationError
  ("Error in expression 'e1': Invalid SEARCH period. ...").
- The FILL grid counts in whole seconds and never turns the period
  itself into a Duration, so it ends for any period, including an
  entry Period passed by a library caller.
- Arithmetic over a SEARCH keeps the SEARCH period, so FILL around it
  fills every period when the query sets no Period.
- The arithmetic wire test now checks every expected row comes back.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AWS CloudWatch: GetMetricData Period is not validated (overflow can hang or crash the server)

1 participant