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

Keymanager fixes for bad file writes #12284

Merged
merged 26 commits into from
Apr 17, 2023
Merged

Keymanager fixes for bad file writes #12284

merged 26 commits into from
Apr 17, 2023

Conversation

james-prysm
Copy link
Contributor

@james-prysm james-prysm commented Apr 14, 2023

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

addresses concerns in #12281 by serializing the process and making sure that local data is not updated until file write is done. if the file is disrupted and cannot finish then the process will roll back.
// 1) Copy the in memory keystore
// 2) Delete the keys from copied in memory keystore
// 3) Save the copy to disk
// 4) Reinitialize account store from disk reinitializing with copy instead
// 5) Verify keys are indeed deleted removing this step as the in memory is updated with copy instead of reading from file.
// 6) Return API response

similar steps needed for Import
// 1) Copy the in memory keystore
// 2) Import the keys into copied in memory keystore
// 3) Save the copy to disk
// 4) Reinitialize account store from disk reinitializing with copy instead
// 5) Verify keys are indeed Imported removing this step as the in memory is updated with copy instead of reading from file.
// 6) Return Statuses

Which issues(s) does this PR fix?

Fixes #12281

@james-prysm james-prysm marked this pull request as ready for review April 14, 2023 23:14
@james-prysm james-prysm requested a review from a team as a code owner April 14, 2023 23:14
@james-prysm james-prysm requested review from kasey, rkapka and nisdas April 14, 2023 23:14
@james-prysm james-prysm changed the title Keymanager delete fixes for bad file writes Keymanager fixes for bad file writes Apr 14, 2023
@james-prysm james-prysm marked this pull request as draft April 14, 2023 23:24
@james-prysm james-prysm marked this pull request as ready for review April 15, 2023 03:01
@terencechain terencechain added the Priority: High High priority item label Apr 15, 2023
validator/keymanager/local/delete.go Outdated Show resolved Hide resolved
validator/keymanager/local/delete_test.go Outdated Show resolved Hide resolved
rauljordan
rauljordan previously approved these changes Apr 16, 2023
Copy link
Member

@prestonvanloon prestonvanloon left a comment

Choose a reason for hiding this comment

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

A few comments on the commentary, but haven't tested this locally yet

validator/keymanager/local/delete.go Outdated Show resolved Hide resolved
validator/keymanager/local/delete.go Outdated Show resolved Hide resolved
validator/keymanager/local/delete.go Outdated Show resolved Hide resolved
validator/keymanager/local/import.go Outdated Show resolved Hide resolved
validator/keymanager/local/import.go Outdated Show resolved Hide resolved
validator/keymanager/local/import.go Outdated Show resolved Hide resolved
validator/accounts/wallet_create.go Outdated Show resolved Hide resolved
validator/keymanager/local/delete.go Outdated Show resolved Hide resolved
validator/keymanager/local/import.go Outdated Show resolved Hide resolved
}
//
// 6) Return Statuses
return statuses, nil
}

// ImportKeypairs directly into the keymanager.
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be updated. This routine does more than import directly into km

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you suggest breaking this function up?

Copy link
Member

Choose a reason for hiding this comment

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

No, I think it's already split up well. I think the comment could mention how this first attempts to save to disk then updates the keymanager rather than "imports directly into keymanager"

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
james-prysm and others added 2 commits April 16, 2023 20:31
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
james-prysm and others added 7 commits April 16, 2023 20:31
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
… instead update the information based on memory on hand
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
km.accountsStore.PublicKeys = append(km.accountsStore.PublicKeys, pk)
km.accountsStore.PrivateKeys = append(km.accountsStore.PrivateKeys, sk)
}
updateAccountsStoreInMemory(km.accountsStore, privateKeys, publicKeys)
Copy link
Member

Choose a reason for hiding this comment

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

You don't set km.accountsStore after executing this function call. Its better to be explicit of what struct fields are being updated in this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the name to better represent this.

@@ -256,17 +264,70 @@ func (km *Keymanager) initializeAccountKeystore(ctx context.Context) error {
}

// CreateAccountsKeystore creates a new keystore holding the provided keys.
func (km *Keymanager) CreateAccountsKeystore(
func (km *Keymanager) CreateAccountsKeystore(ctx context.Context, privateKeys [][]byte, publicKeys [][]byte) (*AccountsKeystoreRepresentation, error) {
if err := km.CreateOrUpdateInMemoryAccountsStore(ctx, privateKeys, publicKeys); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

if this function succeeds but the one following it fails, will that cause issues ?
CreateOrUpdateInMemoryAccountsStore updates it in memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will take a look again this is only used during the wallet creation process with 0 imports perhaps i can make this cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is used in the create wallet function, if it works but the store doesn't it should fail the write. it's only used in tests otherwise besides that.

if err := km.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encodedAccounts); err != nil {
return nil, err
// 5) Verify keys are indeed Imported
if len(km.accountsStore.PublicKeys) < len(storeCopy.PublicKeys) {
Copy link
Member

Choose a reason for hiding this comment

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

any point of this check now ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe not I can probably remove this... as the in memory already updated

prestonvanloon
prestonvanloon previously approved these changes Apr 17, 2023
Copy link
Member

@prestonvanloon prestonvanloon left a comment

Choose a reason for hiding this comment

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

Tested this locally and confirmed that the key is not deleted from the in memory keystore when the keystore is not successfully written to disk.

LGTM - but please address other comments

prestonvanloon
prestonvanloon previously approved these changes Apr 17, 2023
@prestonvanloon prestonvanloon merged commit 10b438e into develop Apr 17, 2023
@delete-merged-branch delete-merged-branch bot deleted the keymanager-fixes branch April 17, 2023 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: High High priority item
Projects
None yet
Development

Successfully merging this pull request may close these issues.

keystore: Deleting keys via keymanager API may not fully delete keys
5 participants