Skip to content

Commit

Permalink
feat: ctx to client API
Browse files Browse the repository at this point in the history
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
  • Loading branch information
Benehiko committed Mar 22, 2024
1 parent 83ae992 commit 80d92fd
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 18 deletions.
5 changes: 3 additions & 2 deletions api/types/client.go
Expand Up @@ -2,6 +2,7 @@ package types // import "github.com/docker/docker/api/types"

import (
"bufio"
"context"
"io"
"net"

Expand Down Expand Up @@ -176,7 +177,7 @@ type ImageLoadResponse struct {
// This function returns the registry authentication
// header value in base 64 format, or an error
// if the privilege request fails.
type RequestPrivilegeFunc func() (string, error)
type RequestPrivilegeFunc func(context.Context) (string, error)

// ImageSearchOptions holds parameters to search images with.
type ImageSearchOptions struct {
Expand Down Expand Up @@ -289,7 +290,7 @@ type PluginInstallOptions struct {
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
RemoteRef string // RemoteRef is the plugin name on the registry
PrivilegeFunc RequestPrivilegeFunc
AcceptPermissionsFunc func(PluginPrivileges) (bool, error)
AcceptPermissionsFunc func(context.Context, PluginPrivileges) (bool, error)
Args []string
}

Expand Down
8 changes: 6 additions & 2 deletions api/types/image/opts.go
@@ -1,6 +1,10 @@
package image

import "github.com/docker/docker/api/types/filters"
import (
"context"

"github.com/docker/docker/api/types/filters"
)

// ImportOptions holds information to import images from the client host.
type ImportOptions struct {
Expand All @@ -27,7 +31,7 @@ type PullOptions struct {
// privilege request fails.
//
// Also see [github.com/docker/docker/api/types.RequestPrivilegeFunc].
PrivilegeFunc func() (string, error)
PrivilegeFunc func(context.Context) (string, error)
Platform string
}

Expand Down
2 changes: 1 addition & 1 deletion client/image_pull.go
Expand Up @@ -36,7 +36,7 @@ func (cli *Client) ImagePull(ctx context.Context, refStr string, options image.P

resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
newAuthHeader, privilegeErr := options.PrivilegeFunc()
newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
if privilegeErr != nil {
return nil, privilegeErr
}
Expand Down
6 changes: 3 additions & 3 deletions client/image_pull_test.go
Expand Up @@ -49,7 +49,7 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "", fmt.Errorf("Error requesting privilege")
}
_, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
Expand All @@ -64,7 +64,7 @@ func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "a-auth-header", nil
}
_, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
}, nil
}),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "IAmValid", nil
}
resp, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
Expand Down
2 changes: 1 addition & 1 deletion client/image_push.go
Expand Up @@ -38,7 +38,7 @@ func (cli *Client) ImagePush(ctx context.Context, image string, options image.Pu

resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
newAuthHeader, privilegeErr := options.PrivilegeFunc()
newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
if privilegeErr != nil {
return nil, privilegeErr
}
Expand Down
6 changes: 3 additions & 3 deletions client/image_push_test.go
Expand Up @@ -54,7 +54,7 @@ func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "", fmt.Errorf("Error requesting privilege")
}
_, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{
Expand All @@ -69,7 +69,7 @@ func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "a-auth-header", nil
}
_, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
}, nil
}),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "IAmValid", nil
}
resp, err := client.ImagePush(context.Background(), "myimage:tag", image.PushOptions{
Expand Down
2 changes: 1 addition & 1 deletion client/image_search.go
Expand Up @@ -34,7 +34,7 @@ func (cli *Client) ImageSearch(ctx context.Context, term string, options types.I
resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
defer ensureReaderClosed(resp)
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
newAuthHeader, privilegeErr := options.PrivilegeFunc()
newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
if privilegeErr != nil {
return results, privilegeErr
}
Expand Down
6 changes: 3 additions & 3 deletions client/image_search_test.go
Expand Up @@ -38,7 +38,7 @@ func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "", fmt.Errorf("Error requesting privilege")
}
_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
Expand All @@ -53,7 +53,7 @@ func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.
client := &Client{
client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "a-auth-header", nil
}
_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
}, nil
}),
}
privilegeFunc := func() (string, error) {
privilegeFunc := func(_ context.Context) (string, error) {
return "IAmValid", nil
}
results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
Expand Down
4 changes: 2 additions & 2 deletions client/plugin_install.go
Expand Up @@ -84,7 +84,7 @@ func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values,
resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
// todo: do inspect before to check existing name before checking privileges
newAuthHeader, privilegeErr := options.PrivilegeFunc()
newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
if privilegeErr != nil {
ensureReaderClosed(resp)
return nil, privilegeErr
Expand All @@ -105,7 +105,7 @@ func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values,
ensureReaderClosed(resp)

if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
accept, err := options.AcceptPermissionsFunc(privileges)
accept, err := options.AcceptPermissionsFunc(ctx, privileges)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 80d92fd

Please sign in to comment.