Externalize Træfik rules in a dedicated package

This commit is contained in:
NicoMen 2018-02-26 15:34:04 +01:00 committed by Traefiker Bot
parent 0306b5e8f7
commit 6f81e3479a
5 changed files with 80 additions and 75 deletions

View file

@ -1,280 +0,0 @@
package server
import (
"errors"
"fmt"
"net"
"net/http"
"reflect"
"sort"
"strings"
"github.com/BurntSushi/ty/fun"
"github.com/containous/mux"
"github.com/containous/traefik/types"
)
// Rules holds rule parsing and configuration
type Rules struct {
route *serverRoute
err error
}
func (r *Rules) host(hosts ...string) *mux.Route {
return r.route.route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
reqHost, _, err := net.SplitHostPort(req.Host)
if err != nil {
reqHost = req.Host
}
for _, host := range hosts {
if types.CanonicalDomain(reqHost) == types.CanonicalDomain(host) {
return true
}
}
return false
})
}
func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
router := r.route.route.Subrouter()
for _, host := range hosts {
router.Host(types.CanonicalDomain(host))
}
return r.route.route
}
func (r *Rules) path(paths ...string) *mux.Route {
router := r.route.route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
}
func (r *Rules) pathPrefix(paths ...string) *mux.Route {
router := r.route.route.Subrouter()
for _, path := range paths {
buildPath(path, router)
}
return r.route.route
}
func buildPath(path string, router *mux.Router) {
cleanPath := strings.TrimSpace(path)
// {} are used to define a regex pattern in http://www.gorillatoolkit.org/pkg/mux.
// if we find a { in the path, that means we use regex, then the gorilla/mux implementation is chosen
// otherwise, we use a lightweight implementation
if strings.Contains(cleanPath, "{") {
router.PathPrefix(cleanPath)
} else {
m := &prefixMatcher{prefix: cleanPath}
router.NewRoute().MatcherFunc(m.Match)
}
}
type prefixMatcher struct {
prefix string
}
func (m *prefixMatcher) Match(r *http.Request, _ *mux.RouteMatch) bool {
return strings.HasPrefix(r.URL.Path, m.prefix) || strings.HasPrefix(r.URL.Path, m.prefix+"/")
}
type bySize []string
func (a bySize) Len() int { return len(a) }
func (a bySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a bySize) Less(i, j int) bool { return len(a[i]) > len(a[j]) }
func (r *Rules) pathStrip(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixes = paths
router := r.route.route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
}
func (r *Rules) pathStripRegex(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixesRegex = paths
router := r.route.route.Subrouter()
for _, path := range paths {
router.Path(strings.TrimSpace(path))
}
return r.route.route
}
func (r *Rules) replacePath(paths ...string) *mux.Route {
for _, path := range paths {
r.route.replacePath = path
}
return r.route.route
}
func (r *Rules) replacePathRegex(paths ...string) *mux.Route {
for _, path := range paths {
r.route.replacePathRegex = path
}
return r.route.route
}
func (r *Rules) addPrefix(paths ...string) *mux.Route {
for _, path := range paths {
r.route.addPrefix = path
}
return r.route.route
}
func (r *Rules) pathPrefixStrip(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixes = paths
router := r.route.route.Subrouter()
for _, path := range paths {
buildPath(path, router)
}
return r.route.route
}
func (r *Rules) pathPrefixStripRegex(paths ...string) *mux.Route {
sort.Sort(bySize(paths))
r.route.stripPrefixesRegex = paths
router := r.route.route.Subrouter()
for _, path := range paths {
router.PathPrefix(strings.TrimSpace(path))
}
return r.route.route
}
func (r *Rules) methods(methods ...string) *mux.Route {
return r.route.route.Methods(methods...)
}
func (r *Rules) headers(headers ...string) *mux.Route {
return r.route.route.Headers(headers...)
}
func (r *Rules) headersRegexp(headers ...string) *mux.Route {
return r.route.route.HeadersRegexp(headers...)
}
func (r *Rules) query(query ...string) *mux.Route {
var queries []string
for _, elem := range query {
queries = append(queries, strings.Split(elem, "=")...)
}
return r.route.route.Queries(queries...)
}
func (r *Rules) parseRules(expression string, onRule func(functionName string, function interface{}, arguments []string) error) error {
functions := map[string]interface{}{
"Host": r.host,
"HostRegexp": r.hostRegexp,
"Path": r.path,
"PathStrip": r.pathStrip,
"PathStripRegex": r.pathStripRegex,
"PathPrefix": r.pathPrefix,
"PathPrefixStrip": r.pathPrefixStrip,
"PathPrefixStripRegex": r.pathPrefixStripRegex,
"Method": r.methods,
"Headers": r.headers,
"HeadersRegexp": r.headersRegexp,
"AddPrefix": r.addPrefix,
"ReplacePath": r.replacePath,
"ReplacePathRegex": r.replacePathRegex,
"Query": r.query,
}
if len(expression) == 0 {
return errors.New("Empty rule")
}
f := func(c rune) bool {
return c == ':'
}
// Allow multiple rules separated by ;
splitRule := func(c rune) bool {
return c == ';'
}
parsedRules := strings.FieldsFunc(expression, splitRule)
for _, rule := range parsedRules {
// get function
parsedFunctions := strings.FieldsFunc(rule, f)
if len(parsedFunctions) == 0 {
return fmt.Errorf("error parsing rule: '%s'", rule)
}
functionName := strings.TrimSpace(parsedFunctions[0])
parsedFunction, ok := functions[functionName]
if !ok {
return fmt.Errorf("error parsing rule: '%s'. Unknown function: '%s'", rule, parsedFunctions[0])
}
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
fargs := func(c rune) bool {
return c == ','
}
// get function
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
if len(parsedArgs) == 0 {
return fmt.Errorf("error parsing args from rule: '%s'", rule)
}
for i := range parsedArgs {
parsedArgs[i] = strings.TrimSpace(parsedArgs[i])
}
err := onRule(functionName, parsedFunction, parsedArgs)
if err != nil {
return fmt.Errorf("Parsing error on rule: %v", err)
}
}
return nil
}
// Parse parses rules expressions
func (r *Rules) Parse(expression string) (*mux.Route, error) {
var resultRoute *mux.Route
err := r.parseRules(expression, func(functionName string, function interface{}, arguments []string) error {
inputs := make([]reflect.Value, len(arguments))
for i := range arguments {
inputs[i] = reflect.ValueOf(arguments[i])
}
method := reflect.ValueOf(function)
if method.IsValid() {
resultRoute = method.Call(inputs)[0].Interface().(*mux.Route)
if r.err != nil {
return r.err
}
if resultRoute.GetError() != nil {
return resultRoute.GetError()
}
} else {
return fmt.Errorf("Method not found: '%s'", functionName)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error parsing rule: %v", err)
}
return resultRoute, nil
}
// ParseDomains parses rules expressions and returns domains
func (r *Rules) ParseDomains(expression string) ([]string, error) {
domains := []string{}
err := r.parseRules(expression, func(functionName string, function interface{}, arguments []string) error {
if functionName == "Host" {
domains = append(domains, arguments...)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error parsing domains: %v", err)
}
return fun.Map(types.CanonicalDomain, domains).([]string), nil
}

View file

@ -1,258 +0,0 @@
package server
import (
"net/http"
"net/url"
"testing"
"github.com/containous/mux"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseOneRule(t *testing.T) {
router := mux.NewRouter()
route := router.NewRoute()
serverRoute := &serverRoute{route: route}
rules := &Rules{route: serverRoute}
expression := "Host:foo.bar"
routeResult, err := rules.Parse(expression)
require.NoError(t, err, "Error while building route for %s", expression)
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.True(t, routeMatch, "Rule %s don't match.", expression)
}
func TestParseTwoRules(t *testing.T) {
router := mux.NewRouter()
route := router.NewRoute()
serverRoute := &serverRoute{route: route}
rules := &Rules{route: serverRoute}
expression := "Host: Foo.Bar ; Path:/FOObar"
routeResult, err := rules.Parse(expression)
require.NoError(t, err, "Error while building route for %s.", expression)
request := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/foobar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.False(t, routeMatch, "Rule %s don't match.", expression)
request = testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/FOObar", nil)
routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
assert.True(t, routeMatch, "Rule %s don't match.", expression)
}
func TestParseDomains(t *testing.T) {
rules := &Rules{}
tests := []struct {
expression string
domain []string
}{
{
expression: "Host:foo.bar,test.bar",
domain: []string{"foo.bar", "test.bar"},
},
{
expression: "Path:/test",
domain: []string{},
},
{
expression: "Host:foo.bar;Path:/test",
domain: []string{"foo.bar"},
},
{
expression: "Host: Foo.Bar ;Path:/test",
domain: []string{"foo.bar"},
},
}
for _, test := range tests {
test := test
t.Run(test.expression, func(t *testing.T) {
t.Parallel()
domains, err := rules.ParseDomains(test.expression)
require.NoError(t, err, "%s: Error while parsing domain.", test.expression)
assert.EqualValues(t, test.domain, domains, "%s: Error parsing domains from expression.", test.expression)
})
}
}
func TestPriorites(t *testing.T) {
router := mux.NewRouter()
router.StrictSlash(true)
rules := &Rules{route: &serverRoute{route: router.NewRoute()}}
expression01 := "PathPrefix:/foo"
routeFoo, err := rules.Parse(expression01)
require.NoError(t, err, "Error while building route for %s", expression01)
fooHandler := &fakeHandler{name: "fooHandler"}
routeFoo.Handler(fooHandler)
routeMatch := router.Match(&http.Request{URL: &url.URL{Path: "/foo"}}, &mux.RouteMatch{})
assert.True(t, routeMatch, "Error matching route")
routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/fo"}}, &mux.RouteMatch{})
assert.False(t, routeMatch, "Error matching route")
multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}}
expression02 := "PathPrefix:/foobar"
routeFoobar, err := multipleRules.Parse(expression02)
require.NoError(t, err, "Error while building route for %s", expression02)
foobarHandler := &fakeHandler{name: "foobarHandler"}
routeFoobar.Handler(foobarHandler)
routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foo"}}, &mux.RouteMatch{})
assert.True(t, routeMatch, "Error matching route")
fooMatcher := &mux.RouteMatch{}
routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foobar"}}, fooMatcher)
assert.True(t, routeMatch, "Error matching route")
assert.NotEqual(t, fooMatcher.Handler, foobarHandler, "Error matching priority")
assert.Equal(t, fooMatcher.Handler, fooHandler, "Error matching priority")
routeFoo.Priority(1)
routeFoobar.Priority(10)
router.SortRoutes()
foobarMatcher := &mux.RouteMatch{}
routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foobar"}}, foobarMatcher)
assert.True(t, routeMatch, "Error matching route")
assert.Equal(t, foobarMatcher.Handler, foobarHandler, "Error matching priority")
assert.NotEqual(t, foobarMatcher.Handler, fooHandler, "Error matching priority")
}
func TestHostRegexp(t *testing.T) {
testCases := []struct {
desc string
hostExp string
urls map[string]bool
}{
{
desc: "capturing group",
hostExp: "{subdomain:(foo\\.)?bar\\.com}",
urls: map[string]bool{
"http://foo.bar.com": true,
"http://bar.com": true,
"http://fooubar.com": false,
"http://barucom": false,
"http://barcom": false,
},
},
{
desc: "non capturing group",
hostExp: "{subdomain:(?:foo\\.)?bar\\.com}",
urls: map[string]bool{
"http://foo.bar.com": true,
"http://bar.com": true,
"http://fooubar.com": false,
"http://barucom": false,
"http://barcom": false,
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rls := &Rules{
route: &serverRoute{
route: &mux.Route{},
},
}
rt := rls.hostRegexp(test.hostExp)
for testURL, match := range test.urls {
req := testhelpers.MustNewRequest(http.MethodGet, testURL, nil)
assert.Equal(t, match, rt.Match(req, &mux.RouteMatch{}))
}
})
}
}
type fakeHandler struct {
name string
}
func (h *fakeHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}
func TestPathPrefix(t *testing.T) {
testCases := []struct {
desc string
path string
urls map[string]bool
}{
{
desc: "leading slash",
path: "/bar",
urls: map[string]bool{
"http://foo.com/bar": true,
"http://foo.com/bar/": true,
},
},
{
desc: "leading trailing slash",
path: "/bar/",
urls: map[string]bool{
"http://foo.com/bar": false,
"http://foo.com/bar/": true,
},
},
{
desc: "no slash",
path: "bar",
urls: map[string]bool{
"http://foo.com/bar": false,
"http://foo.com/bar/": false,
},
},
{
desc: "trailing slash",
path: "bar/",
urls: map[string]bool{
"http://foo.com/bar": false,
"http://foo.com/bar/": false,
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rls := &Rules{
route: &serverRoute{
route: &mux.Route{},
},
}
rt := rls.pathPrefix(test.path)
for testURL, expectedMatch := range test.urls {
req := testhelpers.MustNewRequest(http.MethodGet, testURL, nil)
match := rt.Match(req, &mux.RouteMatch{})
if match != expectedMatch {
t.Errorf("Error matching %s with %s, got %v expected %v", test.path, testURL, match, expectedMatch)
}
}
})
}
}

View file

@ -34,6 +34,7 @@ import (
"github.com/containous/traefik/middlewares/redirect"
"github.com/containous/traefik/middlewares/tracing"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/rules"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/server/cookie"
traefikTls "github.com/containous/traefik/tls"
@ -84,15 +85,6 @@ type serverEntryPoint struct {
certs safe.Safe
}
type serverRoute struct {
route *mux.Route
stripPrefixes []string
stripPrefixesRegex []string
addPrefix string
replacePath string
replacePathRegex string
}
// NewServer returns an initialized Server.
func NewServer(globalConfiguration configuration.GlobalConfiguration, provider provider.Provider) *Server {
server := new(Server)
@ -524,7 +516,7 @@ func (s *Server) postLoadConfiguration() {
if acmeEnabled {
for _, route := range frontend.Routes {
rules := Rules{}
rules := rules.Rules{}
domains, err := rules.ParseDomains(route.Rule)
if err != nil {
log.Errorf("Error parsing domains: %v", err)
@ -899,7 +891,7 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
for _, entryPointName := range frontend.EntryPoints {
log.Debugf("Wiring frontend %s to entryPoint %s", frontendName, entryPointName)
newServerRoute := &serverRoute{route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)}
newServerRoute := &types.ServerRoute{Route: serverEntryPoints[entryPointName].httpRouter.GetHandler().NewRoute().Name(frontendName)}
for routeName, route := range frontend.Routes {
err := getRoute(newServerRoute, &route)
if err != nil {
@ -1177,11 +1169,11 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
log.Debugf("Reusing backend %s", frontend.Backend)
}
if frontend.Priority > 0 {
newServerRoute.route.Priority(frontend.Priority)
newServerRoute.Route.Priority(frontend.Priority)
}
s.wireFrontendBackend(newServerRoute, backends[entryPointName+providerName+frontend.Backend])
err := newServerRoute.route.GetError()
err := newServerRoute.Route.GetError()
if err != nil {
log.Errorf("Error building route: %s", err)
}
@ -1236,48 +1228,48 @@ func configureIPWhitelistMiddleware(whitelistSourceRanges []string) (negroni.Han
return nil, nil
}
func (s *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
func (s *Server) wireFrontendBackend(serverRoute *types.ServerRoute, handler http.Handler) {
// path replace - This needs to always be the very last on the handler chain (first in the order in this function)
// -- Replacing Path should happen at the very end of the Modifier chain, after all the Matcher+Modifiers ran
if len(serverRoute.replacePath) > 0 {
if len(serverRoute.ReplacePath) > 0 {
handler = &middlewares.ReplacePath{
Path: serverRoute.replacePath,
Path: serverRoute.ReplacePath,
Handler: handler,
}
}
if len(serverRoute.replacePathRegex) > 0 {
sp := strings.Split(serverRoute.replacePathRegex, " ")
if len(serverRoute.ReplacePathRegex) > 0 {
sp := strings.Split(serverRoute.ReplacePathRegex, " ")
if len(sp) == 2 {
handler = middlewares.NewReplacePathRegexHandler(sp[0], sp[1], handler)
} else {
log.Warnf("Invalid syntax for ReplacePathRegex: %s. Separate the regular expression and the replacement by a space.", serverRoute.replacePathRegex)
log.Warnf("Invalid syntax for ReplacePathRegex: %s. Separate the regular expression and the replacement by a space.", serverRoute.ReplacePathRegex)
}
}
// add prefix - This needs to always be right before ReplacePath on the chain (second in order in this function)
// -- Adding Path Prefix should happen after all *Strip Matcher+Modifiers ran, but before Replace (in case it's configured)
if len(serverRoute.addPrefix) > 0 {
if len(serverRoute.AddPrefix) > 0 {
handler = &middlewares.AddPrefix{
Prefix: serverRoute.addPrefix,
Prefix: serverRoute.AddPrefix,
Handler: handler,
}
}
// strip prefix
if len(serverRoute.stripPrefixes) > 0 {
if len(serverRoute.StripPrefixes) > 0 {
handler = &middlewares.StripPrefix{
Prefixes: serverRoute.stripPrefixes,
Prefixes: serverRoute.StripPrefixes,
Handler: handler,
}
}
// strip prefix with regex
if len(serverRoute.stripPrefixesRegex) > 0 {
handler = middlewares.NewStripPrefixRegex(handler, serverRoute.stripPrefixesRegex)
if len(serverRoute.StripPrefixesRegex) > 0 {
handler = middlewares.NewStripPrefixRegex(handler, serverRoute.StripPrefixesRegex)
}
serverRoute.route.Handler(handler)
serverRoute.Route.Handler(handler)
}
func (s *Server) buildRedirectHandler(srcEntryPointName string, opt *types.Redirect) (negroni.Handler, error) {
@ -1335,14 +1327,14 @@ func parseHealthCheckOptions(lb healthcheck.LoadBalancer, backend string, hc *ty
}
}
func getRoute(serverRoute *serverRoute, route *types.Route) error {
rules := Rules{route: serverRoute}
func getRoute(serverRoute *types.ServerRoute, route *types.Route) error {
rules := rules.Rules{Route: serverRoute}
newRoute, err := rules.Parse(route.Rule)
if err != nil {
return err
}
newRoute.Priority(serverRoute.route.GetPriority() + len(route.Rule))
serverRoute.route = newRoute
newRoute.Priority(serverRoute.Route.GetPriority() + len(route.Rule))
serverRoute.Route = newRoute
return nil
}

View file

@ -15,6 +15,7 @@ import (
"github.com/containous/traefik/healthcheck"
"github.com/containous/traefik/metrics"
"github.com/containous/traefik/middlewares"
"github.com/containous/traefik/rules"
"github.com/containous/traefik/testhelpers"
"github.com/containous/traefik/tls"
"github.com/containous/traefik/types"
@ -393,8 +394,8 @@ func TestServerMultipleFrontendRules(t *testing.T) {
router := mux.NewRouter()
route := router.NewRoute()
serverRoute := &serverRoute{route: route}
rules := &Rules{route: serverRoute}
serverRoute := &types.ServerRoute{Route: route}
rules := &rules.Rules{Route: serverRoute}
expression := test.expression
routeResult, err := rules.Parse(expression)
@ -417,7 +418,7 @@ func TestServerMultipleFrontendRules(t *testing.T) {
t.Fatalf("got URL %s, expected %s", r.URL.String(), test.expectedURL)
}
}))
serverRoute.route.GetHandler().ServeHTTP(nil, request)
serverRoute.Route.GetHandler().ServeHTTP(nil, request)
})
}
}