diff --git a/client/auth.go b/client/auth.go new file mode 100644 index 0000000000..7d858877b7 --- /dev/null +++ b/client/auth.go @@ -0,0 +1,14 @@ +package client + +import ( + "context" + + "github.com/docker/docker/api/types/registry" +) + +// staticAuth creates a privilegeFn from the given registryAuth. +func staticAuth(registryAuth string) registry.RequestAuthConfig { + return func(ctx context.Context) (string, error) { + return registryAuth, nil + } +} diff --git a/client/image_pull_test.go b/client/image_pull_test.go index 8ad23f5fcc..88b8b18e95 100644 --- a/client/image_pull_test.go +++ b/client/image_pull_test.go @@ -48,43 +48,41 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { client := &Client{ client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), } - privilegeFunc := func(_ context.Context) (string, error) { - return "", errors.New("Error requesting privilege") - } _, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{ - PrivilegeFunc: privilegeFunc, + PrivilegeFunc: func(_ context.Context) (string, error) { + return "", errors.New("error requesting privilege") + }, }) - assert.Check(t, is.Error(err, "Error requesting privilege")) + assert.Check(t, is.Error(err, "error requesting privilege")) } func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { client := &Client{ client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")), } - privilegeFunc := func(_ context.Context) (string, error) { - return "a-auth-header", nil - } _, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{ - PrivilegeFunc: privilegeFunc, + PrivilegeFunc: staticAuth("a-auth-header"), }) assert.Check(t, is.ErrorType(err, cerrdefs.IsUnauthorized)) } func TestImagePullWithPrivilegedFuncNoError(t *testing.T) { const expectedURL = "/images/create" + const invalidAuth = "NotValid" + const validAuth = "IAmValid" client := &Client{ client: newMockClient(func(req *http.Request) (*http.Response, error) { if !strings.HasPrefix(req.URL.Path, expectedURL) { return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } auth := req.Header.Get(registry.AuthHeader) - if auth == "NotValid" { + if auth == invalidAuth { return &http.Response{ StatusCode: http.StatusUnauthorized, Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))), }, nil } - if auth != "IAmValid" { + if auth != validAuth { return nil, fmt.Errorf("invalid auth header: expected %s, got %s", "IAmValid", auth) } query := req.URL.Query() @@ -102,12 +100,9 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) { }, nil }), } - privilegeFunc := func(_ context.Context) (string, error) { - return "IAmValid", nil - } resp, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{ - RegistryAuth: "NotValid", - PrivilegeFunc: privilegeFunc, + RegistryAuth: invalidAuth, + PrivilegeFunc: staticAuth(validAuth), }) assert.NilError(t, err) body, err := io.ReadAll(resp) diff --git a/client/image_push_test.go b/client/image_push_test.go index c4fd95d344..16d17947ec 100644 --- a/client/image_push_test.go +++ b/client/image_push_test.go @@ -75,19 +75,21 @@ func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) func TestImagePushWithPrivilegedFuncNoError(t *testing.T) { const expectedURL = "/images/docker.io/myname/myimage/push" + const invalidAuth = "NotValid" + const validAuth = "IAmValid" client := &Client{ client: newMockClient(func(req *http.Request) (*http.Response, error) { if !strings.HasPrefix(req.URL.Path, expectedURL) { return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) } auth := req.Header.Get(registry.AuthHeader) - if auth == "NotValid" { + if auth == invalidAuth { return &http.Response{ StatusCode: http.StatusUnauthorized, Body: io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))), }, nil } - if auth != "IAmValid" { + if auth != validAuth { return nil, fmt.Errorf("invalid auth header: expected %s, got %s", "IAmValid", auth) } query := req.URL.Query() @@ -101,12 +103,9 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) { }, nil }), } - privilegeFunc := func(_ context.Context) (string, error) { - return "IAmValid", nil - } resp, err := client.ImagePush(context.Background(), "myname/myimage:tag", image.PushOptions{ - RegistryAuth: "NotValid", - PrivilegeFunc: privilegeFunc, + RegistryAuth: invalidAuth, + PrivilegeFunc: staticAuth(validAuth), }) assert.NilError(t, err) body, err := io.ReadAll(resp)