1
0
mirror of https://github.com/getsops/sops.git synced 2026-02-05 12:45:21 +01:00

Handle escaping in original json (#357)

* Handle escaping in original json

* Replace conditional magic with proper json encoding call for key

* swap TestDecodeJSONWithEscaping with new TestEncodeJSONWithEscaping

* fix copy/paste typo
This commit is contained in:
AJ Bahnken
2018-06-01 12:47:27 -07:00
committed by GitHub
parent 5e8d1390eb
commit 3265a66cd3
2 changed files with 26 additions and 1 deletions

View File

@@ -168,7 +168,11 @@ func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("Error encoding value %s: %s", v, err)
}
out += `"` + item.Key.(string) + `": ` + string(v)
k, err := json.Marshal(item.Key.(string))
if err != nil {
return nil, fmt.Errorf("Error encoding key %s: %s", k, err)
}
out += string(k) + `: ` + string(v)
if i != len(tree)-1 {
out += ","
}

View File

@@ -219,6 +219,27 @@ func TestEncodeSimpleJSON(t *testing.T) {
assert.Equal(t, expected, branch)
}
func TestEncodeJSONWithEscaping(t *testing.T) {
branch := sops.TreeBranch{
sops.TreeItem{
Key: "foo\\bar",
Value: "value",
},
sops.TreeItem{
Key: "a_key_with\"quotes\"",
Value: 4.0,
},
sops.TreeItem{
Key: "baz\\\\foo",
Value: 2.0,
},
}
out, err := Store{}.jsonFromTreeBranch(branch)
assert.Nil(t, err)
expected, _ := Store{}.treeBranchFromJSON(out)
assert.Equal(t, expected, branch)
}
func TestEncodeJSONArrayOfObjects(t *testing.T) {
branch := sops.TreeBranch{
sops.TreeItem{