Use dedicated ES256 auth for the Org Mover internal API

What does this MR do and why?

The internal Org Mover API authenticated with the shared gitlab-shell secret. Anything holding that secret (Gitaly, SSH hosts, Zoekt, Geo secondaries) could put an organization into maintenance, and an Org Mover token also worked on every other Shell-backed internal endpoint.

This switches the API to its own credential, following the AppSec recommendation:

  • Requests carry an ES256 JWT. Only the control plane holds the private key. Cells only get public keys, through the new org_mover.public_key_files setting, so a compromised cell can't mint tokens. Several keys can be trusted at once for rotation.
  • Each token is addressed to a single cell. A cell only accepts a token whose aud is exactly its own, so a compromised cell can't replay a token it received against another cell. All tokens are rejected when cells are disabled.
  • The gitlab-shell token no longer works on these endpoints.

Token contract for the control plane

Field Value
Header Gitlab-Org-Mover-Api-Request
Algorithm ES256, with kid set to the RFC 7638 thumbprint of the signing key
iss gitlab-org-mover
aud A single string, gitlab-org-mover-api:cell-<cell id>
iat Current time. Tokens older than 5 minutes, or more than 30 seconds in the future, are rejected
Size At most 2 KB, as a compact JWS

The endpoints are still behind the default-off org_mover_maintenance_api flag, so there's no changelog entry.

Follow-ups

References

How to set up and validate locally

  1. Save this as tmp/org_mover_validate.rb. It generates a key pair, points the settings at the public key for this process only, and mints tokens for different cells:

    key = OpenSSL::PKey::EC.generate('prime256v1')
    File.write('tmp/org_mover_public_key.pem', key.public_to_pem)
    
    Gitlab.config.cell['enabled'] = true
    Gitlab.config.cell['id'] = 2
    Gitlab.config.org_mover['public_key_files'] = ['tmp/org_mover_public_key.pem']
    
    mint = ->(aud) do
      payload = { iss: 'gitlab-org-mover', aud: aud, iat: Time.current.to_i }
      { 'Gitlab-Org-Mover-Api-Request' => JWT.encode(payload, key, 'ES256', { kid: JWT::JWK.new(key, kid_generator: JWT::JWK::Thumbprint).kid }) }
    end
    
    verify = ->(headers) { Gitlab::OrgMover::Authenticator.verify_api_request(headers).present? }
    
    p this_cell: verify.(mint.('gitlab-org-mover-api:cell-2'))
    p other_cell: verify.(mint.('gitlab-org-mover-api:cell-3'))
    p several_cells: verify.(mint.(%w[gitlab-org-mover-api:cell-2 gitlab-org-mover-api:cell-3]))
  2. Run it:

    bundle exec rails runner tmp/org_mover_validate.rb
  3. Only the token for this cell is accepted:

    {:this_cell=>true}
    {:other_cell=>false}
    {:several_cells=>false}

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.

Related to #627692 (closed)

Edited by Michael Kozono

Merge request reports

Loading
Loading