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: add support for Terraform modules in tflint #2297

Merged
merged 10 commits into from
Jan 6, 2022
Merged

feat: add support for Terraform modules in tflint #2297

merged 10 commits into from
Jan 6, 2022

Conversation

colwynlegitscript
Copy link
Contributor

@colwynlegitscript colwynlegitscript commented Jan 5, 2022

Fixes tflint path errors when files and modules are referenced in a relative directory in Terraform and enables tflint module mode usage. tflint has to be run from the same directory as the file under test in order for relative references to work correctly.

I'm not sure if there are other updates that need to be made to the tests so I'd appreciate some guidance there. I added some Terraform that seems like the right test structure to me, but I'm perfectly happy to change it. The image built and make test returned successfully locally.

I feel a little weird about where terraform get is right now but it does work. If you'd like I can try to determine if module mode is enabled and add it to LINTER_COMMAND for TERRAFORM_TFLINT.

Proposed Changes

  1. Changes directory to the location of the Terraform file under test for tflint
  2. Runs terraform get before running tflint
    1. Adds the terraform binary to support this

Example

File structure of the project

$ tree -a -I '.git' .
.
├── .github
│   └── linters
│       └── .tflint.hcl
├── .gitignore
├── modules
│   ├── mymodule
│   │   ├── main.tf
│   │   └── variables.tf
│   └── templates
│       └── template.tmpl
└── src
    └── environment
        └── service
            └── main.tf

Where src/environment/service/main.tf contains

module "good_reference" {
  source = "terraform-aws-modules/s3-bucket/aws"
  version = "2.11.1"

  bucket = "test-bucket"
}

module "good_relative_reference" {
  source = "../../../modules/mymodule"

  policy_json = templatefile("../../../modules/templates/template.tmpl", {
    value = "testvalue"
  })
}

.github/linters/.tflint.hcl contains

config {
  module = true
  force = false
}

plugin "aws" {
  enabled = true
}

Testing

Current build

$ docker run -e RUN_LOCAL=true -e VALIDATE_ALL_CODEBASE=false -e VALIDATE_TERRAFORM_TFLINT=true -v "$(pwd):/tmp/lint" github/super-linter:slim-v4
# ...lots of logs i'm not including...
2022-01-05 19:37:45 [INFO]   File:[/tmp/lint/modules/mymodule/main.tf]
2022-01-05 19:37:46 [INFO]    - File:[main.tf] was linted with [tflint] successfully
2022-01-05 19:37:46 [INFO]   ---------------------------
2022-01-05 19:37:46 [INFO]   File:[/tmp/lint/modules/mymodule/variables.tf]
2022-01-05 19:37:46 [INFO]    - File:[variables.tf] was linted with [tflint] successfully
2022-01-05 19:37:46 [INFO]   ---------------------------
2022-01-05 19:37:46 [INFO]   File:[/tmp/lint/src/environment/service/main.tf]
2022-01-05 19:37:46 [ERROR]   Found errors in [tflint] linter!
2022-01-05 19:37:46 [ERROR]   Error code: 1. Command output:
------
Failed to load configurations. 2 error(s) occurred:

