Skip to content

Commit 379122b

Browse files
committed
cli/config: remove warning for deprecated ~/.dockercfg file
The `~/.dockercfg` file was replaced by `~/.docker/config.json` in 2015 (github.com/docker/docker/commit/18c9b6c6455f116ae59cde8544413b3d7d294a5e). Commit b83bc67 (v23.0.0, but backported to v20.10) added a warning if no "current" config file was found but a legacy file was, and if the CLI would fall back to using the deprecated file. Commit ee218fa removed support for the legacy file, but kept a warning in place if a legacy file was in place, and now ignored. This patch removes the warning as well, fully deprecating the legacy `~/.dockercfg` file. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 9b791b4 commit 379122b

3 files changed

Lines changed: 24 additions & 95 deletions

File tree

cli/config/config.go

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,22 @@ const (
1919
// ConfigFileName is the name of config file
2020
ConfigFileName = "config.json"
2121
configFileDir = ".docker"
22-
oldConfigfile = ".dockercfg" // Deprecated: remove once we stop printing deprecation warning
2322
contextsDir = "contexts"
2423
)
2524

2625
var (
2726
initConfigDir = new(sync.Once)
2827
configDir string
29-
homeDir string
3028
)
3129

32-
// resetHomeDir is used in testing to reset the "homeDir" package variable to
33-
// force re-lookup of the home directory between tests.
34-
func resetHomeDir() {
35-
homeDir = ""
36-
}
37-
38-
func getHomeDir() string {
39-
if homeDir == "" {
40-
homeDir = homedir.Get()
41-
}
42-
return homeDir
43-
}
44-
45-
// resetConfigDir is used in testing to reset the "configDir" package variable
46-
// and its sync.Once to force re-lookup between tests.
47-
func resetConfigDir() {
48-
configDir = ""
49-
initConfigDir = new(sync.Once)
50-
}
51-
52-
func setConfigDir() {
53-
if configDir != "" {
54-
return
55-
}
56-
configDir = os.Getenv("DOCKER_CONFIG")
57-
if configDir == "" {
58-
configDir = filepath.Join(getHomeDir(), configFileDir)
59-
}
60-
}
61-
6230
// Dir returns the directory the configuration file is stored in
6331
func Dir() string {
64-
initConfigDir.Do(setConfigDir)
32+
initConfigDir.Do(func() {
33+
configDir = os.Getenv("DOCKER_CONFIG")
34+
if configDir == "" {
35+
configDir = filepath.Join(homedir.Get(), configFileDir)
36+
}
37+
})
6538
return configDir
6639
}
6740

@@ -97,53 +70,42 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
9770
// Load reads the configuration files in the given directory, and sets up
9871
// the auth config information and returns values.
9972
func Load(configDir string) (*configfile.ConfigFile, error) {
100-
cfg, _, err := load(configDir)
101-
return cfg, err
102-
}
103-
104-
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
105-
// so we can remove the bool return value and collapse this back into `Load`
106-
func load(configDir string) (*configfile.ConfigFile, bool, error) {
107-
printLegacyFileWarning := false
108-
10973
if configDir == "" {
11074
configDir = Dir()
11175
}
76+
return load(configDir)
77+
}
11278

