Skip to content

feat(terraform): add OpenTofu file extension support #8747

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jswank
Copy link

@jswank jswank commented Apr 16, 2025

Add support for OpenTofu file extensions (.tofu and .tofu.json) to enable scanning of OpenTofu infrastructure as code files.

Description

This PR adds detection support for OpenTofu file extensions (.tofu and .tofu.json). These files are functionally identical to Terraform files but use the OpenTofu extension, allowing Trivy to scan OpenTofu infrastructure as code files.

Checklist

Before/After Example

Before

OpenTofu (.tofu) files are not recognized as infrastructure as code files.

$ trivy config /path/to/directory/with/tofu/files
2025-04-16 INFO [misconfig] Misconfiguration scanning is enabled
2025-04-16 INFO Detected config files num=0

After

OpenTofu (.tofu) files are correctly detected and scanned for security issues.

$ trivy config /path/to/directory/with/tofu/files
2025-04-16 INFO [misconfig] Misconfiguration scanning is enabled
2025-04-16 INFO [terraform scanner] Scanning root module file_path="."
2025-04-16 INFO Detected config files num=2

Add support for OpenTofu file extensions (.tofu and .tofu.json) to
enable scanning of OpenTofu infrastructure as code files.
@jswank jswank requested review from simar7 and nikpivkin as code owners April 16, 2025 17:30
@CLAassistant
Copy link

CLAassistant commented Apr 16, 2025

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@simar7 simar7 requested a review from Copilot April 16, 2025 21:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for OpenTofu file extensions to enable scanning of infrastructure as code files with .tofu and .tofu.json extensions.

  • Updated parser logic to recognize tofu file extensions.
  • Added test cases to confirm tofu file detection.
  • Extended detection logic to include tofu extensions in file type determination.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/iac/scanners/terraform/parser/parser.go Updated file parsing to support .tofu and .tofu.json files.
pkg/iac/detection/detect_test.go Added tests for tofu file handling with and without readers.
pkg/iac/detection/detect.go Expanded extension checking to include tofu extensions.

Copy link
Member

@simar7 simar7 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @jswank - adding support for OpenTofu file extension would imply adding support for OpenTofu scanning as a whole. We haven't committed to doing that just yet - there is an open discussion for it #5069

Having said that, it might be time to do so since OpenTofu has been out for a some time now. There are a couple of issues that if added to OpenTofu will make it diverge from Terraform (and may require changes on our end as well) such as:

  1. Direct support for conditional single-instance resources opentofu/opentofu#1306
  2. Support OCI registries for provider and module distribution. opentofu/opentofu#308

cc @nikpivkin @itaysk WDYT?

@jswank
Copy link
Author

jswank commented Apr 16, 2025

Thanks for the response @simar7! It does seems like an important consideration for the project- you certainly don't want to imply support of OpenTofu-specific functionality unintentionally. Especially since some divergence in OpenTofu / Terraform capabilities has begun to occur in recent versions.

The use case I have is fairly limited: my organization has made the decision to standardize on OpenTofu and is in the process of converting existing Terraform IaC. In order to clearly signify where this conversion has happened, we are renaming .tf files to .tofu.

Would you be open to a PR which allowed for the TF (.tf vs .tofu) extension to be specified as a flag? This would simply make use of existing Terraform functionality against a differently targeted set of files, i.e. no suggestion of supporting (potentially) divergent OpenTofu features to Trivy users. Alternatively, is this something that could be reflected in documentation? Caveat emptor, YMMV, etc.

@jswank jswank changed the title feat(iac): add OpenTofu file extension support feat(terraform): add OpenTofu file extension support Apr 16, 2025
@nikpivkin
Copy link
Contributor

nikpivkin commented Apr 17, 2025

Usually, in such cases, Trivy allows customizing file patterns via the file-patterns flag. However, this flag is not applicable if the parser works with several file extensions at once, because it becomes unclear which extension is overridden.

Having said that, it might be time to do so since OpenTofu has been out for a some time now.

If we decide to support OpenTofu, we will also need to implement support for features that exist exclusively in OpenTofu, and only enable them for files with the .tofu extension.

At this point, to my understanding and unless I'm missing something, OpenTofu does not contain any changes that could affect our current scanning goals. However, if we add support for the .tofu extension now, and OpenTofu introduces new features later, it may cause errors when scanning new configurations. On the other hand, for users who use both .tf and .tofu files at the same time, scanning only .tf will look incorrect and incomplete.

@wazy
Copy link

wazy commented May 14, 2025

Usually, in such cases, Trivy allows customizing file patterns via the file-patterns flag. However, this flag is not applicable if the parser works with several file extensions at once, because it becomes unclear which extension is overridden.

Having said that, it might be time to do so since OpenTofu has been out for a some time now.

If we decide to support OpenTofu, we will also need to implement support for features that exist exclusively in OpenTofu, and only enable them for files with the .tofu extension.

At this point, to my understanding and unless I'm missing something, OpenTofu does not contain any changes that could affect our current scanning goals. However, if we add support for the .tofu extension now, and OpenTofu introduces new features later, it may cause errors when scanning new configurations. On the other hand, for users who use both .tf and .tofu files at the same time, scanning only .tf will look incorrect and incomplete.

My org is currently switching over to only .tofu files and would love to use trivy as before with .tf files so this would be great to have included. In the interim I attempted to do the following:

trivy config --file-patterns "hcl:.*.tofu" .

but am unsuccessful in getting that to work no matter what regex is used:

WARN [report] Supported files for scanner(s) not found. scanners=[misconfig]

@nikpivkin Am I missing something obvious or does that flag not work for the misconfig scanner?

@nikpivkin
Copy link
Contributor

trivy config --file-patterns "hcl:.*.tofu"

This is not a valid pattern because the first section should specify one of the following scanners. This flag works for most IaC scanners, but not for Terraform. I left a comment above with an explanation.

@nikpivkin
Copy link
Contributor

@simar7 I've left the comment. Wdyt? I think changing the filePatterns flag to allow specifying a subtype, such as terraform:hcl:.*.tofu or terraform:json:.*.json, might be unnecessary overhead since this need arises only for Terraform and so far for OpenTofu. Unlike Dockerfile, where custom extensions are common, I don't think many users will use custom extensions for Terraform.

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.

5 participants