Error: `good_reference` module is not found. Did you run `terraform init`?

  on /tmp/lint/src/environment/service/main.tf line 7, in module "good_reference":
   7: module "good_reference" {

Error: `good_relative_reference` module is not found. Did you run `terraform init`?

  on /tmp/lint/src/environment/service/main.tf line 14, in module "good_relative_reference":
  14: module "good_relative_reference" {
------
2022-01-05 19:37:47 [INFO]   ----------------------------------------------
2022-01-05 19:37:47 [INFO]   ----------------------------------------------
2022-01-05 19:37:47 [INFO]   The script has completed
2022-01-05 19:37:47 [INFO]   ----------------------------------------------
2022-01-05 19:37:47 [INFO]   ----------------------------------------------
2022-01-05 19:37:47 [ERROR]   ERRORS FOUND in TERRAFORM_TFLINT:[1]
2022-01-05 19:37:47 [FATAL]   Exiting with errors found!

Build with this change

I tagged the image as super-linter-test

$ docker run -e RUN_LOCAL=true -e VALIDATE_ALL_CODEBASE=false -e VALIDATE_TERRAFORM_TFLINT=true -v "$(pwd):/tmp/lint" super-linter-test
# more logs i'm not including
2022-01-05 19:36:42 [INFO]   File:[/tmp/lint/modules/mymodule/main.tf]
2022-01-05 19:36:43 [INFO]    - File:[main.tf] was linted with [tflint] successfully
2022-01-05 19:36:43 [INFO]   ---------------------------
2022-01-05 19:36:44 [INFO]   File:[/tmp/lint/modules/mymodule/variables.tf]
2022-01-05 19:36:45 [INFO]    - File:[variables.tf] was linted with [tflint] successfully
2022-01-05 19:36:45 [INFO]   ---------------------------
2022-01-05 19:36:45 [INFO]   File:[/tmp/lint/src/environment/service/main.tf]
2022-01-05 19:36:49 [INFO]    - File:[main.tf] was linted with [tflint] successfully
2022-01-05 19:36:49 [INFO]   ----------------------------------------------
2022-01-05 19:36:49 [INFO]   ----------------------------------------------
2022-01-05 19:36:49 [INFO]   The script has completed
2022-01-05 19:36:49 [INFO]   ----------------------------------------------
2022-01-05 19:36:49 [INFO]   ----------------------------------------------
2022-01-05 19:36:49 [NOTICE]   All file(s) linted successfully with no errors detected
2022-01-05 19:36:49 [INFO]   ----------------------------------------------

Inserting errors

Just to make sure I wasn't arbitrarily making the linter pass, I removed the version from the good_reference module

$ docker run -e RUN_LOCAL=true -e VALIDATE_ALL_CODEBASE=false -e VALIDATE_TERRAFORM_TFLINT=true -v "$(pwd):/tmp/lint" super-linter-test
# more logs i'm not including
2022-01-05 20:48:02 [INFO]   File:[/tmp/lint/modules/mymodule/main.tf]
2022-01-05 20:48:03 [INFO]    - File:[main.tf] was linted with [tflint] successfully
2022-01-05 20:48:03 [INFO]   ---------------------------
2022-01-05 20:48:03 [INFO]   File:[/tmp/lint/modules/mymodule/variables.tf]
2022-01-05 20:48:04 [INFO]    - File:[variables.tf] was linted with [tflint] successfully
2022-01-05 20:48:04 [INFO]   ---------------------------
2022-01-05 20:48:04 [INFO]   File:[/tmp/lint/src/environment/service/main.tf]
2022-01-05 20:48:09 [ERROR]   Found errors in [tflint] linter!
2022-01-05 20:48:09 [ERROR]   Error code: 2. Command output:
------
Downloading registry.terraform.io/terraform-aws-modules/s3-bucket/aws 2.11.1 for good_reference...
- good_reference in .terraform/modules/good_reference
- good_relative_reference in ../../../modules/mymodule
1 issue(s) found:

Warning: module "good_reference" should specify a version (terraform_module_version)

  on main.tf line 7:
   7: module "good_reference" {

Reference: https://github.com/terraform-linters/tflint/blob/v0.34.1/docs/rules/terraform_module_version.md
------
2022-01-05 20:48:09 [INFO]   ----------------------------------------------
2022-01-05 20:48:09 [INFO]   ----------------------------------------------
2022-01-05 20:48:09 [INFO]   The script has completed
2022-01-05 20:48:09 [INFO]   ----------------------------------------------
2022-01-05 20:48:09 [INFO]   ----------------------------------------------
2022-01-05 20:48:09 [ERROR]   ERRORS FOUND in TERRAFORM_TFLINT:[1]
2022-01-05 20:48:09 [FATAL]   Exiting with errors found!

I also broke the relative module reference by choosing an invalid path ../../../broken/path/mymodule

$ docker run -e RUN_LOCAL=true -e VALIDATE_ALL_CODEBASE=false -e VALIDATE_TERRAFORM_TFLINT=true -v "$(pwd):/tmp/lint" super-linter-test
2022-01-05 20:48:41 [INFO]   File:[/tmp/lint/modules/mymodule/main.tf]
2022-01-05 20:48:42 [INFO]    - File:[main.tf] was linted with [tflint] successfully
2022-01-05 20:48:42 [INFO]   ---------------------------
2022-01-05 20:48:42 [INFO]   File:[/tmp/lint/modules/mymodule/variables.tf]
2022-01-05 20:48:44 [INFO]    - File:[variables.tf] was linted with [tflint] successfully
2022-01-05 20:48:44 [INFO]   ---------------------------
2022-01-05 20:48:44 [INFO]   File:[/tmp/lint/src/environment/service/main.tf]
2022-01-05 20:48:46 [ERROR]   Found errors in [tflint] linter!
2022-01-05 20:48:46 [ERROR]   Error code: 1. Command output:
------
Downloading registry.terraform.io/terraform-aws-modules/s3-bucket/aws 2.11.1 for good_reference...
- good_reference in .terraform/modules/good_reference
- good_relative_reference in
╷
│ Error: Unreadable module directory
│
│ Unable to evaluate directory symlink: lstat ../../../broken: no such file
│ or directory
╵

╷
│ Error: Failed to read module directory
│
│ Module directory  does not exist or cannot be read.
╵

╷
│ Error: Unreadable module directory
│
│ Unable to evaluate directory symlink: lstat ../../../broken: no such file
│ or directory
╵

╷
│ Error: Failed to read module directory
│
│ Module directory  does not exist or cannot be read.
╵

Failed to load configurations. 1 error(s) occurred:

Error: Failed to read module directory

Module directory  does not exist or cannot be read.
------
2022-01-05 20:48:46 [INFO]   ----------------------------------------------
2022-01-05 20:48:46 [INFO]   ----------------------------------------------
2022-01-05 20:48:46 [INFO]   The script has completed
2022-01-05 20:48:46 [INFO]   ----------------------------------------------
2022-01-05 20:48:46 [INFO]   ----------------------------------------------
2022-01-05 20:48:46 [ERROR]   ERRORS FOUND in TERRAFORM_TFLINT:[1]
2022-01-05 20:48:46 [FATAL]   Exiting with errors found!

Readiness Checklist

Author/Contributor

  • If documentation is needed for this change, has that been included in this pull request

Reviewing Maintainer

  • Label as breaking if this is a large fundamental change
  • Label as either automation, bug, documentation, enhancement, infrastructure, or performance

@admiralAwkbar admiralAwkbar merged commit 09b571b into super-linter:main Jan 6, 2022
@colwynlegitscript colwynlegitscript deleted the tflint-module-fix branch January 6, 2022 18:50
sarahc23 pushed a commit to 23andMe/super-linter that referenced this pull request May 6, 2022
* fix: support tflint relative module references

* chore: add test for relative module imports

* chore: add terraform binary

* chore: move tests

* chore: add newlines to tests

* chore: add newlines to tests

* refactor: move terraform get

* refactor: put terraform get back where it was

Co-authored-by: Lukas Gravley <admiralawkbar@github.com>
sarahc23 pushed a commit to 23andMe/super-linter that referenced this pull request May 6, 2022
* fix: support tflint relative module references

* chore: add test for relative module imports

* chore: add terraform binary

* chore: move tests

* chore: add newlines to tests

* chore: add newlines to tests

* refactor: move terraform get

* refactor: put terraform get back where it was

Co-authored-by: Lukas Gravley <admiralawkbar@github.com>
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

2 participants