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.
This commit is contained in:
Josh Black 2020-05-19 16:13:05 -07:00 committed by GitHub
parent 72afe55906
commit f0c8b4c7a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 3 deletions

View file

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

View file

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

View file

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

View file

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

View file

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