diff --git a/docs/content/providers/consul-catalog.md b/docs/content/providers/consul-catalog.md index d177df93c..9ea44de94 100644 --- a/docs/content/providers/consul-catalog.md +++ b/docs/content/providers/consul-catalog.md @@ -529,6 +529,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `connectAware` _Optional, Default=false_ diff --git a/docs/content/providers/docker.md b/docs/content/providers/docker.md index 50cfd2a2a..e3fc985fc 100644 --- a/docs/content/providers/docker.md +++ b/docs/content/providers/docker.md @@ -466,6 +466,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `swarmMode` _Optional, Default=false_ diff --git a/docs/content/providers/ecs.md b/docs/content/providers/ecs.md index 6e3d56a1f..80a11a35e 100644 --- a/docs/content/providers/ecs.md +++ b/docs/content/providers/ecs.md @@ -263,6 +263,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `refreshSeconds` _Optional, Default=15_ diff --git a/docs/content/providers/marathon.md b/docs/content/providers/marathon.md index a0ab85ccb..0b5c2794b 100644 --- a/docs/content/providers/marathon.md +++ b/docs/content/providers/marathon.md @@ -142,6 +142,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `dialerTimeout` _Optional, Default=5s_ diff --git a/docs/content/providers/nomad.md b/docs/content/providers/nomad.md index 572ccc0d6..8ff435b2a 100644 --- a/docs/content/providers/nomad.md +++ b/docs/content/providers/nomad.md @@ -377,6 +377,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `constraints` _Optional, Default=""_ diff --git a/docs/content/providers/rancher.md b/docs/content/providers/rancher.md index b709a5329..f43b70b1f 100644 --- a/docs/content/providers/rancher.md +++ b/docs/content/providers/rancher.md @@ -125,6 +125,13 @@ providers: # ... ``` +??? info "Default rule and Traefik service" + + The exposure of the Traefik container, combined with the default rule mechanism, + can lead to create a router targeting itself in a loop. + In this case, to prevent an infinite loop, + Traefik adds an internal middleware to refuse the request if it comes from the same router. + ### `enableServiceHealthFilter` _Optional, Default=true_ diff --git a/pkg/config/dynamic/http_config.go b/pkg/config/dynamic/http_config.go index db89e049b..af97325d7 100644 --- a/pkg/config/dynamic/http_config.go +++ b/pkg/config/dynamic/http_config.go @@ -48,6 +48,7 @@ type Router struct { Rule string `json:"rule,omitempty" toml:"rule,omitempty" yaml:"rule,omitempty"` Priority int `json:"priority,omitempty" toml:"priority,omitempty,omitzero" yaml:"priority,omitempty" export:"true"` TLS *RouterTLSConfig `json:"tls,omitempty" toml:"tls,omitempty" yaml:"tls,omitempty" label:"allowEmpty" file:"allowEmpty" kv:"allowEmpty" export:"true"` + DefaultRule bool `json:"-" toml:"-" yaml:"-" label:"-" file:"-"` } // +k8s:deepcopy-gen=true diff --git a/pkg/middlewares/denyrouterrecursion/deny_router_recursion.go b/pkg/middlewares/denyrouterrecursion/deny_router_recursion.go new file mode 100644 index 000000000..67e6f79bc --- /dev/null +++ b/pkg/middlewares/denyrouterrecursion/deny_router_recursion.go @@ -0,0 +1,61 @@ +package denyrouterrecursion + +import ( + "errors" + "hash/fnv" + "net/http" + "strconv" + + "github.com/containous/alice" + "github.com/traefik/traefik/v2/pkg/log" +) + +const xTraefikRouter = "X-Traefik-Router" + +type DenyRouterRecursion struct { + routerName string + routerNameHash string + next http.Handler +} + +// WrapHandler Wraps router to alice.Constructor. +func WrapHandler(routerName string) alice.Constructor { + return func(next http.Handler) (http.Handler, error) { + return New(routerName, next) + } +} + +// New creates a new DenyRouterRecursion. +// DenyRouterRecursion middleware is an internal middleware used to avoid infinite requests loop on the same router. +func New(routerName string, next http.Handler) (*DenyRouterRecursion, error) { + if routerName == "" { + return nil, errors.New("routerName cannot be empty") + } + + return &DenyRouterRecursion{ + routerName: routerName, + routerNameHash: makeHash(routerName), + next: next, + }, nil +} + +// ServeHTTP implements http.Handler. +func (l *DenyRouterRecursion) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if req.Header.Get(xTraefikRouter) == l.routerNameHash { + log.WithoutContext().Debugf("Rejecting request in provenance of the same router (%q) to stop potential infinite loop.", l.routerName) + rw.WriteHeader(http.StatusBadRequest) + + return + } + + req.Header.Set(xTraefikRouter, l.routerNameHash) + + l.next.ServeHTTP(rw, req) +} + +func makeHash(routerName string) string { + hasher := fnv.New64() + // purposely ignoring the error, as no error can be returned from the implementation. + _, _ = hasher.Write([]byte(routerName)) + return strconv.FormatUint(hasher.Sum64(), 16) +} diff --git a/pkg/middlewares/denyrouterrecursion/deny_router_recursion_test.go b/pkg/middlewares/denyrouterrecursion/deny_router_recursion_test.go new file mode 100644 index 000000000..c93653db7 --- /dev/null +++ b/pkg/middlewares/denyrouterrecursion/deny_router_recursion_test.go @@ -0,0 +1,38 @@ +package denyrouterrecursion + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestServeHTTP(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "", nil) + require.NoError(t, err) + + _, err = New("", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})) + require.Error(t, err) + + next := 0 + m, err := New("myRouter", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + next++ + })) + require.NoError(t, err) + + recorder := httptest.NewRecorder() + m.ServeHTTP(recorder, req) + + assert.Equal(t, http.StatusOK, recorder.Code) + assert.Equal(t, "995d26092d19a224", m.routerNameHash) + assert.Equal(t, m.routerNameHash, req.Header.Get(xTraefikRouter)) + assert.Equal(t, 1, next) + + recorder = httptest.NewRecorder() + m.ServeHTTP(recorder, req) + + assert.Equal(t, 1, next) + assert.Equal(t, http.StatusBadRequest, recorder.Code) +} diff --git a/pkg/provider/configuration.go b/pkg/provider/configuration.go index 1000e696f..261d68156 100644 --- a/pkg/provider/configuration.go +++ b/pkg/provider/configuration.go @@ -349,7 +349,7 @@ func BuildTCPRouterConfiguration(ctx context.Context, configuration *dynamic.TCP continue } - if len(router.Service) == 0 { + if router.Service == "" { if len(configuration.Services) > 1 { delete(configuration.Routers, routerName) loggerRouter. @@ -368,7 +368,7 @@ func BuildTCPRouterConfiguration(ctx context.Context, configuration *dynamic.TCP func BuildUDPRouterConfiguration(ctx context.Context, configuration *dynamic.UDPConfiguration) { for routerName, router := range configuration.Routers { loggerRouter := log.FromContext(ctx).WithField(log.RouterName, routerName) - if len(router.Service) > 0 { + if router.Service != "" { continue } @@ -413,9 +413,12 @@ func BuildRouterConfiguration(ctx context.Context, configuration *dynamic.HTTPCo delete(configuration.Routers, routerName) continue } + + // Flag default rule routers to add the denyRouterRecursion middleware. + router.DefaultRule = true } - if len(router.Service) == 0 { + if router.Service == "" { if len(configuration.Services) > 1 { delete(configuration.Routers, routerName) loggerRouter. diff --git a/pkg/provider/consulcatalog/config_test.go b/pkg/provider/consulcatalog/config_test.go index 3d54b5436..666320404 100644 --- a/pkg/provider/consulcatalog/config_test.go +++ b/pkg/provider/consulcatalog/config_test.go @@ -49,8 +49,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`foo.bar`)", + Service: "Test", + Rule: "Host(`foo.bar`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -99,8 +100,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: `Host("Test.foo.bar")`, + Service: "Test", + Rule: `Host("Test.foo.bar")`, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -233,8 +235,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test`)", + Service: "Test", + Rule: "Host(`Test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -318,8 +321,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "dev-Test": { - Service: "dev-Test", - Rule: "Host(`dev-Test.traefik.wtf`)", + Service: "dev-Test", + Rule: "Host(`dev-Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -371,8 +375,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "dev-Test": { - Service: "dev-Test", - Rule: "Host(`dev-Test.traefik.wtf`)", + Service: "dev-Test", + Rule: "Host(`dev-Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -454,8 +459,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "dev-Test": { - Service: "dev-Test", - Rule: "Host(`dev-Test.traefik.wtf`)", + Service: "dev-Test", + Rule: "Host(`dev-Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -529,12 +535,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.wtf`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -599,8 +607,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -658,8 +667,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -714,8 +724,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -765,8 +776,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1024,8 +1036,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1081,8 +1094,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1128,8 +1142,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1179,8 +1194,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Services: map[string]*dynamic.Service{ @@ -1242,8 +1258,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -1308,8 +1325,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1381,8 +1399,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1626,8 +1645,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1675,8 +1695,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1934,8 +1955,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1985,6 +2007,7 @@ func Test_buildConfiguration(t *testing.T) { "Test": { Service: "Test", Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, Middlewares: []string{"Middleware1"}, }, }, @@ -2375,8 +2398,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2458,8 +2482,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2656,12 +2681,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, "Test-97077516270503695": { - Service: "Test-97077516270503695", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test-97077516270503695", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/provider/docker/config_test.go b/pkg/provider/docker/config_test.go index ed8b58b7c..cb7877226 100644 --- a/pkg/provider/docker/config_test.go +++ b/pkg/provider/docker/config_test.go @@ -54,8 +54,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`foo.bar`)", + Service: "Test", + Rule: "Host(`foo.bar`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -109,8 +110,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.foo.bar`)", + Service: "Test", + Rule: "Host(`Test.foo.bar`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -166,8 +168,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: `Host("Test.foo.bar")`, + Service: "Test", + Rule: `Host("Test.foo.bar")`, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -321,8 +324,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test`)", + Service: "Test", + Rule: "Host(`Test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -532,8 +536,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -602,12 +607,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.wtf`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -688,8 +695,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -747,8 +755,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1114,8 +1123,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1198,8 +1208,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1263,8 +1274,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1322,8 +1334,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Services: map[string]*dynamic.Service{ @@ -1404,8 +1417,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -1489,8 +1503,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1587,8 +1602,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1977,8 +1993,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2034,8 +2051,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2281,8 +2299,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2515,8 +2534,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2575,6 +2595,7 @@ func Test_buildConfiguration(t *testing.T) { Service: "Test", Rule: "Host(`Test.traefik.wtf`)", Middlewares: []string{"Middleware1"}, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -3025,8 +3046,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -3194,8 +3216,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/provider/ecs/config_test.go b/pkg/provider/ecs/config_test.go index a34a53766..76b18887f 100644 --- a/pkg/provider/ecs/config_test.go +++ b/pkg/provider/ecs/config_test.go @@ -50,8 +50,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`foo.bar`)", + Service: "Test", + Rule: "Host(`foo.bar`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -100,8 +101,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.foo.bar`)", + Service: "Test", + Rule: "Host(`Test.foo.bar`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -152,8 +154,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: `Host("Test.foo.bar")`, + Service: "Test", + Rule: `Host("Test.foo.bar")`, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -292,8 +295,9 @@ func TestDefaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test`)", + Service: "Test", + Rule: "Host(`Test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -482,8 +486,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -542,12 +547,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.wtf`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -618,8 +625,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -672,8 +680,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1004,8 +1013,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1073,8 +1083,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1128,8 +1139,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1182,8 +1194,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Services: map[string]*dynamic.Service{ @@ -1254,8 +1267,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -1329,8 +1343,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1412,8 +1427,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1753,8 +1769,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1805,8 +1822,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1857,8 +1875,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2243,8 +2262,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2298,6 +2318,7 @@ func Test_buildConfiguration(t *testing.T) { Service: "Test", Rule: "Host(`Test.traefik.wtf`)", Middlewares: []string{"Middleware1"}, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -2708,8 +2729,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/provider/marathon/config_test.go b/pkg/provider/marathon/config_test.go index f49559ab9..8bab4768e 100644 --- a/pkg/provider/marathon/config_test.go +++ b/pkg/provider/marathon/config_test.go @@ -58,8 +58,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -124,8 +125,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -169,6 +171,7 @@ func TestBuildConfiguration(t *testing.T) { Service: "app", Rule: "Host(`app.marathon.localhost`)", Middlewares: []string{"Middleware1"}, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -387,12 +390,14 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "foo": { - Service: "foo", - Rule: "Host(`foo.marathon.localhost`)", + Service: "foo", + Rule: "Host(`foo.marathon.localhost`)", + DefaultRule: true, }, "bar": { - Service: "bar", - Rule: "Host(`bar.marathon.localhost`)", + Service: "bar", + Rule: "Host(`bar.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -439,8 +444,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -485,8 +491,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "Service1", - Rule: "Host(`app.marathon.localhost`)", + Service: "Service1", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -714,12 +721,14 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "Service1", - Rule: "Host(`app.marathon.localhost`)", + Service: "Service1", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, "app2": { - Service: "Service1", - Rule: "Host(`app2.marathon.localhost`)", + Service: "Service1", + Rule: "Host(`app2.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -756,12 +765,14 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, "app2": { - Service: "app2", - Rule: "Host(`app2.marathon.localhost`)", + Service: "app2", + Rule: "Host(`app2.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -825,12 +836,14 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, "app2": { - Service: "app2", - Rule: "Host(`app2.marathon.localhost`)", + Service: "app2", + Rule: "Host(`app2.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1041,8 +1054,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1085,8 +1099,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "Service1", - Rule: "Host(`app.marathon.localhost`)", + Service: "Service1", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1341,8 +1356,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1385,8 +1401,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "app", - Rule: "Host(`app.marathon.localhost`)", + Service: "app", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1428,8 +1445,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "a_b_app": { - Service: "a_b_app", - Rule: `Host("app.b.a.marathon.localhost")`, + Service: "a_b_app", + Rule: `Host("app.b.a.marathon.localhost")`, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1752,8 +1770,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "bar", - Rule: "Host(`app.marathon.localhost`)", + Service: "bar", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1812,8 +1831,9 @@ func TestBuildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "app": { - Service: "bar", - Rule: "Host(`app.marathon.localhost`)", + Service: "bar", + Rule: "Host(`app.marathon.localhost`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/provider/nomad/config_test.go b/pkg/provider/nomad/config_test.go index 9d0aa21b8..c4047f924 100644 --- a/pkg/provider/nomad/config_test.go +++ b/pkg/provider/nomad/config_test.go @@ -42,8 +42,9 @@ func Test_defaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`example.com`)", + Service: "Test", + Rule: "Host(`example.com`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -92,8 +93,9 @@ func Test_defaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: `Host("Test.example.com")`, + Service: "Test", + Rule: `Host("Test.example.com")`, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -181,8 +183,9 @@ func Test_defaultRule(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test`)", + Service: "Test", + Rule: "Host(`Test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -251,8 +254,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "dev-Test": { - Service: "dev-Test", - Rule: "Host(`dev-Test.traefik.test`)", + Service: "dev-Test", + Rule: "Host(`dev-Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -305,12 +309,14 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test1": { - Service: "Test1", - Rule: "Host(`Test1.traefik.test`)", + Service: "Test1", + Rule: "Host(`Test1.traefik.test`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.test`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -375,8 +381,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -434,8 +441,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -490,8 +498,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -541,8 +550,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -801,8 +811,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -858,8 +869,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -905,8 +917,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -956,8 +969,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Services: map[string]*dynamic.Service{ @@ -1020,8 +1034,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -1087,8 +1102,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1255,8 +1271,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1304,8 +1321,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1533,8 +1551,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -1585,6 +1604,7 @@ func Test_buildConfig(t *testing.T) { Service: "Test", Rule: "Host(`Test.traefik.test`)", Middlewares: []string{"Middleware1"}, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -1974,8 +1994,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2057,8 +2078,9 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.test`)", + Service: "Service1", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -2254,12 +2276,14 @@ func Test_buildConfig(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.test`)", + Service: "Test", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, "Test-1234154071633021619": { - Service: "Test-1234154071633021619", - Rule: "Host(`Test.traefik.test`)", + Service: "Test-1234154071633021619", + Rule: "Host(`Test.traefik.test`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/provider/rancher/config_test.go b/pkg/provider/rancher/config_test.go index 2f94c559e..efab43f4a 100644 --- a/pkg/provider/rancher/config_test.go +++ b/pkg/provider/rancher/config_test.go @@ -44,8 +44,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -98,12 +99,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test1": { - Service: "Test1", - Rule: "Host(`Test1.traefik.wtf`)", + Service: "Test1", + Rule: "Host(`Test1.traefik.wtf`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.wtf`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -166,12 +169,14 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test1": { - Service: "Test1", - Rule: "Host(`Test1.traefik.wtf`)", + Service: "Test1", + Rule: "Host(`Test1.traefik.wtf`)", + DefaultRule: true, }, "Test2": { - Service: "Test2", - Rule: "Host(`Test2.traefik.wtf`)", + Service: "Test2", + Rule: "Host(`Test2.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -421,8 +426,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -473,6 +479,7 @@ func Test_buildConfiguration(t *testing.T) { Service: "Test", Rule: "Host(`Test.traefik.wtf`)", Middlewares: []string{"Middleware1"}, + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{ @@ -585,8 +592,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Test", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Test", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -893,8 +901,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, @@ -964,8 +973,9 @@ func Test_buildConfiguration(t *testing.T) { HTTP: &dynamic.HTTPConfiguration{ Routers: map[string]*dynamic.Router{ "Test": { - Service: "Service1", - Rule: "Host(`Test.traefik.wtf`)", + Service: "Service1", + Rule: "Host(`Test.traefik.wtf`)", + DefaultRule: true, }, }, Middlewares: map[string]*dynamic.Middleware{}, diff --git a/pkg/server/router/router.go b/pkg/server/router/router.go index 46b6f3cac..e8f214ffb 100644 --- a/pkg/server/router/router.go +++ b/pkg/server/router/router.go @@ -11,6 +11,7 @@ import ( "github.com/traefik/traefik/v2/pkg/log" "github.com/traefik/traefik/v2/pkg/metrics" "github.com/traefik/traefik/v2/pkg/middlewares/accesslog" + "github.com/traefik/traefik/v2/pkg/middlewares/denyrouterrecursion" metricsMiddle "github.com/traefik/traefik/v2/pkg/middlewares/metrics" "github.com/traefik/traefik/v2/pkg/middlewares/recovery" "github.com/traefik/traefik/v2/pkg/middlewares/tracing" @@ -202,6 +203,10 @@ func (m *Manager) buildHTTPHandler(ctx context.Context, router *runtime.RouterIn chain = chain.Append(metricsMiddle.WrapRouterHandler(ctx, m.metricsRegistry, routerName, provider.GetQualifiedName(ctx, router.Service))) } + if router.DefaultRule { + chain = chain.Append(denyrouterrecursion.WrapHandler(routerName)) + } + return chain.Extend(*mHandler).Append(tHandler).Then(sHandler) }