Skip to content

Commit 0c5eb94

Browse files
authored
Merge pull request #4412 from thaJeztah/auth_use_config
cli/command: ResolveAuthConfig, GetDefaultAuthConfig: take ConfigFile as arg
2 parents 69dcccf + 211220c commit 0c5eb94

15 files changed

Lines changed: 38 additions & 60 deletions

File tree

cli/command/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (cli *DockerCli) ManifestStore() manifeststore.Store {
189189
// registry
190190
func (cli *DockerCli) RegistryClient(allowInsecure bool) registryclient.RegistryClient {
191191
resolver := func(ctx context.Context, index *registry.IndexInfo) registry.AuthConfig {
192-
return ResolveAuthConfig(ctx, cli, index)
192+
return ResolveAuthConfig(cli.ConfigFile(), index)
193193
}
194194
return registryclient.NewRegistryClient(resolver, UserAgent(), allowInsecure)
195195
}

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio
114114

115115
// FIXME(thaJeztah): this is the only code-path that uses APIClient.ImageCreate. Rewrite this to use the regular "pull" code (or vice-versa).
116116
func pullImage(ctx context.Context, dockerCli command.Cli, image string, opts *createOptions) error {
117-
encodedAuth, err := command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
117+
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), image)
118118
if err != nil {
119119
return err
120120
}

cli/command/image/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func RunPush(dockerCli command.Cli, opts pushOptions) error {
7676
ctx := context.Background()
7777

7878
// Resolve the Auth config relevant for this server
79-
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
79+
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), repoInfo.Index)
8080
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
8181
if err != nil {
8282
return err

cli/command/image/trust.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,6 @@ func TagTrusted(ctx context.Context, cli command.Cli, trustedRef reference.Canon
339339
// AuthResolver returns an auth resolver function from a command.Cli
340340
func AuthResolver(cli command.Cli) func(ctx context.Context, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
341341
return func(ctx context.Context, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
342-
return command.ResolveAuthConfig(ctx, cli, index)
342+
return command.ResolveAuthConfig(cli.ConfigFile(), index)
343343
}
344344
}

cli/command/plugin/install.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,26 @@ func buildPullConfig(ctx context.Context, dockerCli command.Cli, opts pluginOpti
7979
return types.PluginInstallOptions{}, errors.Errorf("invalid name: %s", ref.String())
8080
}
8181

82-
trusted, err := image.TrustedReference(context.Background(), dockerCli, nt)
82+
trusted, err := image.TrustedReference(ctx, dockerCli, nt)
8383
if err != nil {
8484
return types.PluginInstallOptions{}, err
8585
}
8686
remote = reference.FamiliarString(trusted)
8787
}
8888

89-
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
89+
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), repoInfo.Index)
9090
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
9191
if err != nil {
9292
return types.PluginInstallOptions{}, err
9393
}
94-
registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, cmdName)
9594

9695
options := types.PluginInstallOptions{
9796
RegistryAuth: encodedAuth,
9897
RemoteRef: remote,
9998
Disabled: opts.disable,
10099
AcceptAllPermissions: opts.grantPerms,
101100
AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.remote),
102-
PrivilegeFunc: registryAuthFunc,
101+
PrivilegeFunc: command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, cmdName),
103102
Args: opts.args,
104103
}
105104
return options, nil

