Optimize privilege-escalation rules by pre-indexing IAM permission sets#221
Merged
whitemerch merged 1 commit intoJun 30, 2026
Merged
Conversation
|
🎯 Code Coverage (details) 🔗 Commit SHA: 9570712 | Docs | Datadog PR Page | Give us feedback! |
whitemerch
force-pushed
the
chakib.hamie/fix_privilege_escalation_rules_perf
branch
from
June 29, 2026 22:28
12f61c8 to
05e21d1
Compare
whitemerch
marked this pull request as ready for review
June 29, 2026 22:32
…rules share one document scan per query instead of repeating it per principal.
whitemerch
force-pushed
the
chakib.hamie/fix_privilege_escalation_rules_perf
branch
from
June 30, 2026 08:07
05e21d1 to
9570712
Compare
ChouraquiBen
approved these changes
Jun 30, 2026
|
|
||
| policies := {"aws_iam_role_policy", "aws_iam_user_policy", "aws_iam_group_policy", "aws_iam_policy"} | ||
| resourcePolicy := input.document[_].resource[policies[_]][policy] | ||
| _to_action_array(x) := x if is_array(x) |
Contributor
There was a problem hiding this comment.
Question: That will make queries fail if x is neither an array or a string, is that ok or should there be a default case?
Contributor
Author
There was a problem hiding this comment.
Good question, in practice it's fine as-is. IAM Action is only ever a string or array per the AWS policy schema, and if it's something else (e.g. null in a malformed policy), _to_action_array is undefined and that statement is simply skipped in the comprehension, same outcome as having no actions. No query failure, no false positives. Happy to add else := [] if you prefer making that explicit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The
*_unrecommended_permission_policy_scenarioshelpers inassets/libraries/common.regowere performing a fullinput.document[_]scan for every principal (role, user, group) they were called on. With Terraform module instantiation in place,input.documentcan contain many synthetic module documents, making each helper call O(principals × docs × policies). Because ~64 near-identical privilege-escalation rules call these helpers independently, the total cost was O(64 × principals × docs × policies) per scan, the primary contributor to multi-minute scan times on module-heavy repositories.Benchmarks on a simulated module-inflated input (3,000 roles, 750 policies across 50 documents) measured 3,216 ms → 45 ms per rule (~71× speedup).
Changes
Rule library (
assets/libraries/common.rego)Three helper functions (
role_unrecommended_permission_policy_scenarios,user_unrecommended_permission_policy_scenarios,group_unrecommended_permission_policy_scenarios) are replaced with an indexed design.Two new helpers support the index:
_to_action_arraynormalises anActionfield (string or array) to an array for uniform iteration._policy_wildcard_actionsreturns the set of lowercasedAllow-on-"*"actions for a given policy resource object. OPA memoizes complete-rule calls per unique argument, sojson_unmarshalis paid at most once per unique policy within a query.Three complete set rules (
_role_dangerous_pairs,_user_dangerous_pairs,_group_dangerous_pairs) pre-compute[principalName, permission]pairs across both scenarios (inline policies and policy attachments) for all principals at once. OPA evaluates and caches complete rules on first access; all subsequent lookups within the same query are O(1) set membership checks. This is the key difference from an incremental/partial-rule approach, which OPA specializes per bound key and does not cache across callers.Author Checklist
QA Instruction
opa test assets/libraries/— all 324 existing tests must pass (no new tests are needed since the function signatures are unchanged and existing tests cover both scenarios).go test ./pkg/engine/... -timeout 120s— engine tests must pass.Verify findings count is identical between the patched and unpatched library.
Blast Radius
DataDog IaC Scanner only. The change is limited to three helper functions in
assets/libraries/common.rego.Additional Notes
The ~64
role_with_privilege_escalation_by_actions_*,user_with_privilege_escalation_by_actions_*, andgroup_with_privilege_escalation_by_actions_*rules in the default rule corpus all benefit from this change without modification.I submit this contribution under the Apache-2.0 license.