Skip to content

Commit

Permalink
Improve handling of errors when binary store handles bad data.
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
felixfontein committed Sep 16, 2023
1 parent a750d72 commit 4e756d7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
14 changes: 12 additions & 2 deletions stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,25 @@ func (store BinaryStore) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
return store.store.EmitEncryptedFile(in)
}

const NOT_BINARY_HINT = ("This is likely not an encrypted binary file?" +
" If not, use --output-type to select the correct output type.")

// EmitPlainFile produces plaintext json file's bytes from its corresponding sops.TreeBranches object
func (store BinaryStore) EmitPlainFile(in sops.TreeBranches) ([]byte, error) {
if len(in) != 1 {
return nil, fmt.Errorf("There must be exactly one tree branch. " + NOT_BINARY_HINT)
}
// JSON stores a single object per file
for _, item := range in[0] {
if item.Key == "data" {
return []byte(item.Value.(string)), nil
if value, ok := item.Value.(string); ok {
return []byte(value), nil
} else {
return nil, fmt.Errorf("'data' key in tree does not have a string value. " + NOT_BINARY_HINT)
}
}
}
return nil, fmt.Errorf("No binary data found in tree")
return nil, fmt.Errorf("No binary data found in tree. " + NOT_BINARY_HINT)
}

// EmitValue extracts a value from a generic interface{} object representing a structured set
Expand Down
71 changes: 70 additions & 1 deletion stores/json/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package json
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/getsops/sops/v3"
"github.com/stretchr/testify/assert"
)

func TestDecodeJSON(t *testing.T) {
Expand Down Expand Up @@ -320,6 +320,75 @@ func TestLoadJSONFormattedBinaryFile(t *testing.T) {
assert.Equal(t, "data", branches[0][0].Key)
}

func TestEmitBinaryFile(t *testing.T) {
store := BinaryStore{}
data, err := store.EmitPlainFile(sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "data",
Value: "foo",
},
},
})
assert.Nil(t, err)
assert.Equal(t, []byte("foo"), data)
}

func TestEmitBinaryFileWrongBranches(t *testing.T) {
store := BinaryStore{}
data, err := store.EmitPlainFile(sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "data",
Value: "bar",
},
},
sops.TreeBranch{
sops.TreeItem{
Key: "data",
Value: "bar",
},
},
})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "There must be exactly one tree branch")

data, err = store.EmitPlainFile(sops.TreeBranches{})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "There must be exactly one tree branch")
}

func TestEmitBinaryFileNoData(t *testing.T) {
store := BinaryStore{}
data, err := store.EmitPlainFile(sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: "bar",
},
},
})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "No binary data found in tree")
}

func TestEmitBinaryFileWrongDataType(t *testing.T) {
store := BinaryStore{}
data, err := store.EmitPlainFile(sops.TreeBranches{
sops.TreeBranch{
sops.TreeItem{
Key: "data",
Value: sops.TreeItem{
Key: "foo",
Value: "bar",
},
},
},
})
assert.Nil(t, data)
assert.Contains(t, err.Error(), "'data' key in tree does not have a string value")
}

func TestEmitValueString(t *testing.T) {
bytes, err := (&Store{}).EmitValue("hello")
assert.Nil(t, err)
Expand Down

0 comments on commit 4e756d7

Please sign in to comment.