Skip to content

fix(query): fn for API Gateway With CloudWatch Logging Disabled - terraform/aws#7694

Merged
cx-ricardo-jesus merged 9 commits into
masterfrom
AST-111914-FP-fix_for_API_Gateway_With_Cloudwatch_Logging_Disabled-terraform/aws
Sep 25, 2025
Merged

fix(query): fn for API Gateway With CloudWatch Logging Disabled - terraform/aws#7694
cx-ricardo-jesus merged 9 commits into
masterfrom
AST-111914-FP-fix_for_API_Gateway_With_Cloudwatch_Logging_Disabled-terraform/aws

Conversation

@cx-andre-pereira

@cx-andre-pereira cx-andre-pereira commented Sep 3, 2025

Copy link
Copy Markdown
Contributor

Reason for Proposed Changes

  • Currently the API Gateway With CloudWatch Logging Disabled terraform query does not account for the access_log_settings.destination_arn field of the aws_api_gateway_stage resource.

  • This query is meant to flag whenever "CloudWatch Logs" are not enabled for a aws_api_gateway_stage resource; currently it only checks for a aws_cloudwatch_log_group resource that has a "name" field that references the relevant aws_api_gateway_stage's stage_name . To do this the haveLogs function checks for a specific regex match where given a stage_name like "example" the "name" of the aws_cloudwatch_log_group could be :

    • name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.example.id}/${example}"
  • This is shown in the negative1 test with "var.stage_name" as the stage_name. The "name" also references an aws_api_gateway_rest_api resource but that is irrelevant for the purposes of this query.

  • The missing case is associated with the access_log_settings.destination_arn field from the aws_api_gateway_stage since this field's purpose is to define where to send access logs as per the documentation. This is a glaring flaw and the field must be accounted for.

Proposed Changes

  • Fixed a typo in the original message, and altered said message to be more informative (original message stated "not undefined" when it should say "undefined").

  • Fixed a logic error unrelated to the False Positive that caused the scan of a folder to not give results that scanning an individual file would, this was due to the fact that the auxiliary function was scanning through all "documents" in the generated payload.

  • Added new helper functions and a "master" helper function get_results that will define the flagging order. I decided that, in case a sample does not have a valid "stage_name" or a valid "destination_arn", it is the "destination_arn" that will be flagged since it is a resource that exists solely to properly reference a aws_cloudwatch_log_group, while the stage_name will flag simply because no aws_cloudwatch_log_group resource properly referenced it and not because it is totally invalid.

  • Added 2 different resultvalues depending on the reason for the query flagging. This results are given by the does_not_have_valid_stage_name policy that is very much alike the original query logic and the new does_not_have_valid_destination_arn for the missing case. Note that the "destination_arn" must also respect a specific regex pattern as such :

    • regexPattern := sprintf("aws_cloudwatch_log_group.%s.arn", [name])
  • Where the "name" is the literal name of the aws_cloudwatch_log_group not its "name" field.

  • Added tests for the new case :

    • negative3 has a valid destination_arn
    • negative4 has a null destination_arn but a valid stage_name
    • negative5 has a valid destination_arn and stage_name
    • positive2 has an invalid destination_arn and stage_name
    • positive3 has a null destination_arn and stage_name
    • positive4 has no access_log_settings field and an invalid stage_name.
  • A test case for the access_log_settings field without the destination_arn was not added since the destination_arn is a required for said field.

Note : From my research modules cannot replace this resource since even using modules that can provide similar functions like api-gateway, they cannot define the aws_api_gateway_stage to aws_cloudwatch_log_group interaction that causes this query to flag by themselves.

I submit this contribution under the Apache-2.0 license.

…n field), added searchLine, fixed typo in key values, fixed logic error that caused query to not flag when scaning complete test folder)
@github-actions github-actions Bot added query New query feature terraform Terraform query aws PR related with AWS Cloud labels Sep 3, 2025
@github-actions

github-actions Bot commented Sep 3, 2025

Copy link
Copy Markdown
Contributor

kics-logo

KICS version: v2.1.13

Category Results
CRITICAL CRITICAL 0
HIGH HIGH 0
MEDIUM MEDIUM 0
LOW LOW 0
INFO INFO 0
TRACE TRACE 0
TOTAL TOTAL 0
Metric Values
Files scanned placeholder 1
Files parsed placeholder 1
Files failed to scan placeholder 0
Total executed queries placeholder 47
Queries failed to execute placeholder 0
Execution time placeholder 0

@cx-andre-pereira cx-andre-pereira marked this pull request as ready for review September 3, 2025 14:49
@cx-andre-pereira cx-andre-pereira requested a review from a team as a code owner September 3, 2025 14:49

@cx-artur-ribeiro cx-artur-ribeiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick fix to the message but apart from that everything looks good :)

…ng_disabled/query.rego

Co-authored-by: Artur Ribeiro <153724638+cx-artur-ribeiro@users.noreply.github.com>

@cx-artur-ribeiro cx-artur-ribeiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gitguardian

gitguardian Bot commented Sep 24, 2025

Copy link
Copy Markdown

⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secret in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
20838717 Triggered Generic Password a12d5a1 assets/queries/azureResourceManager/sql_server_database_with_alerts_disabled/test/negative7.bicep View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secret safely. Learn here the best practices.
  3. Revoke and rotate this secret.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@cx-ricardo-jesus cx-ricardo-jesus merged commit 6fdb9b4 into master Sep 25, 2025
27 checks passed
@cx-ricardo-jesus cx-ricardo-jesus deleted the AST-111914-FP-fix_for_API_Gateway_With_Cloudwatch_Logging_Disabled-terraform/aws branch September 25, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aws PR related with AWS Cloud query New query feature terraform Terraform query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants