-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NOREF] add support for queryParams and scheme #354
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,27 +21,11 @@ jobs: | |
uname -a | ||
go version | ||
go env | ||
- name: Vet | ||
if: matrix.platform == 'ubuntu-latest' | ||
run: go vet -v ./... | ||
- name: Lint | ||
if: matrix.platform == 'ubuntu-latest' | ||
run: | | ||
export PATH=$PATH:$(go env GOPATH)/bin | ||
go install golang.org/x/lint/golint@latest | ||
golint -set_exit_status ./... | ||
- name: staticcheck.io | ||
if: matrix.platform == 'ubuntu-latest' | ||
uses: dominikh/[email protected] | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
install-go: false | ||
- name: gofumpt formatting | ||
if: matrix.platform == 'ubuntu-latest' | ||
run: | | ||
export PATH=$PATH:$(go env GOPATH)/bin | ||
go install mvdan.cc/gofumpt@latest | ||
gofumpt -d . | ||
[ -z "$(gofumpt -l .)" ] | ||
skip-build-cache: true | ||
skip-pkg-cache: true | ||
- name: Test | ||
run: go test -count=1 ./... | ||
- name: Test with -race | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
run: | ||
timeout: 10m | ||
linters: | ||
disable-all: true | ||
enable: | ||
- errcheck | ||
- gofumpt | ||
- gosimple | ||
- govet | ||
- ineffassign | ||
- staticcheck | ||
- stylecheck | ||
- typecheck | ||
- unused |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
CURRENTPATH = $(shell echo $(PWD)) | ||
WORKDIR = /src/github.com/getyourguide.com/istio-config-validator | ||
GOLINT_CMD=golangci-lint | ||
GO_FILES=$(shell find . -type f -regex ".*go") | ||
|
||
.PHONY: run lint fix | ||
|
||
run: | ||
docker run -it --rm --name istio_config_validator \ | ||
-v ${CURRENTPATH}:${WORKDIR} \ | ||
-w ${WORKDIR} \ | ||
golang:1.21 \ | ||
go run cmd/istio-config-validator/main.go -t examples/ examples/ | ||
|
||
define check_linter | ||
@if ! command -v $(GOLINT_CMD) > /dev/null; then\ | ||
echo "$(GOLINT_CMD) must be installed to run linting: try brew install $(GOLINT_CMD)";\ | ||
exit 1;\ | ||
fi | ||
endef | ||
|
||
lint: .golangci.yml ## Lint the code | ||
|
||
fix: ## Fix any issues found during linting | ||
@$(call check_linter) | ||
$(GOLINT_CMD) run --fix | ||
|
||
.golangci.yml: $(GO_FILES) | ||
@$(call check_linter) | ||
$(GOLINT_CMD) version | ||
$(GOLINT_CMD) run | ||
touch .golangci.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,37 @@ import ( | |
"istio.io/api/networking/v1alpha3" | ||
) | ||
|
||
// MapMatcher checks map parameters, which requires executing multiple ExtendedStringMatch checks | ||
type MapMatcher struct { | ||
matchers map[string]*v1alpha3.StringMatch | ||
} | ||
|
||
func (mm MapMatcher) Match(input map[string]string) (bool, error) { | ||
for param, matcher := range mm.matchers { | ||
value, exists := input[param] | ||
if !exists { | ||
return false, nil | ||
} | ||
|
||
if matcher.GetMatchType() == nil { | ||
// match type exact: "", and {} checks for presence in query and headers respectively | ||
continue | ||
} | ||
|
||
extendedMatcher := &ExtendedStringMatch{StringMatch: matcher} | ||
matched, err := extendedMatcher.Match(value) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if !matched { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ExtendedStringMatch copies istio ExtendedStringMatch definition and extends it to add helper methods. | ||
type ExtendedStringMatch struct { | ||
*v1alpha3.StringMatch | ||
|
@@ -26,57 +57,58 @@ func (sm *ExtendedStringMatch) Match(s string) (bool, error) { | |
return true, nil | ||
} | ||
|
||
switch { | ||
case sm.GetExact() != "": | ||
return sm.GetExact() == s, nil | ||
case sm.GetPrefix() != "": | ||
switch m := sm.GetMatchType().(type) { | ||
case *v1alpha3.StringMatch_Exact: | ||
return m.Exact == s, nil | ||
case *v1alpha3.StringMatch_Prefix: | ||
return strings.HasPrefix(s, sm.GetPrefix()), nil | ||
case sm.GetRegex() != "": | ||
case *v1alpha3.StringMatch_Regex: | ||
// The rule will not match if only a subsequence of the string matches the regex. | ||
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routematch-safe-regex | ||
r, err := regexp.Compile("^" + sm.GetRegex() + "$") | ||
if err != nil { | ||
return false, fmt.Errorf("could not compile regex %s: %v", sm.GetRegex(), err) | ||
} | ||
return r.MatchString(s), nil | ||
default: | ||
return false, fmt.Errorf("unknown matcher type %T", sm.GetMatchType()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning an error here is a behaviour change - could also be a log |
||
} | ||
return false, nil | ||
} | ||
|
||
// matchRequest takes an Input and evaluates against a HTTPMatchRequest block. It replicates | ||
// Istio VirtualService semantic returning true when ALL conditions within the block are true. | ||
// TODO: Add support for all fields within a match block. The ones supported today are: | ||
// Authority, Uri, Method and Headers. | ||
// Authority, Uri, Method, Headers, Scheme, and QueryParams. | ||
func matchRequest(input parser.Input, httpMatchRequest *v1alpha3.HTTPMatchRequest) (bool, error) { | ||
authority := &ExtendedStringMatch{httpMatchRequest.Authority} | ||
uri := &ExtendedStringMatch{httpMatchRequest.Uri} | ||
method := &ExtendedStringMatch{httpMatchRequest.Method} | ||
if matched, err := uri.Match(input.URI); !matched || err != nil { | ||
return false, err | ||
} | ||
|
||
for headerName, sm := range httpMatchRequest.Headers { | ||
if _, ok := input.Headers[headerName]; !ok { | ||
return false, nil | ||
} | ||
header := &ExtendedStringMatch{sm} | ||
match, err := header.Match(input.Headers[headerName]) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !match { | ||
return false, nil | ||
} | ||
authority := &ExtendedStringMatch{httpMatchRequest.Authority} | ||
if matched, err := authority.Match(input.Authority); !matched || err != nil { | ||
return false, err | ||
} | ||
|
||
uriMatch, err := uri.Match(input.URI) | ||
if err != nil { | ||
method := &ExtendedStringMatch{httpMatchRequest.Method} | ||
if matched, err := method.Match(input.Method); !matched || err != nil { | ||
return false, err | ||
} | ||
authorityMatch, err := authority.Match(input.Authority) | ||
if err != nil { | ||
|
||
scheme := &ExtendedStringMatch{httpMatchRequest.Scheme} | ||
if matched, err := scheme.Match(input.Scheme); !matched || err != nil { | ||
return false, err | ||
} | ||
methodMatch, err := method.Match(input.Method) | ||
if err != nil { | ||
|
||
headers := &MapMatcher{matchers: httpMatchRequest.Headers} | ||
if matched, err := headers.Match(input.Headers); !matched || err != nil { | ||
return false, err | ||
} | ||
return authorityMatch && uriMatch && methodMatch, nil | ||
|
||
query := &MapMatcher{matchers: httpMatchRequest.QueryParams} | ||
if matched, err := query.Match(input.QueryParams); !matched || err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apiVersion: networking.istio.io/v1alpha3 | ||
kind: VirtualService | ||
metadata: | ||
name: example | ||
namespace: example | ||
spec: | ||
gateways: | ||
- mesh | ||
hosts: | ||
- www.example.com | ||
- example.com | ||
http: | ||
- match: | ||
- headers: | ||
x-custom-header: | ||
exact: ok | ||
x-prefix-header: | ||
prefix: start | ||
route: | ||
- destination: | ||
host: has-header | ||
- match: | ||
- headers: | ||
x-custom-header: {} | ||
route: | ||
- destination: | ||
host: has-present-header | ||
- match: | ||
route: | ||
- destination: | ||
host: default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
TestCases: | ||
- description: match headers with a variety of values | ||
wantMatch: true | ||
request: | ||
authority: ["www.example.com"] | ||
method: ["GET"] | ||
uri: ["/headers/v1"] | ||
headers: | ||
x-custom-header: ok | ||
x-prefix-header: start-with-prefix | ||
route: | ||
- destination: | ||
host: has-header | ||
- description: match a header by presence | ||
wantMatch: true | ||
request: | ||
authority: ["www.example.com"] | ||
method: ["GET"] | ||
uri: ["/headers/v1"] | ||
headers: | ||
x-custom-header: ok | ||
route: | ||
- destination: | ||
host: has-present-header | ||
- description: missing headers goes to default | ||
wantMatch: true | ||
request: | ||
authority: ["www.example.com"] | ||
method: ["GET"] | ||
uri: ["/headers/v1"] | ||
route: | ||
- destination: | ||
host: default |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a separate field for the parameters or can we use the
uri
and parse from it?Also, the same parameter can be set multiple times and interpreted differently by web frameworks as there is no apparent standard, this would be another reason not to use map[string]string
https://go.dev/play/p/dcExJ0RlFYT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed will keep as is for now - the envoy behaviour is to check the first query param