Skip to content

Commit a53f942

Browse files
committed
fix bug in resource attribute expansion
1 parent b845d03 commit a53f942

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

test/e2e/authorization_utils.go

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,22 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
118118
verbExpansions := make([]authv1.ResourceAttributes, 0)
119119
if len(t.data.verbs) > 0 {
120120
for _, verb := range t.data.verbs {
121-
for _, ra := range ras {
122-
// copy the ResourceAttributes object to avoid modifying the original object
123-
// and make it safe to user in the next iterations
124-
copy := ra
125-
copy.Verb = verb
126-
verbExpansions = append(verbExpansions, copy)
121+
// If an expansion already take place, we need to copy and
122+
// change the existing objects
123+
if len(ras) > 0 {
124+
for _, ra := range ras {
125+
// copy the ResourceAttributes object to avoid modifying the original object
126+
// and make it safe to user in the next iterations
127+
copy := ra
128+
copy.Verb = verb
129+
verbExpansions = append(verbExpansions, copy)
130+
}
131+
} else {
132+
// If no expansion has taken place, we need to create a new object
133+
ra := authv1.ResourceAttributes{
134+
Verb: verb,
135+
}
136+
verbExpansions = append(verbExpansions, ra)
127137
}
128138
}
129139
// we update the expanded list with verb expansions
@@ -134,10 +144,17 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
134144
apiGroupExpansions := make([]authv1.ResourceAttributes, 0)
135145
if len(t.data.apiGroups) > 0 {
136146
for _, apiGroup := range t.data.apiGroups {
137-
for _, ra := range ras {
138-
copy := ra
139-
copy.Group = apiGroup
140-
apiGroupExpansions = append(apiGroupExpansions, copy)
147+
if len(ras) > 0 {
148+
for _, ra := range ras {
149+
copy := ra
150+
copy.Group = apiGroup
151+
apiGroupExpansions = append(apiGroupExpansions, copy)
152+
}
153+
} else {
154+
ra := authv1.ResourceAttributes{
155+
Group: apiGroup,
156+
}
157+
apiGroupExpansions = append(apiGroupExpansions, ra)
141158
}
142159
}
143160
// we update the expanded list with apiGroup expansions
@@ -148,24 +165,44 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
148165
resourceExpansions := make([]authv1.ResourceAttributes, 0)
149166
if len(t.data.resources) > 0 {
150167
for _, resource := range t.data.resources {
151-
for _, ra := range ras {
152-
copy := ra
168+
if len(ras) > 0 {
169+
for _, ra := range ras {
170+
copy := ra
171+
// split the resource string to get the group, resource and subresource
172+
parts := strings.Split(resource, "/")
173+
if len(parts) > 1 {
174+
switch len(parts) {
175+
case 2:
176+
copy.Group = parts[0]
177+
copy.Resource = parts[1]
178+
case 3:
179+
copy.Group = parts[0]
180+
copy.Resource = parts[1]
181+
copy.Subresource = parts[2]
182+
}
183+
} else {
184+
copy.Resource = parts[0]
185+
}
186+
resourceExpansions = append(resourceExpansions, copy)
187+
}
188+
} else {
189+
ra := authv1.ResourceAttributes{}
153190
// split the resource string to get the group, resource and subresource
154191
parts := strings.Split(resource, "/")
155192
if len(parts) > 1 {
156193
switch len(parts) {
157194
case 2:
158-
copy.Group = parts[0]
159-
copy.Resource = parts[1]
195+
ra.Group = parts[0]
196+
ra.Resource = parts[1]
160197
case 3:
161-
copy.Group = parts[0]
162-
copy.Resource = parts[1]
163-
copy.Subresource = parts[2]
198+
ra.Group = parts[0]
199+
ra.Resource = parts[1]
200+
ra.Subresource = parts[2]
164201
}
165202
} else {
166-
copy.Resource = parts[0]
203+
ra.Resource = parts[0]
167204
}
168-
resourceExpansions = append(resourceExpansions, copy)
205+
resourceExpansions = append(resourceExpansions, ra)
169206
}
170207
}
171208
// we update the expanded list with resource expansions
@@ -176,10 +213,17 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
176213
subresourceExpansions := make([]authv1.ResourceAttributes, 0)
177214
if len(t.data.subresources) > 0 {
178215
for _, subresource := range t.data.subresources {
179-
for _, ra := range ras {
180-
copy := ra
181-
copy.Subresource = subresource
182-
subresourceExpansions = append(subresourceExpansions, copy)
216+
if len(ras) > 0 {
217+
for _, ra := range ras {
218+
copy := ra
219+
copy.Subresource = subresource
220+
subresourceExpansions = append(subresourceExpansions, copy)
221+
}
222+
} else {
223+
ra := authv1.ResourceAttributes{
224+
Subresource: subresource,
225+
}
226+
subresourceExpansions = append(subresourceExpansions, ra)
183227
}
184228
}
185229
// we update the expanded list with subresource expansions
@@ -190,10 +234,17 @@ func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
190234
nameExpansions := make([]authv1.ResourceAttributes, 0)
191235
if len(t.data.names) > 0 {
192236
for _, name := range t.data.names {
193-
for _, ra := range ras {
194-
copy := ra
195-
copy.Name = name
196-
nameExpansions = append(nameExpansions, copy)
237+
if len(ras) > 0 {
238+
for _, ra := range ras {
239+
copy := ra
240+
copy.Name = name
241+
nameExpansions = append(nameExpansions, copy)
242+
}
243+
} else {
244+
ra := authv1.ResourceAttributes{
245+
Name: name,
246+
}
247+
nameExpansions = append(nameExpansions, ra)
197248
}
198249
}
199250
// we update the expanded list with name expansions

0 commit comments

Comments
 (0)