Skip to content

Commit

Permalink
feat: emit warnings if regex-like routes are used with prefix path fo…
Browse files Browse the repository at this point in the history
…rmat
  • Loading branch information
GGabriele committed Sep 14, 2022
1 parent 2b9f975 commit df1e1a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
39 changes: 31 additions & 8 deletions file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ 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 @@ -34,8 +36,6 @@ type stateBuilder struct {
err error
}

var kong140Version = semver.MustParse("1.4.0")

// uuid generates a UUID string and returns a pointer to it.
// It is a variable for testing purpose, to override and supply
// a deterministic UUID generator.
Expand Down Expand Up @@ -295,7 +295,7 @@ func (b *stateBuilder) ingestKeyAuths(creds []kong.KeyAuth) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.KeyAuths = append(b.rawState.KeyAuths, &cred)
Expand All @@ -316,7 +316,7 @@ func (b *stateBuilder) ingestBasicAuths(creds []kong.BasicAuth) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.BasicAuths = append(b.rawState.BasicAuths, &cred)
Expand All @@ -337,7 +337,7 @@ func (b *stateBuilder) ingestHMACAuths(creds []kong.HMACAuth) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.HMACAuths = append(b.rawState.HMACAuths, &cred)
Expand All @@ -358,7 +358,7 @@ func (b *stateBuilder) ingestJWTAuths(creds []kong.JWTAuth) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.JWTAuths = append(b.rawState.JWTAuths, &cred)
Expand All @@ -379,7 +379,7 @@ func (b *stateBuilder) ingestOauth2Creds(creds []kong.Oauth2Credential) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.Oauth2Creds = append(b.rawState.Oauth2Creds, &cred)
Expand All @@ -402,7 +402,7 @@ func (b *stateBuilder) ingestACLGroups(creds []kong.ACLGroup) error {
cred.ID = kong.String(*existingCred.ID)
}
}
if b.kongVersion.GTE(kong140Version) {
if b.kongVersion.GTE(utils.Kong140Version) {
utils.MustMergeTags(&cred, b.selectTags)
}
b.rawState.ACLGroups = append(b.rawState.ACLGroups, &cred)
Expand Down Expand Up @@ -768,6 +768,25 @@ 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(
"From the config file, for the route '%s', a regex pattern usage was detected\n"+
"for the '%s' paths in the 'paths' field.\n"+
"Kong gateway versions 3.0 and above require that regular expressions\n"+
"start with a '~' character to distinguish from simple prefix match.\n\n"+
"For related information, please visit: https://docs.konghq.com/gateway/latest/\n",
*route.Name, strings.Join(pathsWarnings, ", "))
}
}

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

if utils.Kong300Version.LTE(b.kongVersion) {
checkRoutePaths300AndAbove(r)
}

// plugins for the route
var plugins []FPlugin
for _, p := range r.Plugins {
Expand Down
2 changes: 1 addition & 1 deletion file/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ func Test_stateBuilder_consumers(t *testing.T) {
b := &stateBuilder{
targetContent: tt.fields.targetContent,
currentState: tt.fields.currentState,
kongVersion: kong140Version,
kongVersion: utils.Kong140Version,
}
if tt.fields.kongVersion != nil {
b.kongVersion = *tt.fields.kongVersion
Expand Down
13 changes: 12 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ import (
"github.com/kong/go-kong/kong"
)

var kongVersionRegex = regexp.MustCompile(`^\d+\.\d+`)
var (
kongVersionRegex = regexp.MustCompile(`^\d+\.\d+`)
pathRegexPattern = regexp.MustCompile(`[^a-zA-Z0-9._~/%-]`)

Kong140Version = semver.MustParse("1.4.0")
Kong300Version = semver.MustParse("3.0.0")
)

// IsPathRegexLike checks if a path string contains a regex pattern.
func IsPathRegexLike(path string) bool {
return pathRegexPattern.MatchString(path)
}

// Empty checks if a string referenced by s or s itself is empty.
func Empty(s *string) bool {
Expand Down

0 comments on commit df1e1a9

Please sign in to comment.