Skip to content

aws_write_only_arguments: recommend write-only arguments where available #860

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

Conversation

aristosvo
Copy link
Contributor

@aristosvo aristosvo commented Apr 9, 2025

Fixes #859 partially

A review on the direction of these kind of checks would be really appreciated, I would happily implement all the write-only arguments once we have a direction.

@aristosvo aristosvo force-pushed the feat/warn-against-non-ephemeral-secret-attributes branch 3 times, most recently from b441ecd to 5420040 Compare April 9, 2025 11:16
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.

I would suggest tying this up into one meta-rule for recommending ephemeral resources and write only. Or two. Versus doing this on a per-resource basis.

Then you can just drive it off configuration/data and quickly handle all of them without all boilerplate. E.g. for write only attributes, a map of resource to a list of attributes with write only alternatives:

type writeOnlyAttribute struct {
    original    string
    alternative string
}

writeOnlyAttributes := map[string][]writeOnlyAttribute{
    "aws_secretsmanager_secret_version": {
        {
            original:    "secret_string",
            alternative: "secret_string_wo",
        },
    },
}

Additionally, you should use EmitIssueWithFix here so that users can automatically convert their code with --fix. There are no existing autofixes in this repository but you can see an example here:

https://github.com/terraform-linters/tflint-ruleset-terraform/blob/c2e1c2e3337034dc475f77d574de64213384c2b4/rules/terraform_deprecated_index.go#L80

Here's the methods you can call to make replacements:

https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/tflint#Fixer

@aristosvo aristosvo marked this pull request as ready for review April 10, 2025 01:06
@aristosvo
Copy link
Contributor Author

aristosvo commented Apr 10, 2025

Thanks @bendrucker ! Based on your suggestions it is implemented for the write-only attributes. I also included suggestions for non-writeOnly solutions like managed passwords via SecretsManager.

I’ll have to puzzle a bit how to recommend using an ephemeral resource instead of a data source, but I think that would be a different MR.

I didn’t include the automatic version checking (yet), as that raised a lot of extra questions like:

  • if they are on a version lower that 1.11, should I raise this issue at all?
  • if raised, should I recommend them to update, maybe even auto fix that?
  • can I make check opt-in for versions lower than 1.11, and opt-out for versions 1.11 and later?

Sorry for all the questions, but it seems this rule has some unique patterns to it 😅

@bendrucker bendrucker self-requested a review April 10, 2025 18:22
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.

Nice, few more edits, but getting close.

Also, it'll be annoying, but in reviewing the docs:

https://developer.hashicorp.com/terraform/language/resources/ephemeral/write-only

They decided to call these write-only arguments and not attributes, presumably to emphasize that they cannot be referenced like traditional attributes. I know I called them by the wrong name initially/in my example, but we do need to match Terraform's nomenclature.

@aristosvo aristosvo changed the title Validate against secret attributes, recommending write-only attributes Validate against sensitive attributes, recommending write-only arguments Apr 11, 2025
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.

Overall good! Only comment left with concern about autofix.

@aristosvo aristosvo force-pushed the feat/warn-against-non-ephemeral-secret-attributes branch from 37aade3 to 71a2249 Compare April 18, 2025 12:56
@aristosvo aristosvo force-pushed the feat/warn-against-non-ephemeral-secret-attributes branch from 71a2249 to 6780682 Compare April 18, 2025 12:58
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.

Nice work on the auto-fix!

Last thing: this rule isn't an ideal candidate for a generator, at least for the rule code itself. You need a generator when you want to build multiple rules from data. You can't create types in a loop.

But in this case you just have one rule, and could have your generator just write the relevant data for the rule to use (e.g., a JSON file) in its Check method. You'd embed the file. Or, you could generate Go code with a global variable.

Either way, this means you can test your rule for a handful of representative cases rather than having generated tests for every resource.

@aristosvo
Copy link
Contributor Author

aristosvo commented Apr 21, 2025

Nice work on the auto-fix!

Last thing: this rule isn't an ideal candidate for a generator, at least for the rule code itself. You need a generator when you want to build multiple rules from data. You can't create types in a loop.

But in this case you just have one rule, and could have your generator just write the relevant data for the rule to use (e.g., a JSON file) in its Check method. You'd embed the file. Or, you could generate Go code with a global variable.

Either way, this means you can test your rule for a handful of representative cases rather than having generated tests for every resource.

Thanks @bendrucker!

  • I generated a global variable instead of the complete rule. Embedding a JSON file felt a bit off compared to the global variable, although that might be a taste issue 🙃
  • Reduced tests to one type of resource instead of all of them

Last bit might be to arrange the files differently, but I don't know what you'd think of that. For me it is fine, but I can imagine that as a maintainer you have a stricter/more defined/longer term vision on this.

@bendrucker bendrucker self-requested a review April 22, 2025 18:05
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.

Looks good to me. Will merge after @bendrucker's approval.

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.

Nice! Two small comments, neither essential.

@bendrucker
Copy link
Member

Thanks again!

@bendrucker bendrucker changed the title Validate against sensitive attributes, recommending write-only arguments aws_write_only_arguments: recommend write-only arguments where available Apr 24, 2025
@bendrucker bendrucker merged commit ab59f0d into terraform-linters:master Apr 24, 2025
9 checks passed
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.

Warn against secrets in state and promote ephemeral resources and write-only arguments
3 participants