diff --git a/stores/json/store.go b/stores/json/store.go index da1cc34ff1..3744563bca 100644 --- a/stores/json/store.go +++ b/stores/json/store.go @@ -250,8 +250,10 @@ func (store Store) treeBranchFromJSON(in []byte) (sops.TreeBranch, error) { func (store Store) reindentJSON(in []byte) ([]byte, error) { var out bytes.Buffer indent := "\t" - if store.config.Indent != -1 { + if store.config.Indent > -1 { indent = strings.Repeat(" ", store.config.Indent) + } else if store.config.Indent < -1 { + return nil, errors.New("JSON Indentation parameter smaller than -1 is not accepted") } err := json.Indent(&out, in, "", indent) return out.Bytes(), err diff --git a/stores/yaml/store.go b/stores/yaml/store.go index aae9614746..4d036f366b 100644 --- a/stores/yaml/store.go +++ b/stores/yaml/store.go @@ -2,6 +2,7 @@ package yaml //import "github.com/getsops/sops/v3/stores/yaml" import ( "bytes" + "errors" "fmt" "io" "strings" @@ -326,11 +327,13 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) { return branches, nil } -func (store *Store) getIndentation() int { - if store.config.Indent != 0 { - return store.config.Indent +func (store *Store) getIndentation() (int, error) { + if store.config.Indent > 0 { + return store.config.Indent, nil + } else if store.config.Indent < 0 { + return 0, errors.New("YAML Negative indentation not accepted") } - return IndentDefault + return IndentDefault, nil } // EmitEncryptedFile returns the encrypted bytes of the yaml file corresponding to a @@ -338,7 +341,11 @@ func (store *Store) getIndentation() int { func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) { var b bytes.Buffer e := yaml.NewEncoder(io.Writer(&b)) - e.SetIndent(store.getIndentation()) + indent, err := store.getIndentation() + if err != nil { + return nil, err + } + e.SetIndent(indent) for _, branch := range in.Branches { // Document root var doc = yaml.Node{} @@ -370,7 +377,11 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) { func (store *Store) EmitPlainFile(branches sops.TreeBranches) ([]byte, error) { var b bytes.Buffer e := yaml.NewEncoder(io.Writer(&b)) - e.SetIndent(store.getIndentation()) + indent, err := store.getIndentation() + if err != nil { + return nil, err + } + e.SetIndent(indent) for _, branch := range branches { // Document root var doc = yaml.Node{}