@@ -141,7 +141,154 @@ func TestAppProject_IsDestinationPermitted(t *testing.T) {
141
141
Destinations : data .projDest ,
142
142
},
143
143
}
144
- assert .Equal (t , proj .IsDestinationPermitted (data .appDest ), data .isPermitted )
144
+ assert .Equal (t , data .isPermitted , proj .IsDestinationPermitted (data .appDest ))
145
+ }
146
+ }
147
+
148
+ func TestAppProject_IsNegatedDestinationPermitted (t * testing.T ) {
149
+ testData := []struct {
150
+ projDest []ApplicationDestination
151
+ appDest ApplicationDestination
152
+ isPermitted bool
153
+ }{{
154
+ projDest : []ApplicationDestination {{
155
+ Server : "!https://kubernetes.default.svc" , Namespace : "default" ,
156
+ }},
157
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "default" },
158
+ isPermitted : false ,
159
+ }, {
160
+ projDest : []ApplicationDestination {{
161
+ Server : "https://kubernetes.default.svc" , Namespace : "!default" ,
162
+ }},
163
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
164
+ isPermitted : true ,
165
+ }, {
166
+ projDest : []ApplicationDestination {{
167
+ Server : "!https://my-cluster" , Namespace : "default" ,
168
+ }},
169
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "default" },
170
+ isPermitted : true ,
171
+ }, {
172
+ projDest : []ApplicationDestination {{
173
+ Server : "!https://kubernetes.default.svc" , Namespace : "*" ,
174
+ }},
175
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
176
+ isPermitted : false ,
177
+ }, {
178
+ projDest : []ApplicationDestination {{
179
+ Server : "!https://*.default.svc" , Namespace : "default" ,
180
+ }},
181
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "default" },
182
+ isPermitted : false ,
183
+ }, {
184
+ projDest : []ApplicationDestination {{
185
+ Server : "!https://team1-*" , Namespace : "default" ,
186
+ }},
187
+ appDest : ApplicationDestination {Server : "https://test2-dev-cluster" , Namespace : "default" },
188
+ isPermitted : true ,
189
+ }, {
190
+ projDest : []ApplicationDestination {{
191
+ Server : "https://kubernetes.default.svc" , Namespace : "!test-*" ,
192
+ }},
193
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "test-foo" },
194
+ isPermitted : false ,
195
+ }, {
196
+ projDest : []ApplicationDestination {{
197
+ Server : "https://kubernetes.default.svc" , Namespace : "!test-*" ,
198
+ }},
199
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "test" },
200
+ isPermitted : true ,
201
+ }, {
202
+ projDest : []ApplicationDestination {{
203
+ Server : "" , Namespace : "*" , Name : "!test" ,
204
+ }},
205
+ appDest : ApplicationDestination {Name : "test" , Namespace : "test" },
206
+ isPermitted : false ,
207
+ }, {
208
+ projDest : []ApplicationDestination {{
209
+ Server : "" , Namespace : "*" , Name : "!test2" ,
210
+ }},
211
+ appDest : ApplicationDestination {Name : "test" , Namespace : "test" },
212
+ isPermitted : true ,
213
+ }, {
214
+ projDest : []ApplicationDestination {{
215
+ Server : "*" , Namespace : "kube-system" ,
216
+ }, {
217
+ Server : "*" , Namespace : "!kube-system" ,
218
+ }},
219
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
220
+ isPermitted : false ,
221
+ }, {
222
+ projDest : []ApplicationDestination {{
223
+ Server : "*" , Namespace : "*" ,
224
+ }, {
225
+ Server : "*" , Namespace : "!kube-system" ,
226
+ }},
227
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
228
+ isPermitted : false ,
229
+ }, {
230
+ projDest : []ApplicationDestination {{
231
+ Server : "https://kubernetes.default.svc" , Namespace : "*" ,
232
+ }, {
233
+ Server : "!https://kubernetes.default.svc" , Namespace : "*" ,
234
+ }},
235
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
236
+ isPermitted : false ,
237
+ }, {
238
+ projDest : []ApplicationDestination {{
239
+ Server : "*" , Namespace : "*" ,
240
+ }, {
241
+ Server : "!https://kubernetes.default.svc" , Namespace : "kube-system" ,
242
+ }},
243
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
244
+ isPermitted : false ,
245
+ }, {
246
+ projDest : []ApplicationDestination {{
247
+ Server : "*" , Namespace : "*" ,
248
+ }, {
249
+ Server : "!https://kubernetes.default.svc" , Namespace : "kube-system" ,
250
+ }, {
251
+ Server : "*" , Namespace : "!kube-system" ,
252
+ }},
253
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "kube-system" },
254
+ isPermitted : false ,
255
+ }, {
256
+ projDest : []ApplicationDestination {{
257
+ Server : "*" , Namespace : "*" ,
258
+ }, {
259
+ Server : "!https://kubernetes.default.svc" , Namespace : "kube-system" ,
260
+ }, {
261
+ Server : "*" , Namespace : "!kube-system" ,
262
+ }},
263
+ appDest : ApplicationDestination {Server : "https://kubernetes.default.svc" , Namespace : "default" },
264
+ isPermitted : false ,
265
+ }, {
266
+ projDest : []ApplicationDestination {{
267
+ Server : "*" , Namespace : "*" ,
268
+ }, {
269
+ Server : "!https://kubernetes.default.svc" , Namespace : "kube-system" ,
270
+ }, {
271
+ Server : "*" , Namespace : "!kube-system" ,
272
+ }},
273
+ appDest : ApplicationDestination {Server : "https://test-dev-cluster" , Namespace : "kube-system" },
274
+ isPermitted : false ,
275
+ }, {
276
+ projDest : []ApplicationDestination {{
277
+ Server : "" , Namespace : "*" , Name : "test" ,
278
+ }, {
279
+ Server : "" , Namespace : "*" , Name : "!test" ,
280
+ }},
281
+ appDest : ApplicationDestination {Name : "test" , Namespace : "test" },
282
+ isPermitted : false ,
283
+ }}
284
+
285
+ for _ , data := range testData {
286
+ proj := AppProject {
287
+ Spec : AppProjectSpec {
288
+ Destinations : data .projDest ,
289
+ },
290
+ }
291
+ assert .Equal (t , data .isPermitted , proj .IsDestinationPermitted (data .appDest ))
145
292
}
146
293
}
147
294
@@ -260,11 +407,88 @@ func TestAppProject_RemoveGroupFromRole(t *testing.T) {
260
407
func newTestProject () * AppProject {
261
408
p := AppProject {
262
409
ObjectMeta : metav1.ObjectMeta {Name : "my-proj" },
263
- Spec : AppProjectSpec {Roles : []ProjectRole {{Name : "my-role" }}},
410
+ Spec : AppProjectSpec {Roles : []ProjectRole {{Name : "my-role" }}, Destinations : [] ApplicationDestination {{}} },
264
411
}
265
412
return & p
266
413
}
267
414
415
+ // TestAppProject_ValidateDestinations tests for an invalid destination
416
+ func TestAppProject_ValidateDestinations (t * testing.T ) {
417
+ p := newTestProject ()
418
+ err := p .ValidateProject ()
419
+ assert .NoError (t , err )
420
+ badNamespaces := []string {
421
+ "!*" ,
422
+ }
423
+ for _ , badName := range badNamespaces {
424
+ p .Spec .Destinations [0 ].Namespace = badName
425
+ err = p .ValidateProject ()
426
+ assert .Error (t , err )
427
+ }
428
+
429
+ goodNamespaces := []string {
430
+ "*" ,
431
+ "some-namespace" ,
432
+ }
433
+ for _ , goodNamespace := range goodNamespaces {
434
+ p .Spec .Destinations [0 ].Namespace = goodNamespace
435
+ err = p .ValidateProject ()
436
+ assert .NoError (t , err )
437
+ }
438
+
439
+ badServers := []string {
440
+ "!*" ,
441
+ }
442
+ for _ , badServer := range badServers {
443
+ p .Spec .Destinations [0 ].Server = badServer
444
+ err = p .ValidateProject ()
445
+ assert .Error (t , err )
446
+ }
447
+
448
+ goodServers := []string {
449
+ "*" ,
450
+ "some-server" ,
451
+ }
452
+ for _ , badName := range goodServers {
453
+ p .Spec .Destinations [0 ].Server = badName
454
+ err = p .ValidateProject ()
455
+ assert .NoError (t , err )
456
+ }
457
+
458
+ badNames := []string {
459
+ "!*" ,
460
+ }
461
+ for _ , badName := range badNames {
462
+ p .Spec .Destinations [0 ].Name = badName
463
+ err = p .ValidateProject ()
464
+ assert .Error (t , err )
465
+ }
466
+
467
+ goodNames := []string {
468
+ "*" ,
469
+ "some-name" ,
470
+ }
471
+ for _ , goodName := range goodNames {
472
+ p .Spec .Destinations [0 ].Name = goodName
473
+ err = p .ValidateProject ()
474
+ assert .NoError (t , err )
475
+ }
476
+
477
+ validDestination := ApplicationDestination {
478
+ Server : "some-server" ,
479
+ Namespace : "some-namespace" ,
480
+ }
481
+
482
+ p .Spec .Destinations [0 ] = validDestination
483
+ err = p .ValidateProject ()
484
+ assert .NoError (t , err )
485
+
486
+ //no duplicates allowed
487
+ p .Spec .Destinations = []ApplicationDestination {validDestination , validDestination }
488
+ err = p .ValidateProject ()
489
+ assert .Error (t , err )
490
+ }
491
+
268
492
// TestValidateRoleName tests for an invalid role name
269
493
func TestAppProject_ValidateRoleName (t * testing.T ) {
270
494
p := newTestProject ()
0 commit comments