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

feat(terraform): Add provider address to resources #6266

Merged
merged 19 commits into from
May 29, 2024

Conversation

ChanochShayner
Copy link
Contributor

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

Description

  • If the provider is not defined, use the default provider.
  • If the provider is explicitly defined in the resource, use that provider.
  • If the provider is defined in the module, use that provider.

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

Copy link
Collaborator

@tsmithv11 tsmithv11 left a comment

Choose a reason for hiding this comment

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

🍻

@@ -139,6 +145,85 @@ def _add_block_data_to_graph(self, idx: int, block: TerraformBlock) -> None:
self.in_edges[idx] = []
self.out_edges[idx] = []

def _add_provider_attr_to_resources(self) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add documentation to this function? looks rather complex...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

return cast(str, module_providers[list(module_providers.keys())[0]].replace("$", "").replace("{", "").replace("}", ""))
else:
for p_address in provider_address:
if m_alias.replace("$", "").replace("{", "").replace("}", "") == self.vertices[p_address].name:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you extract those strings to consts? you use them multiple times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

def _get_resource_name_and_type_from_name(name: str) -> tuple[str, str]:
resource_name, resource_type = '', ''
split_name = name.split('.')
if len(split_name) >= 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

this will not work if you have for example . inside a foreach key.
Instead you can use this function get_sanitized_terraform_resource_id (or a variation of it) from here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove, already implemented logic in another func

@@ -461,6 +462,7 @@ def parse_hcl_module_from_tf_definitions(
)
self.add_tfvars(module, source)
copy_of_tf_definitions = pickle_deepcopy(tf_definitions)
module.temp_tf_definition = tf_definitions # type:ignore # will be TFDefinitionKey and not string
Copy link
Contributor

Choose a reason for hiding this comment

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

why ignore instead of changing the type in line 51?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already tried to play with it a couple of times and it didn't work, (u can see at the first couple of commits).

provider_address_with_alias = local_graph.vertices[4].attributes.get('__address__')
assert resource_provider_address_with_alias == provider_address_with_alias

def test_provider_edge_cases(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

great job with the tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😸

@ChanochShayner ChanochShayner merged commit 657bad4 into main May 29, 2024
41 checks passed
@ChanochShayner ChanochShayner deleted the add-provider-address-to-resources branch May 29, 2024 14:31
pull bot pushed a commit to tooniez/checkov that referenced this pull request May 29, 2024
* Add provider address to resources

* remove unrelated change

* Fix UTs and mypy issues

* Fix tf definition type

* Fix tf definition type

* Fix tf definition type

* remove unused import

* add edge cases and UTs

* remove unrelated changes

* flake8 and UTs

* mypy

* mypy

* Fix UT + add env var with default true

* remove un needed code + func doc + CR fixes

* UT fix
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.

None yet

4 participants