mirror of
https://github.com/docker/cli.git
synced 2026-09-27 01:40:31 -04:00
registry: map login HTTP status through errhttp.ToNative
translateV2AuthError only unwraps a url.Error from Do(); a 401 status never went through it. Use errhttp.ToNative so Auth() treats that 401 as unauthorized and stops. Signed-off-by: Dean Chen <862469039@qq.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/errdefs/pkg/errhttp"
|
||||
"github.com/containerd/log"
|
||||
"github.com/docker/distribution/registry/client/auth"
|
||||
"github.com/docker/distribution/registry/client/auth/challenge"
|
||||
@@ -67,11 +68,7 @@ func loginV2(ctx context.Context, authConfig *registry.AuthConfig, endpoint APIE
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// TODO(dmcgowan): Attempt to further interpret result, status code and error code string
|
||||
err := fmt.Errorf("login attempt to %s failed with status: %d %s", endpointStr, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return "", unauthorizedErr{err}
|
||||
}
|
||||
return "", err
|
||||
return "", fmt.Errorf("login attempt to %s failed with status: %d %s: %w", endpointStr, resp.StatusCode, http.StatusText(resp.StatusCode), errhttp.ToNative(resp.StatusCode))
|
||||
}
|
||||
|
||||
return credentialAuthConfig.IdentityToken, nil
|
||||
|
||||
@@ -33,6 +33,7 @@ func TestLoginV2BasicAuthUnauthorized(t *testing.T) {
|
||||
_, err = loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "wrong"}, endpoint, "docker-test")
|
||||
assert.ErrorContains(t, err, "401")
|
||||
assert.Check(t, errdefs.IsUnauthorized(err))
|
||||
assert.Check(t, is.ErrorType(err, errdefs.IsUnauthorized))
|
||||
|
||||
token, err := loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "secret"}, endpoint, "docker-test")
|
||||
assert.NilError(t, err)
|
||||
|
||||
+1
-1
@@ -14,6 +14,7 @@ tool (
|
||||
require (
|
||||
dario.cat/mergo v1.0.2
|
||||
github.com/containerd/errdefs v1.0.0
|
||||
github.com/containerd/errdefs/pkg v0.3.0
|
||||
github.com/containerd/log v0.1.0
|
||||
github.com/containerd/platforms v1.0.0-rc.5
|
||||
github.com/creack/pty v1.1.24
|
||||
@@ -75,7 +76,6 @@ require (
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
|
||||
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
|
||||
github.com/docker/go-events v0.0.0-20260608200158-dbf6103125a4 // indirect
|
||||
github.com/docker/go-metrics v0.1.0 // indirect
|
||||
|
||||
Reference in New Issue
Block a user