Skip to content

Fix synthetic module doc model for local module evaluation#233

Merged
whitemerch merged 1 commit into
mainfrom
chakib.hamie/fix_sg_grouped_docs
Jul 3, 2026
Merged

Fix synthetic module doc model for local module evaluation#233
whitemerch merged 1 commit into
mainfrom
chakib.hamie/fix_sg_grouped_docs

Conversation

@whitemerch

@whitemerch whitemerch commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Motivation

When --x-local-module-eval is enabled, the scanner instantiates Terraform module resources and emits synthetic OPA documents for them. The previous implementation had two correctness bugs identified when I re-reviewed:

  1. Cross-root dedup used per-file content only. Two callers from different root stacks could be incorrectly merged if their shared resource file had identical content but sibling files differed (e.g. one caller's instance.tf references a security group; the other's does not). This caused one caller's findings to be silently dropped.

  2. count/for_each instances collapsed to base name. When a resource expanded to multiple instances (e.g. replica[0], replica[1]), all instances were written into a single map under the bare label replica. Each subsequent instance silently overwrote the previous one, so only one finding was produced instead of one per instance.

Changes

Per-instance findings

Each expanded module resource instance (from count or for_each) is now scanned independently. A module that creates two bucket replicas with the same misconfiguration will report two findings instead of one.

Cross-file references inside a module

When a module splits resources across multiple .tf files, references between them are now visible to cross-resource rules. A security group defined in sg.tf and used by an instance in instance.tf is correctly treated as in use, so rules like "Security group not used" no longer fire false positives in that case.

Deduplication across callers

When the same module is called from multiple root stacks, callers are only treated as identical when the entire module call resolves to the same content, not just when one file matches. Two stacks that share an identical security group definition but differ elsewhere (e.g. one references the SG, the other does not) are no longer merged, so each stack keeps its own findings. Identical callers from different roots are still deduplicated as before.

QA Instruction

  1. go test ./pkg/engine/... — all existing and new tests pass.
  2. golangci-lint run ./pkg/engine/... — no issues.
  3. Build: go build -o /tmp/iac-scanner ./cmd/scanner/
  4. Scan a Terraform repository with local modules: ./iac-scanner scan --x-local-module-eval --type terraform --path <repo> and confirm no unexpected changes in finding counts for cross-resource rules like "Security group not used".

Impact

Affects only the --x-local-module-eval experimental code path. The baseline scan pipeline (flag off) is unchanged. Modules without count/for_each and without cross-file references are unaffected in behaviour; only the two buggy edge cases now produce correct results.

@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Jul 2, 2026

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 98.21%
Overall Coverage: 50.59% (+0.14%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 4872006 | Docs | Datadog PR Page | Give us feedback!

@whitemerch
whitemerch force-pushed the chakib.hamie/fix_sg_grouped_docs branch 3 times, most recently from 225e75c to 4dd988d Compare July 2, 2026 13:20
@whitemerch
whitemerch marked this pull request as ready for review July 2, 2026 13:21
@whitemerch
whitemerch requested a review from a team as a code owner July 2, 2026 13:21
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch marked this pull request as draft July 2, 2026 13:25
@whitemerch
whitemerch force-pushed the chakib.hamie/fix_sg_grouped_docs branch from 4dd988d to 07919e8 Compare July 2, 2026 14:08
@whitemerch whitemerch changed the title Fix false positives in cross-resource rules under local module evaluation Fix synthetic module doc model for local module evaluation Jul 2, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/fix_sg_grouped_docs branch from d7bddfc to ed75fba Compare July 2, 2026 14:24
@whitemerch
whitemerch force-pushed the chakib.hamie/fix_sg_grouped_docs branch from ed75fba to 4872006 Compare July 2, 2026 14:44
@whitemerch
whitemerch marked this pull request as ready for review July 2, 2026 14:46

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4872006b94

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

resource "aws_instance" "app" {
ami = "ami-12345678"
instance_type = "t3.micro"
vpc_security_group_ids = ["aws_security_group.web.id"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve real Terraform references in sibling refs

Because this fixture quotes the SG reference, HCL evaluates it as a plain string; real Terraform uses vpc_security_group_ids = [aws_security_group.web.id]. In that real case tfeval evaluates the expression against only configured resource attrs, so provider-computed .id is missing and AttributesToDocument becomes the unknown placeholder before _refs is built. The new sibling-ref logic therefore still reports an SG used from a sibling file as unused; please cover the unquoted reference and preserve or synthesize resource references.

Useful? React with 👍 / 👎.

@MikaYuoadas MikaYuoadas 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 👍

@whitemerch
whitemerch merged commit 2a684f4 into main Jul 3, 2026
20 checks passed
@whitemerch
whitemerch deleted the chakib.hamie/fix_sg_grouped_docs branch July 3, 2026 08:30
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.

2 participants