Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/command/registry/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func loginWithDeviceCodeFlow(ctx context.Context, dockerCLI command.Cli) (msg st
}

func storeCredentials(cfg *configfile.ConfigFile, authConfig registrytypes.AuthConfig) error {
authConfig.ServerAddress = strings.ToLower(authConfig.ServerAddress)
creds := cfg.GetCredentialsStore(authConfig.ServerAddress)
if err := creds.Store(configtypes.AuthConfig{
Username: authConfig.Username,
Expand Down
22 changes: 22 additions & 0 deletions cli/command/registry/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ func TestRunLogin(t *testing.T) {
},
},
},
{
doc: "mixed-case server address updates lowercase credential key",
priorCredentials: map[string]configtypes.AuthConfig{
"myregistry.example.com": {
Username: "my-username",
Password: "old-password",
ServerAddress: "myregistry.example.com",
},
},
input: loginOptions{
serverAddress: "MyRegistry.Example.com",
user: "my-username",
password: "new-password",
},
expectedCredentials: map[string]configtypes.AuthConfig{
"myregistry.example.com": {
Username: "my-username",
Password: "new-password",
ServerAddress: "myregistry.example.com",
},
},
},
{
doc: "unknown user w/ prior credentials",
priorCredentials: map[string]configtypes.AuthConfig{
Expand Down
5 changes: 5 additions & 0 deletions cli/command/registry/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"context"
"fmt"
"strings"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -60,6 +61,10 @@ func runLogout(ctx context.Context, dockerCLI command.Cli, serverAddress string)
// the tries below are kept for backward compatibility where a user could have
// saved the registry in one of the following format.
regsToLogout = append(regsToLogout, hostnameAddress, "http://"+hostnameAddress, "https://"+hostnameAddress)
// Also try lowercase variants for backward compatibility with case-insensitive
// login normalization in getAuthConfigKey and storeCredentials.
lowercaseAddress := strings.ToLower(hostnameAddress)
regsToLogout = append(regsToLogout, lowercaseAddress, "http://"+lowercaseAddress, "https://"+lowercaseAddress)
}

if isDefaultRegistry {
Expand Down
48 changes: 48 additions & 0 deletions cli/command/registry/logout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package registry

import (
"context"
"path/filepath"
"testing"

"github.com/docker/cli/cli/config/configfile"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
)

func TestRunLogoutMixedCaseServerAddress(t *testing.T) {
cfg := configfile.New(filepath.Join(t.TempDir(), "config.json"))
cli := test.NewFakeCli(nil)
cli.SetConfigFile(cfg)

const serverAddress = "myregistry.example.com"
assert.NilError(t, cfg.GetCredentialsStore(serverAddress).Store(configtypes.AuthConfig{
Username: "my-username",
Password: "my-password",
ServerAddress: serverAddress,
}))

assert.NilError(t, runLogout(context.Background(), cli, "MyRegistry.Example.com"))
credentials, err := cfg.GetAllCredentials()
assert.NilError(t, err)
assert.DeepEqual(t, credentials, map[string]configtypes.AuthConfig{})
}

func TestRunLogoutUpperCaseServerAddress(t *testing.T) {
cfg := configfile.New(filepath.Join(t.TempDir(), "config.json"))
cli := test.NewFakeCli(nil)
cli.SetConfigFile(cfg)

const serverAddress = "MYREGISTRY.EXAMPLE.COM"
assert.NilError(t, cfg.GetCredentialsStore(serverAddress).Store(configtypes.AuthConfig{
Username: "my-username",
Password: "my-password",
ServerAddress: serverAddress,
}))

assert.NilError(t, runLogout(context.Background(), cli, serverAddress))
credentials, err := cfg.GetAllCredentials()
assert.NilError(t, err)
assert.DeepEqual(t, credentials, map[string]configtypes.AuthConfig{})
}
7 changes: 6 additions & 1 deletion cli/command/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func TestRetrieveAuthTokenFromImage(t *testing.T) {
"localhost": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="},
"localhost:5000": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="},
"registry-1.docker.io": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="},
"registry.hub.docker.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}
"registry.hub.docker.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="},
"registry.example.com": {"auth": "dXNlcm5hbWU6cGFzc3dvcmQ="}
}
}`
cfg := configfile.ConfigFile{}
Expand Down Expand Up @@ -157,6 +158,10 @@ func TestRetrieveAuthTokenFromImage(t *testing.T) {
prefix: "registry.hub.docker.com",
expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry.hub.docker.com"},
},
{
prefix: "Registry.Example.com",
expectedAuthCfg: registry.AuthConfig{Username: "username", Password: "password", ServerAddress: "registry.example.com"},
},
{
prefix: "[::1]",
expectedAddress: "[::1]",
Expand Down
1 change: 1 addition & 0 deletions cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const authConfigKey = "https://index.docker.io/v1/"
//
// [registry.GetAuthConfigKey]: https://pkg.go.dev/github.com/docker/docker@v28.5.1+incompatible/registry#GetAuthConfigKey
func getAuthConfigKey(domainName string) string {
domainName = strings.ToLower(domainName)
if domainName == "docker.io" || domainName == "index.docker.io" {
return authConfigKey
}
Expand Down
14 changes: 14 additions & 0 deletions cli/config/configfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"gotest.tools/v3/golden"
)

func TestGetAuthConfigKey(t *testing.T) {
tests := map[string]string{
"MyRegistry.Example.com": "myregistry.example.com",
"DOCKER.IO": authConfigKey,
"Index.Docker.IO": authConfigKey,
}

for domainName, expected := range tests {
t.Run(domainName, func(t *testing.T) {
assert.Equal(t, getAuthConfigKey(domainName), expected)
})
}
}

func TestEncodeAuth(t *testing.T) {
newAuthConfig := &types.AuthConfig{Username: "ken", Password: "test"}
authStr := encodeAuth(newAuthConfig)
Expand Down