@@ -584,7 +584,7 @@ var CheckDeny = check.Or(
584
584
585
585
func TestAuthorizationL4 (t * testing.T ) {
586
586
framework .NewTest (t ).Run (func (t framework.TestContext ) {
587
- t .NewSubTest ("allow " ).Run (func (t framework.TestContext ) {
587
+ t .NewSubTest ("L4 Authorization " ).Run (func (t framework.TestContext ) {
588
588
if len (apps .ServiceWithWaypointAtServiceGranularity ) == 0 {
589
589
t .Fatal (fmt .Errorf ("need at least 1 instance of apps.ServiceWithWaypointAtServiceGranularity" ))
590
590
}
@@ -598,48 +598,91 @@ func TestAuthorizationL4(t *testing.T) {
598
598
t .Fatal (fmt .Errorf ("need at least 2 clients" ))
599
599
}
600
600
selectedAddress := addresses [0 ]
601
- t .ConfigIstio ().Eval (apps .Namespace .Name (), map [string ]string {
602
- "Destination" : dst .Config ().Service ,
603
- "Ip" : selectedAddress ,
604
- }, `apiVersion: security.istio.io/v1beta1
601
+
602
+ authzCases := []struct {
603
+ name string
604
+ spec string
605
+ }{
606
+ {
607
+ name : "allow" ,
608
+ spec : `
609
+ action: ALLOW
610
+ ` ,
611
+ },
612
+ {
613
+ name : "deny" ,
614
+ spec : `
615
+ action: DENY
616
+ ` ,
617
+ },
618
+ }
619
+
620
+ chooseChecker := func (action string , ip string ) echo.Checker {
621
+ switch action {
622
+ case "allow" :
623
+ if ip != selectedAddress {
624
+ return CheckDeny
625
+ } else {
626
+ return check .OK ()
627
+ }
628
+ case "deny" :
629
+ if ip != selectedAddress {
630
+ return check .OK ()
631
+ } else {
632
+ return CheckDeny
633
+ }
634
+ default :
635
+ t .Fatal ("invalid action" )
636
+ }
637
+
638
+ return check .OK ()
639
+ }
640
+
641
+ for _ , tc := range authzCases {
642
+ t .ConfigIstio ().Eval (apps .Namespace .Name (), map [string ]string {
643
+ "Destination" : dst .Config ().Service ,
644
+ "Ip" : selectedAddress ,
645
+ }, `apiVersion: security.istio.io/v1beta1
605
646
kind: AuthorizationPolicy
606
647
metadata:
607
648
name: policy
608
649
spec:
609
650
selector:
610
651
matchLabels:
611
652
app: "{{.Destination}}"
612
- action: ALLOW
653
+ ` + tc . spec + `
613
654
rules:
614
655
- from:
615
656
- source:
616
657
ipBlocks:
617
658
- "{{.Ip}}"
618
659
` ).ApplyOrFail (t )
619
660
620
- for _ , client := range clients {
621
- opt := echo.CallOptions {
622
- To : dst ,
623
- Port : echo.Port {Name : "tcp" },
624
- Scheme : scheme .TCP ,
625
- Count : 10 ,
626
- // Due to the mechanism of Kmesh L4 authorization, we need to set the timeout slightly longer.
627
- NewConnectionPerRequest : true ,
628
- Timeout : time .Minute * 2 ,
629
- Check : check .OK (),
630
- }
661
+ for _ , client := range clients {
662
+ opt := echo.CallOptions {
663
+ To : dst ,
664
+ Port : echo.Port {Name : "tcp" },
665
+ Scheme : scheme .TCP ,
666
+ Count : 10 ,
667
+ // Due to the mechanism of Kmesh L4 authorization, we need to set the timeout slightly longer.
668
+ NewConnectionPerRequest : true ,
669
+ Timeout : time .Minute * 2 ,
670
+ Check : check .OK (),
671
+ }
631
672
632
- fmt .Printf ("--- client.Address() is %v, selectedAddress is %v\n " , client .Address (), selectedAddress )
673
+ var name string
674
+ if client .Address () != selectedAddress {
675
+ name = tc .name + ", not selected address"
676
+ } else {
677
+ name = tc .name + ", selected address"
678
+ }
633
679
634
- if client .Address () != selectedAddress {
635
- fmt .Printf ("--- Use CheckDeny\n " )
636
- opt .Check = CheckDeny
637
- }
680
+ opt .Check = chooseChecker (tc .name , client .Address ())
638
681
639
- t .NewSubTestf ("%v" , opt . Scheme ).Run (func (t framework.TestContext ) {
640
- result := src .WithWorkloads (client ).CallOrFail (t , opt )
641
- fmt . Printf ( "-- call result is %v \n " , result . Responses )
642
- })
682
+ t .NewSubTestf ("%v" , name ).Run (func (t framework.TestContext ) {
683
+ src .WithWorkloads (client ).CallOrFail (t , opt )
684
+ } )
685
+ }
643
686
}
644
687
})
645
688
})
0 commit comments