Fix synthetic module doc model for local module evaluation#233
Conversation
|
🎯 Code Coverage (details) 🔗 Commit SHA: 4872006 | Docs | Datadog PR Page | Give us feedback! |
225e75c to
4dd988d
Compare
4dd988d to
07919e8
Compare
d7bddfc to
ed75fba
Compare
ed75fba to
4872006
Compare
There was a problem hiding this comment.
💡 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"] |
There was a problem hiding this comment.
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 👍 / 👎.
Motivation
When
--x-local-module-evalis 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: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.tfreferences a security group; the other's does not). This caused one caller's findings to be silently dropped.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 labelreplica. 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
countorfor_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
.tffiles, references between them are now visible to cross-resource rules. A security group defined insg.tfand used by an instance ininstance.tfis 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
go test ./pkg/engine/...— all existing and new tests pass.golangci-lint run ./pkg/engine/...— no issues.go build -o /tmp/iac-scanner ./cmd/scanner/./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-evalexperimental code path. The baseline scan pipeline (flag off) is unchanged. Modules withoutcount/for_eachand without cross-file references are unaffected in behaviour; only the two buggy edge cases now produce correct results.