cli: ensure 'acl auth-method update' doesn't deep merge the Config field (#7839)

This commit is contained in:
R.B. Boyer 2020-05-11 14:21:17 -05:00 committed by GitHub
parent 97bca3476e
commit c54211ad52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 4 deletions

View File

@ -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"`
}

View File

@ -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

View File

@ -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)
})