Skip to content

Commit

Permalink
Redirect assertion (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
cainelli authored Sep 24, 2024
1 parent 84a9ed6 commit b7ee7f5
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ jobs:
run: go test -vet=off -count=1 ./...
- name: Test with -race
run: go test -vet=off -race -count=1 ./...
- name: Run
run: make run
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ WORKDIR = /src/github.com/getyourguide.com/istio-config-validator


run:
docker run -it --rm --name istio_config_validator \
-v ${CURRENTPATH}:${WORKDIR} \
-w ${WORKDIR} \
golang:1.22 \
go run cmd/istio-config-validator/main.go -t examples/ examples/
go run cmd/istio-config-validator/main.go -t examples/ examples/

build:
go build -o istio-config-validator cmd/istio-config-validator/main.go
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ testCases:
Have a look in the [TestCase Reference](docs/test-cases.md) to learn more how to define the tests.

## Installation

Either install the go package

```
# go install github.com/getyourguide/istio-config-validator/cmd/istio-config-validator@latest
```
Or alternatively install the docker image
```
# docker pull getyourguide/istio-config-validator:latest
```
Expand Down Expand Up @@ -115,17 +119,15 @@ If you're interested in contributing to this project or running a dev version, h
The API for test cases does not cover all aspects of VirtualServices.
- Supported [HTTPMatchRequests](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPMatchRequest) fields to match requests against are: `authority`, `method`, `headers` and `uri`.
- Not supported ones: `scheme`, `port`, `queryParams`, etc.
- Supported [HTTPMatchRequests](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPMatchRequest) fields to match requests against are: `authority`, `method`, `headers` and `uri`.
- Not supported ones: `scheme`, `port`, `queryParams`, etc.
- Supported assert against [HTTPRouteDestination](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRouteDestination) and [HTTPRewrite](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRewrite)
- Not supported ones: [HTTPRedirect](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRedirect), etc.
- Supported assert against [HTTPRouteDestination](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRouteDestination), [HTTPRewrite](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRewrite), [HTTPFaultInjection](https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPFaultInjection), [Headers](https://istio.io/latest/docs/reference/config/networking/virtual-service/#Headers), [Delegate](https://istio.io/latest/docs/reference/config/networking/virtual-service/#Delegate) and [HTTPRedirect](https://istio.io/docs/reference/config/networking/virtual-service/#HTTPRedirect).
## Security
For sensitive security matters please contact [[email protected]](mailto:[email protected]).
## Legal
Copyright 2020 GetYourGuide GmbH.
Expand Down
16 changes: 8 additions & 8 deletions examples/multidocument_virtualservice.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: example
namespace: example
name: example-2
namespace: example-2
spec:
gateways:
- mesh
hosts:
- www.example.com
- example.com
- www.example2.com
- example2.com
http:
- match:
- uri:
Expand All @@ -26,14 +26,14 @@ spec:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: example
namespace: example
name: example-3
namespace: example-3
spec:
gateways:
- mesh
hosts:
- www.example.com
- example.com
- www.example3.com
- example3.com
http:
- match:
- uri:
Expand Down
8 changes: 7 additions & 1 deletion examples/virtualservice.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: networking.istio.io/v1alpha3
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: example
Expand All @@ -10,6 +10,12 @@ spec:
- www.example.com
- example.com
http:
- match:
- uri:
prefix: /home
redirect:
uri: /
authority: www.example.com
- match:
- uri:
regex: /users(/.*)?
Expand Down
9 changes: 9 additions & 0 deletions examples/virtualservice_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ testCases:
---
# Multidoc test
testCases:
- description: Redirect /home to /
wantMatch: true
request:
authority: ["www.example.com"]
method: ["GET"]
uri: ["/home"]
redirect:
uri: "/"
authority: "www.example.com"
- description: Reseller is rewritten as partner
wantMatch: true
request:
Expand Down
9 changes: 4 additions & 5 deletions internal/pkg/istio-router-check/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func (c *RootCommand) prepareTests(ctx context.Context) error {
log.V(LevelDebug).Info("skipping rewrite test", "test", tc.Description, "reason", "format assertion is different in envoy tests")
continue
}
if tc.Redirect != nil {
log.V(LevelDebug).Info("skipping redirect test", "test", tc.Description, "reason", "format assertion is different in envoy tests")
continue
}
for _, req := range inputs {
var reqHeaders []envoy.Header
for key, value := range req.Headers {
Expand Down Expand Up @@ -243,10 +247,5 @@ func convertValidate(input envoy.Input, tc *parser.TestCase) (envoy.Validate, er
route.GetDestination().GetHost(),
)
}
if tc.Redirect != nil {
authority := cmp.Or(tc.Redirect.GetAuthority(), input.Authority)
scheme := cmp.Or(tc.Redirect.GetScheme(), "https")
output.PathRedirect = fmt.Sprintf("%s://%s%s", scheme, authority, tc.Redirect.GetUri())
}
return output, nil
}
12 changes: 5 additions & 7 deletions internal/pkg/parser/virtualservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@ func TestParseVirtualServices(t *testing.T) {
}

func TestParseMultipleVirtualServices(t *testing.T) {
expectedTestCases := []*v1alpha3.VirtualService{{Spec: networkingv1alpha3.VirtualService{
Hosts: []string{"www.example.com", "example.com"},
}}}
wantHosts := []string{"www.example2.com", "example2.com", "www.example3.com", "example3.com"}

configfiles := []string{"../../../examples/multidocument_virtualservice.yml"}
virtualServices, err := ParseVirtualServices(configfiles)
require.NoError(t, err)
require.NotEmpty(t, virtualServices)
require.GreaterOrEqual(t, len(virtualServices), 2)

for _, expected := range expectedTestCases {
for _, out := range virtualServices {
assert.ElementsMatch(t, expected.Spec.Hosts, out.Spec.Hosts)
}
var gotHosts []string
for _, vs := range virtualServices {
gotHosts = append(gotHosts, vs.Spec.Hosts...)
}
require.ElementsMatch(t, wantHosts, gotHosts)
}

func TestVirtualServiceUnknownFields(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func Run(testfiles, configfiles []string, strict bool) ([]string, []string, erro
return summary, details, fmt.Errorf("headers missmatch=%v, want %v, rule matched: %v", route.Headers, testCase.Headers, route.Match)
}
}

if testCase.Redirect != nil {
if reflect.DeepEqual(route.Redirect, testCase.Redirect) != testCase.WantMatch {
details = append(details, fmt.Sprintf("FAIL input:[%v]", input))
return summary, details, fmt.Errorf("redirect missmatch=%v, want %v, rule matched: %v", route.Redirect, testCase.Redirect, route.Match)
}
}
details = append(details, fmt.Sprintf("PASS input:[%v]", input))
}
inputCount += len(inputs)
Expand Down

0 comments on commit b7ee7f5

Please sign in to comment.