Mint the per-user Artifact Registry token (S02 per-user token exchange plan: 1/1)

What does this MR do and why?

Makes ArtifactRegistry::TokenExchange#token_for mint a real short-lived JWT for the acting user. It returned nil (a stub) before, so every per-user Artifact Registry management-API call (repository, repositories, create/update/delete_repository, packages, images) failed closed with AuthorizationError before a request was even issued, and the AR UI could not read anything.

The token is minted in process through Authn::TokenExchange::TokenIssuer, not over the public POST /api/v4/token_exchange endpoint. A GraphQL resolver has no credential to call that endpoint with, and Authz::ArtifactRegistry::BaseService sets the in-process precedent. The endpoint's rate limit and gate_token_exchange_endpoint kill switch are deliberately not reproduced: this surface is already gated per organization by the artifact_registry_ui feature flag, and a resolver mints at most one token per request. This reasoning now lives in the code comment.

The change follows the merged plan: the seam takes one argument, token_for(current_user). The issuer scopes no resource, so the client drops the slug it used to pass. (An earlier revision of this MR kept the parameter as _slug, following the still-open follow-up plan MR !2005 (merged); this revision reverts to the merged plan on main, which drops it.)

Behavior at the seam:

  • A non-User principal, or a User with no home organization, yields nil so the client's blank-credential guard still fails closed.
  • A signing-key or crypto failure during minting (for example a missing or undecryptable Cloud Connector key) is caught at the seam, logged, and converted to the client's ConfigurationError, so the resolver renders ServiceUnavailable instead of a 500 across the whole GraphQL request.
  • The mint is memoized per user (strong_memoize_with), one token per request.

Security note

After this lands, a request carrying a valid user token reaches AR's management API, which authenticates it and then applies no role check yet (tracked in ops/artifact-registry#652). So Rails' read_artifact_registry on the GraphQL types is the only access control over the per-user surface. Important nuance: that control is per-organization, but the target namespace is currently the shared literal acme slug for every organization, and read_artifact_registry is bare membership at any access level. This is acceptable for a closed beta gated by artifact_registry_ui (defaults off).

The token names two audiences (gitlab-artifact-registry and gitlab-iam-data-access), which is what AR's verifier expects.

The token carries the acting user's home organization UUID. A user acting on an organization they are a member of but not homed in presents a token naming a different organization. This is a known issuer limitation, pinned by a test so an upstream fix fails loudly, and filed as #626558 (closed). When AR rejects such a request the resolver now logs before rendering the empty connection, so the rejection is greppable rather than a silent empty page.

Changes since the last review

  • Dropped the slug parameter to match the merged plan (token_for(current_user)), updated client.rb, and removed the now-tautological slug test.
  • Added a rescue at the seam converting a signing-key or crypto failure to ConfigurationError (renders ServiceUnavailable, not a 500), pinned by a spec.
  • Log a rejected per-user read before rendering the empty connection in the resolver, pinned by a spec.
  • Reverted the two client-spec default-provider contexts to instance_double(User) so the non-User guard branch keeps client-level coverage, removing the duplicated create(:user) plus stubbed-organization setup.
  • Added a cross-user memoization test (a shared instance must not hand one user's token to another) and a TTL assertion pinning the issuer's five-minute default.
  • Fixed the guard_service_transport! comment (the per-user path now carries a signed JWT over the plaintext in-cluster URL; transport tightening tracked in #625006 (closed)) and documented the request-scoped memo and dropped-controls reasoning on the seam.

References

Screenshots or screen recordings

Before After
N/A, no UI change N/A, no UI change

How to set up and validate locally

  1. On a fresh instance, create a Cloud Connector signing key: bundle exec rake cloud_connector:keys:create.
  2. Start a console: bundle exec rails console.
  3. Paste the script below.
  4. Confirm it prints the claim shape, the fail-closed nils, the ConfigurationError line, and finally OK.
require 'jwt'

user = User.first
seam = ArtifactRegistry::TokenExchange.new

# 1. Happy path: a user with an organization gets a token AR accepts.
token = seam.token_for(user)
payload, header = JWT.decode(token, nil, false)
puts header.slice('typ', 'alg')
puts payload.slice('aud', 'ver', 'sub')
puts payload['gitlab'].slice('origin', 'origin_id', 'local_id', 'identity_kind', 'organization_role')
puts "ttl seconds: #{payload['exp'] - payload['iat']}"
raise 'unexpected aud' unless payload['aud'] == %w[gitlab-artifact-registry gitlab-iam-data-access]
raise 'unexpected ver' unless payload['ver'] == 1

# 2. Fail-closed arms return nil, so the client raises AuthorizationError (no request).
puts "nil principal      -> #{seam.token_for(nil).inspect}"
puts "non-User principal -> #{seam.token_for(Object.new).inspect}"
raise 'nil arm leaked' unless seam.token_for(nil).nil? && seam.token_for(Object.new).nil?

# 3. A signing-key failure surfaces as ConfigurationError, not a 500.
CloudConnector::CachingKeyLoader.define_singleton_method(:private_jwk) { raise 'no key' }
begin
  ArtifactRegistry::TokenExchange.new.token_for(user)
  puts 'key failure: DID NOT RAISE (bad)'
rescue ArtifactRegistry::Client::ConfigurationError => e
  puts "key failure -> ConfigurationError: #{e.message}"
end

puts 'OK'

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Edited by Narendran

Merge request reports

Loading
Loading