@@ -41,6 +41,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
4141 testCases := []struct {
4242 name string
4343 clusterNamespace string
44+ isClusterScoped bool
4445 allowedNs * infrav1.AllowedNamespaces
4546 setup func (* testing.T , client.Client )
4647 expectedResult bool
@@ -49,20 +50,23 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
4950 {
5051 name : "All clusters are permitted to use identity if allowedNamespaces is empty" ,
5152 clusterNamespace : "default" ,
53+ isClusterScoped : false ,
5254 allowedNs : & infrav1.AllowedNamespaces {},
5355 expectedResult : true ,
5456 expectErr : false ,
5557 },
5658 {
5759 name : "No clusters are permitted to use identity if allowedNamespaces is nil" ,
5860 clusterNamespace : "default" ,
61+ isClusterScoped : false ,
5962 allowedNs : nil ,
6063 expectedResult : false ,
6164 expectErr : false ,
6265 },
6366 {
6467 name : "A namespace is permitted if allowedNamespaces list has it" ,
6568 clusterNamespace : "match" ,
69+ isClusterScoped : false ,
6670 allowedNs : & infrav1.AllowedNamespaces {
6771 NamespaceList : []string {"match" },
6872 Selector : metav1.LabelSelector {},
@@ -87,6 +91,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
8791 {
8892 name : "A namespace is not permitted if allowedNamespaces list does not have it" ,
8993 clusterNamespace : "default" ,
94+ isClusterScoped : false ,
9095 allowedNs : & infrav1.AllowedNamespaces {
9196 NamespaceList : []string {"nomatch" },
9297 Selector : metav1.LabelSelector {},
@@ -111,6 +116,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
111116 {
112117 name : "A namespace is not permitted if allowedNamespaces list and selector do not have it" ,
113118 clusterNamespace : "default" ,
119+ isClusterScoped : false ,
114120 allowedNs : & infrav1.AllowedNamespaces {
115121 NamespaceList : []string {"nomatch" },
116122 Selector : metav1.LabelSelector {
@@ -137,6 +143,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
137143 {
138144 name : "A namespace is not permitted if allowedNamespaces list and selector do not have it" ,
139145 clusterNamespace : "default" ,
146+ isClusterScoped : false ,
140147 allowedNs : & infrav1.AllowedNamespaces {
141148 NamespaceList : nil ,
142149 Selector : metav1.LabelSelector {
@@ -163,6 +170,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
163170 {
164171 name : "A namespace is permitted if allowedNamespaces list does not have it but selector matches its label" ,
165172 clusterNamespace : "default" ,
173+ isClusterScoped : false ,
166174 allowedNs : & infrav1.AllowedNamespaces {
167175 NamespaceList : []string {"noMatch" },
168176 Selector : metav1.LabelSelector {
@@ -189,6 +197,49 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
189197 },
190198 }
191199
200+ // Add test cases for cluster-scoped resources
201+ clusterScopedTestCases := []struct {
202+ name string
203+ allowedNs * infrav1.AllowedNamespaces
204+ expectedResult bool
205+ expectErr bool
206+ }{
207+ {
208+ name : "Cluster-scoped resource bypasses allowedNamespaces check when allowedNamespaces is nil" ,
209+ allowedNs : nil ,
210+ expectedResult : true ,
211+ expectErr : false ,
212+ },
213+ {
214+ name : "Cluster-scoped resource bypasses allowedNamespaces check when allowedNamespaces restricts to specific namespace" ,
215+ allowedNs : & infrav1.AllowedNamespaces {
216+ NamespaceList : []string {"specific-namespace" },
217+ },
218+ expectedResult : true ,
219+ expectErr : false ,
220+ },
221+ }
222+
223+ for _ , tc := range clusterScopedTestCases {
224+ testCases = append (testCases , struct {
225+ name string
226+ clusterNamespace string
227+ isClusterScoped bool
228+ allowedNs * infrav1.AllowedNamespaces
229+ setup func (* testing.T , client.Client )
230+ expectedResult bool
231+ expectErr bool
232+ }{
233+ name : tc .name ,
234+ clusterNamespace : "" , // cluster-scoped resources return empty namespace
235+ isClusterScoped : true ,
236+ allowedNs : tc .allowedNs ,
237+ setup : nil ,
238+ expectedResult : tc .expectedResult ,
239+ expectErr : tc .expectErr ,
240+ })
241+ }
242+
192243 for _ , tc := range testCases {
193244 t .Run (tc .name , func (t * testing.T ) {
194245 g := NewWithT (t )
@@ -201,7 +252,7 @@ func TestIsClusterPermittedToUsePrincipal(t *testing.T) {
201252 if tc .setup != nil {
202253 tc .setup (t , k8sClient )
203254 }
204- result , err := isClusterPermittedToUsePrincipal (k8sClient , tc .allowedNs , tc .clusterNamespace )
255+ result , err := isClusterPermittedToUsePrincipal (k8sClient , tc .allowedNs , tc .clusterNamespace , tc . isClusterScoped )
205256 if tc .expectErr {
206257 g .Expect (err ).ToNot (BeNil ())
207258 } else {
0 commit comments