@@ -3,24 +3,22 @@ package permissions
3
3
import (
4
4
"testing"
5
5
6
- "github.com/d4l3k/messagediff"
7
6
"github.com/jetstack/preflight/pkg/agent"
8
7
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
8
+ "github.com/maxatome/go-testdeep/td"
9
9
rbac "k8s.io/api/rbac/v1"
10
10
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
11
"k8s.io/apimachinery/pkg/runtime/schema"
12
12
)
13
13
14
- func TestGenerateRBAC (t * testing.T ) {
15
- // Use these test cases to check if Generate function is correct
14
+ func TestGenerateAgentRBACManifests (t * testing.T ) {
16
15
testCases := []struct {
17
- // expectedClusterRoles is the collection of ClusterRole
18
- expectedClusterRoles []rbac.ClusterRole
19
- dataGatherers []agent.DataGatherer
20
- description string
16
+ description string
17
+ dataGatherers []agent.DataGatherer
18
+ expectedAgentRBACManifests AgentRBACManifests
21
19
}{
22
20
{
23
- description : "Generate RBAC struct for pods datagatherer " ,
21
+ description : "Generate ClusterRole and ClusterRoleBinding for simple pod dg use case " ,
24
22
dataGatherers : []agent.DataGatherer {
25
23
{
26
24
Name : "k8s/pods" ,
@@ -32,87 +30,138 @@ func TestGenerateRBAC(t *testing.T) {
32
30
},
33
31
},
34
32
},
35
- {
36
- Name : "k8s/secrets" ,
37
- Kind : "k8s-dynamic" ,
38
- Config : & k8s.ConfigDynamic {
39
- GroupVersionResource : schema.GroupVersionResource {
40
- Version : "v1" ,
41
- Resource : "secrets" ,
33
+ },
34
+ expectedAgentRBACManifests : AgentRBACManifests {
35
+ ClusterRoles : []rbac.ClusterRole {
36
+ {
37
+ TypeMeta : metav1.TypeMeta {
38
+ Kind : "ClusterRole" ,
39
+ APIVersion : "rbac.authorization.k8s.io/v1" ,
40
+ },
41
+ ObjectMeta : metav1.ObjectMeta {
42
+ Name : "jetstack-secure-agent-pods-reader" ,
43
+ },
44
+ Rules : []rbac.PolicyRule {
45
+ {
46
+ Verbs : []string {"get" , "list" , "watch" },
47
+ APIGroups : []string {"" },
48
+ Resources : []string {"pods" },
49
+ },
50
+ },
51
+ },
52
+ },
53
+ ClusterRoleBindings : []rbac.ClusterRoleBinding {
54
+ {
55
+ TypeMeta : metav1.TypeMeta {
56
+ Kind : "ClusterRoleBinding" ,
57
+ APIVersion : "rbac.authorization.k8s.io/v1" ,
58
+ },
59
+ ObjectMeta : metav1.ObjectMeta {
60
+ Name : "jetstack-secure-agent-pods-reader" ,
61
+ },
62
+ Subjects : []rbac.Subject {
63
+ {
64
+ Kind : "ServiceAccount" ,
65
+ Name : "agent" ,
66
+ Namespace : "jetstack-secure" ,
67
+ },
68
+ },
69
+ RoleRef : rbac.RoleRef {
70
+ Kind : "ClusterRole" ,
71
+ Name : "jetstack-secure-agent-pods-reader" ,
72
+ APIGroup : "rbac.authorization.k8s.io" ,
42
73
},
43
74
},
44
75
},
76
+ },
77
+ },
78
+ {
79
+ description : "Generate RBAC config for simple pod dg use case where only two namespace are included" ,
80
+ dataGatherers : []agent.DataGatherer {
45
81
{
46
- Name : "k8s/awspcaissuer " ,
82
+ Name : "k8s/pods " ,
47
83
Kind : "k8s-dynamic" ,
48
84
Config : & k8s.ConfigDynamic {
49
85
GroupVersionResource : schema.GroupVersionResource {
50
- Group : "awspca.cert-manager.io" ,
51
86
Version : "v1" ,
52
- Resource : "awspcaissuers " ,
87
+ Resource : "pods " ,
53
88
},
89
+ IncludeNamespaces : []string {"example" , "foobar" },
54
90
},
55
91
},
56
92
},
57
- expectedClusterRoles : []rbac.ClusterRole {
58
- {
59
- TypeMeta : metav1.TypeMeta {
60
- Kind : "ClusterRole" ,
61
- APIVersion : "rbac.authorization.k8s.io/v1" ,
62
- },
63
- ObjectMeta : metav1.ObjectMeta {
64
- Name : "jetstack-secure-agent-pods-reader" ,
65
- },
66
- Rules : []rbac.PolicyRule {
67
- {
68
- Verbs : []string {"get" , "list" , "watch" },
69
- APIGroups : []string {"" },
70
- Resources : []string {"pods" },
93
+ expectedAgentRBACManifests : AgentRBACManifests {
94
+ ClusterRoles : []rbac.ClusterRole {
95
+ {
96
+ TypeMeta : metav1.TypeMeta {
97
+ Kind : "ClusterRole" ,
98
+ APIVersion : "rbac.authorization.k8s.io/v1" ,
71
99
},
72
- },
73
- },
74
- {
75
- TypeMeta : metav1.TypeMeta {
76
- Kind : "ClusterRole" ,
77
- APIVersion : "rbac.authorization.k8s.io/v1" ,
78
- },
79
- ObjectMeta : metav1.ObjectMeta {
80
- Name : "jetstack-secure-agent-secrets-reader" ,
81
- },
82
- Rules : []rbac.PolicyRule {
83
- {
84
- Verbs : []string {"get" , "list" , "watch" },
85
- APIGroups : []string {"" },
86
- Resources : []string {"secrets" },
100
+ ObjectMeta : metav1.ObjectMeta {
101
+ Name : "jetstack-secure-agent-pods-reader" ,
102
+ },
103
+ Rules : []rbac.PolicyRule {
104
+ {
105
+ Verbs : []string {"get" , "list" , "watch" },
106
+ APIGroups : []string {"" },
107
+ Resources : []string {"pods" },
108
+ },
87
109
},
88
110
},
89
111
},
90
- {
91
- TypeMeta : metav1.TypeMeta {
92
- Kind : "ClusterRole" ,
93
- APIVersion : "rbac.authorization.k8s.io/v1" ,
94
- },
95
- ObjectMeta : metav1.ObjectMeta {
96
- Name : "jetstack-secure-agent-awspcaissuers-reader" ,
112
+ RoleBindings : []rbac.RoleBinding {
113
+ {
114
+ TypeMeta : metav1.TypeMeta {
115
+ Kind : "RoleBinding" ,
116
+ APIVersion : "rbac.authorization.k8s.io/v1" ,
117
+ },
118
+ ObjectMeta : metav1.ObjectMeta {
119
+ Name : "jetstack-secure-agent-pods-reader" ,
120
+ Namespace : "example" ,
121
+ },
122
+ Subjects : []rbac.Subject {
123
+ {
124
+ Kind : "ServiceAccount" ,
125
+ Name : "agent" ,
126
+ Namespace : "jetstack-secure" ,
127
+ },
128
+ },
129
+ RoleRef : rbac.RoleRef {
130
+ Kind : "ClusterRole" ,
131
+ Name : "jetstack-secure-agent-pods-reader" ,
132
+ APIGroup : "rbac.authorization.k8s.io" ,
133
+ },
97
134
},
98
- Rules : []rbac.PolicyRule {
99
- {
100
- Verbs : []string {"get" , "list" , "watch" },
101
- APIGroups : []string {"awspca.cert-manager.io" },
102
- Resources : []string {"awspcaissuers" },
135
+ {
136
+ TypeMeta : metav1.TypeMeta {
137
+ Kind : "RoleBinding" ,
138
+ APIVersion : "rbac.authorization.k8s.io/v1" ,
139
+ },
140
+ ObjectMeta : metav1.ObjectMeta {
141
+ Name : "jetstack-secure-agent-pods-reader" ,
142
+ Namespace : "foobar" ,
143
+ },
144
+ Subjects : []rbac.Subject {
145
+ {
146
+ Kind : "ServiceAccount" ,
147
+ Name : "agent" ,
148
+ Namespace : "jetstack-secure" ,
149
+ },
150
+ },
151
+ RoleRef : rbac.RoleRef {
152
+ Kind : "ClusterRole" ,
153
+ Name : "jetstack-secure-agent-pods-reader" ,
154
+ APIGroup : "rbac.authorization.k8s.io" ,
103
155
},
104
156
},
105
157
},
106
158
},
107
159
},
108
- // Try adding more test cases
109
160
}
110
161
111
162
for _ , input := range testCases {
112
- got := GenerateRoles (input .dataGatherers )
113
- if diff , equal := messagediff .PrettyDiff (input .expectedClusterRoles , got ); ! equal {
114
- t .Errorf ("%s:\n %s" , input .description , diff )
115
- t .Fatalf ("unexpected difference in RBAC cluster role: \n got \n %v\n want\n %v" , got , input .expectedClusterRoles )
116
- }
163
+ got := GenerateAgentRBACManifests (input .dataGatherers )
164
+
165
+ td .Cmp (t , input .expectedAgentRBACManifests , got )
117
166
}
118
167
}
0 commit comments