@@ -242,50 +242,165 @@ func TestAPIServerHost(t *testing.T) {
242
242
}
243
243
244
244
func TestGettingSecurityRules (t * testing.T ) {
245
- g := NewWithT (t )
246
-
247
- cluster := & clusterv1.Cluster {
248
- ObjectMeta : metav1.ObjectMeta {
249
- Name : "my-cluster" ,
250
- Namespace : "default" ,
245
+ tests := []struct {
246
+ name string
247
+ cluster * clusterv1.Cluster
248
+ azureCluster * infrav1.AzureCluster
249
+ expectedRuleCount int
250
+ }{
251
+ {
252
+ name : "default control plane subnet with no rules should have 2 security rules defaulted" ,
253
+ cluster : & clusterv1.Cluster {
254
+ ObjectMeta : metav1.ObjectMeta {
255
+ Name : "my-cluster" ,
256
+ Namespace : "default" ,
257
+ },
258
+ },
259
+ azureCluster : & infrav1.AzureCluster {
260
+ ObjectMeta : metav1.ObjectMeta {
261
+ Name : "my-azure-cluster" ,
262
+ },
263
+ Spec : infrav1.AzureClusterSpec {
264
+ AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
265
+ SubscriptionID : "123" ,
266
+ IdentityRef : & corev1.ObjectReference {
267
+ Kind : infrav1 .AzureClusterIdentityKind ,
268
+ },
269
+ },
270
+ ControlPlaneEnabled : true ,
271
+ NetworkSpec : infrav1.NetworkSpec {
272
+ Subnets : infrav1.Subnets {
273
+ {
274
+ SubnetClassSpec : infrav1.SubnetClassSpec {
275
+ Role : infrav1 .SubnetNode ,
276
+ Name : "node" ,
277
+ },
278
+ },
279
+ },
280
+ },
281
+ },
282
+ },
283
+ expectedRuleCount : 2 ,
251
284
},
252
- }
253
-
254
- azureCluster := & infrav1.AzureCluster {
255
- ObjectMeta : metav1.ObjectMeta {
256
- Name : "my-azure-cluster" ,
285
+ {
286
+ name : "additional rules are preserved" ,
287
+ cluster : & clusterv1.Cluster {
288
+ ObjectMeta : metav1.ObjectMeta {
289
+ Name : "my-cluster" ,
290
+ Namespace : "default" ,
291
+ },
292
+ },
293
+ azureCluster : & infrav1.AzureCluster {
294
+ ObjectMeta : metav1.ObjectMeta {
295
+ Name : "my-azure-cluster" ,
296
+ },
297
+ Spec : infrav1.AzureClusterSpec {
298
+ AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
299
+ SubscriptionID : "123" ,
300
+ IdentityRef : & corev1.ObjectReference {
301
+ Kind : infrav1 .AzureClusterIdentityKind ,
302
+ },
303
+ },
304
+ ControlPlaneEnabled : true ,
305
+ NetworkSpec : infrav1.NetworkSpec {
306
+ Subnets : infrav1.Subnets {
307
+ {
308
+ SecurityGroup : infrav1.SecurityGroup {
309
+ SecurityGroupClass : infrav1.SecurityGroupClass {
310
+ SecurityRules : []infrav1.SecurityRule {{
311
+ Name : "allow_9345" ,
312
+ Description : "Allow port 9345" ,
313
+ Priority : 2200 ,
314
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
315
+ Direction : infrav1 .SecurityRuleDirectionInbound ,
316
+ Source : ptr .To ("*" ),
317
+ SourcePorts : ptr .To ("*" ),
318
+ Destination : ptr .To ("*" ),
319
+ DestinationPorts : ptr .To ("9345" ),
320
+ Action : infrav1 .SecurityRuleActionAllow ,
321
+ }},
322
+ },
323
+ },
324
+ SubnetClassSpec : infrav1.SubnetClassSpec {
325
+ Role : infrav1 .SubnetControlPlane ,
326
+ Name : string (infrav1 .SubnetControlPlane ),
327
+ },
328
+ },
329
+ },
330
+ },
331
+ },
332
+ },
333
+ expectedRuleCount : 3 ,
257
334
},
258
- Spec : infrav1.AzureClusterSpec {
259
- AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
260
- SubscriptionID : "123" ,
261
- IdentityRef : & corev1.ObjectReference {
262
- Kind : infrav1 .AzureClusterIdentityKind ,
335
+ {
336
+ name : "override rules are accepted" ,
337
+ cluster : & clusterv1.Cluster {
338
+ ObjectMeta : metav1.ObjectMeta {
339
+ Name : "my-cluster" ,
340
+ Namespace : "default" ,
263
341
},
264
342
},
265
- ControlPlaneEnabled : true ,
266
- NetworkSpec : infrav1.NetworkSpec {
267
- Subnets : infrav1.Subnets {
268
- {
269
- SubnetClassSpec : infrav1.SubnetClassSpec {
270
- Role : infrav1 .SubnetNode ,
271
- Name : "node" ,
343
+ azureCluster : & infrav1.AzureCluster {
344
+ ObjectMeta : metav1.ObjectMeta {
345
+ Name : "my-azure-cluster" ,
346
+ },
347
+ Spec : infrav1.AzureClusterSpec {
348
+ AzureClusterClassSpec : infrav1.AzureClusterClassSpec {
349
+ SubscriptionID : "123" ,
350
+ IdentityRef : & corev1.ObjectReference {
351
+ Kind : infrav1 .AzureClusterIdentityKind ,
352
+ },
353
+ },
354
+ ControlPlaneEnabled : true ,
355
+ NetworkSpec : infrav1.NetworkSpec {
356
+ Subnets : infrav1.Subnets {
357
+ {
358
+ SecurityGroup : infrav1.SecurityGroup {
359
+ SecurityGroupClass : infrav1.SecurityGroupClass {
360
+ SecurityRules : []infrav1.SecurityRule {{
361
+ Name : "deny_ssh" ,
362
+ Description : "Deny SSH" ,
363
+ Priority : 2200 ,
364
+ Protocol : infrav1 .SecurityGroupProtocolTCP ,
365
+ Direction : infrav1 .SecurityRuleDirectionInbound ,
366
+ Source : ptr .To ("*" ),
367
+ SourcePorts : ptr .To ("*" ),
368
+ Destination : ptr .To ("*" ),
369
+ DestinationPorts : ptr .To ("22" ),
370
+ Action : infrav1 .SecurityRuleActionDeny ,
371
+ }},
372
+ },
373
+ },
374
+ SubnetClassSpec : infrav1.SubnetClassSpec {
375
+ Role : infrav1 .SubnetControlPlane ,
376
+ Name : string (infrav1 .SubnetControlPlane ),
377
+ },
378
+ },
272
379
},
273
380
},
274
381
},
275
382
},
383
+ expectedRuleCount : 2 ,
276
384
},
277
385
}
278
- azureCluster .Default ()
279
386
280
- clusterScope := & ClusterScope {
281
- Cluster : cluster ,
282
- AzureCluster : azureCluster ,
283
- }
284
- clusterScope .SetControlPlaneSecurityRules ()
387
+ for _ , tt := range tests {
388
+ t .Run (tt .name , func (t * testing.T ) {
389
+ g := NewWithT (t )
285
390
286
- subnet , err := clusterScope .AzureCluster .Spec .NetworkSpec .GetControlPlaneSubnet ()
287
- g .Expect (err ).NotTo (HaveOccurred ())
288
- g .Expect (subnet .SecurityGroup .SecurityRules ).To (HaveLen (2 ))
391
+ tt .azureCluster .Default ()
392
+
393
+ clusterScope := & ClusterScope {
394
+ Cluster : tt .cluster ,
395
+ AzureCluster : tt .azureCluster ,
396
+ }
397
+ clusterScope .SetControlPlaneSecurityRules ()
398
+
399
+ subnet , err := clusterScope .AzureCluster .Spec .NetworkSpec .GetControlPlaneSubnet ()
400
+ g .Expect (err ).NotTo (HaveOccurred ())
401
+ g .Expect (subnet .SecurityGroup .SecurityRules ).To (HaveLen (tt .expectedRuleCount ))
402
+ })
403
+ }
289
404
}
290
405
291
406
func TestPublicIPSpecs (t * testing.T ) {
0 commit comments