Skip to content

Commit 799c7a2

Browse files
authored
Merge pull request #1452 from reindlt/ini-store-duplicate-sections
add duplicate section support to ini store
2 parents 49fbb0d + 424416c commit 799c7a2

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

stores/ini/store.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func NewStore(c *config.INIStoreConfig) *Store {
2424
}
2525

2626
func (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {
27-
iniFile := ini.Empty()
27+
iniFile := ini.Empty(ini.LoadOptions{AllowNonUniqueSections: true})
28+
iniFile.DeleteSection(ini.DefaultSection)
2829
for _, branch := range branches {
2930
for _, item := range branch {
3031
if _, ok := item.Key.(sops.Comment); ok {
@@ -95,7 +96,7 @@ func (store Store) iniFromTreeBranches(branches sops.TreeBranches) ([]byte, erro
9596
}
9697

9798
func (store Store) treeBranchesFromIni(in []byte) (sops.TreeBranches, error) {
98-
iniFile, err := ini.Load(in)
99+
iniFile, err := ini.LoadSources(ini.LoadOptions{AllowNonUniqueSections: true}, in)
99100
if err != nil {
100101
return nil, err
101102
}
@@ -143,7 +144,7 @@ func (store Store) treeItemFromSection(section *ini.Section) (sops.TreeItem, err
143144

144145
// LoadEncryptedFile loads encrypted INI file's bytes onto a sops.Tree runtime object
145146
func (store *Store) LoadEncryptedFile(in []byte) (sops.Tree, error) {
146-
iniFileOuter, err := ini.Load(in)
147+
iniFileOuter, err := ini.LoadSources(ini.LoadOptions{AllowNonUniqueSections: true}, in)
147148
if err != nil {
148149
return sops.Tree{}, err
149150
}

stores/ini/store_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package ini
33
import (
44
"testing"
55

6-
"github.com/stretchr/testify/assert"
76
"github.com/getsops/sops/v3"
7+
"github.com/stretchr/testify/assert"
88
)
99

1010
func TestDecodeIni(t *testing.T) {
@@ -127,6 +127,55 @@ func TestEncodeIniWithEscaping(t *testing.T) {
127127
assert.Equal(t, expected, branches)
128128
}
129129

130+
func TestEncodeIniWithDuplicateSections(t *testing.T) {
131+
branches := sops.TreeBranches{
132+
sops.TreeBranch{
133+
sops.TreeItem{
134+
Key: "DEFAULT",
135+
Value: interface{}(sops.TreeBranch(nil)),
136+
},
137+
sops.TreeItem{
138+
Key: "foo",
139+
Value: sops.TreeBranch{
140+
sops.TreeItem{
141+
Key: "foo",
142+
Value: "bar",
143+
},
144+
sops.TreeItem{
145+
Key: "baz",
146+
Value: "3.0",
147+
},
148+
sops.TreeItem{
149+
Key: "qux",
150+
Value: "false",
151+
},
152+
},
153+
},
154+
sops.TreeItem{
155+
Key: "foo",
156+
Value: sops.TreeBranch{
157+
sops.TreeItem{
158+
Key: "foo",
159+
Value: "bar",
160+
},
161+
sops.TreeItem{
162+
Key: "baz",
163+
Value: "3.0",
164+
},
165+
sops.TreeItem{
166+
Key: "qux",
167+
Value: "false",
168+
},
169+
},
170+
},
171+
},
172+
}
173+
out, err := Store{}.iniFromTreeBranches(branches)
174+
assert.Nil(t, err)
175+
expected, _ := Store{}.treeBranchesFromIni(out)
176+
assert.Equal(t, expected, branches)
177+
}
178+
130179
func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) {
131180
data := []byte(`hello=2`)
132181
store := Store{}

0 commit comments

Comments
 (0)