Merge pull request #4281 from thaJeztah/remove_deprecated_config_warning

cli/config: remove warning for deprecated ~/.dockercfg file
This commit is contained in:
Sebastiaan van Stijn 2023-05-16 18:12:26 +01:00 committed by GitHub
commit 97795bb75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 96 deletions

View File

@ -19,49 +19,22 @@ const (
// ConfigFileName is the name of config file // ConfigFileName is the name of config file
ConfigFileName = "config.json" ConfigFileName = "config.json"
configFileDir = ".docker" configFileDir = ".docker"
oldConfigfile = ".dockercfg" // Deprecated: remove once we stop printing deprecation warning
contextsDir = "contexts" contextsDir = "contexts"
) )
var ( var (
initConfigDir = new(sync.Once) initConfigDir = new(sync.Once)
configDir string configDir string
homeDir string
) )
// resetHomeDir is used in testing to reset the "homeDir" package variable to
// force re-lookup of the home directory between tests.
func resetHomeDir() {
homeDir = ""
}
func getHomeDir() string {
if homeDir == "" {
homeDir = homedir.Get()
}
return homeDir
}
// resetConfigDir is used in testing to reset the "configDir" package variable
// and its sync.Once to force re-lookup between tests.
func resetConfigDir() {
configDir = ""
initConfigDir = new(sync.Once)
}
func setConfigDir() {
if configDir != "" {
return
}
configDir = os.Getenv("DOCKER_CONFIG")
if configDir == "" {
configDir = filepath.Join(getHomeDir(), configFileDir)
}
}
// Dir returns the directory the configuration file is stored in // Dir returns the directory the configuration file is stored in
func Dir() string { func Dir() string {
initConfigDir.Do(setConfigDir) initConfigDir.Do(func() {
configDir = os.Getenv("DOCKER_CONFIG")
if configDir == "" {
configDir = filepath.Join(homedir.Get(), configFileDir)
}
})
return configDir return configDir
} }
@ -96,55 +69,43 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
// Load reads the configuration files in the given directory, and sets up // Load reads the configuration files in the given directory, and sets up
// the auth config information and returns values. // the auth config information and returns values.
// FIXME: use the internal golang config parser
func Load(configDir string) (*configfile.ConfigFile, error) { func Load(configDir string) (*configfile.ConfigFile, error) {
cfg, _, err := load(configDir)
return cfg, err
}
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
// so we can remove the bool return value and collapse this back into `Load`
func load(configDir string) (*configfile.ConfigFile, bool, error) {
printLegacyFileWarning := false
if configDir == "" { if configDir == "" {
configDir = Dir() configDir = Dir()
} }
return load(configDir)
}
func load(configDir string) (*configfile.ConfigFile, error) {
filename := filepath.Join(configDir, ConfigFileName) filename := filepath.Join(configDir, ConfigFileName)
configFile := configfile.New(filename) configFile := configfile.New(filename)
// Try happy path first - latest config file file, err := os.Open(filename)
if file, err := os.Open(filename); err == nil { if err != nil {
if os.IsNotExist(err) {
//
// if file is there but we can't stat it for any reason other
// than it doesn't exist then stop
return configFile, nil
}
// if file is there but we can't stat it for any reason other
// than it doesn't exist then stop
return configFile, nil
}
defer file.Close() defer file.Close()
err = configFile.LoadFromReader(file) err = configFile.LoadFromReader(file)
if err != nil { if err != nil {
err = errors.Wrap(err, filename) err = errors.Wrap(err, filename)
} }
return configFile, printLegacyFileWarning, err return configFile, err
} else if !os.IsNotExist(err) {
// if file is there but we can't stat it for any reason other
// than it doesn't exist then stop
return configFile, printLegacyFileWarning, errors.Wrap(err, filename)
}
// Can't find latest config file so check for the old one
filename = filepath.Join(getHomeDir(), oldConfigfile)
if _, err := os.Stat(filename); err == nil {
printLegacyFileWarning = true
}
return configFile, printLegacyFileWarning, nil
} }
// LoadDefaultConfigFile attempts to load the default config file and returns // LoadDefaultConfigFile attempts to load the default config file and returns
// an initialized ConfigFile struct if none is found. // an initialized ConfigFile struct if none is found.
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile { func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
configFile, printLegacyFileWarning, err := load(Dir()) configFile, err := load(Dir())
if err != nil { if err != nil {
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err) _, _ = fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
}
if printLegacyFileWarning {
_, _ = fmt.Fprintln(stderr, "WARNING: Support for the legacy ~/.dockercfg configuration file and file-format has been removed and the configuration file will be ignored")
} }
if !configFile.ContainsAuth() { if !configFile.ContainsAuth() {
configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore) configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore)

View File

@ -5,27 +5,15 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials" "github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/cli/config/types"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"
) )
var homeKey = "HOME"
func init() {
if runtime.GOOS == "windows" {
homeKey = "USERPROFILE"
}
}
func setupConfigDir(t *testing.T) string { func setupConfigDir(t *testing.T) string {
tmpdir := t.TempDir() tmpdir := t.TempDir()
oldDir := Dir() oldDir := Dir()
@ -93,24 +81,6 @@ func TestEmptyJSON(t *testing.T) {
saveConfigAndValidateNewFormat(t, config, tmpHome) saveConfigAndValidateNewFormat(t, config, tmpHome)
} }
func TestOldJSONFallbackDeprecationWarning(t *testing.T) {
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
tmpHome := fs.NewDir(t, t.Name(), fs.WithFile(oldConfigfile, js))
defer tmpHome.Remove()
env.PatchAll(t, map[string]string{homeKey: tmpHome.Path(), "DOCKER_CONFIG": ""})
// reset the homeDir, configDir, and its sync.Once, to force them being resolved again
resetHomeDir()
resetConfigDir()
buffer := new(bytes.Buffer)
configFile := LoadDefaultConfigFile(buffer)
expected := configfile.New(tmpHome.Join(configFileDir, ConfigFileName))
expected.AuthConfigs = map[string]types.AuthConfig{}
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"))
assert.Check(t, is.DeepEqual(expected, configFile))
}
func TestNewJSON(t *testing.T) { func TestNewJSON(t *testing.T) {
tmpHome := t.TempDir() tmpHome := t.TempDir()

View File

@ -380,9 +380,6 @@ Given that the old file format encourages insecure storage of credentials
Docker v1.7.0 has created this file, support for this file, and its format has Docker v1.7.0 has created this file, support for this file, and its format has
been removed. been removed.
A warning is printed in situations where the CLI would fall back to the old file,
notifying the user that the legacy file is present, but ignored.
### Configuration options for experimental CLI features ### Configuration options for experimental CLI features
**Deprecated in Release: v19.03** **Deprecated in Release: v19.03**