Browse code

feat: ctx to client API

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>

Alano Terblanche authored on 2024/03/08 21:39:06
Showing 9 changed files
... ...
@@ -2,6 +2,7 @@ package types // import "github.com/docker/docker/api/types"
2 2
 
3 3
 import (
4 4
 	"bufio"
5
+	"context"
5 6
 	"io"
6 7
 	"net"
7 8
 
... ...
@@ -176,7 +177,7 @@ type ImageLoadResponse struct {
176 176
 // This function returns the registry authentication
177 177
 // header value in base 64 format, or an error
178 178
 // if the privilege request fails.
179
-type RequestPrivilegeFunc func() (string, error)
179
+type RequestPrivilegeFunc func(context.Context) (string, error)
180 180
 
181 181
 // ImageSearchOptions holds parameters to search images with.
182 182
 type ImageSearchOptions struct {
... ...
@@ -289,7 +290,7 @@ type PluginInstallOptions struct {
289 289
 	RegistryAuth          string // RegistryAuth is the base64 encoded credentials for the registry
290 290
 	RemoteRef             string // RemoteRef is the plugin name on the registry
291 291
 	PrivilegeFunc         RequestPrivilegeFunc
292
-	AcceptPermissionsFunc func(PluginPrivileges) (bool, error)
292
+	AcceptPermissionsFunc func(context.Context, PluginPrivileges) (bool, error)
293 293
 	Args                  []string
294 294
 }
295 295
 
... ...
@@ -1,6 +1,10 @@
1 1
 package image
2 2
 
3
-import "github.com/docker/docker/api/types/filters"
3
+import (
4
+	"context"
5
+
6
+	"github.com/docker/docker/api/types/filters"
7
+)
4 8
 
5 9
 // ImportOptions holds information to import images from the client host.
6 10
 type ImportOptions struct {
... ...
@@ -27,7 +31,7 @@ type PullOptions struct {
27 27
 	// privilege request fails.
28 28
 	//
29 29
 	// Also see [github.com/docker/docker/api/types.RequestPrivilegeFunc].
30
-	PrivilegeFunc func() (string, error)
30
+	PrivilegeFunc func(context.Context) (string, error)
31 31
 	Platform      string
32 32
 }
33 33
 
... ...
@@ -36,7 +36,7 @@ func (cli *Client) ImagePull(ctx context.Context, refStr string, options image.P
36 36
 
37 37
 	resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
38 38
 	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
39
-		newAuthHeader, privilegeErr := options.PrivilegeFunc()
39
+		newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
40 40
 		if privilegeErr != nil {
41 41
 			return nil, privilegeErr
42 42
 		}
... ...
@@ -49,7 +49,7 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
49 49
 	client := &Client{
50 50
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
51 51
 	}
52
-	privilegeFunc := func() (string, error) {
52
+	privilegeFunc := func(_ context.Context) (string, error) {
53 53
 		return "", fmt.Errorf("Error requesting privilege")
54 54
 	}
55 55
 	_, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
... ...
@@ -64,7 +64,7 @@ func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
64 64
 	client := &Client{
65 65
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
66 66
 	}
67
-	privilegeFunc := func() (string, error) {
67
+	privilegeFunc := func(_ context.Context) (string, error) {
68 68
 		return "a-auth-header", nil
69 69
 	}
70 70
 	_, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
... ...
@@ -105,7 +105,7 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
105 105
 			}, nil
106 106
 		}),
107 107
 	}
108
-	privilegeFunc := func() (string, error) {
108
+	privilegeFunc := func(_ context.Context) (string, error) {
109 109
 		return "IAmValid", nil
110 110
 	}
111 111
 	resp, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
... ...
@@ -38,7 +38,7 @@ func (cli *Client) ImagePush(ctx context.Context, image string, options image.Pu
38 38
 
39 39
 	resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
40 40
 	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
41
-		newAuthHeader, privilegeErr := options.PrivilegeFunc()
41
+		newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
42 42
 		if privilegeErr != nil {
43 43
 			return nil, privilegeErr
44 44
 		}
... ...
@@ -54,7 +54,7 @@ func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
54 54
 	client := &Client{
55 55
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
56 56
 	}
57
-	privilegeFunc := func() (string, error) {
57
+	privilegeFunc := func(_ context.Context) (string, error) {
58 58
 		return "", fmt.Errorf("Error requesting privilege")
59 59
 	}
60 60
 	_, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{
... ...
@@ -69,7 +69,7 @@ func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T)
69 69
 	client := &Client{
70 70
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
71 71
 	}
72
-	privilegeFunc := func() (string, error) {
72
+	privilegeFunc := func(_ context.Context) (string, error) {
73 73
 		return "a-auth-header", nil
74 74
 	}
75 75
 	_, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{
... ...
@@ -106,7 +106,7 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
106 106
 			}, nil
107 107
 		}),
108 108
 	}
109
-	privilegeFunc := func() (string, error) {
109
+	privilegeFunc := func(_ context.Context) (string, error) {
110 110
 		return "IAmValid", nil
111 111
 	}
112 112
 	resp, err := client.ImagePush(context.Background(), "myimage:tag", image.PushOptions{
... ...
@@ -34,7 +34,7 @@ func (cli *Client) ImageSearch(ctx context.Context, term string, options types.I
34 34
 	resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
35 35
 	defer ensureReaderClosed(resp)
36 36
 	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
37
-		newAuthHeader, privilegeErr := options.PrivilegeFunc()
37
+		newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
38 38
 		if privilegeErr != nil {
39 39
 			return results, privilegeErr
40 40
 		}
... ...
@@ -38,7 +38,7 @@ func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
38 38
 	client := &Client{
39 39
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
40 40
 	}
41
-	privilegeFunc := func() (string, error) {
41
+	privilegeFunc := func(_ context.Context) (string, error) {
42 42
 		return "", fmt.Errorf("Error requesting privilege")
43 43
 	}
44 44
 	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
... ...
@@ -53,7 +53,7 @@ func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.
53 53
 	client := &Client{
54 54
 		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
55 55
 	}
56
-	privilegeFunc := func() (string, error) {
56
+	privilegeFunc := func(_ context.Context) (string, error) {
57 57
 		return "a-auth-header", nil
58 58
 	}
59 59
 	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
... ...
@@ -98,7 +98,7 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
98 98
 			}, nil
99 99
 		}),
100 100
 	}
101
-	privilegeFunc := func() (string, error) {
101
+	privilegeFunc := func(_ context.Context) (string, error) {
102 102
 		return "IAmValid", nil
103 103
 	}
104 104
 	results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
... ...
@@ -84,7 +84,7 @@ func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values,
84 84
 	resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
85 85
 	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
86 86
 		// todo: do inspect before to check existing name before checking privileges
87
-		newAuthHeader, privilegeErr := options.PrivilegeFunc()
87
+		newAuthHeader, privilegeErr := options.PrivilegeFunc(ctx)
88 88
 		if privilegeErr != nil {
89 89
 			ensureReaderClosed(resp)
90 90
 			return nil, privilegeErr
... ...
@@ -105,7 +105,7 @@ func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values,
105 105
 	ensureReaderClosed(resp)
106 106
 
107 107
 	if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
108
-		accept, err := options.AcceptPermissionsFunc(privileges)
108
+		accept, err := options.AcceptPermissionsFunc(ctx, privileges)
109 109
 		if err != nil {
110 110
 			return nil, err
111 111
 		}