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

resource_missing_tags: rewrite to only consider keys #517

Merged
merged 3 commits into from Jul 22, 2023
Merged

Conversation

bendrucker
Copy link
Member

@bendrucker bendrucker commented Jul 19, 2023

Rewrites the resource_missing_tags rule to explicitly deal with slices of keys instead of maps. In doing so, it acknowledges that keys may be unknown, which should fix #516 (in theory). When unknown keys are found we should return early and skip checking that resource. This PR also refactors this cty.Value to []string logic out as it was previously duplicated in two locations.

While a map would be more efficient and map[string]struct{} approximates a set in Go, the sizes are small enough that the clearer code of slices makes more sense. There was a bunch of wasted effort (and bugs) in dealing with tag values when ultimately we don't care about the tag values at all, just the keys. In cases where we're merging two lists of tag keys there's no need to de-duplicate as ultimately we are just looking at whether the slice contains our tag of interest.

It's not actually possible to test this currently as it's impossible to produce an unknown map value within what's allowed by the test runner, as far as I can tell.

This won't work:

resource "aws_s3_bucket" "b" {
  name = "b"
  tags = {
    (var.tag) = "t"
  }
}

Errors with:

Null value as key; Can't use a null value as a key.

@bendrucker bendrucker requested a review from wata727 July 19, 2023 04:22
rules/aws_resource_missing_tags.go Outdated Show resolved Hide resolved
return true
}

keys = append(keys, key.AsString())
Copy link
Member

@wata727 wata727 Jul 19, 2023

Choose a reason for hiding this comment

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

AsString causes a panic under the following conditions:

  • Marked as sensitive
  • Not a string
  • NULL

https://github.com/zclconf/go-cty/blob/v1.13.2/cty/value_ops.go#L1385-L1391

As for sensitive values, we might Unmark them here (probably very few cases where tag keys are really sensitive). In other cases, it's probably invalid in Terraform, and like unknown values, it's good to skip inspection of the tag.

Copy link
Member Author

Choose a reason for hiding this comment

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

Terraform should reject a map with a null key, but I've handled that now anyway. I ended up ignoring marked keys too. They can easily be unmarked and inspected but if the user declared them sensitive it seems reasonable to treat them as a black box (unknown).

@bendrucker bendrucker requested a review from wata727 July 20, 2023 00:17
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

Nice!

@wata727 wata727 merged commit 5406ce5 into master Jul 22, 2023
8 checks passed
@wata727 wata727 deleted the tags-keys branch July 22, 2023 09:28
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.

resource_missing_tags panic: value is unknown
2 participants