From 09b571b1b0f3a62a22988dc2dbcd676d91c6896f Mon Sep 17 00:00:00 2001 From: Colwyn Fritze-Moor <80352425+colwynlegitscript@users.noreply.github.com> Date: Thu, 6 Jan 2022 09:04:10 -0800 Subject: [PATCH] feat: add support for Terraform modules in tflint (#2297) * 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 --- .../good/terraform_tflint_good_2.tf | 10 ++++++++++ .../terraform_tflint/modules/ec2_instance/main.tf | 14 ++++++++++++++ Dockerfile | 6 ++++++ lib/functions/worker.sh | 9 +++++++++ 4 files changed, 39 insertions(+) create mode 100644 .automation/test/terraform_tflint/good/terraform_tflint_good_2.tf create mode 100644 .automation/test/terraform_tflint/modules/ec2_instance/main.tf diff --git a/.automation/test/terraform_tflint/good/terraform_tflint_good_2.tf b/.automation/test/terraform_tflint/good/terraform_tflint_good_2.tf new file mode 100644 index 00000000..771ee9c8 --- /dev/null +++ b/.automation/test/terraform_tflint/good/terraform_tflint_good_2.tf @@ -0,0 +1,10 @@ +module "s3_bucket" { + source = "terraform-aws-modules/s3-bucket/aws" + version = "2.11.1" + + bucket = "test-bucket" +} + +module "good_relative_reference" { + source = "../modules/ec2_instance" +} diff --git a/.automation/test/terraform_tflint/modules/ec2_instance/main.tf b/.automation/test/terraform_tflint/modules/ec2_instance/main.tf new file mode 100644 index 00000000..48b98e43 --- /dev/null +++ b/.automation/test/terraform_tflint/modules/ec2_instance/main.tf @@ -0,0 +1,14 @@ +resource "aws_instance" "good" { + ami = "ami-0ff8a91507f77f867" + instance_type = "t2.small" + associate_public_ip_address = false + + vpc_security_group_ids = ["sg-12345678901234567"] + metadata_options { + http_endpoint = "disabled" + } + + ebs_block_device { + encrypted = true + } +} diff --git a/Dockerfile b/Dockerfile index 7e61dd10..bcf07648 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ FROM yoheimuta/protolint:v0.35.2 as protolint FROM golangci/golangci-lint:v1.43.0 as golangci-lint FROM koalaman/shellcheck:v0.8.0 as shellcheck FROM ghcr.io/terraform-linters/tflint-bundle:v0.34.1.1 as tflint +FROM hashicorp/terraform:1.1.2 as terraform FROM alpine/terragrunt:1.1.2 as terragrunt FROM mvdan/shfmt:v3.4.2 as shfmt FROM accurics/terrascan:1.13.0 as terrascan @@ -129,6 +130,11 @@ COPY --from=shellcheck /bin/shellcheck /usr/bin/ ##################### COPY --from=golangci-lint /usr/bin/golangci-lint /usr/bin/ +##################### +# Install Terraform # +##################### +COPY --from=terraform /bin/terraform /usr/bin/ + ################## # Install TFLint # ################## diff --git a/lib/functions/worker.sh b/lib/functions/worker.sh index 96c81205..b11014fa 100755 --- a/lib/functions/worker.sh +++ b/lib/functions/worker.sh @@ -257,6 +257,15 @@ function LintCodebase() { cd "${DIR_NAME}" || exit ${LINTER_COMMAND} "${FILE_NAME}" 2>&1 ) + ############################################################################################ + # Corner case for TERRAFORM_TFLINT as it cant use the full path and needs to fetch modules # + ############################################################################################ + elif [[ ${FILE_TYPE} == "TERRAFORM_TFLINT" ]]; then + LINT_CMD=$( + cd "${DIR_NAME}" || exit + terraform get 2>&1 + ${LINTER_COMMAND} "${FILE_NAME}" 2>&1 + ) else ################################ # Lint the file with the rules #