Skip to content

Commit 97795bb

Browse files
authored
Merge pull request #4281 from thaJeztah/remove_deprecated_config_warning
cli/config: remove warning for deprecated ~/.dockercfg file
2 parents 0f78de7 + 379122b commit 97795bb

3 files changed

Lines changed: 24 additions & 96 deletions

File tree

cli/config/config.go

Lines changed: 24 additions & 63 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

@@ -96,55 +69,43 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
9669

9770
// Load reads the configuration files in the given directory, and sets up
9871
// the auth config information and returns values.
99-
// FIXME: use the internal golang config parser
10072
func Load(configDir string) (*configfile.ConfigFile, error) {
101-
cfg, _, err := load(configDir)
102-
return cfg, err
103-
}
104-
105-
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
106-
// so we can remove the bool return value and collapse this back into `Load`
107-
func load(configDir string) (*configfile.ConfigFile, bool, error) {
108-
printLegacyFileWarning := false
109-
11073
if configDir == "" {
11174
configDir = Dir()
11275
}
76+
return load(configDir)
77+
}
11378

79+
func load(configDir string) (*configfile.ConfigFile, error) {
11480
filename := filepath.Join(configDir, ConfigFileName)
11581
configFile := configfile.New(filename)
11682

117-
// Try happy path first - latest config file
118-
if file, err := os.Open(filename); err == nil {
119-
defer file.Close()
120-
err = configFile.LoadFromReader(file)
121-
if err != nil {
122-
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
12390
}
124-
return configFile, printLegacyFileWarning, err
125-
} else if !os.IsNotExist(err) {
12691
// if file is there but we can't stat it for any reason other
12792
// than it doesn't exist then stop
128-
return configFile, printLegacyFileWarning, errors.Wrap(err, filename)
93+
return configFile, nil
12994
}
130-
131-
// Can't find latest config file so check for the old one
132-
filename = filepath.Join(getHomeDir(), oldConfigfile)
133-
if _, err := os.Stat(filename); err == nil {
134-
printLegacyFileWarning = true
95+
defer file.Close()
96+
err = configFile.LoadFromReader(file)
97+
if err != nil {
98+
err = errors.Wrap(err, filename)
13599
}
136-
return configFile, printLegacyFileWarning, nil
100+
return configFile, err
137101
}
138102

139103
// LoadDefaultConfigFile attempts to load the default config file and returns
140104
// an initialized ConfigFile struct if none is found.
141105
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
142-
configFile, printLegacyFileWarning, err := load(Dir())
106+
configFile, err := load(Dir())
143107
if err != nil {
144-
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
145-
}
146-
if printLegacyFileWarning {
147-
_, _ = 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)
148109
}
149110
if !configFile.ContainsAuth() {
150111
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)