cli/command/plugin/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func runPush(dockerCli command.Cli, opts pushOptions) error {
5555
if err != nil {
5656
return err
5757
}
58-
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
58+
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), repoInfo.Index)
5959
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
6060
if err != nil {
6161
return err

cli/command/registry.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package command
22

33
import (
44
"bufio"
5-
"context"
65
"fmt"
76
"io"
87
"os"
98
"runtime"
109
"strings"
1110

11+
"github.com/docker/cli/cli/config/configfile"
1212
configtypes "github.com/docker/cli/cli/config/types"
1313
"github.com/docker/cli/cli/streams"
1414
"github.com/docker/distribution/reference"
@@ -26,7 +26,7 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
2626
fmt.Fprintf(cli.Out(), "\nPlease login prior to %s:\n", cmdName)
2727
indexServer := registry.GetAuthConfigKey(index)
2828
isDefaultRegistry := indexServer == registry.IndexServer
29-
authConfig, err := GetDefaultAuthConfig(cli, true, indexServer, isDefaultRegistry)
29+
authConfig, err := GetDefaultAuthConfig(cli.ConfigFile(), true, indexServer, isDefaultRegistry)
3030
if err != nil {
3131
fmt.Fprintf(cli.Err(), "Unable to retrieve stored credentials for %s, error: %s.\n", indexServer, err)
3232
}
@@ -44,26 +44,26 @@ func RegistryAuthenticationPrivilegedFunc(cli Cli, index *registrytypes.IndexInf
4444
//
4545
// It is similar to [registry.ResolveAuthConfig], but uses the credentials-
4646
// store, instead of looking up credentials from a map.
47-
func ResolveAuthConfig(_ context.Context, cli Cli, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
47+
func ResolveAuthConfig(cfg *configfile.ConfigFile, index *registrytypes.IndexInfo) registrytypes.AuthConfig {
4848
configKey := index.Name
4949
if index.Official {
5050
configKey = registry.IndexServer
5151
}
5252

53-
a, _ := cli.ConfigFile().GetAuthConfig(configKey)
53+
a, _ := cfg.GetAuthConfig(configKey)
5454
return registrytypes.AuthConfig(a)
5555
}
5656

5757
// GetDefaultAuthConfig gets the default auth config given a serverAddress
5858
// If credentials for given serverAddress exists in the credential store, the configuration will be populated with values in it
59-
func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, isDefaultRegistry bool) (registrytypes.AuthConfig, error) {
59+
func GetDefaultAuthConfig(cfg *configfile.ConfigFile, checkCredStore bool, serverAddress string, isDefaultRegistry bool) (registrytypes.AuthConfig, error) {
6060
if !isDefaultRegistry {
6161
serverAddress = registry.ConvertToHostname(serverAddress)
6262
}
6363
authconfig := configtypes.AuthConfig{}
6464
var err error
6565
if checkCredStore {
66-
authconfig, err = cli.ConfigFile().GetAuthConfig(serverAddress)
66+
authconfig, err = cfg.GetAuthConfig(serverAddress)
6767
if err != nil {
6868
return registrytypes.AuthConfig{
6969
ServerAddress: serverAddress,
@@ -72,8 +72,7 @@ func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, is
7272
}
7373
authconfig.ServerAddress = serverAddress
7474
authconfig.IdentityToken = ""
75-
res := registrytypes.AuthConfig(authconfig)
76-
return res, nil
75+
return registrytypes.AuthConfig(authconfig), nil
7776
}
7877

7978
// ConfigureAuth handles prompting of user's username and password if needed
@@ -172,9 +171,9 @@ func promptWithDefault(out io.Writer, prompt string, configDefault string) {
172171
//
173172
// For details on base64url encoding, see:
174173
// - RFC4648, section 5: https://tools.ietf.org/html/rfc4648#section-5
175-
func RetrieveAuthTokenFromImage(ctx context.Context, cli Cli, image string) (string, error) {
174+
func RetrieveAuthTokenFromImage(cfg *configfile.ConfigFile, image string) (string, error) {
176175
// Retrieve encoded auth token from the image reference
177-
authConfig, err := resolveAuthConfigFromImage(ctx, cli, image)
176+
authConfig, err := resolveAuthConfigFromImage(cfg, image)
178177
if err != nil {
179178
return "", err
180179
}
@@ -186,7 +185,7 @@ func RetrieveAuthTokenFromImage(ctx context.Context, cli Cli, image string) (str
186185
}
187186

188187
// resolveAuthConfigFromImage retrieves that AuthConfig using the image string
189-
func resolveAuthConfigFromImage(ctx context.Context, cli Cli, image string) (registrytypes.AuthConfig, error) {
188+
func resolveAuthConfigFromImage(cfg *configfile.ConfigFile, image string) (registrytypes.AuthConfig, error) {
190189
registryRef, err := reference.ParseNormalizedNamed(image)
191190
if err != nil {
192191
return registrytypes.AuthConfig{}, err
@@ -195,5 +194,5 @@ func resolveAuthConfigFromImage(ctx context.Context, cli Cli, image string) (reg
195194
if err != nil {
196195
return registrytypes.AuthConfig{}, err
197196
}
198-
return ResolveAuthConfig(ctx, cli, repoInfo.Index), nil
197+
return ResolveAuthConfig(cfg, repoInfo.Index), nil
199198
}

cli/command/registry/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint:gocyclo
117117
}
118118

119119
isDefaultRegistry := serverAddress == registry.IndexServer
120-
authConfig, err := command.GetDefaultAuthConfig(dockerCli, opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry)
120+
authConfig, err := command.GetDefaultAuthConfig(dockerCli.ConfigFile(), opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry)
121121
if err == nil && authConfig.Username != "" && authConfig.Password != "" {
122122
response, err = loginWithCredStoreCreds(ctx, dockerCli, &authConfig)
123123
}

cli/command/registry/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func runSearch(dockerCli command.Cli, options searchOptions) error {
5454
return err
5555
}
5656

57-
ctx := context.Background()
58-
authConfig := command.ResolveAuthConfig(ctx, dockerCli, indexInfo)
57+
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo)
5958
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
6059
if err != nil {
6160
return err
6261
}
6362

63+
ctx := context.Background()
6464
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, indexInfo, "search")
6565
results, err := dockerCli.Client().ImageSearch(ctx, options.term, types.ImageSearchOptions{
6666
RegistryAuth: encodedAuth,

cli/command/registry_test.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
package command_test
22

33
import (
4-
"bytes"
5-
"context"
64
"fmt"
75
"testing"
86

97
. "github.com/docker/cli/cli/command" // Prevents a circular import with "github.com/docker/cli/internal/test"
8+
"github.com/docker/cli/cli/config/configfile"
109
configtypes "github.com/docker/cli/cli/config/types"
11-
"github.com/docker/cli/internal/test"
1210
"github.com/docker/docker/api/types/registry"
13-
"github.com/docker/docker/api/types/system"
14-
"github.com/docker/docker/client"
1511
"gotest.tools/v3/assert"
1612
is "gotest.tools/v3/assert/cmp"
1713
)
1814

19-
type fakeClient struct {
20-
client.Client
21-
infoFunc func() (system.Info, error)
22-
}
23-
2415
var testAuthConfigs = []registry.AuthConfig{
2516
{
2617
ServerAddress: "https://index.docker.io/v1/",
@@ -34,13 +25,6 @@ var testAuthConfigs = []registry.AuthConfig{
3425
},
3526
}
3627

37-
func (cli *fakeClient) Info(_ context.Context) (system.Info, error) {
38-
if cli.infoFunc != nil {
39-
return cli.infoFunc()
40-
}
41-
return system.Info{}, nil
42-
}
43-
4428
func TestGetDefaultAuthConfig(t *testing.T) {
4529
testCases := []struct {
4630
checkCredStore bool
@@ -77,15 +61,13 @@ func TestGetDefaultAuthConfig(t *testing.T) {
7761
expectedAuthConfig: testAuthConfigs[1],
7862
},
7963
}
80-
cli := test.NewFakeCli(&fakeClient{})
81-
errBuf := new(bytes.Buffer)
82-
cli.SetErr(errBuf)
64+
cfg := configfile.New("filename")
8365
for _, authconfig := range testAuthConfigs {
84-
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
66+
cfg.GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
8567
}
8668
for _, tc := range testCases {
8769
serverAddress := tc.inputServerAddress
88-
authconfig, err := GetDefaultAuthConfig(cli, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
70+
authconfig, err := GetDefaultAuthConfig(cfg, tc.checkCredStore, serverAddress, serverAddress == "https://index.docker.io/v1/")
8971
if tc.expectedErr != "" {
9072
assert.Check(t, err != nil)
9173
assert.Check(t, is.Equal(tc.expectedErr, err.Error()))
@@ -97,15 +79,14 @@ func TestGetDefaultAuthConfig(t *testing.T) {
9779
}
9880

9981
func TestGetDefaultAuthConfig_HelperError(t *testing.T) {
100-
cli := test.NewFakeCli(&fakeClient{})
101-
errBuf := new(bytes.Buffer)
102-
cli.SetErr(errBuf)
103-
cli.ConfigFile().CredentialsStore = "fake-does-not-exist"
104-
serverAddress := "test-server-address"
82+
cfg := configfile.New("filename")
83+
cfg.CredentialsStore = "fake-does-not-exist"
84+
85+
const serverAddress = "test-server-address"
10586
expectedAuthConfig := registry.AuthConfig{
10687
ServerAddress: serverAddress,
10788
}
108-
authconfig, err := GetDefaultAuthConfig(cli, true, serverAddress, serverAddress == "https://index.docker.io/v1/")
89+
authconfig, err := GetDefaultAuthConfig(cfg, true, serverAddress, serverAddress == "https://index.docker.io/v1/")
10990
assert.Check(t, is.DeepEqual(expectedAuthConfig, authconfig))
11091
assert.Check(t, is.ErrorContains(err, "docker-credential-fake-does-not-exist"))
11192
}

0 commit comments

Comments
 (0)