Skip to content

Evaluate each query against only its platform's documents#224

Merged
whitemerch merged 1 commit into
mainfrom
chakib.hamie/per_platform_payload_scoping
Jun 30, 2026
Merged

Evaluate each query against only its platform's documents#224
whitemerch merged 1 commit into
mainfrom
chakib.hamie/per_platform_payload_scoping

Conversation

@whitemerch

@whitemerch whitemerch commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Motivation

On large polyglot repositories the scanner builds a single OPA payload containing every parsed document across all platforms, then evaluates each rule query against the full cross-platform payload. This means, for example, that every Ansible rule rebuilds its task index over thousands of Kubernetes, Helm-rendered, CloudFormation, and Terraform documents that are irrelevant to it. On a real-world Go monorepo (49 k YAML files, ~48 k parsed documents), a single Ansible rule takes ~27 s to evaluate due to this cross-platform document scanning; with ~221 Ansible rules the total Ansible eval time exceeds 100 minutes. The same mechanism inflates eval time for CloudFormation and Kubernetes rules on large mixed repos.

Changes

Platform classification

A new ClassifyFile function in the analyzer package determines a file's platform from its extension and content, mirroring the per-file detection already done by the analyzer but making it available to the parsing layer. Kind is used as the authoritative signal for unambiguous types (Helm→kubernetes, Terraform→terraform, Bicep→azureresourcemanager, Dockerfile→dockerfile, GRPC→grpc, INI/CFG→ansible); ambiguous YAML/JSON files fall back to content-based regex classification.

Scan pipeline

Both the regular sink and the resolver sink now populate a Platform field on each FileMetadata at parse time. Synthetic Terraform module documents inherit the platform of their parent file via FileMetadata cloning.

Rule engine

Inspect partitions the combined document set by platform and builds one OPA payload per platform that is actually queried in that scan. Each rule query receives only its own platform's documents. Documents whose platform could not be determined are included in every platform's payload so no rule loses coverage. common-platform queries (force-enabled by the engine and not observed in production slow-rule telemetry) continue to receive the full cross-platform payload. Two small helpers, normalizePlatformKey and selectPlatformPayload, handle the query-metadata-to-platform-key mapping (e.g. k8skubernetes, Bicepazureresourcemanager).

Author Checklist

  • I have reviewed my own PR.
  • I have added or updated relevant unit tests where necessary. If no tests are added, I've explained why.
  • All new and existing tests pass.
  • I have tested my changes on staging (if applicable).
  • I have updated any relevant documentation (if applicable).

QA Instruction

  1. go test ./pkg/engine/... ./pkg/analyzer/... ./pkg/runner/... ./pkg/model/... ./pkg/scan/... — all packages should pass.
  2. Scan a fixture directory that contains multiple platform types (e.g. test/fixtures/test_helm alongside a Terraform or Ansible fixture) and confirm the SARIF output is identical to the output produced by main.
  3. On a large mixed-platform repository, confirm per-rule eval times drop significantly for Ansible and CloudFormation rules by comparing scan durations.

Blast Radius

This change affects all scans that parse more than one platform type. Scans of a single platform are unaffected (the per-platform payload equals the full payload). The findings set is unchanged, validated by a findings diff on a 1 048-file mixed repository (Kubernetes, Helm-rendered, Ansible, Terraform, CloudFormation) showing identical results (1 016 findings, zero lost, zero gained) against the main baseline.

Additional Notes

Measured on a real-world monorepo (49 k YAML files):

  • Single Ansible rule eval: 27.4 s → 3.1 ms (~8 800×)
  • Full scan of 1 048-file mixed subset: ~16 min (baseline) → ~9 s (this PR)

I submit this contribution under the Apache-2.0 license.

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jun 30, 2026

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 67.47%
Overall Coverage: 50.15% (+0.06%)

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/per_platform_payload_scoping branch from d654918 to d45bd2a Compare June 30, 2026 05:13
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@whitemerch
whitemerch force-pushed the chakib.hamie/per_platform_payload_scoping branch from d45bd2a to ac24841 Compare June 30, 2026 05:49
@whitemerch
whitemerch marked this pull request as ready for review June 30, 2026 05:50
@whitemerch
whitemerch requested a review from a team as a code owner June 30, 2026 05:50

@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: ac248411a5

ℹ️ 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".

Comment thread pkg/engine/inspector.go
case platformServerlessFW:
return []string{platformServerlessFW, platformCloudFormation}
}
return []string{key}

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 Keep Crossplane manifests in Kubernetes payloads

When scanning the normal supported platform set, Crossplane YAML is admitted by the analyzer as Kubernetes because Crossplane is not in pkg/platforms/platforms.go and the Kubernetes regex still matches apiVersion/kind. The sink then reclassifies the same file with all regexes as crossplane; this default branch buckets it only under crossplane, but no Crossplane queries are loaded, so Kubernetes rules receive an empty payload and findings for those manifests disappear. Please also bucket Crossplane documents with Kubernetes, or preserve the analyzer-selected platform for this case.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Crossplane has its own dedicated queries (assets/queries/crossplane/**) with "platform": "Crossplane", so crossplane-bucketed documents do get evaluated, by crossplane rules. Additionally, crossplane is not currently enabled so it's a no-op

@whitemerch
whitemerch force-pushed the chakib.hamie/per_platform_payload_scoping branch from ac24841 to 0533db3 Compare June 30, 2026 06:53
@whitemerch
whitemerch marked this pull request as draft June 30, 2026 06:54
@whitemerch
whitemerch force-pushed the chakib.hamie/per_platform_payload_scoping branch from 0533db3 to be6bef0 Compare June 30, 2026 07:14
@whitemerch
whitemerch marked this pull request as ready for review June 30, 2026 07:18
@whitemerch
whitemerch force-pushed the chakib.hamie/per_platform_payload_scoping branch from be6bef0 to 6fb32a2 Compare June 30, 2026 08:07
MarshalX
MarshalX previously approved these changes Jun 30, 2026

@MarshalX MarshalX 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 force-pushed the chakib.hamie/per_platform_payload_scoping branch from 6fb32a2 to f3872a6 Compare June 30, 2026 17:35
@whitemerch
whitemerch merged commit bd63f68 into main Jun 30, 2026
19 of 20 checks passed
@whitemerch
whitemerch deleted the chakib.hamie/per_platform_payload_scoping branch June 30, 2026 18:12
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