79+
func load(configDir string) (*configfile.ConfigFile, error) {
11380
filename := filepath.Join(configDir, ConfigFileName)
11481
configFile := configfile.New(filename)
11582

116-
// Try happy path first - latest config file
117-
if file, err := os.Open(filename); err == nil {
118-
defer file.Close()
119-
err = configFile.LoadFromReader(file)
120-
if err != nil {
121-
err = errors.Wrap(err, filename)
83+
file, err := os.Open(filename)
84+
if err != nil {
85+
if os.IsNotExist(err) {
86+
//
87+
// if file is there but we can't stat it for any reason other
88+
// than it doesn't exist then stop
89+
return configFile, nil
12290
}
123-
return configFile, printLegacyFileWarning, err
124-
} else if !os.IsNotExist(err) {
12591
// if file is there but we can't stat it for any reason other
12692
// than it doesn't exist then stop
127-
return configFile, printLegacyFileWarning, errors.Wrap(err, filename)
93+
return configFile, nil
12894
}
129-
130-
// Can't find latest config file so check for the old one
131-
filename = filepath.Join(getHomeDir(), oldConfigfile)
132-
if _, err := os.Stat(filename); err == nil {
133-
printLegacyFileWarning = true
95+
defer file.Close()
96+
err = configFile.LoadFromReader(file)
97+
if err != nil {
98+
err = errors.Wrap(err, filename)
13499
}
135-
return configFile, printLegacyFileWarning, nil
100+
return configFile, err
136101
}
137102

138103
// LoadDefaultConfigFile attempts to load the default config file and returns
139104
// an initialized ConfigFile struct if none is found.
140105
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
141-
configFile, printLegacyFileWarning, err := load(Dir())
106+
configFile, err := load(Dir())
142107
if err != nil {
143-
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
144-
}
145-
if printLegacyFileWarning {
146-
_, _ = fmt.Fprintln(stderr, "WARNING: Support for the legacy ~/.dockercfg configuration file and file-format has been removed and the configuration file will be ignored")
108+
_, _ = fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
147109
}
148110
if !configFile.ContainsAuth() {
149111
configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore)

cli/config/config_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,15 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"runtime"
98
"strings"
109
"testing"
1110

1211
"github.com/docker/cli/cli/config/configfile"
1312
"github.com/docker/cli/cli/config/credentials"
14-
"github.com/docker/cli/cli/config/types"
1513
"gotest.tools/v3/assert"
1614
is "gotest.tools/v3/assert/cmp"
17-
"gotest.tools/v3/env"
18-
"gotest.tools/v3/fs"
1915
)
2016

21-
var homeKey = "HOME"
22-
23-
func init() {
24-
if runtime.GOOS == "windows" {
25-
homeKey = "USERPROFILE"
26-
}
27-
}
28-
2917
func setupConfigDir(t *testing.T) string {
3018
tmpdir := t.TempDir()
3119
oldDir := Dir()
@@ -93,24 +81,6 @@ func TestEmptyJSON(t *testing.T) {
9381
saveConfigAndValidateNewFormat(t, config, tmpHome)
9482
}
9583

96-
func TestOldJSONFallbackDeprecationWarning(t *testing.T) {
97-
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
98-
tmpHome := fs.NewDir(t, t.Name(), fs.WithFile(oldConfigfile, js))
99-
defer tmpHome.Remove()
100-
env.PatchAll(t, map[string]string{homeKey: tmpHome.Path(), "DOCKER_CONFIG": ""})
101-
102-
// reset the homeDir, configDir, and its sync.Once, to force them being resolved again
103-
resetHomeDir()
104-
resetConfigDir()
105-
106-
buffer := new(bytes.Buffer)
107-
configFile := LoadDefaultConfigFile(buffer)
108-
expected := configfile.New(tmpHome.Join(configFileDir, ConfigFileName))
109-
expected.AuthConfigs = map[string]types.AuthConfig{}
110-
assert.Assert(t, strings.Contains(buffer.String(), "WARNING: Support for the legacy ~/.dockercfg configuration file and file-format has been removed and the configuration file will be ignored"))
111-
assert.Check(t, is.DeepEqual(expected, configFile))
112-
}
113-
11484
func TestNewJSON(t *testing.T) {
11585
tmpHome := t.TempDir()
11686

docs/deprecated.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,6 @@ Given that the old file format encourages insecure storage of credentials
380380
Docker v1.7.0 has created this file, support for this file, and its format has
381381
been removed.
382382

383-
A warning is printed in situations where the CLI would fall back to the old file,
384-
notifying the user that the legacy file is present, but ignored.
385-
386383
### Configuration options for experimental CLI features
387384

388385
**Deprecated in Release: v19.03**

0 commit comments

Comments
 (0)