Skip to content
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

Fix regex, remove useless separator #97

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ run: install-local generate fmt vet ## Run a controller from your host, proxying
ktunnel expose -n kusk-system kusk-xds-service 18000 & ENABLE_WEBHOOKS=false bin/manager ; fg

docker-build: ## Build docker image with the manager.
DOCKER_BUILDKIT=1 docker build -t ${IMG} .
eval $(minikube docker-env --profile "kgw") && DOCKER_BUILDKIT=1 docker build -t ${IMG} .

docker-build-debug: ## Build docker image with the manager and debugger.
DOCKER_BUILDKIT=1 docker build -t "${IMG}-debug" -f ./Dockerfile-debug .
docker-build-debug:## Build docker image with the manager and debugger.
eval $(minikube docker-env --profile "kgw") && DOCKER_BUILDKIT=1 docker build -t "${IMG}-debug" -f ./Dockerfile-debug .
docker-push: ## Push docker image with the manager.
docker push ${IMG}

Expand Down
53 changes: 31 additions & 22 deletions envoy/config/config_routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ var (
307: "TEMPORARY_REDIRECT",
308: "PERMANENT_REDIRECT",
}

// regexes for path that has OpenAPI parameters names ({petID})
// OpenAPI supports:
// * strings
// * number (double and float)
// * integer (int32, int64)
// we don't use OpenAPI "format" or "pattern" right now
parameterTypeRegexReplacements = map[string]string{
"string": `([A-z]+)`,
"number": `(\d+)`,
"string": `([.a-zA-Z0-9-]+)`,
"integer": `([0-9]+)`,
"number": `([0-9]*[.])?[0-9]+`,
}
)

Expand Down Expand Up @@ -93,28 +101,29 @@ func generateRouteMatch(path string, method string, pathParameters map[string]Pa
}

var routeMatcher *route.RouteMatch
// if regex in the path - matcher is using RouteMatch_Regex with /{pattern} replaced by /([A-z0-9]+) regex
routePath := path
for _, match := range rePathParams.FindAllString(routePath, -1) {
param := pathParameters[match]

replacementRegex := ""
// if type = enum, construct enum regex capture grouup
if len(param.Enum) > 0 {
enumStrings := convertToStringSlice(param.Enum)
replacementRegex = fmt.Sprintf("(%s)", strings.Join(enumStrings, "|"))
} else if regex, ok := parameterTypeRegexReplacements[param.Type]; ok {
replacementRegex = regex
} else {
replacementRegex = "([A-z0-9]+)"
}

routePath = strings.ReplaceAll(routePath, match, replacementRegex)
}
// Create Path matcher - either regex from above, prefix or simple path match
// Create Path matcher - either regex if there are parameters, prefix or simple path match
switch {
// if has regex - regex matcher
case rePathParams.MatchString(path):
// if regex in the path - matcher is using RouteMatch_Regex with /{pattern} replaced by related regex
routePath := path
for _, match := range rePathParams.FindAllString(routePath, -1) {
param := pathParameters[match]

// default replacement regex
replacementRegex := ""
// if type = enum, construct enum regex capture grouup
if len(param.Enum) > 0 {
enumStrings := convertToStringSlice(param.Enum)
replacementRegex = fmt.Sprintf("(%s)", strings.Join(enumStrings, "|"))
} else if regex, ok := parameterTypeRegexReplacements[param.Type]; ok {
replacementRegex = regex
} else {
// If param type didn't match, we use string, basically - anything valid for URL path
replacementRegex = parameterTypeRegexReplacements["string"]
}
routePath = strings.ReplaceAll(routePath, match, replacementRegex)
}
routeMatcher = &route.RouteMatch{
PathSpecifier: &route.RouteMatch_SafeRegex{
SafeRegex: &envoytypematcher.RegexMatcher{
Expand Down Expand Up @@ -277,7 +286,7 @@ func generateRoutePath(base, path string) string {
}

// Avoids path joins (removes // in e.g. /path//subpath, or //subpath)
return fmt.Sprintf(`%s/%s`, strings.TrimSuffix(base, httpPathSeparator), strings.TrimPrefix(path, httpPathSeparator))
return fmt.Sprintf(`%s/%s`, strings.TrimSuffix(base, "/"), strings.TrimPrefix(path, "/"))
}

func generateRewriteRegex(pattern string, substitution string) *envoytypematcher.RegexMatchAndSubstitute {
Expand Down
2 changes: 0 additions & 2 deletions envoy/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ Domain search order:
4. Special wildcard * matching any domain.
*/

const httpPathSeparator string = "/"

// UpdateConfigFromAPIOpts updates Envoy configuration from OpenAPI spec and x-kusk options
func (e *envoyConfiguration) UpdateConfigFromAPIOpts(opts *options.Options, spec *openapi3.T) error {
vhosts := []string{}
Expand Down