Skip to content

Commit 4a419f2

Browse files
committed
add deny scenario
Signed-off-by: YaoZengzeng <[email protected]>
1 parent eb76910 commit 4a419f2

File tree

2 files changed

+70
-27
lines changed

2 files changed

+70
-27
lines changed

test/e2e/baseline_test.go

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ var CheckDeny = check.Or(
584584

585585
func TestAuthorizationL4(t *testing.T) {
586586
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) {
588588
if len(apps.ServiceWithWaypointAtServiceGranularity) == 0 {
589589
t.Fatal(fmt.Errorf("need at least 1 instance of apps.ServiceWithWaypointAtServiceGranularity"))
590590
}
@@ -598,48 +598,91 @@ func TestAuthorizationL4(t *testing.T) {
598598
t.Fatal(fmt.Errorf("need at least 2 clients"))
599599
}
600600
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
605646
kind: AuthorizationPolicy
606647
metadata:
607648
name: policy
608649
spec:
609650
selector:
610651
matchLabels:
611652
app: "{{.Destination}}"
612-
action: ALLOW
653+
`+tc.spec+`
613654
rules:
614655
- from:
615656
- source:
616657
ipBlocks:
617658
- "{{.Ip}}"
618659
`).ApplyOrFail(t)
619660

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+
}
631672

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+
}
633679

634-
if client.Address() != selectedAddress {
635-
fmt.Printf("--- Use CheckDeny\n")
636-
opt.Check = CheckDeny
637-
}
680+
opt.Check = chooseChecker(tc.name, client.Address())
638681

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+
}
643686
}
644687
})
645688
})

test/e2e/run_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,6 @@ if [[ -z "${SKIP_SETUP:-}" ]]; then
240240
setup_kmesh
241241
fi
242242

243-
cmd="go test -v -tags=integ $ROOT_DIR/test/e2e/... -count=1 -istio.test.nocleanup $PARAMS"
243+
cmd="go test -v -tags=integ $ROOT_DIR/test/e2e/... -count=1 $PARAMS"
244244

245245
bash -c "$cmd"

0 commit comments

Comments
 (0)