Skip to content

Fix/request first auth - #326

Open
sjalloq wants to merge 9 commits into
oras-project:mainfrom
sjalloq:fix/request-first-auth
Open

sjalloq wants to merge 9 commits into
oras-project:mainfrom
sjalloq:fix/request-first-auth

Conversation

@sjalloq

@sjalloq sjalloq commented Nov 12, 2026

Copy link
Copy Markdown

This PR is the bigger of the 4 I'm opening. This fixes #320. Stacked on #323; only the last six commits are new.

What changes

The client now sends the request first and authenticates only when the registry asks:

  1. Send the request, with a cached token if there is one for the scopes the request is expected to need.
  2. If the answer is not 401 Unauthorized, return it.
  3. On a 401, read the challenge. For a bearer challenge, get one token that covers the expected scopes and the scopes the challenge names. For any other challenge, use the HTTP Basic credentials.
  4. Send the request once more.

Before, the client sent GET /v2/ ahead of every request to learn how the registry authenticates, and chose the token scope itself: pull or push on the repository of the request. It never read the challenge of the real request.

The catalog now expects the scope registry:catalog:*, and the challenge supplies anything else the registry wants. Cached tokens are keyed by their scopes, not by repository and operation.

There is no public API change. Client::auth is untouched and still probes /v2/.

Why it is done this way

  • No fixed scope works for the catalog. registry:3 requires registry:catalog:* and Zot requires repository::pull. The narrow fix in the issue, a RegistryOperation::Catalog variant, would fix one and not the other. Only the registry's challenge says what it wants.
  • It is what oras-go does. The flow follows Client.Do, and the catalog scope is the hint oras-go uses.
  • It removes a request. A registry without authentication now gets one request per call, not two.
  • It removes the stricter case from Fix/token exchange errors #323. A registry that serves a request without a token is never asked for one, so a failing token service cannot break that call.

