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_generator now points to Gitlab::DoorkeeperSecretStoring::Token::RoutableAccessToken. The payload carries c (cell), o (organization) and u (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 UniqueToken for them.
  • Doorkeeper::Concerns::LoadBalancerSticking only 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/auth or Git HTTP doesn't 401 because of replica lag.
  • There's no database change. oauth_access_tokens.token stores 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 suggest group::authentication for 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

Screenshots or screen recordings

Not applicable. This change has no UI.

How to set up and validate locally

  1. 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>}
  2. 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"
  3. Optional, with the GDK HTTP Router enabled: run gdk tail gitlab-http-router, then send the curl from step 2. The router log shows ruleApplied: session_token_header_authorization_pat. With curl -u 'oauth2:<token>' on /jwt/auth?service=container_registry, it shows session_token_header_authorization_basic_routable_token.

  4. Run Feature.disable(:routable_oauth_access_token) and create another token. plaintext_token is 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.

Merge request reports

Loading
Loading