Skip to content

Commit

Permalink
Catch duplicate endpoints #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schneider authored and Marcel Ludwig committed Nov 9, 2020
1 parent 31ee60e commit 84948e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -35,6 +36,8 @@ var (
errorMissingServer = fmt.Errorf("missing server definitions")
)

var reCleanPattern = regexp.MustCompile(`{([^}]+)}`)

type backendDefinition struct {
conf *config.Backend
handler http.Handler
Expand Down Expand Up @@ -108,10 +111,11 @@ func NewServerConfiguration(conf *config.Gateway, httpConf *HTTPConfig, log *log
for _, endpoint := range srvConf.API.Endpoint {
pattern := utils.JoinPath("/", srvConf.BasePath, srvConf.API.BasePath, endpoint.Pattern)

if endpoints[pattern] {
unique, cleanPattern := isUnique(endpoints, pattern)
if !unique {
log.Fatal("Duplicate endpoint: ", pattern)
}
endpoints[pattern] = true
endpoints[cleanPattern] = true

// setACHandlerFn individual wrap for access_control configuration per endpoint
setACHandlerFn := func(protectedBackend backendDefinition) {
Expand Down Expand Up @@ -397,3 +401,9 @@ func getPathsFromHosts(defaultPort int, hosts []string, path string) []string {
}
return list
}

func isUnique(endpoints map[string]bool, pattern string) (bool, string) {
pattern = reCleanPattern.ReplaceAllString(pattern, "{}")

return !endpoints[pattern], pattern
}
33 changes: 33 additions & 0 deletions config/runtime/server_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package runtime

import "testing"

func TestServer_isUnique(t *testing.T) {
endpoints := make(map[string]bool)

type testCase struct {
pattern string
clean string
unique bool
}

for i, tc := range []testCase{
{"/abc", "/abc", true},
{"/abc", "/abc", false},
{"/abc/", "/abc/", true},
{"/x/{xxx}", "/x/{}", true},
{"/x/{yyy}", "/x/{}", false},
{"/x/{xxx}/a/{yyy}", "/x/{}/a/{}", true},
{"/x/{yyy}/a/{xxx}", "/x/{}/a/{}", false},
} {
unique, cleanPattern := isUnique(endpoints, tc.pattern)
endpoints[cleanPattern] = true

if unique != tc.unique {
t.Errorf("%d: Unexpected unique status given: %t", i+1, unique)
}
if cleanPattern != tc.clean {
t.Errorf("%d: Unexpected cleanPattern given: %s, want %s", i+1, cleanPattern, tc.clean)
}
}
}

0 comments on commit 84948e8

Please sign in to comment.