Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terraform): Nested source_module_objects with missing foreach key #5580

Merged
merged 11 commits into from
Sep 20, 2023

Conversation

bo156
Copy link
Contributor

@bo156 bo156 commented Sep 20, 2023

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Description

A bug was found where the source_module_object of TerraformLocalGraph.vertices might had missing/unassigned foreach_idx in cases with nested foreach terraform objects (see changed test for example).
To solve it I:

  • Made TFModule and TFDefinitionKey immutable objects (as we use them as dictionary keys, which is extremely bad with mutable objects and caused bugs while debugging).
  • Changed all usages of those objects to create new instances rather than mutating existing ones.
  • Fixed the bug itself by correctly finding the vertices to update under ForeachModuleHandler._update_children_foreach_index
  • Improved test to check all created vertices contain source_module_objects with foreach_idx assigned

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my feature, policy, or fix is effective and works
  • New and existing tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@bo156 bo156 temporarily deployed to scan-security September 20, 2023 10:55 — with GitHub Actions Inactive
@bo156 bo156 temporarily deployed to scan-security September 20, 2023 14:13 — with GitHub Actions Inactive
Copy link
Contributor

@gruebel gruebel left a comment

Choose a reason for hiding this comment

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

🏖️

@bo156 bo156 merged commit 840e6b3 into main Sep 20, 2023
30 checks passed
@bo156 bo156 deleted the bugfix/no-mutable-dict-keys branch September 20, 2023 14:22
pull bot pushed a commit to Julian-Louis/checkov that referenced this pull request Sep 20, 2023
…bridgecrewio#5580)

* made TFModule a frozen immutable dataclass, didn't update all usages yet

* replaced one mutable location with a function that recursively nullifies all foreach indices

* Updated second todo area to use recursion with immutable objects instead of mutable

* Also made TFDefinitionKey immutable

* Fixed insertion of foreach keys for all vertices and updated test to check for them all

* middle of final debug - fixed foreach key for 12/16 resources in test

* Fixed creation of foreach indices for all resources of all types

* flake8

* Added another tf_definitions check for the new test to make sure all foreach key exists always

* moved _get_module_with_only_relevant_foreach_idx to module_handler as this is the only place where it's used

* empty commit to retrigger checks
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.

3 participants