Skip to content

fix(terraform): resolve relative module paths from file directory not CWD - #7656

Open
pankhuriVarshney wants to merge 3 commits into
bridgecrewio:mainfrom
pankhuriVarshney:fix-terraform-relative-module-paths
Open

fix(terraform): resolve relative module paths from file directory not CWD#7656
pankhuriVarshney wants to merge 3 commits into
bridgecrewio:mainfrom
pankhuriVarshney:fix-terraform-relative-module-paths

Conversation

@pankhuriVarshney

Copy link
Copy Markdown

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

Description

This PR fixes an issue where Checkov resolves relative Terraform module paths from the current working directory instead of from the directory containing the .tf file that declares the module.

In monorepo structures with nested Terraform stacks referencing modules via relative paths like ../../../../modules/cloudsql-mysql, running Checkov from the repository root caused FileNotFoundError because the relative path was being joined with the scan root directory rather than the declaring file's directory.

The fix ensures os.path.abspath() is used when computing the base directory for relative module source resolution in TFParser.get_module_source(), so the resolved path is absolute and correct regardless of where Checkov is executed from.

Fixes #7547

Checklist:

  • 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

… CWD

When Terraform files in nested directories reference local modules using
relative paths (e.g. ../../../../modules/foo), Checkov was resolving
these paths relative to the current working directory instead of the
declaring file's directory. This caused module loading failures with
FileNotFoundError when running Checkov from the repository root on
monorepo structures.

The fix ensures os.path.abspath() is used when computing the base
directory for relative module source resolution, so the resolved path
is absolute and correct regardless of where Checkov is executed from.

Fixes bridgecrewio#7547
… CWD

When Terraform files in nested directories reference local modules using
relative paths (e.g. ../../../../modules/foo), Checkov was resolving
these paths relative to the current working directory instead of the
declaring file's directory. This caused module loading failures with
FileNotFoundError when running Checkov from the repository root on
monorepo structures.

The fix ensures os.path.abspath() is used when computing the base
directory for relative module source resolution, so the resolved path
is absolute and correct regardless of where Checkov is executed from.

Fixes bridgecrewio#7547
…tion

Add tests covering GitHub issue bridgecrewio#7547:
- test_relative_module_path_resolved_from_file_directory: end-to-end
  integration test verifying a nested stack can load a module via
  relative paths when parsed from the repo root
- test_get_module_source_resolves_relative_path_from_cwd: direct unit
  test for TFParser.get_module_source() ensuring relative sources are
  resolved to absolute paths from the declaring file's directory
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.

Checkov computing relative path from where it was run

1 participant