Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Implement expiry for access tokens#59565

Merged
chwarwick merged 24 commits into
mainfrom
cw/access-token-expire
Jan 18, 2024
Merged

Implement expiry for access tokens#59565
chwarwick merged 24 commits into
mainfrom
cw/access-token-expire

Conversation

@chwarwick

@chwarwick chwarwick commented Jan 13, 2024

Copy link
Copy Markdown
Contributor

Add expiry to access tokens. Users can now select a maximum timespan for which a token is valid. Tokens will automatically lose access after this period.

By default all new tokens will require expiration; however there is a site configuration setting to allow users to create tokens without expiration if necessary. The preconfigured options when creating a token are 7, 14, 30, 60, or 90 days with the selection defaulting to 90 days.

Tokens that are generated in the background for example from the Cody VS Code extension will use the default expiration period for the instance.

Defaults
Screenshot 2024-01-15 at 2 55 18 PM

Options when allowing no expiration
Screenshot 2024-01-15 at 2 54 53 PM

Allow no expiration but show warning
Screenshot 2024-01-15 at 2 54 38 PM

Created from VS Code shows using the default
Screenshot 2024-01-15 at 2 56 17 PM

closes https://github.com/sourcegraph/sourcegraph/issues/59545

Test plan

  • Updated existing and added new tests for token lookup and creation
  • Update integration tests
  • Manual testing of configuration options and creating from clients.

@cla-bot cla-bot Bot added the cla-signed label Jan 13, 2024
@chwarwick chwarwick marked this pull request as ready for review January 15, 2024 20:28
@chwarwick chwarwick requested review from a team and eseliger January 15, 2024 20:40
@chwarwick chwarwick force-pushed the cw/access-token-expire branch from 0d9fcfe to 5f16566 Compare January 15, 2024 21:08
Comment thread CHANGELOG.md Outdated
@emidoots

Copy link
Copy Markdown
Member

Have you checked what happens if a token is expired in the VS Code client? e.g. do you get an obscure error or is it clear about what you need to do to get the Cody client working again?

@emidoots emidoots left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks correct & well-tested to me, great work

@chwarwick chwarwick force-pushed the cw/access-token-expire branch from 5a72e46 to 273451f Compare January 16, 2024 15:36
@chwarwick

Copy link
Copy Markdown
Contributor Author

Have you checked what happens if a token is expired in the VS Code client? e.g. do you get an obscure error or is it clear about what you need to do to get the Cody client working again?

VS Code isn't too bad, JetBrains you need to look a little deeper, I opened issues for it:
https://github.com/sourcegraph/cody/issues/2788
sourcegraph/jetbrains#311

@chwarwick chwarwick requested a review from vdavid January 17, 2024 21:29
@chwarwick chwarwick requested a review from unknwon January 17, 2024 21:29
@chwarwick

Copy link
Copy Markdown
Contributor Author

@unknwon @vdavid tagging you here because I don't want to introduce any new complications in the SAMS/SSC PLG workflows.

@unknwon unknwon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread client/web/dev/utils/create-js-context.ts Outdated
Comment on lines +307 to +310
This is a one-time access token to connect your account to {requester.name}. This
token will expire in {defaultAccessTokenExpiryDays}{' '}
{pluralize('day', defaultAccessTokenExpiryDays)}. You will not be able to see this
token again once the window is closed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What will this message say, when a user created a token without expiry?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@unknwon This is the callback token generation page that is used by the extensions, the user doesn't have the ability to pick an expiration here. The token in generated without their input, it will use whatever the default is and currently I didn't provide a way to default to no expiration, since enforcing an expiration for this pathway is the primary purpose of this pr.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ahh, I see, NVM then!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does one-time access token mean in this context? I'd expect it to work for only one request

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's the 2nd half of this page, when you expand to see the token. That was the original copy but that isn't accurate so I'll remove the one-time.
image

Comment thread cmd/frontend/graphqlbackend/access_tokens.go Outdated
creator_user_id | integer | | not null |
scopes | text[] | | not null |
internal | boolean | | | false
expires_at | timestamp with time zone | | |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe worth checking whether we need an update to the access_tokens_lookup index
(line 41)?

Comment thread schema/site.schema.json Outdated
Comment thread cmd/frontend/graphqlbackend/access_tokens.go Outdated
Comment on lines +307 to +310
This is a one-time access token to connect your account to {requester.name}. This
token will expire in {defaultAccessTokenExpiryDays}{' '}
{pluralize('day', defaultAccessTokenExpiryDays)}. You will not be able to see this
token again once the window is closed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does one-time access token mean in this context? I'd expect it to work for only one request

@vdavid

vdavid commented Jan 18, 2024

Copy link
Copy Markdown
Contributor

Thanks for tagging me, @chwarwick!
Cody PLG and Cody Web use internal access tokens to talk to Cody Gateway.
If I understand this and this correctly, internal access tokens are created with NULL expiration, which means they never expire, which makes our case easy. @chwarwick, please confirm that my understanding is correct.
I can't think of other ways this affects PLG specifically.

@sourcegraph/cody-plg, do you think introducing token expirations affects us in any other way?

@chwarwick

Copy link
Copy Markdown
Contributor Author

Thanks for tagging me, @chwarwick! Cody PLG and Cody Web use internal access tokens to talk to Cody Gateway. If I understand this and this correctly, internal access tokens are created with NULL expiration, which means they never expire, which makes our case easy. @chwarwick, please confirm that my understanding is correct. I can't think of other ways this affects PLG specifically.

@sourcegraph/cody-plg, do you think introducing token expirations affects us in any other way?

@vdavid Correct the internal tokens will continue to have no expiration.

@chwarwick

Copy link
Copy Markdown
Contributor Author

@unknwon @eseliger @slimsag I updated based on the feedback, changes were a bit more extensive since I changed to mutation to accept a duration instead of an expiration time. I plan to merge later today if you want a 2nd look.

Comment thread cmd/frontend/graphqlbackend/access_tokens.go Outdated
@chwarwick chwarwick merged commit b909979 into main Jan 18, 2024
@chwarwick chwarwick deleted the cw/access-token-expire branch January 18, 2024 23:54
@daxmc99

daxmc99 commented Feb 5, 2024

Copy link
Copy Markdown
Contributor

@chwarwick For cloud, we are getting createAccessToken Access token creation requires a valid expiration

Should we set allowNoExpiration? We will need to set this on Cloud instances and today we do not have a mechanism to perform rotation.

Can we set allowNoExpiration for just some tokens?

@chwarwick

Copy link
Copy Markdown
Contributor Author

@chwarwick For cloud, we are getting createAccessToken Access token creation requires a valid expiration

Should we set allowNoExpiration? We will need to set this on Cloud instances and today we do not have a mechanism to perform rotation.

Can we set allowNoExpiration for just some tokens?

@daxmc99 What is the purpose of these tokens, happy to talk live to find a solution.

@alexAtSourcegraph

Copy link
Copy Markdown
Contributor

@chwarwick What would happen to existing access tokens configured prior to this change once this site configuration is set?

@chwarwick

Copy link
Copy Markdown
Contributor Author

@chwarwick What would happen to existing access tokens configured prior to this change once this site configuration is set?

@alexAtSourcegraph They do not expire, this only effects new tokens created.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P0: Expiry for a token

7 participants