@@ -2,37 +2,85 @@ package e2e
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
7
6
authv1 "k8s.io/api/authorization/v1"
8
7
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
8
"k8s.io/client-go/kubernetes"
10
- "k8s.io/client-go/tools/clientcmd"
11
9
)
12
10
13
- func createSubjectAccessReview (ctx context.Context , cs kubernetes.Interface , sar authv1.SubjectAccessReview ) (* authv1.SubjectAccessReview , error ) {
14
- return cs .AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , & sar , metav1.CreateOptions {})
11
+ // testCase is a struct that represents a single testcase.
12
+ type testCase struct {
13
+ data testcaseData
14
+ output testcaseOutput
15
+ }
16
+
17
+ // testcaseData is a struct that makes it user-friendly to write testcases
18
+ // more logically. This will be used to generate individual SubjectAccessReview
19
+ // objects in order to test the authorization rules.
20
+ type testcaseData struct {
21
+ namespaces []string
22
+ names []string
23
+ verbs []string
24
+ apiGroups []string
25
+ resources []string
26
+ subresources []string
27
+ nonResourceVerbs []string
28
+ nonResourcePaths []string
29
+ users []string
30
+
31
+ // this is double slice since we need to check individually for
32
+ // each group of users. A single slice would mean that the same user
33
+ // is part of all the groups.
34
+ groups [][]string
15
35
}
16
36
17
- func createLocalClient () (kubernetes.Interface , error ) {
18
- kubeconfigPath := "/workdir/test/e2e/kubeconfig"
37
+ // testcaseOutput is a struct that represents the expected result of a testcase.
38
+ // This is based on the SubjectAccessReviewStatus type. It provides simplicity in testcase
39
+ // writing since one testcase can have multiple SubjectAccessReview objects
40
+ // and we need to determine the "final" result and expose that as the testcase output.
41
+ type testcaseOutput struct {
42
+ // the final result based on results of individual SubjectAccessReview objects
43
+ allowed , denied bool
44
+ // the set of reasons from individual SubjectAccessReview objects
45
+ reason []string
46
+ }
19
47
20
- // use the current context in kubeconfig
21
- config , err := clientcmd .BuildConfigFromFlags ("" , kubeconfigPath )
48
+ func (t * testCase ) run (ctx context.Context , cs kubernetes.Interface ) error {
49
+ // Generate the list of SubjectAccessReview objects based on the testcase data
50
+ sars , err := generateSubjectAccessReviews (t .data )
22
51
if err != nil {
23
- return nil , err
52
+ return err
24
53
}
25
54
26
- client , err := kubernetes .NewForConfig (config )
55
+ // Create the SubjectAccessReview objects in the cluster
56
+ err = createSubjectAccessReviews (ctx , cs , sars )
27
57
if err != nil {
28
- return nil , fmt . Errorf ( "unable to create a client: %v" , err )
58
+ return err
29
59
}
30
60
31
- return client , nil
61
+ // TODO: Implement the logic to determine the final result based on the results of individual SubjectAccessReview objects
62
+ return nil
32
63
}
33
64
34
- type testCase struct {
35
- name string
36
- sar authv1.SubjectAccessReview
37
- expectedStatus authv1.SubjectAccessReviewStatus
65
+ // accessReviewGenerator generates a list of SubjectAccessReview objects based on the
66
+ // testcase data provided.
67
+ func generateSubjectAccessReviews (data testcaseData ) ([]authv1.SubjectAccessReview , error ) {
68
+ // TODO: Implement this function
69
+ return nil , nil
70
+ }
71
+
72
+ // createSubjectAccessReviews creates provided SubjectAccessReview objects in the cluster
73
+ func createSubjectAccessReviews (ctx context.Context , cs kubernetes.Interface , sars []authv1.SubjectAccessReview ) error {
74
+ for _ , sar := range sars {
75
+ _ , err := createSubjectAccessReview (ctx , cs , sar )
76
+ if err != nil {
77
+ return err
78
+ }
79
+ }
80
+ return nil
81
+ }
82
+
83
+ // createSubjectAccessReview creates a SubjectAccessReview object in the cluster
84
+ func createSubjectAccessReview (ctx context.Context , cs kubernetes.Interface , sar authv1.SubjectAccessReview ) (* authv1.SubjectAccessReview , error ) {
85
+ return cs .AuthorizationV1 ().SubjectAccessReviews ().Create (ctx , & sar , metav1.CreateOptions {})
38
86
}
0 commit comments