Skip to content

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Jul 27, 2024

Follow up of #194

This fixes some issues with the terraform_map_duplicate_keys rule added in #194.

First, EvaluateExpr call fails due to terraform-linters/tflint-plugin-sdk#338 if map key is a naked identifier.

$ cat main.tf
locals {
  map = {
    foo = 1
    bar = 2
    bar = 3 # duplicate key
  }
}
$ tflint
Failed to check ruleset; failed to check "terraform_map_duplicate_keys" rule: <nil>: failed to evaluate expression; main.tf:3,5-8: Invalid reference; A reference to a resource type must be followed by at least one attribute access, specifying the resource name., and 2 other diagnostic(s)

To prevent this, we need to get the values of the keys in a way similar to ObjectConsKeyExpr.Value as a workaround.
https://github.com/hashicorp/hcl/blob/v2.21.0/hclsyntax/expression.go#L1311-L1338

Second, since val.AsString can panic on sensitive values ​​or non-string keys, This PR made it ignore these.

variable "sensitve"  {
  default = "secret"
  sensitive = true
}

locals {
  map = {
    (var.sensitive) = 1 # sensitive key
    1 = 2               # number is converted to string
    {} = 3              # invalid in the Terraform language, but valid in the HCL layer
  }
}

Finally, I fixed an issue where added rules were not registered in presets and there was a lack of documentation.

@wata727 wata727 force-pushed the follow_up_of_terraform_map_duplicate_keys branch from d640dce to 6e2e561 Compare July 27, 2024 15:02
@wata727
Copy link
Member Author

wata727 commented Jul 27, 2024

@bendrucker Could you take a look at this?
I'd especially like some input on whether this rule should be added to the recommended preset.

@wata727 wata727 force-pushed the follow_up_of_terraform_map_duplicate_keys branch from 6e2e561 to d5fcc2d Compare July 28, 2024 16:00
Copy link
Member

@bendrucker bendrucker left a comment

Choose a reason for hiding this comment

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

Thanks! Agree that it makes sense in the recommended preset.

@wata727 wata727 merged commit 2156dd3 into main Jul 30, 2024
@wata727 wata727 deleted the follow_up_of_terraform_map_duplicate_keys branch July 30, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants