Skip to content

Commit

Permalink
feat: check routes' paths with 'validate' subcommand too
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele committed Sep 23, 2022
1 parent e40d834 commit 2295077
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
8 changes: 6 additions & 2 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ this command unless --online flag is used.

func validateWithKong(ctx context.Context, kongClient *kong.Client, ks *state.KongState) []error {
// make sure we are able to connect to Kong
_, err := fetchKongVersion(ctx, rootConfig)
kongVersion, err := fetchKongVersion(ctx, rootConfig)
if err != nil {
return []error{fmt.Errorf("couldn't fetch Kong version: %w", err)}
}
parsedKongVersion, err := utils.ParseKongVersion(kongVersion)
if err != nil {
return []error{fmt.Errorf("parsing Kong version: %w", err)}
}
opts := validate.ValidatorOpts{
Ctx: ctx,
State: ks,
Expand All @@ -128,7 +132,7 @@ func validateWithKong(ctx context.Context, kongClient *kong.Client, ks *state.Ko
RBACResourcesOnly: validateCmdRBACResourcesOnly,
}
validator := validate.NewValidator(opts)
return validator.Validate()
return validator.Validate(parsedKongVersion)
}

func getKongClient(ctx context.Context, targetContent *file.Content) (*kong.Client, error) {
Expand Down
27 changes: 1 addition & 26 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/blang/semver/v4"
"github.com/kong/deck/cprint"
"github.com/kong/deck/konnect"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
Expand Down Expand Up @@ -774,29 +772,6 @@ func getStripPathBasedOnProtocols(route kong.Route) (*bool, error) {
return route.StripPath, nil
}

func checkRoutePaths300AndAbove(route FRoute) {
pathsWarnings := []string{}
for _, p := range route.Paths {
if strings.HasPrefix(*p, "~/") || !utils.IsPathRegexLike(*p) {
continue
}
pathsWarnings = append(pathsWarnings, *p)
}
if len(pathsWarnings) > 0 {
cprint.UpdatePrintf(
"In Route '%s', a path with regular expression was detected.\n"+
"In Kong Gateway versions 3.0 and above, all paths that use regular expressions \n"+
"must be prefixed with a ~ character. Without the ~ prefix, regular expression \n"+
"based paths will not be matched and processed correctly. \n\n"+
"Please run the following command to upgrade your config: \n\n"+
"deck convert --from kong-gateway-2.x --to kong-gateway-3.x "+
"--input-file <config-file> --output-file <new-config-file>\n\n"+
"Please refer to the following document for further details:\n"+
"https://docs.konghq.com/deck/latest/3.0-upgrade.\n\n",
*route.ID)
}
}

func (b *stateBuilder) ingestRoute(r FRoute) error {
if utils.Empty(r.ID) {
route, err := b.currentState.Routes.Get(*r.Name)
Expand Down Expand Up @@ -825,7 +800,7 @@ func (b *stateBuilder) ingestRoute(r FRoute) error {
}

if b.checkRoutePaths {
checkRoutePaths300AndAbove(r)
utils.CheckRoutePaths300AndAbove(r.Route)
}

// plugins for the route
Expand Down
26 changes: 26 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/blang/semver/v4"
"github.com/kong/deck/cprint"
"github.com/kong/go-kong/kong"
)

Expand Down Expand Up @@ -188,3 +189,28 @@ func ConfigFilesInDir(dir string) ([]string, error) {
}
return res, nil
}

// CheckRoutePaths300AndAbove checks routes' paths format and prints out
// a warning if regex-like patters without the '~' prefix are found.
func CheckRoutePaths300AndAbove(route kong.Route) {
pathsWarnings := []string{}
for _, p := range route.Paths {
if strings.HasPrefix(*p, "~/") || !IsPathRegexLike(*p) {
continue
}
pathsWarnings = append(pathsWarnings, *p)
}
if len(pathsWarnings) > 0 {
cprint.UpdatePrintf(
"In Route '%s', a path with regular expression was detected.\n"+
"In Kong Gateway versions 3.0 and above, all paths that use regular expressions \n"+
"must be prefixed with a ~ character. Without the ~ prefix, regular expression \n"+
"based paths will not be matched and processed correctly. \n\n"+
"Please run the following command to upgrade your config: \n\n"+
"deck convert --from kong-gateway-2.x --to kong-gateway-3.x "+
"--input-file <config-file> --output-file <new-config-file>\n\n"+
"Please refer to the following document for further details:\n"+
"https://docs.konghq.com/deck/latest/3.0-upgrade.\n\n",
*route.ID)
}
}
15 changes: 14 additions & 1 deletion validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"sync"

"github.com/blang/semver/v4"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (v *Validator) entities(obj interface{}, entityType string) []error {
return errors
}

func (v *Validator) Validate() []error {
func (v *Validator) Validate(kongVersion semver.Version) []error {
allErr := []error{}

// validate RBAC resources first.
Expand Down Expand Up @@ -175,5 +176,17 @@ func (v *Validator) Validate() []error {
if err := v.entities(v.state.Upstreams, "upstreams"); err != nil {
allErr = append(allErr, err...)
}

// validate routes format with Kong 3.x
if utils.Kong300Version.LTE(kongVersion) {
validate3xRoutes(v.state.Routes)
}
return allErr
}

func validate3xRoutes(routes *state.RoutesCollection) {
results, _ := routes.GetAll()
for _, r := range results {
utils.CheckRoutePaths300AndAbove(r.Route)
}
}

0 comments on commit 2295077

Please sign in to comment.