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

Add a hash to random_password with UpgradeState #255

Merged

Conversation

bendbennett
Copy link
Contributor

@bendbennett bendbennett commented May 13, 2022

This is evaluating whether the changes proposed in #73 can be combined with a UpgradeState once the provider has been upgraded to use Framework.

Further test coverage is required.

diags.AddError(
"Hash Generation Error",
"While attempting to generate a hash from of the password an error occurred.\n\n"+
"Verify that the state contains a populated 'result' field and retry the operation\n\n"+
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth mentioning the terraform state show command. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment to use terraform state show.

}
}

func importPassword(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to the note on the sdk/v2 version of this, since the new attribute is only handled once on creation, it will also need to be handled during import. It may be desirable to optionally include it in the import ID so folks can have a stable value when doing terraform state rm and terraform import, although that does introduce yet more complexity. Maybe that can be punted on until there is a specific ask.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, could the bcrypt_hash value be just computed during import?

After all what matters about it is not strictly having a specific value, but that it's stable once generated.

I'd leave out the option to import it with the ID only if it comes up again, and hopefully deferred to when "import-from-config" is a thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated importPassword to handle bcrypt_hash during import.

I'm inclined to defer handling of a passed bcrypt_hash during import until/unless it is specifically requested.

}
}

func importPassword(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, could the bcrypt_hash value be just computed during import?

After all what matters about it is not strictly having a specific value, but that it's stable once generated.

I'd leave out the option to import it with the ID only if it comes up again, and hopefully deferred to when "import-from-config" is a thing.

@@ -15,7 +17,17 @@ func (r resourcePasswordType) GetSchema(context.Context) (tfsdk.Schema, diag.Dia
"data handling in the [Terraform documentation](https://www.terraform.io/docs/language/state/sensitive-data.html).\n" +
"\n" +
"This resource *does* use a cryptographic random number generator."
return getStringSchemaV1(true, description), nil

schema := getStringSchemaV1(true, description)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: the name of the function getStringSchemaV1 makes one thing that the schema that comes out of it is V1.

But it's not right? It's V0. V1 is what you are creating now, by adding bcrypt_hash.

IMHO: I'd try to preserve consistency and have 2 functions able to provide the schema, and the version correctly respected in their name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Have refactored.

@github-actions github-actions bot added size/XL and removed size/L labels May 16, 2022
@bendbennett bendbennett marked this pull request as ready for review May 17, 2022 13:30
@bendbennett bendbennett requested a review from a team as a code owner May 17, 2022 13:30
Comment on lines 59 to 61
0: {
StateUpgrader: migratePasswordStateV0toV1,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason to opt for handling RawState in the StateUpgrader versus specifying the version 0 schema here in the PriorSchema field? If you use PriorSchema, you can call req.PriorState.Get() to populate the already defined data type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I went the route of using RawState as req.State is nil in the tfsdk.UpgradeResourceStateRequest and I can't see a PriorState field in req.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I meant, UpgradeResourceStateRequest.State. It get's filled in when ResourceStateUpgrader.PriorSchema is defined. It should make the logic a lot easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification. I've refactored to use StateUpgrader with PriorSchema.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Actually clicking the button 😅


err := bcrypt.CompareHashAndPassword([]byte(actual.BcryptHash.Value), []byte(actual.Result.Value))
if err != nil {
t.Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Might be helpful to add error context here, e.g.

Suggested change
t.Error(err)
t.Errorf("unexpected bcrypt comparison error: %s", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed this before merging but have added into #249

@bendbennett bendbennett merged commit 3ca20b0 into bendbennett/issues-177 May 19, 2022
@bendbennett bendbennett deleted the add_hash_to_random_password_framework branch May 19, 2022 15:46
@bendbennett bendbennett added this to the v4.0.0 milestone May 19, 2022
@bendbennett bendbennett modified the milestones: v4.0.0-rc.1, v3.2.0 Jun 24, 2022
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants