From f0c8b4c7a995156849ea6b1d7bfbb472a7cb2daf Mon Sep 17 00:00:00 2001 From: Josh Black Date: Tue, 19 May 2020 16:13:05 -0700 Subject: [PATCH] Fix JSON encoding adding newlines. (#8928) Fix JSON encoding adding newlines. This manifested itself when encoding config values, which all map to strings. An extra new line would get added by json.Encode, which caused other things to break with confusing error messagges. Switching to json.Marshal seems to solve the problem. --- command/server/config.go | 4 +- command/server/config_test.go | 8 +++ command/server/config_test_helpers.go | 53 ++++++++++++++++++- command/server/test-fixtures/config4.hcl | 17 ++++++ command/server/test-fixtures/config4.hcl.json | 20 +++++++ 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 command/server/test-fixtures/config4.hcl create mode 100644 command/server/test-fixtures/config4.hcl.json diff --git a/command/server/config.go b/command/server/config.go index f757581d97..064fa57cd4 100644 --- a/command/server/config.go +++ b/command/server/config.go @@ -1,6 +1,7 @@ package server import ( + "encoding/json" "errors" "fmt" "io" @@ -16,7 +17,6 @@ import ( "github.com/hashicorp/hcl" "github.com/hashicorp/hcl/hcl/ast" "github.com/hashicorp/vault/internalshared/configutil" - "github.com/hashicorp/vault/sdk/helper/jsonutil" "github.com/hashicorp/vault/sdk/helper/parseutil" ) @@ -535,7 +535,7 @@ func ParseStorage(result *Config, list *ast.ObjectList, name string) error { m[key] = valStr continue } - valBytes, err := jsonutil.EncodeJSON(val) + valBytes, err := json.Marshal(val) if err != nil { return err } diff --git a/command/server/config_test.go b/command/server/config_test.go index e287a1a125..6f4694701d 100644 --- a/command/server/config_test.go +++ b/command/server/config_test.go @@ -22,6 +22,14 @@ func TestLoadConfigFile_json2(t *testing.T) { testLoadConfigFile_json2(t, nil) } +func TestLoadConfigFileIntegerAndBooleanValues(t *testing.T) { + testLoadConfigFileIntegerAndBooleanValues(t) +} + +func TestLoadConfigFileIntegerAndBooleanValuesJson(t *testing.T) { + testLoadConfigFileIntegerAndBooleanValuesJson(t) +} + func TestLoadConfigDir(t *testing.T) { testLoadConfigDir(t) } diff --git a/command/server/config_test_helpers.go b/command/server/config_test_helpers.go index 67605dd158..4ee3eeeda7 100644 --- a/command/server/config_test_helpers.go +++ b/command/server/config_test_helpers.go @@ -17,7 +17,7 @@ func testConfigRaftRetryJoin(t *testing.T) { if err != nil { t.Fatal(err) } - retryJoinConfig := `[{"leader_api_addr":"http://127.0.0.1:8200"},{"leader_api_addr":"http://127.0.0.2:8200"},{"leader_api_addr":"http://127.0.0.3:8200"}]` + "\n" + retryJoinConfig := `[{"leader_api_addr":"http://127.0.0.1:8200"},{"leader_api_addr":"http://127.0.0.2:8200"},{"leader_api_addr":"http://127.0.0.3:8200"}]` expected := &Config{ SharedConfig: &configutil.SharedConfig{ Listeners: []*configutil.Listener{ @@ -294,6 +294,57 @@ func testParseEntropy(t *testing.T, oss bool) { } } +func testLoadConfigFileIntegerAndBooleanValues(t *testing.T) { + testLoadConfigFileIntegerAndBooleanValuesCommon(t, "./test-fixtures/config4.hcl") +} + +func testLoadConfigFileIntegerAndBooleanValuesJson(t *testing.T) { + testLoadConfigFileIntegerAndBooleanValuesCommon(t, "./test-fixtures/config4.hcl.json") +} + +func testLoadConfigFileIntegerAndBooleanValuesCommon(t *testing.T, path string) { + config, err := LoadConfigFile(path) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := &Config{ + SharedConfig: &configutil.SharedConfig{ + Listeners: []*configutil.Listener{ + { + Type: "tcp", + Address: "127.0.0.1:8200", + }, + }, + DisableMlock: true, + }, + + Storage: &Storage{ + Type: "raft", + Config: map[string]string{ + "path": "/storage/path/raft", + "node_id": "raft1", + "performance_multiplier": "1", + "foo": "bar", + "baz": "true", + }, + ClusterAddr: "127.0.0.1:8201", + }, + + ClusterAddr: "127.0.0.1:8201", + + DisableCache: true, + DisableCacheRaw: true, + EnableUI: true, + EnableUIRaw: true, + } + + config.Listeners[0].RawConfig = nil + if diff := deep.Equal(config, expected); diff != nil { + t.Fatal(diff) + } +} + func testLoadConfigFile(t *testing.T) { config, err := LoadConfigFile("./test-fixtures/config.hcl") if err != nil { diff --git a/command/server/test-fixtures/config4.hcl b/command/server/test-fixtures/config4.hcl new file mode 100644 index 0000000000..b620f3c7e7 --- /dev/null +++ b/command/server/test-fixtures/config4.hcl @@ -0,0 +1,17 @@ +disable_cache = true +disable_mlock = true +ui = true + +listener "tcp" { + address = "127.0.0.1:8200" +} + +storage "raft" { + path = "/storage/path/raft" + node_id = "raft1" + performance_multiplier = 1 + foo = "bar" + baz = true +} + +cluster_addr = "127.0.0.1:8201" diff --git a/command/server/test-fixtures/config4.hcl.json b/command/server/test-fixtures/config4.hcl.json new file mode 100644 index 0000000000..e0ca7e8137 --- /dev/null +++ b/command/server/test-fixtures/config4.hcl.json @@ -0,0 +1,20 @@ +{ + "disable_cache": true, + "disable_mlock": true, + "ui":true, + "listener": [{ + "tcp": { + "address": "127.0.0.1:8200" + } + }], + "storage": { + "raft": { + "path": "/storage/path/raft", + "node_id": "raft1", + "performance_multiplier": 1, + "foo": "bar", + "baz": true + } + }, + "cluster_addr": "127.0.0.1:8201" +}