Repository navigation
Conversation
sjalloq
requested review from
bacongobbler,
davidfestal,
flavio and
thomastaylor312
as code owners
November 12, 2026 08:14
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
force-pushed
the
fix/request-first-auth
branch
from
November 28, 2026 08:40
1642b15 to
8ac1a09
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
401 Unauthorized, return it.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::authis untouched and still probes/v2/.Why it is done this way
registry:3requiresregistry:catalog:*and Zot requiresrepository::pull. The narrow fix in the issue, aRegistryOperation::Catalogvariant, would fix one and not the other. Only the registry's challenge says what it wants.Client.Do, and the catalog scope is the hint oras-go uses.How behaviour differs from
mainmainit fails withUnauthorizedError.GET /v2/before each request.Not in this PR
These differences from oras-go remain, and each can be its own follow-up:
Commits and tests
test(token_cache): pins which requests share a cached token. Passes before and after the next commit.refactor(token_cache): keys cached tokens by their scopes. No behaviour change.test(client): five tests of the new flow intests/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.test(client): makes an existing fake registry challenge the upload request, as a real registry does. Passes with the old and the new client.fix(client): the fix. Enables the five tests and updates the existing tests whose expected request sequence changes.test(client): runs the client againstregistry:3.1.2and 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
rcgenas 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-ignoredon commit 3.