glab artifact-registry: present the CI/CD ID token at the token exchange so published artifacts carry build attribution
## What
In a CI/CD job, `glab artifact-registry get-token` and `glab artifact-registry login` exchange the job's `CI_JOB_TOKEN` at `POST /api/v4/token_exchange`. Artifacts published with the resulting token carry no build attribution.
When the job has a CI/CD ID token for the token exchange, glab should present that token instead. The minted token then carries the job's project, commit, pipeline and job in its `gitlab.job` claim.
The CI/CD template already exchanges the token and exports the result as `ARTIFACT_CENTRAL_TOKEN`. When that variable is set, glab uses it as is.
This moves the glab part of https://gitlab.com/gitlab-org/gitlab/-/work_items/633756. That item also covered:
- the CI/CD template, now https://gitlab.com/gitlab-org/gitlab/-/work_items/633755
- its user docs, now https://gitlab.com/gitlab-org/ops/artifact-registry/-/work_items/2136
## Server contract
The server side is merged, behind the `token_exchange_ci_id_token` feature flag (https://gitlab.com/gitlab-org/gitlab/-/work_items/633754).
- **Request.** Send the ID token as `Authorization: Bearer <id_token>` to `POST /api/v4/token_exchange`. Success is `201`.
- **Audience.** The job's `id_tokens:` block sets the ID token's `aud` to `$CI_SERVER_URL/api/v4/token_exchange` and its `target_audience` to `gitlab-artifact-registry`. glab already sends `audience=gitlab-artifact-registry`; when both are present, they must match.
- **Age.** The exchange refuses an ID token issued more than 15 minutes ago with `401`. The ID token itself stays valid until the job timeout; the 15 minutes apply only to the exchange. The ID token is issued at job pickup, so image pull, clone and cache restore count against the 15 minutes. The minted token's lifetime is separate: `expires_in`, capped at 12 hours and at the ID token's `exp`.
- **Refusals.** Every refusal of the ID token itself (flag off, too old, bad signature, composite identity, and so on) is a bare `401 Unauthorized`, with no reason. Other statuses (`400`, `403`, `404`, `429`) come from checks the job-token exchange also runs.
- **Multiple organizations.** A user in more than one Artifact Central organization gets `409` (https://gitlab.com/gitlab-org/gitlab/-/work_items/636235). The job-token exchange gets the same `409`.
- **Attribution in the response.** The `201` body has no field for it. It is visible only as the `gitlab.job` claim of the minted token.
- **Flag rollout.** The rollout (https://gitlab.com/gitlab-org/gitlab/-/work_items/638630) requires glab to fall back to `CI_JOB_TOKEN` when the ID-token exchange is refused. Until the flag is on, every ID-token exchange gets `401`.
## What the CI/CD template does
The template `Artifact-Central/Login.gitlab-ci.yml` (https://gitlab.com/gitlab-org/gitlab/-/merge_requests/273005) does the same exchange with `curl`:
- It requests `ARTIFACT_CENTRAL_ID_TOKEN` with the `aud` and `target_audience` above.
- It exchanges the token in `before_script`, with `expires_in` set to `CI_JOB_TIMEOUT`, capped at 12 hours.
- It trusts `CI_SERVER_TLS_CA_FILE`, and retries with the default trust store on a certificate error.
- On any failure except `409`, it warns and falls back to `CI_JOB_TOKEN`. If `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION` is `"true"`, the job fails instead, including when the job has no ID token.
- A `409` never falls back.
- It exports the minted token as `ARTIFACT_CENTRAL_TOKEN`. After a fallback, that token has no build attribution.
Once glab can do this, https://gitlab.com/gitlab-org/gitlab/-/work_items/642605 hands the template's login to glab.
## Scope
The work has two changes. Both use the same exchange step in `internal/api/artifactregistry`, so they share the behavior in "Both commands".
### Both commands
**Finding the token**
- glab tries these in order: `ARTIFACT_CENTRAL_TOKEN`, then the ID token exchange, then the `CI_JOB_TOKEN` exchange. The first one that works is used. None needs `GLAB_ENABLE_CI_AUTOLOGIN`.
- `ARTIFACT_CENTRAL_TOKEN` is the token the CI/CD template minted. When it is set and not empty, glab uses it without a request.
- glab presents the ID token when `ARTIFACT_CENTRAL_ID_TOKEN` is set. The job opts in by declaring it in `id_tokens:`, so glab needs no flag.
- The ID token and its job context work without `GLAB_ENABLE_CI_AUTOLOGIN`. This applies only to the token exchange in `get-token` and `login`. Other glab commands don't change.
- glab sends the ID token only to the host in `CI_SERVER_URL`, which is the host its `aud` names. If `--hostname` names another host, glab doesn't present the ID token and exchanges as it does today.
- glab trusts `CI_SERVER_TLS_CA_FILE` in addition to the system trust store, so instances with a private CA work. Adding the file, instead of replacing the trust store as curl's `--cacert` does, covers the template's retry in one request. If the file can't be read, glab uses the system trust store.
- glab uses `ARTIFACT_CENTRAL_TOKEN` only when the exchange host is the host in `CI_SERVER_URL`, and the token hasn't expired. The expiry is the token's `exp` claim, with a one-minute margin. Otherwise glab warns on stderr and goes on to the ID token. A value that isn't a JWT is used as is, if it contains only printable ASCII. A value with whitespace, a line break or any other byte is not used: glab warns and goes on to the ID token.
- With `--require-build-attribution`, or `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION` set to `"true"`, glab skips an `ARTIFACT_CENTRAL_TOKEN` that has no `gitlab.job` claim, with a warning, and goes on to the ID token. The template exports such a token after it falls back. If the ID token is missing or its exchange fails, glab fails.
**Falling back**
- On any exchange failure except `409` and a cancelled command, glab writes a warning to stderr and exchanges `CI_JOB_TOKEN` instead. The warning says the token has no build attribution and how to fail instead. glab retries network errors and `5xx` before it falls back, as it does today.
- The fallback is always `CI_JOB_TOKEN`, even when `GITLAB_TOKEN` is set.
- A `409` fails with the template's message, which links https://gitlab.com/gitlab-org/gitlab/-/work_items/636235. It doesn't fall back.
**Requiring build attribution**
- `--require-build-attribution`, or `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION` set to exactly `"true"`, makes glab fail instead of falling back. It also fails when `ARTIFACT_CENTRAL_ID_TOKEN` isn't set.
- On failure, glab exits non-zero, prints nothing to stdout, and writes no configuration.
**Duration**
- When glab presents the ID token and `--duration` isn't given, the duration is `CI_JOB_TIMEOUT`, capped at 12 hours. An explicit `--duration` wins.
- `--duration` has no effect on `ARTIFACT_CENTRAL_TOKEN`, because the template set its lifetime.
**Handling the ID token**
- glab reads the ID token only from the environment, never from a flag value, so it stays out of the process list. It never writes it to the configuration or the keyring, and never logs it.
- `ARTIFACT_CENTRAL_TOKEN` gets the same care. `login` writes it to the client's configuration file, as it writes any minted token.
- The glab docs treat the ID token as sensitive, like `CI_JOB_TOKEN`.
### Change 1: `get-token`
`get-token` prints one token to stdout, and jobs capture it in a shell variable.
- stdout stays the bare token, or the JSON object with `--output json`. Warnings go to stderr only.
- The JSON object adds `build_attribution`: `true` when the minted token carries `gitlab.job`, `false` otherwise.
- With `ARTIFACT_CENTRAL_TOKEN`, it prints that token, and `build_attribution` is read from it. In JSON output, `expires_at` is omitted when the token's expiry is unknown.
- `--duration` applies to the exchange as it does today, with the default above in a job.
- The glab docs show `get-token` in a CI/CD job, including the `id_tokens:` block the job needs.
### Change 2: `login`
`login` exchanges a token, then configures a client with it. Its writers work in two ways:
- **Writers that store the token:** `--maven`, `--gradle`, `--npm`, `--yarn` and `--sbt`. They write one token into the client's config file, so they present the ID token once, at login. With `ARTIFACT_CENTRAL_TOKEN`, they write that token and make no exchange.
- **`--docker`:** this writer installs a credential helper that, today, exchanges a fresh token for every `docker` command. In a job, the ID token is refused 15 minutes after pickup, and the registry records attribution only on the manifest `PUT`, so a push late in the job would lose it. In a job with `ARTIFACT_CENTRAL_ID_TOKEN`:
- `login --docker` exchanges once, with the duration above, and saves the minted token in a cache keyed by registry and `CI_JOB_ID`, readable only by the user.
- The helper uses the cached token while it hasn't expired. Otherwise, and outside a job, it behaves as it does today.
- The fallback warning and `--require-build-attribution` apply at login. When login fails, it caches nothing and installs no helper.
- Expired cache entries and stale temporary files are removed, and the cache directory is restricted to the user.
- With `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION` set to `"true"`, the helper doesn't exchange when it finds no cached token. It fails and says to run `glab artifact-registry login --docker` in the job. It doesn't fall back to stored container registry credentials.
- With `ARTIFACT_CENTRAL_TOKEN`, `login --docker` makes no exchange and caches nothing. The helper returns that token while it is valid and the registry's GitLab host is the one in `CI_SERVER_URL`, because the helper inherits the job's environment.
The glab docs show `login` in a CI/CD job for both kinds of writer.
### Not in scope
- the template handoff (https://gitlab.com/gitlab-org/gitlab/-/work_items/642605)
- users in more than one organization (https://gitlab.com/gitlab-org/gitlab/-/work_items/636235)
- a flag that names a different ID token variable. It can follow if jobs need one.
## Decisions
### Both commands
1. **How glab finds the ID token:** the fixed name `ARTIFACT_CENTRAL_ID_TOKEN`, the template's variable. No flag names the variable.
2. **When glab presents it:** whenever the variable is set. Declaring `id_tokens:` is the opt-in. It doesn't need `GLAB_ENABLE_CI_AUTOLOGIN`, which is off by default.
3. **Failing instead of falling back:** both `--require-build-attribution` and `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION` set to `"true"`.
4. **`--duration` default in a job:** `CI_JOB_TIMEOUT`, capped at 12 hours, as the template uses.
5. **The rename to Artifact Central** (https://gitlab.com/gitlab-org/cli/-/merge_requests/4491): this work doesn't wait for it. Whichever lands second rebases. The variables already use the Artifact Central name.
6. **Using the template's token:** glab uses `ARTIFACT_CENTRAL_TOKEN` first, then the ID token, then `CI_JOB_TOKEN`. The template already runs the exchange, the fallback and strict mode, so glab reuses its result. The Docker helper needs no exchange or cache when the variable is set.
### `get-token`
7. **Build attribution in JSON:** yes, as `build_attribution`, read from the minted token's `gitlab.job` claim.
8. **How the template detects a capable glab:** `glab artifact-registry get-token --help` lists `--require-build-attribution`. The check needs no network and no exchange.
### `login`
9. **Does `login --docker` present the ID token:** yes, once, at login.
10. **How later requests keep build attribution:** the helper reuses the token minted at login, cached for the job.
## Done when
In a job with an ID token for the token exchange, and no `GLAB_ENABLE_CI_AUTOLOGIN`:
- **`get-token`** prints a token that carries `gitlab.job`, and its JSON says `"build_attribution": true`. When the exchange refuses the ID token, it prints a `CI_JOB_TOKEN` token and warns on stderr.
- **`login`** with a writer that stores the token writes a token that carries `gitlab.job`, with the same fallback and warning.
- **`login --docker`** makes a `docker push` more than 15 minutes into the job record build attribution.
- **With `--require-build-attribution` or `ARTIFACT_CENTRAL_REQUIRE_BUILD_ATTRIBUTION: "true"`**, both commands fail instead of falling back.
- **`ARTIFACT_CENTRAL_TOKEN`** set by the template: `get-token` prints it, `login` writes it, and the Docker helper returns it, each without an exchange. With `--require-build-attribution`, a token without `gitlab.job` fails.
- **A `409`** fails on both commands without falling back.
The glab docs cover each case.
## References
- Epic: https://gitlab.com/groups/gitlab-org/-/work_items/24520
- ADR-020: https://gitlab.com/gitlab-org/ops/artifact-registry/-/blob/main/docs/adr/020_authentication_flow.md
- Product decision on opt-in and the template's variables as the interface: https://gitlab.com/gitlab-org/gitlab/-/work_items/633755#note_4161462004
issue
GitLab AI Context
Project: gitlab-org/cli
Instance: https://gitlab.com
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://gitlab.com/gitlab-org/cli/-/raw/main/CONTRIBUTING.md — contribution guidelines
- https://gitlab.com/gitlab-org/cli/-/raw/main/README.md — project overview and setup
- https://gitlab.com/gitlab-org/cli/-/raw/main/AGENTS.md — AI agent instructions
- https://gitlab.com/gitlab-org/cli/-/raw/main/CLAUDE.md — Claude Code instructions
Repository: https://gitlab.com/gitlab-org/cli
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD