Browse code

api: add const for 'X-Registry-Auth'

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2021/08/27 04:08:38
Showing 20 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"net/http"
6 6
 
7
+	"github.com/docker/docker/api/types/registry"
7 8
 	"github.com/sirupsen/logrus"
8 9
 )
9 10
 
... ...
@@ -30,7 +31,7 @@ func (c CORSMiddleware) WrapHandler(handler func(ctx context.Context, w http.Res
30 30
 
31 31
 		logrus.Debugf("CORS header is enabled and set to: %s", corsHeaders)
32 32
 		w.Header().Add("Access-Control-Allow-Origin", corsHeaders)
33
-		w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
33
+		w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, "+registry.AuthHeader)
34 34
 		w.Header().Add("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE, PUT, OPTIONS")
35 35
 		return handler(ctx, w, r, vars)
36 36
 	}
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/docker/distribution/reference"
14 14
 	"github.com/docker/docker/api/server/httputils"
15 15
 	"github.com/docker/docker/api/types"
16
-	registrytypes "github.com/docker/docker/api/types/registry"
16
+	"github.com/docker/docker/api/types/registry"
17 17
 	"github.com/docker/docker/errdefs"
18 18
 	v1 "github.com/opencontainers/image-spec/specs-go/v1"
19 19
 	"github.com/pkg/errors"
... ...
@@ -28,8 +28,8 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
28 28
 
29 29
 	var (
30 30
 		config              = &types.AuthConfig{}
31
-		authEncoded         = r.Header.Get("X-Registry-Auth")
32
-		distributionInspect registrytypes.DistributionInspect
31
+		authEncoded         = r.Header.Get(registry.AuthHeader)
32
+		distributionInspect registry.DistributionInspect
33 33
 	)
34 34
 
35 35
 	if authEncoded != "" {
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/docker/docker/api/server/httputils"
15 15
 	"github.com/docker/docker/api/types"
16 16
 	"github.com/docker/docker/api/types/filters"
17
+	"github.com/docker/docker/api/types/registry"
17 18
 	"github.com/docker/docker/api/types/versions"
18 19
 	"github.com/docker/docker/errdefs"
19 20
 	"github.com/docker/docker/image"
... ...
@@ -63,7 +64,7 @@ func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrite
63 63
 			}
64 64
 		}
65 65
 
66
-		authEncoded := r.Header.Get("X-Registry-Auth")
66
+		authEncoded := r.Header.Get(registry.AuthHeader)
67 67
 		authConfig := &types.AuthConfig{}
