From 2b1138c118e587dc0c3ddeab792036e311f42c0f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 29 Sep 2020 17:01:55 +0200 Subject: [PATCH] Fix initializing client modifying custom HTTPHeaders When initializing the API client, the User-Agent was added to any custom HTTPHeaders that were configured. However, because the map was not properly dereferenced, the original map was modified, causing the User-Agent to also be saved to config.json after `docker login` and `docker logout`: Before this change; $ cat ~/.docker/config.json cat: can't open '/root/.docker/config.json': No such file or directory $ docker login -u myusername Password: ... Login Succeeded $ cat ~/.docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "" } }, "HttpHeaders": { "User-Agent": "Docker-Client/19.03.12 (linux)" } } $ docker logout { "auths": {}, "HttpHeaders": { "User-Agent": "Docker-Client/19.03.12 (linux)" } } After this change: $ cat ~/.docker/config.json cat: can't open '/root/.docker/config.json': No such file or directory $ docker login -u myusername Password: ... Login Succeeded $ cat ~/.docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "" } } } $ docker logout Removing login credentials for https://index.docker.io/v1/ $ cat ~/.docker/config.json { "auths": {} } Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 6 +++--- cli/command/cli_test.go | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index a9e7e30763..7b5048e98f 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -308,9 +308,9 @@ func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigF if err != nil { return nil, err } - customHeaders := configFile.HTTPHeaders - if customHeaders == nil { - customHeaders = map[string]string{} + customHeaders := make(map[string]string, len(configFile.HTTPHeaders)) + for k, v := range configFile.HTTPHeaders { + customHeaders[k] = v } customHeaders["User-Agent"] = UserAgent() clientOpts = append(clientOpts, client.WithHTTPHeaders(customHeaders)) diff --git a/cli/command/cli_test.go b/cli/command/cli_test.go index 32c999df75..83f42e9df0 100644 --- a/cli/command/cli_test.go +++ b/cli/command/cli_test.go @@ -45,6 +45,7 @@ func TestNewAPIClientFromFlags(t *testing.T) { } assert.Check(t, is.DeepEqual(expectedHeaders, apiclient.(*client.Client).CustomHTTPHeaders())) assert.Check(t, is.Equal(api.DefaultVersion, apiclient.ClientVersion())) + assert.DeepEqual(t, configFile.HTTPHeaders, map[string]string{"My-Header": "Custom-Value"}) } func TestNewAPIClientFromFlagsForDefaultSchema(t *testing.T) {