Skip to content

Speed up IAM privilege-escalation rules on large Terraform repos#227

Merged
whitemerch merged 2 commits into
mainfrom
chakib.hamie/fix_iam_rules_pt2
Jul 1, 2026
Merged

Speed up IAM privilege-escalation rules on large Terraform repos#227
whitemerch merged 2 commits into
mainfrom
chakib.hamie/fix_iam_rules_pt2

Conversation

@whitemerch

@whitemerch whitemerch commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Motivation

On large Terraform repositories, the IAM privilege-escalation rule family was a major scan bottleneck. Each rule rebuilt dangerous-permission sets by resolving policy_arn attachments with an O(attachments × documents) document rescan. This is a followup of #221, which improved these queries, but there was a bigger improvement possible.

Changes

IAM helpers (common.rego)

Build _policy_name_wildcard_perms once per query in a single pass over managed policy resources. Attachment branches for roles, users, and groups now resolve policy_arn references via membership in that set instead of rescanning every document for each attachment.

Tests

Add regression coverage for inline policies, cross-document attachment resolution, and non-wildcard policy exclusion.

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. opa test assets/libraries/ --v1-compatible
  2. Build the scanner and scan a large Terraform repo with many IAM roles and policy attachments.
  3. Confirm privilege-escalation rules no longer appear in slow-rule logs and total scan time drops materially.

On cloud-inventory (29,788 files, full rule corpus): scan time dropped from ~8m to ~1m53s, with 0 slow IAM privilege-escalation rules (previously 19 rules at ~3 min each).

Blast Radius

Affects Terraform IAM privilege-escalation rules that use *_unrecommended_permission_policy_scenarios in common.rego. No CLI or engine behavior changes.

Additional Notes

N/A

I submit this contribution under the Apache-2.0 license.

@datadog-prod-us1-3

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

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 50.09% (+0.00%)

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

… attachment lookups avoid rescanning every document per attachment.
@whitemerch
whitemerch force-pushed the chakib.hamie/fix_iam_rules_pt2 branch from e38cc08 to d374ba5 Compare June 30, 2026 16:27
@whitemerch
whitemerch marked this pull request as ready for review June 30, 2026 17:18
@whitemerch
whitemerch requested a review from a team as a code owner June 30, 2026 17:18
MarshalX
MarshalX previously approved these changes Jul 1, 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.

Awesome!

Nit: we do have tests for role and user attachment, but not for group

@whitemerch
whitemerch merged commit 6d429d6 into main Jul 1, 2026
21 checks passed
@whitemerch
whitemerch deleted the chakib.hamie/fix_iam_rules_pt2 branch July 1, 2026 10:49
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