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

Opportunistic token refresh (for BitBucket Server) #19715

Merged
merged 3 commits into from May 13, 2024

Conversation

geropl
Copy link
Member

@geropl geropl commented May 8, 2024

Description

This aims to introduce a new feature for token refreshes with SCMs dubbed "opportunistic refresh". If the feature flag is enabled, AND the SCM requires it (for now only BitBucket Server!), we ask for a fresh token whenever we "safely" can.

This "safely" is determined by us storing a new reservedUntilDate with each SCM token, with is set/updated whenever we re-use a token. This way, whenever we do a lookup on a SCM token, we know whether we can safely refresh it or not.

Operational controls:

  • introduces the workspace_start_scm_access_token_lifetime feature flag which controls the requested lifetime of a SCM token
    • the default for SCM tokens is 5mins
    • the default for SCM tokens on workspace start is 10mins
    • we can override the later with this feature flag
  • introduces the opportunistic_token_refresh feature flag which controls whether "opportunistic refresh" is enabled at all
  • updates the gitpod_scm_token_refresh_requests_total metric with a new label opportunisticRefresh ("true" | "false" | "reserved")

Related Issue(s)

Fixes ENT-4

How to test

  • check the unit tests here
  • manual testing:
    • observe kubectl logs server-5d5cdb8db5-tj9bl -f | grep "Token refreshed" and noticed how values change
    • join my org, and open a repo from our BitBucket instance

Documentation

Preview status

gitpod:summary

Build Options

Build
  • /werft with-werft
    Run the build with werft instead of GHA
  • leeway-no-cache
  • /werft no-test
    Run Leeway with --dont-test
Publish
  • /werft publish-to-npm
  • /werft publish-to-jb-marketplace
Installer
  • analytics=segment
  • with-dedicated-emulation
  • workspace-feature-flags
    Add desired feature flags to the end of the line above, space separated
Preview Environment / Integration Tests
  • /werft with-local-preview
    If enabled this will build install/preview
  • /werft with-preview
  • /werft with-large-vm
  • /werft with-gce-vm
    If enabled this will create the environment on GCE infra
  • /werft preemptible
    Saves cost. Untick this only if you're really sure you need a non-preemtible machine.
  • with-integration-tests=all
    Valid options are all, workspace, webapp, ide, jetbrains, vscode, ssh. If enabled, with-preview and with-large-vm will be enabled.
  • with-monitoring

/hold

@@ -1852,8 +1854,8 @@ export class WorkspaceStarter {
targetMode = CloneTargetMode.REMOTE_HEAD;
}

const tokenValidityThreshold = (await isLongAccessTokenValidityThresholdEnabled(user)) ? 50 : 30;
Copy link
Member Author

Choose a reason for hiding this comment

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

The default for "start workspace" was 30 minutes before. We are reducing to 10 here, but can adjust any time with the new feature flag.

Copy link
Member

@svenefftinge svenefftinge left a comment

Choose a reason for hiding this comment

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

The code looks a bit complicated but AFAICT it would do what we want 👍

components/server/src/workspace/workspace-starter.ts Outdated Show resolved Hide resolved
components/server/src/workspace/workspace-starter.ts Outdated Show resolved Hide resolved
components/server/src/user/token-service.ts Show resolved Hide resolved

@injectable()
export class TokenService implements TokenProvider {
static readonly GITPOD_AUTH_PROVIDER_ID = "Gitpod";
static readonly DEFAULT_EXPIRY_THRESHOLD = 5;
/** [mins] */
static readonly DEFAULT_LIFETIME = 5;
Copy link
Member

Choose a reason for hiding this comment

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

Since we explicitly pass in the long value in workspace start situation, I think the default should be small. OTOH making this rather small would trigger refreshes more often, which doesn't seem good. Maybe worth writing a comment about these implications for our future selves?

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively we could make this rather small, but only start opportunistic refreshes when expiry is in e.g 30m

Copy link
Member Author

Choose a reason for hiding this comment

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

OTOH making this rather small would trigger refreshes more often, which doesn't seem good.

This is only true if we don't guess the estimated lifetime correctly, no? And only use SCM tokens transitively, except for the Workspaces case where we use a custom lifetime.

Alternatively we could make this rather small, but only start opportunistic refreshes when expiry is in e.g 30m

Not sure I understand: Wouldn't that remove all the benefits we are getting from this approach in the first place? 🤷
I get that we have the additional restriction, but I can't imagine this being worth it.

I'd say let's try this out in practice, and if we can tune the parameters to make it work for BBS.

@geropl
Copy link
Member Author

geropl commented May 8, 2024

We'll wait until Monday with merging.

@geropl
Copy link
Member Author

geropl commented May 13, 2024

/unhold

@roboquat roboquat merged commit 435fa5e into main May 13, 2024
15 checks passed
@roboquat roboquat deleted the gpl/bbs-token-refresh-2 branch May 13, 2024 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants