Skip to content

Commit 6bfbe46

Browse files
committed
add logic to expand into resourceAttributes and nonResourceAttributes from testCaseData
1 parent d3c1578 commit 6bfbe46

File tree

1 file changed

+159
-7
lines changed

1 file changed

+159
-7
lines changed

test/e2e/authorization_utils.go

Lines changed: 159 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ type testcaseOutput struct {
4747

4848
func (t *testCase) run(ctx context.Context, cs kubernetes.Interface) error {
4949
// Generate the list of SubjectAccessReview objects based on the testcase data
50-
sars, err := generateSubjectAccessReviews(t.data)
51-
if err != nil {
52-
return err
53-
}
50+
sars := t.generateSubjectAccessReviews()
5451

5552
// Create the SubjectAccessReview objects in the cluster
5653
createdSars, err := createSubjectAccessReviews(ctx, cs, sars)
@@ -67,9 +64,163 @@ func (t *testCase) run(ctx context.Context, cs kubernetes.Interface) error {
6764

6865
// accessReviewGenerator generates a list of SubjectAccessReview objects based on the
6966
// testcase data provided.
70-
func generateSubjectAccessReviews(data testcaseData) ([]authv1.SubjectAccessReview, error) {
71-
// TODO: Implement this function
72-
return nil, nil
67+
func (t *testCase) generateSubjectAccessReviews() []authv1.SubjectAccessReview {
68+
// Initialize the list of SubjectAccessReview objects
69+
sars := make([]authv1.SubjectAccessReview, 0)
70+
71+
// expand the testcase data to generate a list of ResourceAttributes
72+
resourceAttributes := t.expandResourceAttributes()
73+
74+
// expand the testcase data to generate a list of NonResourceAttributes
75+
// nonResourceAttributes := t.expandNonResourceAttributes()
76+
77+
// expand the testcase data to generate a list of SubjectAccessReview objects
78+
// based on the ResourceAttributes and NonResourceAttributes
79+
for _, ra := range resourceAttributes {
80+
for _, user := range t.data.users {
81+
for _, group := range t.data.groups {
82+
sar := authv1.SubjectAccessReview{
83+
Spec: authv1.SubjectAccessReviewSpec{
84+
ResourceAttributes: &ra,
85+
User: user,
86+
Groups: group,
87+
},
88+
}
89+
sars = append(sars, sar)
90+
}
91+
}
92+
}
93+
return sars
94+
}
95+
96+
// expandResourceAttributes expands the testcase data to generate a list of ResourceAttributes
97+
func (t *testCase) expandResourceAttributes() []authv1.ResourceAttributes {
98+
// This will hold the expanded ResourceAttributes
99+
ras := make([]authv1.ResourceAttributes, 0)
100+
101+
// TODO: Convert this logic in a function similar to the way it is implemented
102+
// today to avoid code duplication
103+
nsExpansions := make([]authv1.ResourceAttributes, 0)
104+
// expand on namespaces
105+
if len(t.data.namespaces) > 0 {
106+
for _, ns := range t.data.namespaces {
107+
ra := authv1.ResourceAttributes{
108+
Namespace: ns,
109+
}
110+
nsExpansions = append(nsExpansions, ra)
111+
}
112+
// we update the expanded list with namespace expansions
113+
ras = nsExpansions
114+
}
115+
116+
// expand on verbs
117+
verbExpansions := make([]authv1.ResourceAttributes, 0)
118+
if len(t.data.verbs) > 0 {
119+
for _, verb := range t.data.verbs {
120+
for _, ra := range ras {
121+
// copy the ResourceAttributes object to avoid modifying the original object
122+
// and make it safe to user in the next iterations
123+
copy := ra
124+
copy.Verb = verb
125+
verbExpansions = append(verbExpansions, copy)
126+
}
127+
}
128+
// we update the expanded list with verb expansions
129+
ras = verbExpansions
130+
}
131+
132+
// expand on apiGroups
133+
apiGroupExpansions := make([]authv1.ResourceAttributes, 0)
134+
if len(t.data.apiGroups) > 0 {
135+
for _, apiGroup := range t.data.apiGroups {
136+
for _, ra := range ras {
137+
copy := ra
138+
copy.Group = apiGroup
139+
apiGroupExpansions = append(apiGroupExpansions, copy)
140+
}
141+
}
142+
// we update the expanded list with apiGroup expansions
143+
ras = apiGroupExpansions
144+
}
145+
146+
// expand on resources
147+
resourceExpansions := make([]authv1.ResourceAttributes, 0)
148+
if len(t.data.resources) > 0 {
149+
for _, resource := range t.data.resources {
150+
for _, ra := range ras {
151+
copy := ra
152+
copy.Resource = resource
153+
resourceExpansions = append(resourceExpansions, copy)
154+
}
155+
}
156+
// we update the expanded list with resource expansions
157+
ras = resourceExpansions
158+
}
159+
160+
// expand on subresources
161+
subresourceExpansions := make([]authv1.ResourceAttributes, 0)
162+
if len(t.data.subresources) > 0 {
163+
for _, subresource := range t.data.subresources {
164+
for _, ra := range ras {
165+
copy := ra
166+
copy.Subresource = subresource
167+
subresourceExpansions = append(subresourceExpansions, copy)
168+
}
169+
}
170+
// we update the expanded list with subresource expansions
171+
ras = subresourceExpansions
172+
}
173+
174+
// expand on names
175+
nameExpansions := make([]authv1.ResourceAttributes, 0)
176+
if len(t.data.names) > 0 {
177+
for _, name := range t.data.names {
178+
for _, ra := range ras {
179+
copy := ra
180+
copy.Name = name
181+
nameExpansions = append(nameExpansions, copy)
182+
}
183+
}
184+
// we update the expanded list with name expansions
185+
ras = nameExpansions
186+
}
187+
188+
return ras
189+
}
190+
191+
// expandNonResourceAttributes expands the testcase data to generate a list of NonResourceAttributes
192+
func (t *testCase) expandNonResourceAttributes() []authv1.NonResourceAttributes {
193+
// This will hold the expanded NonResourceAttributes
194+
nras := make([]authv1.NonResourceAttributes, 0)
195+
196+
// expand on paths
197+
pathExpansions := make([]authv1.NonResourceAttributes, 0)
198+
if len(t.data.nonResourcePaths) > 0 {
199+
for _, path := range t.data.nonResourcePaths {
200+
nra := authv1.NonResourceAttributes{
201+
Path: path,
202+
}
203+
pathExpansions = append(pathExpansions, nra)
204+
}
205+
// we update the expanded list with path expansions
206+
nras = pathExpansions
207+
}
208+
209+
// expand on verbs
210+
verbExpansions := make([]authv1.NonResourceAttributes, 0)
211+
if len(t.data.nonResourceVerbs) > 0 {
212+
for _, verb := range t.data.nonResourceVerbs {
213+
for _, nra := range nras {
214+
copy := nra
215+
copy.Verb = verb
216+
verbExpansions = append(verbExpansions, copy)
217+
}
218+
}
219+
// we update the expanded list with verb expansions
220+
nras = verbExpansions
221+
}
222+
223+
return nras
73224
}
74225

75226
// createSubjectAccessReviews creates provided SubjectAccessReview objects in the cluster
@@ -91,6 +242,7 @@ func createSubjectAccessReview(ctx context.Context, cs kubernetes.Interface, sar
91242
return cs.AuthorizationV1().SubjectAccessReviews().Create(ctx, &sar, metav1.CreateOptions{})
92243
}
93244

245+
// evaluateOutput evaluates the output based on the created SubjectAccessReview objects
94246
func (t *testCase) evaluateOutput(createdSars []authv1.SubjectAccessReview) {
95247
tcOutput := testcaseOutput{}
96248

0 commit comments

Comments
 (0)