Make OAuth access tokens routable
What does this MR do and why?
Generates OAuth access tokens in the routable token format, behind the routable_oauth_access_token flag. The HTTP Router can then route requests made with them to the right cell, both Authorization: Bearer <token> and oauth2:<token> Basic auth (docker login, Git over HTTP).
- Doorkeeper's
access_token_generatornow points toGitlab::DoorkeeperSecretStoring::Token::RoutableAccessToken. The payload carriesc(cell),o(organization) andu(resource owner). - The flag is checked against the resource owner. When it's off, tokens fall back to
Doorkeeper::OAuth::Helpers::UniqueToken(64 hex characters). - Refresh tokens keep the hex format. Doorkeeper hard-codes
UniqueTokenfor them. Doorkeeper::Concerns::LoadBalancerStickingonly stuck 64-hex-character secrets to a caught-up replica. It now covers routable access tokens too, so a new token used straight away on/jwt/author Git HTTP doesn't 401 because of replica lag.- There's no database change.
oauth_access_tokens.tokenstores the SHA-512 digest, so the token's length doesn't matter.
Notes for reviewers
- Entropy: routable tokens carry 16 random bytes (128 bits). Hex tokens carry 32 bytes (256 bits). Routable PATs and deploy tokens use the same format.
- Flag actor: the flag uses the user as its actor, because ChatOps and the Features API can't target an OAuth application. Client credentials tokens have no resource owner, so they stay hex until the flag is fully on. The flag is per cell, like any other.
- Flag group: the flag is set to
group::cells infrastructure, because this is Cells routing work. The authentication principles suggestgroup::authenticationfor auth flags. I'm happy to change it if Authentication prefers. - Out of scope: the router doesn't route
POST /oauth/token(no path rule, and no session cookie for API clients) or the?access_token=query parameter. Both are follow-ups for http-router.
References
- Closes #636298 (closed)
- Cells issue: gitlab-com/gl-infra/tenant-scale/cells-infrastructure/team#965
- Parent: gitlab-com/gl-infra/tenant-scale/cells-infrastructure/team#917
- The same change for deploy tokens: !262950 (merged)
Screenshots or screen recordings
Not applicable. This change has no UI.
How to set up and validate locally
-
In
gdk rails console, enable the flag and create a token:Feature.enable(:routable_oauth_access_token) user = User.find_by_username('root') token = Doorkeeper::AccessToken.create!( application: Authn::OauthApplication.first, resource_owner_id: user.id, organization_id: user.organizations.first.id, scopes: 'api' ) plaintext = token.plaintext_token # => "<base64>.01.<len><crc>" Authn::TokenField::Decoders::V1::RoutablePayload.new(plaintext).decode # => {"c"=>1, "o"=><organization ID>, "u"=><user ID>} -
Check that the token works as a Bearer token and as a password:
curl -i -H "Authorization: Bearer <token>" 'http://127.0.0.1:3000/api/v4/user' git clone "http://oauth2:<token>@127.0.0.1:3000/<group>/<project>.git" -
Optional, with the GDK HTTP Router enabled: run
gdk tail gitlab-http-router, then send thecurlfrom step 2. The router log showsruleApplied: session_token_header_authorization_pat. Withcurl -u 'oauth2:<token>'on/jwt/auth?service=container_registry, it showssession_token_header_authorization_basic_routable_token. -
Run
Feature.disable(:routable_oauth_access_token)and create another token.plaintext_tokenis back to 64 hex characters.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.