Skip to content

Commit 55b2abb

Browse files
authored
Merge pull request #3000 from thaJeztah/20.10_backport_old_config_deprecation_warning
[20.10 backport] config: print deprecation warning when falling back to ~/.dockercfg
2 parents e77605c + 4c3b87d commit 55b2abb

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

cli/config/config.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ const (
2424
)
2525

2626
var (
27-
initConfigDir sync.Once
27+
initConfigDir = new(sync.Once)
2828
configDir string
2929
homeDir string
3030
)
3131

32-
// resetHomeDir is used in testing to resets the "homeDir" package variable to
32+
// resetHomeDir is used in testing to reset the "homeDir" package variable to
3333
// force re-lookup of the home directory between tests.
3434
func resetHomeDir() {
3535
homeDir = ""
@@ -42,6 +42,13 @@ func getHomeDir() string {
4242
return homeDir
4343
}
4444

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+
4552
func setConfigDir() {
4653
if configDir != "" {
4754
return
@@ -97,10 +104,15 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
97104
return &configFile, err
98105
}
99106

107+
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
108+
var printLegacyFileWarning bool
109+
100110
// Load reads the configuration files in the given directory, and sets up
101111
// the auth config information and returns values.
102112
// FIXME: use the internal golang config parser
103113
func Load(configDir string) (*configfile.ConfigFile, error) {
114+
printLegacyFileWarning = false
115+
104116
if configDir == "" {
105117
configDir = Dir()
106118
}
@@ -125,6 +137,7 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
125137
// Can't find latest config file so check for the old one
126138
filename = filepath.Join(getHomeDir(), oldConfigfile)
127139
if file, err := os.Open(filename); err == nil {
140+
printLegacyFileWarning = true
128141
defer file.Close()
129142
if err := configFile.LegacyLoadFromReader(file); err != nil {
130143
return configFile, errors.Wrap(err, filename)
@@ -140,6 +153,9 @@ func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
140153
if err != nil {
141154
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
142155
}
156+
if printLegacyFileWarning {
157+
_, _ = fmt.Fprintln(stderr, "WARNING: Support for the legacy ~/.dockercfg configuration file and file-format is deprecated and will be removed in an upcoming release")
158+
}
143159
if !configFile.ContainsAuth() {
144160
configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore)
145161
}

cli/config/config_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212

1313
"github.com/docker/cli/cli/config/configfile"
1414
"github.com/docker/cli/cli/config/credentials"
15+
"github.com/docker/cli/cli/config/types"
1516
"gotest.tools/v3/assert"
1617
is "gotest.tools/v3/assert/cmp"
1718
"gotest.tools/v3/env"
19+
"gotest.tools/v3/fs"
1820
)
1921

2022
var homeKey = "HOME"
@@ -223,6 +225,31 @@ func TestOldJSON(t *testing.T) {
223225
}
224226
}
225227

228+
func TestOldJSONFallbackDeprecationWarning(t *testing.T) {
229+
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
230+
tmpHome := fs.NewDir(t, t.Name(), fs.WithFile(oldConfigfile, js))
231+
defer tmpHome.Remove()
232+
defer env.PatchAll(t, map[string]string{homeKey: tmpHome.Path(), "DOCKER_CONFIG": ""})()
233+
234+
// reset the homeDir, configDir, and its sync.Once, to force them being resolved again
235+
resetHomeDir()
236+
resetConfigDir()
237+
238+
buffer := new(bytes.Buffer)
239+
configFile := LoadDefaultConfigFile(buffer)
240+
expected := configfile.New(tmpHome.Join(configFileDir, ConfigFileName))
241+
expected.AuthConfigs = map[string]types.AuthConfig{
242+
"https://index.docker.io/v1/": {
243+
Username: "joejoe",
244+
Password: "hello",
245+
Email: "user@example.com",
246+
ServerAddress: "https://index.docker.io/v1/",
247+
},
248+
}
249+
assert.Assert(t, strings.Contains(buffer.String(), "WARNING: Support for the legacy ~/.dockercfg configuration file and file-format is deprecated and will be removed in an upcoming release"))
250+
assert.Check(t, is.DeepEqual(expected, configFile))
251+
}
252+
226253
func TestNewJSON(t *testing.T) {
227254
tmpHome, err := ioutil.TempDir("", "config-test")
228255
assert.NilError(t, err)

cmd/docker/docker_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
func TestClientDebugEnabled(t *testing.T) {
1818
defer debug.Disable()
1919

20-
tcmd := newDockerCommand(&command.DockerCli{})
20+
cli, err := command.NewDockerCli()
21+
assert.NilError(t, err)
22+
tcmd := newDockerCommand(cli)
2123
tcmd.SetFlag("debug", "true")
2224
cmd, _, err := tcmd.HandleGlobalFlags()
2325
assert.NilError(t, err)

0 commit comments

Comments
 (0)