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

Feat: keychain rotate passphrase #944

Merged
merged 23 commits into from
May 27, 2021
Merged

Feat: keychain rotate passphrase #944

merged 23 commits into from
May 27, 2021

Conversation

zeim839
Copy link
Contributor

@zeim839 zeim839 commented May 14, 2021

This is a rough implementation supporting rotating keychain passphrases. I need some suggestions on how to proceed. I haven't received a reply so I've copied the original conversation here for reference:

async changeKeychainPassword(oldPassword, newPassword){
    if (typeof oldPassword !== 'string' || typeof newPassword !== 'string') {
      throw new Error(`Invalid pass type '${typeof oldPassword}'`);
    }
    if (newPassword.length < 20) {
      throw new Error('pass must be least 20 characters')
    }
    // TODO: Need to decide on how to import/generate opts
    const newDek = newPassword
      ? crypto.pbkdf2(
        newPassword,
        this.opts.dek.salt,
        this.opts.dek.iterationCount,
        this.opts.dek.keyLength,
        this.opts.dek.hash)
      : ''
    const oldDek = privates.get(this).dek
    privates.set(this, { "dek":newDek })
    var keys = await this.listKeys()
    await keys.forEach(async key =>{
      // TODO: Decide how to handle deleting and importing the "self" key. 
          // importKey and removeKey throw an error when handling "self"
      if (key.name != "self"){
        var keyAsPEM = await this._getPrivateKey(key.name)
        await this.removeKey(key.name)
        this.importKey(key.name, keyAsPEM, oldDek)
      }
    })
  }

src/keychain/index.js

What's left is to decide how to handle the "self" key (this.importKey() and this.removeKey() throw an error on "self") and how to handle changing passwords in the context of src/index.js (see here). I'm trying to be cautious/minimal with the changes I make so I'd appreciate some suggestions on how to proceed.

Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @zeim839

I think I addressed all the comments

src/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
@zeim839
Copy link
Contributor Author

zeim839 commented May 19, 2021

@vasco-santos Please take a look at the new changes and let me know what you think. Regarding tests, I've incorporated testing for validation but I'm not exactly sure how to go about the following:

it('can rotate keychain passphrase', async () => {  
    //TODO...
})

@zeim839
Copy link
Contributor Author

zeim839 commented May 20, 2021

I think I've come up with a possible solution. If its ok with you then everything should be good to go. I also decided to create a new keychain here (instead of using ks) to avoid influencing any other/future tests that dont use the new rotated password.

@zeim839 zeim839 changed the title [WIP] Feat: keychain rotate passphrase Feat: keychain rotate passphrase May 20, 2021
Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

I did an initial iteration on the PR. The new pm does not seem to be created with the new password. I left inline comments on how I think it would look like.

Also, before submitting the PR try to get the CI green please 🙏🏼 I added small inline suggestion to fix the lint, but I probably did not get to all of them. You should run both npm run lint and npm run test

Thanks @zeim839

src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
test/keychain/keychain.spec.js Show resolved Hide resolved
@zeim839 zeim839 marked this pull request as draft May 21, 2021 20:40
@zeim839
Copy link
Contributor Author

zeim839 commented May 22, 2021

Also, before submitting the PR try to get the CI green please 🙏🏼 I added small inline suggestion to fix the lint, but I probably did not get to all of them. You should run both npm run lint and npm run test

Sorry about that, I shouldve read the contrib guidelines more carefully. Anyway, I've ran lint and fixed any issues that I could trace back to me but I'm not sure If I can even get the CI to green because linting fails regardless of my edits. I've fixed the mistakes from the previous review and wrote the test as described.

However, I keep getting "Error: Cannot read the key, most likely the password is wrong or not a RSA key" here when testing on browser, despite it working fine in test:node. Am I missing/overlooking something?

@zeim839 zeim839 requested a review from vasco-santos May 22, 2021 10:03
test/keychain/keychain.spec.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
@vasco-santos
Copy link
Member

Sorry about that, I shouldve read the contrib guidelines more carefully. Anyway, I've ran lint and fixed any issues that I could trace back to me but I'm not sure If I can even get the CI to green because linting fails regardless of my edits. I've fixed the mistakes from the previous review and wrote the test as described.

No problem, thanks for looking into it. I already submitted a suggestion fixing the lint. I will try to replicate the browser issue and get back to you

Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Thanks for the new iteration. This is mostly done, I tried the changes I suggested below and I got everything to work.

Moreover, I added a lint skip for the error you had, as the linter was complaining for the ternary condition in the constant in the test, which is fine. When you add the suggestion, please check if the linting is still fine, given that the suggestion spaces could break the spacing.

Also, you will need to pull the changes done in the commit that fixed the linting
when you add the suggestions

src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
test/keychain/keychain.spec.js Outdated Show resolved Hide resolved
test/keychain/keychain.spec.js Outdated Show resolved Hide resolved
test/keychain/keychain.spec.js Outdated Show resolved Hide resolved
zeim839 and others added 3 commits May 26, 2021 11:36
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
@zeim839 zeim839 marked this pull request as ready for review May 26, 2021 08:42
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
src/keychain/index.js Outdated Show resolved Hide resolved
@zeim839
Copy link
Contributor Author

zeim839 commented May 26, 2021

@vasco-santos Thanks for the help, I wasn't aware of that. I've removed the redundancy and everything works as expected now.

Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

LGTM!
Thanks for landing this @zeim839

@vasco-santos vasco-santos merged commit 478963a into libp2p:master May 27, 2021
@vasco-santos
Copy link
Member

Released in 0.31.6

@zeim839
Copy link
Contributor Author

zeim839 commented May 29, 2021

@vasco-santos Thanks for all the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants