Skip to content

Commit 5720804

Browse files
pebrccharith-elastic
authored andcommitted
Avoid pollution of map constants via defensive copy (#3599) (#3600)
1 parent 0c9ef00 commit 5720804

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

pkg/controller/elasticsearch/user/roles.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,14 @@ func (r RolesFileContent) FileBytes() ([]byte, error) {
192192
return yaml.Marshal(&r)
193193
}
194194

195-
// mergeWith merges multiple rolesFileContent, giving priority to other.
195+
// mergeWith merges multiple rolesFileContent, giving priority to other returning a new RolesFileContent.
196196
func (r RolesFileContent) MergeWith(other RolesFileContent) RolesFileContent {
197+
result := make(RolesFileContent)
198+
for roleName, roleSpec := range r {
199+
result[roleName] = roleSpec
200+
}
197201
for roleName, roleSpec := range other {
198-
r[roleName] = roleSpec
202+
result[roleName] = roleSpec
199203
}
200-
return r
204+
return result
201205
}

pkg/controller/elasticsearch/user/roles_test.go

+102
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package user
66

77
import (
8+
"reflect"
89
"testing"
910

1011
"github.com/ghodss/yaml"
@@ -43,3 +44,104 @@ click_admins:
4344
// the initial yaml and the serialized/de-serialized rolesFileContent should be the same
4445
require.Equal(t, r, rAgain)
4546
}
47+
48+
func TestRolesFileContent_MergeWith(t *testing.T) {
49+
type args struct {
50+
other RolesFileContent
51+
}
52+
tests := []struct {
53+
name string
54+
r RolesFileContent
55+
args args
56+
want RolesFileContent
57+
assert func(r, other, result RolesFileContent)
58+
}{
59+
60+
{
61+
name: "when r is nil",
62+
r: nil,
63+
args: args{
64+
other: RolesFileContent(map[string]interface{}{"a": "c"}),
65+
},
66+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
67+
},
68+
{
69+
name: "when other is nil",
70+
r: RolesFileContent(map[string]interface{}{"a": "c"}),
71+
args: args{
72+
other: nil,
73+
},
74+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
75+
},
76+
{
77+
name: "when both are nil",
78+
r: nil,
79+
args: args{
80+
other: nil,
81+
},
82+
want: RolesFileContent(map[string]interface{}{}),
83+
},
84+
{
85+
name: "when r is empty",
86+
r: RolesFileContent(map[string]interface{}{}),
87+
args: args{
88+
other: RolesFileContent(map[string]interface{}{"a": "c"}),
89+
},
90+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
91+
},
92+
{
93+
name: "when other is empty",
94+
r: RolesFileContent(map[string]interface{}{"a": "c"}),
95+
args: args{
96+
other: RolesFileContent(map[string]interface{}{}),
97+
},
98+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
99+
},
100+
{
101+
name: "when r has more items",
102+
r: RolesFileContent(map[string]interface{}{"a": "b", "d": "e"}),
103+
args: args{
104+
other: RolesFileContent(map[string]interface{}{"a": "c"}),
105+
},
106+
want: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}),
107+
},
108+
{
109+
name: "when other has more items",
110+
r: RolesFileContent(map[string]interface{}{"a": "b"}),
111+
args: args{
112+
other: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}),
113+
},
114+
want: RolesFileContent(map[string]interface{}{"a": "c", "d": "e"}),
115+
},
116+
{
117+
name: "does give priority to other",
118+
r: RolesFileContent(map[string]interface{}{"a": "b"}),
119+
args: args{
120+
other: RolesFileContent(map[string]interface{}{"a": "c"}),
121+
},
122+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
123+
},
124+
{
125+
name: "does not mutate in place",
126+
r: RolesFileContent(map[string]interface{}{"a": "b"}),
127+
args: args{
128+
other: RolesFileContent(map[string]interface{}{"a": "c"}),
129+
},
130+
want: RolesFileContent(map[string]interface{}{"a": "c"}),
131+
assert: func(r, other, result RolesFileContent) {
132+
require.Equal(t, r, RolesFileContent(map[string]interface{}{"a": "b"}))
133+
},
134+
},
135+
}
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
got := tt.r.MergeWith(tt.args.other)
139+
if !reflect.DeepEqual(got, tt.want) {
140+
t.Errorf("MergeWith() = %v, want %v", got, tt.want)
141+
}
142+
if tt.assert != nil {
143+
tt.assert(tt.r, tt.args.other, got)
144+
}
145+
})
146+
}
147+
}

0 commit comments

Comments
 (0)