How behaviour differs from main

  • The catalog works on registries with token authentication. On main it fails with UnauthorizedError.
  • No GET /v2/ before each request.
  • Credentials are sent only when the registry answers 401. If a registry serves a request anonymously, the client never authenticates for it. oras-go behaves the same way.
  • One more round trip in one case. When the challenge names a scope the client did not expect (Zot's catalog), each call gets a 401 first and then uses the cached token. oras-go does the same.
  • A request with a streamed body is not sent twice. If it gets a 401, the caller gets the 401. In an upload, the request that opens the session has no body, so the token is normally fetched there. It is not covered by a test.
  • The tests from Fix/token exchange errors #323 change. The registry now receives the first request without credentials before the token refusal is reported. The error the caller gets is the same.

Not in this PR

These differences from oras-go remain, and each can be its own follow-up:

  • Validation of the token service URL in the challenge.
  • oras-go's checks on redirects to another origin or repository. The existing check that credentials only go to the registry's own host is kept.
  • The OAuth2 POST token flow.
  • The retry for registries that answer an expired token with a 400.

Commits and tests

  1. test(token_cache): pins which requests share a cached token. Passes before and after the next commit.
  2. refactor(token_cache): keys cached tokens by their scopes. No behaviour change.
  3. test(client): five tests of the new flow in tests/request_first_auth.rs, ignored because they fail until commit 5. They cover the catalog with each registry's scope, a token fetched after the challenge and used again, the same for Basic credentials, and a registry without authentication.
  4. test(client): makes an existing fake registry challenge the upload request, as a real registry does. Passes with the old and the new client.
  5. fix(client): the fix. Enables the five tests and updates the existing tests whose expected request sequence changes.
  6. test(client): runs the client against registry:3.1.2 and Zot v2.1.22, both with token authentication. Both tests fail on the commit before the fix and pass after it.

Commit 6 is optional. It adds rcgen as a dev-dependency, to generate the key that signs the tokens, and CI pulls three more images. It can be dropped without touching the fix.

To see the failures: cargo test --test request_first_auth -- --include-ignored on commit 3.

When the token service refuses a token request, the client sends the
request to the registry without credentials. The caller gets the 401 of
the registry, and the reason the token service gave is lost.

These tests describe the expected behaviour: the refusal is returned and
the registry does not receive the request. They are ignored because they
fail until the fix in the next commit.

Refs: oras-project#321

Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
`get_auth_token` turned an error from `_auth` into `None` with `.ok()`.
When the token service refused the token request, the request went to
the registry without an `Authorization` header. The caller got the 401
of the registry, and the reason the token service gave was lost.

`get_auth_token` now returns the error and `apply_auth` propagates it.
The registry does not receive the request.

Fixes: oras-project#321

Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
The token service of a registry is often another host than the registry.
When the client cannot connect to it, the client must report the error of
that request. On main it sends the request to the registry without
credentials and returns the UnauthorizedError of the registry.

The fake registry can now name another address for its token service, and
the new test names an address that nothing listens on.

Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
A token is cached for one registry, one repository and one operation.
The next commit changes the key of the cache, and this test shows that
the change keeps the same tokens apart.

Refs: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
The cache kept a token under its registry, repository and operation.
The scope that the token was requested for was computed from the same
repository and operation in another place, in `_auth`.

The cache now keeps a token under its registry and its scopes, and
`_auth` takes the scopes from the same function. The behaviour is the
same: one repository and one operation give one scope.

This prepares the fix of oras-project#320. The catalog needs a token for a scope
that is not a repository operation, and a registry can name more than
one scope in its challenge.

Refs: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
… request

The client asks `/v2/` how the registry authenticates and decides the
scope of the token itself. It does not read the challenge that the
registry sends for the request. The catalog fails on a registry with
token authentication, because the registry requires a scope that the
client does not ask for, and every call on a registry without
authentication costs a request to `/v2/`.

These tests describe the expected behaviour: the client sends the
request, follows the challenge that it gets, and sends the request
again. They are ignored because they fail until the fix in a later
commit.

Refs: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
The fake registry of the upload location tests sent a challenge on
`/v2/` only, and accepted an upload request without credentials. A
registry with authentication sends a challenge on the request itself.

The fake registry now refuses an upload request without credentials.
The tests pass with the current client, which asks `/v2/` first, and
with a client that follows the challenge of the request.

Refs: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
…egistry

The client asked `/v2/` how the registry authenticates before a request,
and decided the scope of the token itself: pull or push on the
repository of the request. It did not read the challenge that the
registry sends for the request.

- The catalog failed on a registry with token authentication. The
  catalog is not an operation on a repository, and registries do not
  agree on its scope: the distribution registry requires
  `registry:catalog:*` and Zot requires `repository::pull`.
- Every call on a registry without authentication cost a request to
  `/v2/`.

The client now does what oras-go does in `auth.Client.Do`. It sends the
request, with a token from the cache if it has one for the scopes that
it expects the request to need. If the registry answers with `401
Unauthorized`, the client reads the challenge, gets one token for the
scopes that it expected and the scopes that the challenge names, and
sends the request one more time. For a challenge that is not a bearer
challenge, it sends its HTTP Basic credentials.

A request with a streamed body cannot be sent a second time. The client
then returns the `401 Unauthorized` response. In an upload, the request
that opens the upload session has no body and gets the token first.

`Client::auth` is not changed. It still asks `/v2/`, and the token that
it gets is in the cache for the requests that follow.

Tests that expected a request to `/v2/` before the request now expect
the request, the challenge and the request with credentials.

Fixes: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
…tion

The distribution registry and Zot require different scopes for the
catalog. The tests of the client used fake registries only, so they
showed what the client does with the challenges that we expect, and not
with the challenges that the registries send.

The new tests start the distribution registry and Zot with token
authentication, and `cesanta/docker_auth` as the token service. They
list the catalog, push an image and list its tags with one client. They
fail before the previous commit: the catalog is refused on both
registries.

The tests are behind the `test-registry` feature, like the other tests
that need Docker. The token service signs with an RSA key, which
`rcgen` generates. It is a new dev-dependency; its `aws_lc_rs` backend
is already a dev-dependency through `hyper-rustls`.

Refs: oras-project#320
Signed-off-by: Shareef Jalloq <shareef@jalloq.co.uk>
@sjalloq
sjalloq force-pushed the fix/request-first-auth branch from 1642b15 to 8ac1a09 Compare November 28, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

catalog fails on token-auth registries because it asks for a repository:<name>:pull token

1 participant