Skip to content

feat(terraform): support optional() type constraints in variable rendering#7502

Open
pszypowicz wants to merge 1 commit into
bridgecrewio:mainfrom
pszypowicz:fix/support-optional-type-constraints
Open

feat(terraform): support optional() type constraints in variable rendering#7502
pszypowicz wants to merge 1 commit into
bridgecrewio:mainfrom
pszypowicz:fix/support-optional-type-constraints

Conversation

@pszypowicz
Copy link
Copy Markdown

@pszypowicz pszypowicz commented Apr 7, 2026

Summary

Resolves #4874

When a Terraform variable uses optional(type, default) in its type constraint and the caller omits that field, checkov does not resolve the default value. This causes false-positive/false-negative check results -- for example, optional(string, "RSA-HSM") for a Key Vault key type is not resolved, so CKV_AZURE_112 (KeyBackedByHSM) incorrectly fails.

Changes

  • New optional_type_parser module that parses optional() defaults from type constraint strings, with recursive descent into nested objects at arbitrary depth (map/list/set/object at every level)
  • Renderer integration at all three variable resolution points (MODULE edge, TF_VARIABLE edge, extract_value_from_vertex)
  • Fix each.value.a.b.c multi-level path resolution in the for_each handler -- the old code only took split('.')[-1], breaking nested object access. The fix reuses the existing find_in_dict utility. Also handles array indexing (each.value.items[0].field) and multiple references in one expression (merge(each.value.tags, {Name = each.value.name}))

Test plan

  • Unit tests for the parser: default value types (string, number, bool, null, float, map, list), edge cases (empty string, negative numbers, colons in values), nested types with mutation safety
  • Graph-level tests: for_each expansion, direct variable reference, 3-level nested object resolution
  • End-to-end CKV_AZURE_112 regression test with 4 scenarios: list(object) PASS/FAIL + map(object) nested PASS/FAIL
  • Full terraform test suite: 2873 passed, 0 regressions

@pszypowicz pszypowicz force-pushed the fix/support-optional-type-constraints branch from 7cf76a2 to 4e1d174 Compare April 8, 2026 08:18
@pszypowicz pszypowicz force-pushed the fix/support-optional-type-constraints branch from 4e1d174 to 8e54d12 Compare April 8, 2026 11:26
@pszypowicz pszypowicz force-pushed the fix/support-optional-type-constraints branch from 8e54d12 to 46bc64d Compare April 8, 2026 11:50
@pszypowicz pszypowicz force-pushed the fix/support-optional-type-constraints branch from 46bc64d to f182e07 Compare April 8, 2026 12:01
@pszypowicz pszypowicz force-pushed the fix/support-optional-type-constraints branch from f182e07 to b1cf868 Compare April 9, 2026 10:10
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.

Terraform - Support optional() in variable type constraints

1 participant