From c54211ad529c1f02727dd94916e2101f4039125d Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Mon, 11 May 2020 14:21:17 -0500 Subject: [PATCH] cli: ensure 'acl auth-method update' doesn't deep merge the Config field (#7839) --- agent/consul/authmethod/testauth/testing.go | 3 +- .../authmethod/update/authmethod_update.go | 2 + .../update/authmethod_update_test.go | 43 +++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/agent/consul/authmethod/testauth/testing.go b/agent/consul/authmethod/testauth/testing.go index ff0df4467..9101baaa9 100644 --- a/agent/consul/authmethod/testauth/testing.go +++ b/agent/consul/authmethod/testauth/testing.go @@ -81,7 +81,8 @@ func GetSessionToken(sessionID string, token string) (map[string]string, bool) { } type Config struct { - SessionID string // unique identifier for this set of tokens in the database + SessionID string // unique identifier for this set of tokens in the database + Data map[string]string `json:",omitempty"` // random data for testing enterpriseConfig `mapstructure:",squash"` } diff --git a/command/acl/authmethod/update/authmethod_update.go b/command/acl/authmethod/update/authmethod_update.go index dcb2bf588..c29d696c9 100644 --- a/command/acl/authmethod/update/authmethod_update.go +++ b/command/acl/authmethod/update/authmethod_update.go @@ -253,6 +253,8 @@ func (c *cmd) Run(args []string) int { c.UI.Error(fmt.Sprintf("Error loading configuration file: %v", err)) return 1 } + // Don't attempt a deep merge. + method.Config = make(map[string]interface{}) if err := json.Unmarshal([]byte(data), &method.Config); err != nil { c.UI.Error(fmt.Sprintf("Error parsing JSON for auth method config: %v", err)) return 1 diff --git a/command/acl/authmethod/update/authmethod_update_test.go b/command/acl/authmethod/update/authmethod_update_test.go index 7edffc746..0beb95ce3 100644 --- a/command/acl/authmethod/update/authmethod_update_test.go +++ b/command/acl/authmethod/update/authmethod_update_test.go @@ -100,15 +100,17 @@ func TestAuthMethodUpdateCommand(t *testing.T) { return methodName } - t.Run("update all fields", func(t *testing.T) { - name := createAuthMethod(t) + finalName := createAuthMethod(t) + t.Run("update all fields", func(t *testing.T) { + name := finalName args := []string{ "-http-addr=" + a.HTTPAddr(), "-token=root", "-name=" + name, "-display-name", "updated display", "-description", "updated description", + "-config", `{ "SessionID": "foo" }`, } ui := cli.NewMockUi() @@ -124,7 +126,42 @@ func TestAuthMethodUpdateCommand(t *testing.T) { Type: "testing", DisplayName: "updated display", Description: "updated description", - Config: map[string]interface{}{}, + Config: map[string]interface{}{ + "SessionID": "foo", + }, + } + require.Equal(t, expect, got) + }) + + t.Run("update config field and prove no merging happens", func(t *testing.T) { + name := finalName + args := []string{ + "-http-addr=" + a.HTTPAddr(), + "-token=root", + "-name=" + name, + "-display-name", "updated display", + "-description", "updated description", + "-config", `{ "Data": { "foo": "bar"} }`, + } + + ui := cli.NewMockUi() + cmd := New(ui) + + code := cmd.Run(args) + require.Equal(t, code, 0) + require.Empty(t, ui.ErrorWriter.String()) + + got := getTestMethod(t, client, name) + expect := &api.ACLAuthMethod{ + Name: name, + Type: "testing", + DisplayName: "updated display", + Description: "updated description", + Config: map[string]interface{}{ + "Data": map[string]interface{}{ + "foo": "bar", + }, + }, } require.Equal(t, expect, got) })