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

Rule aws_s3_bucket_name does work on module parameters #194

Closed
stksz opened this issue Nov 4, 2021 · 3 comments · Fixed by #195
Closed

Rule aws_s3_bucket_name does work on module parameters #194

stksz opened this issue Nov 4, 2021 · 3 comments · Fixed by #195
Labels
bug Something isn't working

Comments

@stksz
Copy link

stksz commented Nov 4, 2021

I have the following in place:

tflint-config:

plugin "aws" {
  enabled = true
  version = "0.8.0"
  source  = "github.com/terraform-linters/tflint-ruleset-aws"
}

config {
  module = true
}

rule "aws_s3_bucket_name" {
  enabled = true
  regex = "^[a-z\\-]+$"
  prefix = "my-prefix-"
}

Terrafrom "main-module":

module "my_bucket" {
  source = "../modules/s3"
  bucket_name = "my-new-bucket......" # <--- This is on purpose wrong!!!
}

The used Terraform-module:

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name # <-- This variable is defined in a "variables.tf" from within the module-directory
  acl    = "private"

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}

When I now run "tflint" within the "main-modules"-directory I don't get any error even though the rule for "aws_s3_bucket_name" should produce an error as the value for "bucket_name" does not match die regex and the prefix of the rule. Changing "bucket" from within the module from "bucket = var.bucket_name" to something like "bucket = "foobar"" results in getting the expected error-message as the bucket-name does not adhere to the rule(s).

Version

$ tflint -v
TFLint version 0.33.1
+ ruleset.aws (0.8.0)
$ terraform -v
Terraform v1.0.10
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.63.0
@bendrucker
Copy link
Member

Hmm, the correct behavior would be the reverse. TFLint's module feature is meant to raise errors caused by the supplied variables in a call, not to check the static content of the module itself.

@bendrucker
Copy link
Member

Confirmed this is not an issue with that rule. It evaluates the variable and the logic is correct. This is a bug with TFLint's propagation of issues from modules. Issues should only be propagated when their expression references a module variable:

https://github.com/terraform-linters/tflint/blob/4db35875d3981252938d78c6bfe5672a2a35ae52/tflint/runner.go#L285-L302

This logic is not working correctly for plugins. Will need to spend more time debugging to find a fix.

@bendrucker bendrucker changed the title Rule aws_s3_bucket_name does work on module parameters Issues are not emitted for plugin rules that reference module variables Nov 4, 2021
@wata727
Copy link
Member

wata727 commented Nov 6, 2021

This is a bug in the rule. In order to propagate the issue and expression associations to the host, the plugin must use the appropriate API.

The aws_s3_bucket_name rule uses the EmitIssue API and you can't specify the association with the expression:
https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.8.0/rules/aws_s3_bucket_name.go#L73-L77

In this case, the rule should use EmitIssueOnExpr API.
https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.8.0/rules/aws_api_gateway_model_invalid_name.go#L55-L59

@wata727 wata727 changed the title Issues are not emitted for plugin rules that reference module variables Rule aws_s3_bucket_name does work on module parameters Nov 6, 2021
@wata727 wata727 transferred this issue from terraform-linters/tflint Nov 6, 2021
@wata727 wata727 added the bug Something isn't working label Nov 6, 2021
@wata727 wata727 closed this as completed Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
3 participants