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

Validate write-only attributes returned from providers during the UpgradeResourceState RPC #36305

Merged
merged 8 commits into from
Jan 14, 2025

Conversation

SarahFrench
Copy link
Member

@SarahFrench SarahFrench commented Jan 10, 2025

This PR add validation to ensure providers don't return values for write-only attributes during UpgradeResourceState. Terraform core doesn't know if a provider has been updated since the last time the state was updated, and this RPC allows providers to upgrade state to match any schema changes for a particular resource.

The relevant changes for WO attributes are:

  1. A field has been changed to become write-only
  2. A field has changed to stop being write-only

In case number 2 there is no concern. The state will contain null from when the field was write-only, and then the provider may return a new non-null value on state upgrade.

For case number 1 we don't want a value to be returned to TF core. That is what this PR's changes protects against.

Target Release

1.11.x

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

This change is for a feature that is not yet released; no changelog needed

@SarahFrench SarahFrench marked this pull request as ready for review January 10, 2025 18:25
@SarahFrench SarahFrench requested a review from a team as a code owner January 10, 2025 18:25
Copy link
Member

@liamcervante liamcervante left a comment

Choose a reason for hiding this comment

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

Just to make sure I'm not missing something - Terraform should already be storing write-only attributes as null in the state file right? I agree validating that the provider isn't doing something silly is a good idea, but the changes to the mock provider might not be necessary (or even detrimental).

The mock provider returning only the existing state, combined with the check that this isn't allowed, should actually verify that Terraform isn't ever writing real values for write-only attributes. If the mock provider ever fails in a test because it returned a non-null write-only attribute, then we know that there's a bug elsewhere that wrote a non-null write-only attribute in the first place (I think).

.changes/unreleased/ENHANCEMENTS-20250110-134832.yaml Outdated Show resolved Hide resolved
@radeksimko
Copy link
Member

The mock provider returning only the existing state, combined with the check that this isn't allowed, should actually verify that Terraform isn't ever writing real values for write-only attributes. If the mock provider ever fails in a test because it returned a non-null write-only attribute, then we know that there's a bug elsewhere that wrote a non-null write-only attribute in the first place (I think).

Just to clarify - are you implying that the mock provider should behave the same way as SDK/framework, i.e. always omit write-only attribute values, so then the mocking of the various provider methods wouldn't be necessary as part of each test?

We considered that but initially felt like it would make the tests a bit too smart but I suppose it's fine.

@liamcervante
Copy link
Member

liamcervante commented Jan 13, 2025

Sorry! I misread the PR before!

I don't think I was trying to be so complicated - my understanding is that the state should never be written with any write-only attributes set. So, I'd imagine that both Terraform and the provider SDK/framework validate this (or the provider framework removes the attributes and Terraform validates it).

I had thought the changes on line 641-650 were making it so that for all write-only tests the mock provider was always stripping out the write-only attributes, and I was concerned this might conceal another bug elsewhere if Terraform was for example writing write-only attributes into state when it shouldn't have been. I have read it properly now and I see that it's only been set for exactly one test and not all tests, so that seems fine to me.

(side note, I wish it wouldn't take away the review from everyone else when I only had a question. I'll leave this for someone with more context to actually approve)

@liamcervante liamcervante requested a review from a team January 13, 2025 13:30
@radeksimko
Copy link
Member

I had thought the changes on line 641-650 were making it so that for all write-only tests the mock provider was always stripping out the write-only attributes, and I was concerned this might conceal another bug elsewhere if Terraform was for example writing write-only attributes into state when it shouldn't have been. I have read it properly now and I see that it's only been set for exactly one test and not all tests, so that seems fine to me.

Yep, exactly - as mentioned - we considered what you probably thought we did here (i.e. make the MockProvider always behave like in the modified test) - but decided against it for the reasons mentioned. 😄

@SarahFrench
Copy link
Member Author

Thanks @DanielMSchmidt!

@SarahFrench SarahFrench merged commit e5d4a51 into main Jan 14, 2025
11 checks passed
@SarahFrench SarahFrench deleted the validate-non-null-wo-attr-state-upgrade branch January 14, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants