Skip to content

Commit 372bb56

Browse files
committed
cli/command: replace EncodeAuthToBase64 for registry.EncodeAuthConfig
Replace uses of this function in favor of the implementation in the API types, so that we have a single, canonical implementation. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 534bfc2 commit 372bb56

8 files changed

Lines changed: 24 additions & 23 deletions

File tree

cli/command/container/create.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/distribution/reference"
1717
"github.com/docker/docker/api/types"
1818
"github.com/docker/docker/api/types/container"
19+
registrytypes "github.com/docker/docker/api/types/registry"
1920
"github.com/docker/docker/api/types/versions"
2021
apiclient "github.com/docker/docker/client"
2122
"github.com/docker/docker/pkg/jsonmessage"
@@ -125,7 +126,7 @@ func pullImage(ctx context.Context, dockerCli command.Cli, image string, platfor
125126
}
126127

127128
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
128-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
129+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
129130
if err != nil {
130131
return err
131132
}

cli/command/image/push.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/cli/cli/streams"
1212
"github.com/docker/distribution/reference"
1313
"github.com/docker/docker/api/types"
14+
registrytypes "github.com/docker/docker/api/types/registry"
1415
"github.com/docker/docker/pkg/jsonmessage"
1516
"github.com/docker/docker/registry"
1617
"github.com/pkg/errors"
@@ -76,7 +77,7 @@ func RunPush(dockerCli command.Cli, opts pushOptions) error {
7677

7778
// Resolve the Auth config relevant for this server
7879
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
79-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
80+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
8081
if err != nil {
8182
return err
8283
}

cli/command/image/trust.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth)
264264
func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
265265
ref := reference.FamiliarString(imgRefAndAuth.Reference())
266266

267-
encodedAuth, err := command.EncodeAuthToBase64(*imgRefAndAuth.AuthConfig())
267+
encodedAuth, err := registrytypes.EncodeAuthConfig(*imgRefAndAuth.AuthConfig())
268268
if err != nil {
269269
return err
270270
}

cli/command/plugin/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/docker/cli/cli/command/image"
1111
"github.com/docker/distribution/reference"
1212
"github.com/docker/docker/api/types"
13+
registrytypes "github.com/docker/docker/api/types/registry"
1314
"github.com/docker/docker/pkg/jsonmessage"
1415
"github.com/docker/docker/registry"
1516
"github.com/pkg/errors"
@@ -86,8 +87,7 @@ func buildPullConfig(ctx context.Context, dockerCli command.Cli, opts pluginOpti
8687
}
8788

8889
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
89-
90-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
90+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
9191
if err != nil {
9292
return types.PluginInstallOptions{}, err
9393
}

cli/command/plugin/push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/docker/cli/cli/command"
88
"github.com/docker/cli/cli/command/image"
99
"github.com/docker/distribution/reference"
10+
registrytypes "github.com/docker/docker/api/types/registry"
1011
"github.com/docker/docker/pkg/jsonmessage"
1112
"github.com/docker/docker/registry"
1213
"github.com/pkg/errors"
@@ -55,8 +56,7 @@ func runPush(dockerCli command.Cli, opts pushOptions) error {
5556
return err
5657
}
5758
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
58-
59-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
59+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
6060
if err != nil {
6161
return err
6262
}

cli/command/registry.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package command
33
import (
44
"bufio"
55
"context"
6-
"encoding/base64"
7-
"encoding/json"
86
"fmt"
97
"io"
108
"os"
@@ -21,13 +19,9 @@ import (
2119
"github.com/pkg/errors"
2220
)
2321

24-
// EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload
22+
// EncodeAuthToBase64 serializes the auth configuration as JSON base64 payload.
2523
func EncodeAuthToBase64(authConfig registrytypes.AuthConfig) (string, error) {
26-
buf, err := json.Marshal(authConfig)
27-
if err != nil {
28-
return "", err
29-
}
30-
return base64.URLEncoding.EncodeToString(buf), nil
24+
return registrytypes.EncodeAuthConfig(authConfig)
3125
}
3226

3327
// RegistryAuthenticationPrivilegedFunc returns a RequestPrivilegeFunc from the specified registry index info
@@ -45,7 +39,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4539
if err != nil {
4640
return "", err
4741
}
48-
return EncodeAuthToBase64(authConfig)
42+
return registrytypes.EncodeAuthConfig(authConfig)
4943
}
5044
}
5145

@@ -177,14 +171,19 @@ func promptWithDefault(out io.Writer, prompt string, configDefault string) {
177171
}
178172
}
179173

180-
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete image
174+
// RetrieveAuthTokenFromImage retrieves an encoded auth token given a complete
175+
// image. The auth configuration is serialized as a base64url encoded RFC4648,
176+
// section 5) JSON string for sending through the X-Registry-Auth header.
177+
//
178+
// For details on base64url encoding, see:
179+
// - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
181180
func RetrieveAuthTokenFromImage(ctx context.Context, cli Cli, image string) (string, error) {
182181
// Retrieve encoded auth token from the image reference
183182
authConfig, err := resolveAuthConfigFromImage(ctx, cli, image)
184183
if err != nil {
185184
return "", err
186185
}
187-
encodedAuth, err := EncodeAuthToBase64(authConfig)
186+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
188187
if err != nil {
189188
return "", err
190189
}

cli/command/registry/search.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/cli/cli/command/formatter"
99
"github.com/docker/cli/opts"
1010
"github.com/docker/docker/api/types"
11+
registrytypes "github.com/docker/docker/api/types/registry"
1112
"github.com/docker/docker/registry"
1213
"github.com/spf13/cobra"
1314
)
@@ -54,15 +55,13 @@ func runSearch(dockerCli command.Cli, options searchOptions) error {
5455
}
5556

5657
ctx := context.Background()
57-
5858
authConfig := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
59-
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
60-
61-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
59+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
6260
if err != nil {
6361
return err
6462
}
6563

64+
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
6665
results, err := dockerCli.Client().ImageSearch(ctx, options.term, types.ImageSearchOptions{
6766
RegistryAuth: encodedAuth,
6867
PrivilegeFunc: requestPrivilege,

cli/command/trust/sign.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/cli/cli/command/image"
1414
"github.com/docker/cli/cli/trust"
1515
"github.com/docker/docker/api/types"
16+
registrytypes "github.com/docker/docker/api/types/registry"
1617
"github.com/pkg/errors"
1718
"github.com/spf13/cobra"
1819
"github.com/theupdateframework/notary/client"
@@ -93,7 +94,7 @@ func runSignImage(cli command.Cli, options signOptions) error {
9394
fmt.Fprintf(cli.Err(), "Signing and pushing trust data for local image %s, may overwrite remote trust data\n", imageName)
9495

9596
authConfig := command.ResolveAuthConfig(ctx, cli, imgRefAndAuth.RepoInfo().Index)
96-
encodedAuth, err := command.EncodeAuthToBase64(authConfig)
97+
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
9798
if err != nil {
9899
return err
99100
}

0 commit comments

Comments
 (0)