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(auth): Add key previews to personal API keys #22362

Merged
merged 8 commits into from
May 21, 2024

Conversation

mishushakov
Copy link
Contributor

Problem

Fixes issue #22360

Changes

  • Changes PersonalAPIKey data model
  • Adds function to mask keys
  • Returns masked value in API
  • Displays the masked value in the frontend Settings > Personal API keys:
Screenshot 2024-05-20 at 17 19 32

Does this work well for both Cloud and self-hosted?

Works on self-hosted.

How did you test this code?

Ran in codespaces on localhost.

@mishushakov
Copy link
Contributor Author

The mask was inspired by OpenAI API keys page, let me know if you want this changed

Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

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

I like the idea but we definitely don't want a migration and a separate field for it. The key is available - we should just read it and do the masking at read time instead.

Also then we can just use the existing field

@Twixes
Copy link
Collaborator

Twixes commented May 20, 2024

Ah @benjackwhite This is not is clear in the code, but the PersonalAPIKey.value field is not used. We no longer store non-hashed values of keys, to prevent a leak of the table's contents from granting any malicious actor access to user accounts. So we do need a migration to facilitate this

Copy link
Collaborator

@Twixes Twixes 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 suggestions, but in principle sounds good to me!
It's effectively a sacrifice of ~24 bits of entropy, out the key's 256 bits total, so we could also increase the key's random part to 280 bits. Let's do that in the generate_random_token_personal() function, by changing generate_random_token() to generate_random_token(35)

posthog/models/personal_api_key.py Outdated Show resolved Hide resolved
frontend/src/scenes/settings/user/PersonalAPIKeys.tsx Outdated Show resolved Hide resolved
@mishushakov mishushakov requested a review from Twixes May 20, 2024 15:39
@Twixes Twixes changed the title Adds a way to preview API Key without exposing the value feat(auth): Add key previews to personal API keys May 20, 2024
Copy link
Collaborator

@Twixes Twixes left a comment

Choose a reason for hiding this comment

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

Made a few small tweaks – the PR looks good to me! Will wait for a second opinion from @benjackwhite though

@Twixes Twixes requested a review from benjackwhite May 20, 2024 18:46
Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

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

Ah yeah - shouldn't have been trying to review this on the go 😅 Yeah we need the extra field indeed and overall this looks good.

Only remaining issue will be the double migration. I think our CI requires a max of one migration per PR so needs to be combined into one

@mishushakov
Copy link
Contributor Author

Hey @benjackwhite, I have updated the migration code 😄

Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

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

Amazing! Great addition!

@benjackwhite
Copy link
Contributor

benjackwhite commented May 21, 2024

I could imagine some of the tests failing as they check the returned payload so that might be the final thing to get this over the line https://github.com/PostHog/posthog/blob/master/posthog/api/test/test_personal_api_keys.py#L27

@mishushakov
Copy link
Contributor Author

Have updated the tests as you pointed out, just in case 👍

@Twixes Twixes merged commit b11ebf5 into PostHog:master May 21, 2024
77 of 81 checks passed
timgl pushed a commit that referenced this pull request May 24, 2024
* added a way to preview the key value without exposing the key

* updated api keys preview

* Make the UI a bit nicer

* Write out the reasoning behind personal API key entropy

* Include underscore in masked value

* updated migrations for API key mask

* updated personal api keys tests

---------

Co-authored-by: Michael Matloka <michal@matloka.com>
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.

None yet

3 participants