From 37e9cabf1158977f2ab3528a31140cc21723ff3a Mon Sep 17 00:00:00 2001 From: Jon Johnson Date: Wed, 2 Oct 2019 12:41:50 -0700 Subject: [PATCH 1/3] Allow username/password in config file Signed-off-by: Jon Johnson --- cli/config/configfile/file.go | 8 +++++--- cli/config/configfile/file_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cli/config/configfile/file.go b/cli/config/configfile/file.go index 67ae655b0b..388a5d54d6 100644 --- a/cli/config/configfile/file.go +++ b/cli/config/configfile/file.go @@ -123,9 +123,11 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error { } var err error for addr, ac := range configFile.AuthConfigs { - ac.Username, ac.Password, err = decodeAuth(ac.Auth) - if err != nil { - return err + if ac.Auth != "" { + ac.Username, ac.Password, err = decodeAuth(ac.Auth) + if err != nil { + return err + } } ac.Auth = "" ac.ServerAddress = addr diff --git a/cli/config/configfile/file_test.go b/cli/config/configfile/file_test.go index dc00572677..dc4831ea3a 100644 --- a/cli/config/configfile/file_test.go +++ b/cli/config/configfile/file_test.go @@ -2,6 +2,7 @@ package configfile import ( "bytes" + "encoding/json" "io/ioutil" "os" "testing" @@ -380,6 +381,33 @@ func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) { assert.Check(t, is.Equal(0, testCredHelper.(*mockNativeStore).GetAllCallCount)) } +func TestLoadFromReaderWithUsernamePassword(t *testing.T) { + configFile := New("test-load") + defer os.Remove("test-load") + + want := types.AuthConfig{ + Username: "user", + Password: "pass", + } + cf := ConfigFile{ + AuthConfigs: map[string]types.AuthConfig{ + "example.com/foo": want, + }, + } + + b, err := json.Marshal(cf) + assert.NilError(t, err) + + err = configFile.LoadFromReader(bytes.NewReader(b)) + assert.NilError(t, err) + + got, err := configFile.GetAuthConfig("example.com/foo") + assert.NilError(t, err) + + assert.Check(t, is.DeepEqual(want.Username, got.Username)) + assert.Check(t, is.DeepEqual(want.Password, got.Password)) +} + func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) { testCases := []struct { name string From 415f608620df6e3160dfa67000994a4252f95b20 Mon Sep 17 00:00:00 2001 From: Jon Johnson Date: Wed, 2 Oct 2019 12:41:50 -0700 Subject: [PATCH 2/3] Add test case to cover non-empty auth entry Signed-off-by: Jon Johnson --- cli/config/configfile/file_test.go | 38 ++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/cli/config/configfile/file_test.go b/cli/config/configfile/file_test.go index dc4831ea3a..e072d261ef 100644 --- a/cli/config/configfile/file_test.go +++ b/cli/config/configfile/file_test.go @@ -389,23 +389,31 @@ func TestLoadFromReaderWithUsernamePassword(t *testing.T) { Username: "user", Password: "pass", } - cf := ConfigFile{ - AuthConfigs: map[string]types.AuthConfig{ - "example.com/foo": want, + + for _, tc := range []types.AuthConfig{ + want, + types.AuthConfig{ + Auth: encodeAuth(&want), }, + } { + cf := ConfigFile{ + AuthConfigs: map[string]types.AuthConfig{ + "example.com/foo": tc, + }, + } + + b, err := json.Marshal(cf) + assert.NilError(t, err) + + err = configFile.LoadFromReader(bytes.NewReader(b)) + assert.NilError(t, err) + + got, err := configFile.GetAuthConfig("example.com/foo") + assert.NilError(t, err) + + assert.Check(t, is.DeepEqual(want.Username, got.Username)) + assert.Check(t, is.DeepEqual(want.Password, got.Password)) } - - b, err := json.Marshal(cf) - assert.NilError(t, err) - - err = configFile.LoadFromReader(bytes.NewReader(b)) - assert.NilError(t, err) - - got, err := configFile.GetAuthConfig("example.com/foo") - assert.NilError(t, err) - - assert.Check(t, is.DeepEqual(want.Username, got.Username)) - assert.Check(t, is.DeepEqual(want.Password, got.Password)) } func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) { From 8f11fbc87622702f8644cc385191bb95d4d19143 Mon Sep 17 00:00:00 2001 From: Jon Johnson Date: Wed, 16 Oct 2019 09:59:49 -0700 Subject: [PATCH 3/3] Fix lint issue Signed-off-by: Jon Johnson --- cli/config/configfile/file_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/config/configfile/file_test.go b/cli/config/configfile/file_test.go index e072d261ef..83574bf85c 100644 --- a/cli/config/configfile/file_test.go +++ b/cli/config/configfile/file_test.go @@ -392,7 +392,7 @@ func TestLoadFromReaderWithUsernamePassword(t *testing.T) { for _, tc := range []types.AuthConfig{ want, - types.AuthConfig{ + { Auth: encodeAuth(&want), }, } {