68 68
 		if authEncoded != "" {
69 69
 			authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
... ...
@@ -100,7 +101,7 @@ func (s *imageRouter) postImagesPush(ctx context.Context, w http.ResponseWriter,
100 100
 	}
101 101
 	authConfig := &types.AuthConfig{}
102 102
 
103
-	authEncoded := r.Header.Get("X-Registry-Auth")
103
+	authEncoded := r.Header.Get(registry.AuthHeader)
104 104
 	if authEncoded != "" {
105 105
 		// the new format is to handle the authConfig as a header
106 106
 		authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
... ...
@@ -360,7 +361,7 @@ func (s *imageRouter) getImagesSearch(ctx context.Context, w http.ResponseWriter
360 360
 	}
361 361
 	var (
362 362
 		config      *types.AuthConfig
363
-		authEncoded = r.Header.Get("X-Registry-Auth")
363
+		authEncoded = r.Header.Get(registry.AuthHeader)
364 364
 		headers     = map[string][]string{}
365 365
 	)
366 366
 
... ...
@@ -27,7 +27,7 @@ func parseHeaders(headers http.Header) (map[string][]string, *types.AuthConfig)
27 27
 	}
28 28
 
29 29
 	// Get X-Registry-Auth
30
-	authEncoded := headers.Get("X-Registry-Auth")
30
+	authEncoded := headers.Get(registry.AuthHeader)
31 31
 	authConfig := &types.AuthConfig{}
32 32
 	if authEncoded != "" {
33 33
 		authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	basictypes "github.com/docker/docker/api/types"
11 11
 	"github.com/docker/docker/api/types/backend"
12 12
 	"github.com/docker/docker/api/types/filters"
13
+	"github.com/docker/docker/api/types/registry"
13 14
 	types "github.com/docker/docker/api/types/swarm"
14 15
 	"github.com/docker/docker/api/types/versions"
15 16
 	"github.com/docker/docker/errdefs"
... ...
@@ -207,7 +208,7 @@ func (sr *swarmRouter) createService(ctx context.Context, w http.ResponseWriter,
207 207
 	}
208 208
 
209 209
 	// Get returns "" if the header does not exist
210
-	encodedAuth := r.Header.Get("X-Registry-Auth")
210
+	encodedAuth := r.Header.Get(registry.AuthHeader)
211 211
 	queryRegistry := false
212 212
 	if v := httputils.VersionFromContext(ctx); v != "" {
213 213
 		if versions.LessThan(v, "1.30") {
... ...
@@ -240,7 +241,7 @@ func (sr *swarmRouter) updateService(ctx context.Context, w http.ResponseWriter,
240 240
 	var flags basictypes.ServiceUpdateOptions
241 241
 
242 242
 	// Get returns "" if the header does not exist
243
-	flags.EncodedRegistryAuth = r.Header.Get("X-Registry-Auth")
243
+	flags.EncodedRegistryAuth = r.Header.Get(registry.AuthHeader)
244 244
 	flags.RegistryAuthFrom = r.URL.Query().Get("registryAuthFrom")
245 245
 	flags.Rollback = r.URL.Query().Get("rollback")
246 246
 	queryRegistry := false
247 247
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+package registry // import "github.com/docker/docker/api/types/registry"
1
+
2
+// AuthHeader is the name of the header used to send encoded registry
3
+// authorization credentials for registry operations (push/pull).
4
+const AuthHeader = "X-Registry-Auth"
... ...
@@ -5,13 +5,13 @@ import (
5 5
 	"encoding/json"
6 6
 	"net/url"
7 7
 
8
-	registrytypes "github.com/docker/docker/api/types/registry"
8
+	"github.com/docker/docker/api/types/registry"
9 9
 )
10 10
 
11 11
 // DistributionInspect returns the image digest with the full manifest.
12
-func (cli *Client) DistributionInspect(ctx context.Context, image, encodedRegistryAuth string) (registrytypes.DistributionInspect, error) {
12
+func (cli *Client) DistributionInspect(ctx context.Context, image, encodedRegistryAuth string) (registry.DistributionInspect, error) {
13 13
 	// Contact the registry to retrieve digest and platform information
14
-	var distributionInspect registrytypes.DistributionInspect
14
+	var distributionInspect registry.DistributionInspect
15 15
 	if image == "" {
16 16
 		return distributionInspect, objectNotFoundError{object: "distribution", id: image}
17 17
 	}
... ...
@@ -23,7 +23,7 @@ func (cli *Client) DistributionInspect(ctx context.Context, image, encodedRegist
23 23
 
24 24
 	if encodedRegistryAuth != "" {
25 25
 		headers = map[string][]string{
26
-			"X-Registry-Auth": {encodedRegistryAuth},
26
+			registry.AuthHeader: {encodedRegistryAuth},
27 27
 		}
28 28
 	}
29 29
 
... ...
@@ -8,6 +8,7 @@ import (
8 8
 
9 9
 	"github.com/docker/distribution/reference"
10 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/api/types/registry"
11 12
 )
12 13
 
13 14
 // ImageCreate creates a new image based on the parent options.
... ...
@@ -32,6 +33,6 @@ func (cli *Client) ImageCreate(ctx context.Context, parentReference string, opti
32 32
 }
33 33
 
34 34
 func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
35
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
35
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
36 36
 	return cli.post(ctx, "/images/create", query, nil, headers)
37 37
 }
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"testing"
11 11
 
12 12
 	"github.com/docker/docker/api/types"
13
+	"github.com/docker/docker/api/types/registry"
13 14
 	"github.com/docker/docker/errdefs"
14 15
 )
15 16
 
... ...
@@ -34,9 +35,9 @@ func TestImageCreate(t *testing.T) {
34 34
 			if !strings.HasPrefix(r.URL.Path, expectedURL) {
35 35
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
36 36
 			}
37
-			registryAuth := r.Header.Get("X-Registry-Auth")
37
+			registryAuth := r.Header.Get(registry.AuthHeader)
38 38
 			if registryAuth != expectedRegistryAuth {
39
-				return nil, fmt.Errorf("X-Registry-Auth header not properly set in the request. Expected '%s', got %s", expectedRegistryAuth, registryAuth)
39
+				return nil, fmt.Errorf("%s header not properly set in the request. Expected '%s', got %s", registry.AuthHeader, expectedRegistryAuth, registryAuth)
40 40
 			}
41 41
 
42 42
 			query := r.URL.Query()
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"testing"
11 11
 
12 12
 	"github.com/docker/docker/api/types"
13
+	"github.com/docker/docker/api/types/registry"
13 14
 	"github.com/docker/docker/errdefs"
14 15
 )
15 16
 
... ...
@@ -83,7 +84,7 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
83 83
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
84 84
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
85 85
 			}
86
-			auth := req.Header.Get("X-Registry-Auth")
86
+			auth := req.Header.Get(registry.AuthHeader)
87 87
 			if auth == "NotValid" {
88 88
 				return &http.Response{
89 89
 					StatusCode: http.StatusUnauthorized,
... ...
@@ -8,6 +8,7 @@ import (
8 8
 
9 9
 	"github.com/docker/distribution/reference"
10 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/api/types/registry"
11 12
 	"github.com/docker/docker/errdefs"
12 13
 )
13 14
 
... ...
@@ -49,6 +50,6 @@ func (cli *Client) ImagePush(ctx context.Context, image string, options types.Im
49 49
 }
50 50
 
51 51
 func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) {
52
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
52
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
53 53
 	return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
54 54
 }
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"testing"
11 11
 
12 12
 	"github.com/docker/docker/api/types"
13
+	"github.com/docker/docker/api/types/registry"
13 14
 	"github.com/docker/docker/errdefs"
14 15
 )
15 16
 
... ...
@@ -88,7 +89,7 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
88 88
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
89 89
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
90 90
 			}
91
-			auth := req.Header.Get("X-Registry-Auth")
91
+			auth := req.Header.Get(registry.AuthHeader)
92 92
 			if auth == "NotValid" {
93 93
 				return &http.Response{
94 94
 					StatusCode: http.StatusUnauthorized,
... ...
@@ -48,6 +48,6 @@ func (cli *Client) ImageSearch(ctx context.Context, term string, options types.I
48 48
 }
49 49
 
50 50
 func (cli *Client) tryImageSearch(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
51
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
51
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
52 52
 	return cli.get(ctx, "/images/search", query, headers)
53 53
 }
... ...
@@ -73,7 +73,7 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
73 73
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
74 74
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
75 75
 			}
76
-			auth := req.Header.Get("X-Registry-Auth")
76
+			auth := req.Header.Get(registry.AuthHeader)
77 77
 			if auth == "NotValid" {
78 78
 				return &http.Response{
79 79
 					StatusCode: http.StatusUnauthorized,
... ...
@@ -8,6 +8,7 @@ import (
8 8
 
9 9
 	"github.com/docker/distribution/reference"
10 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/api/types/registry"
11 12
 	"github.com/docker/docker/errdefs"
12 13
 	"github.com/pkg/errors"
13 14
 )
... ...
@@ -67,12 +68,12 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types
67 67
 }
68 68
 
69 69
 func (cli *Client) tryPluginPrivileges(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {
70
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
70
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
71 71
 	return cli.get(ctx, "/plugins/privileges", query, headers)
72 72
 }
73 73
 
74 74
 func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileges types.PluginPrivileges, registryAuth string) (serverResponse, error) {
75
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
75
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
76 76
 	return cli.post(ctx, "/plugins/pull", query, privileges, headers)
77 77
 }
78 78
 
... ...
@@ -3,11 +3,13 @@ package client // import "github.com/docker/docker/client"
3 3
 import (
4 4
 	"context"
5 5
 	"io"
6
+
7
+	"github.com/docker/docker/api/types/registry"
6 8
 )
7 9
 
8 10
 // PluginPush pushes a plugin to a registry
9 11
 func (cli *Client) PluginPush(ctx context.Context, name string, registryAuth string) (io.ReadCloser, error) {
10
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
12
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
11 13
 	resp, err := cli.post(ctx, "/plugins/"+name+"/push", nil, nil, headers)
12 14
 	if err != nil {
13 15
 		return nil, err
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"strings"
10 10
 	"testing"
11 11
 
12
+	"github.com/docker/docker/api/types/registry"
12 13
 	"github.com/docker/docker/errdefs"
13 14
 )
14 15
 
... ...
@@ -34,9 +35,9 @@ func TestPluginPush(t *testing.T) {
34 34
 			if req.Method != http.MethodPost {
35 35
 				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
36 36
 			}
37
-			auth := req.Header.Get("X-Registry-Auth")
37
+			auth := req.Header.Get(registry.AuthHeader)
38 38
 			if auth != "authtoken" {
39
-				return nil, fmt.Errorf("Invalid auth header : expected 'authtoken', got %s", auth)
39
+				return nil, fmt.Errorf("invalid auth header : expected 'authtoken', got %s", auth)
40 40
 			}
41 41
 			return &http.Response{
42 42
 				StatusCode: http.StatusOK,
... ...
@@ -7,6 +7,7 @@ import (
7 7
 
8 8
 	"github.com/docker/distribution/reference"
9 9
 	"github.com/docker/docker/api/types"
10
+	"github.com/docker/docker/api/types/registry"
10 11
 	"github.com/pkg/errors"
11 12
 )
12 13
 
... ...
@@ -34,6 +35,6 @@ func (cli *Client) PluginUpgrade(ctx context.Context, name string, options types
34 34
 }
35 35
 
36 36
 func (cli *Client) tryPluginUpgrade(ctx context.Context, query url.Values, privileges types.PluginPrivileges, name, registryAuth string) (serverResponse, error) {
37
-	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
37
+	headers := map[string][]string{registry.AuthHeader: {registryAuth}}
38 38
 	return cli.post(ctx, "/plugins/"+name+"/upgrade", query, privileges, headers)
39 39
 }
... ...
@@ -8,6 +8,7 @@ import (
8 8
 
9 9
 	"github.com/docker/distribution/reference"
10 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/api/types/registry"
11 12
 	"github.com/docker/docker/api/types/swarm"
12 13
 	"github.com/opencontainers/go-digest"
13 14
 	"github.com/pkg/errors"
... ...
@@ -21,7 +22,7 @@ func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec,
21 21
 	}
22 22
 
23 23
 	if options.EncodedRegistryAuth != "" {
24
-		headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
24
+		headers[registry.AuthHeader] = []string{options.EncodedRegistryAuth}
25 25
 	}
26 26
 
27 27
 	// Make sure containerSpec is not nil when no runtime is set or the runtime is set to container
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"net/url"
7 7
 
8 8
 	"github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/api/types/registry"
9 10
 	"github.com/docker/docker/api/types/swarm"
10 11
 )
11 12
 
... ...
@@ -23,7 +24,7 @@ func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, version
23 23
 	}
24 24
 
25 25
 	if options.EncodedRegistryAuth != "" {
26
-		headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
26
+		headers[registry.AuthHeader] = []string{options.EncodedRegistryAuth}
27 27
 	}
28 28
 
29 29
 	if options.RegistryAuthFrom != "" {