traefik/pkg/server/middleware/middlewares.go

320 lines
8.9 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package middleware
import (
"context"
2019-04-01 13:30:07 +00:00
"errors"
2018-11-14 09:18:03 +00:00
"fmt"
"net/http"
"strings"
2018-11-14 09:18:03 +00:00
"github.com/containous/alice"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/middlewares/addprefix"
"github.com/containous/traefik/pkg/middlewares/auth"
"github.com/containous/traefik/pkg/middlewares/buffering"
"github.com/containous/traefik/pkg/middlewares/chain"
"github.com/containous/traefik/pkg/middlewares/circuitbreaker"
"github.com/containous/traefik/pkg/middlewares/compress"
"github.com/containous/traefik/pkg/middlewares/customerrors"
"github.com/containous/traefik/pkg/middlewares/headers"
"github.com/containous/traefik/pkg/middlewares/ipwhitelist"
"github.com/containous/traefik/pkg/middlewares/maxconnection"
"github.com/containous/traefik/pkg/middlewares/passtlsclientcert"
"github.com/containous/traefik/pkg/middlewares/ratelimiter"
"github.com/containous/traefik/pkg/middlewares/redirect"
"github.com/containous/traefik/pkg/middlewares/replacepath"
"github.com/containous/traefik/pkg/middlewares/replacepathregex"
"github.com/containous/traefik/pkg/middlewares/retry"
"github.com/containous/traefik/pkg/middlewares/stripprefix"
"github.com/containous/traefik/pkg/middlewares/stripprefixregex"
"github.com/containous/traefik/pkg/middlewares/tracing"
"github.com/containous/traefik/pkg/server/internal"
2018-11-14 09:18:03 +00:00
)
type middlewareStackType int
const (
middlewareStackKey middlewareStackType = iota
)
2018-11-14 09:18:03 +00:00
// Builder the middleware builder
type Builder struct {
configs map[string]*config.Middleware
serviceBuilder serviceBuilder
}
type serviceBuilder interface {
BuildHTTP(ctx context.Context, serviceName string, responseModifier func(*http.Response) error) (http.Handler, error)
2018-11-14 09:18:03 +00:00
}
// NewBuilder creates a new Builder
func NewBuilder(configs map[string]*config.Middleware, serviceBuilder serviceBuilder) *Builder {
return &Builder{configs: configs, serviceBuilder: serviceBuilder}
}
// BuildChain creates a middleware chain
func (b *Builder) BuildChain(ctx context.Context, middlewares []string) *alice.Chain {
2018-11-14 09:18:03 +00:00
chain := alice.New()
for _, name := range middlewares {
middlewareName := internal.GetQualifiedName(ctx, name)
chain = chain.Append(func(next http.Handler) (http.Handler, error) {
constructorContext := internal.AddProviderInContext(ctx, middlewareName)
if _, ok := b.configs[middlewareName]; !ok {
return nil, fmt.Errorf("middleware %q does not exist", middlewareName)
}
var err error
2019-04-01 13:30:07 +00:00
if constructorContext, err = checkRecursion(constructorContext, middlewareName); err != nil {
return nil, err
}
2018-11-14 09:18:03 +00:00
constructor, err := b.buildConstructor(constructorContext, middlewareName, *b.configs[middlewareName])
if err != nil {
return nil, fmt.Errorf("error during instanciation of %s: %v", middlewareName, err)
}
return constructor(next)
})
}
return &chain
}
2019-04-01 13:30:07 +00:00
func checkRecursion(ctx context.Context, middlewareName string) (context.Context, error) {
currentStack, ok := ctx.Value(middlewareStackKey).([]string)
if !ok {
currentStack = []string{}
}
if inSlice(middlewareName, currentStack) {
return ctx, fmt.Errorf("could not instantiate middleware %s: recursion detected in %s", middlewareName, strings.Join(append(currentStack, middlewareName), "->"))
2018-11-14 09:18:03 +00:00
}
return context.WithValue(ctx, middlewareStackKey, append(currentStack, middlewareName)), nil
2018-11-14 09:18:03 +00:00
}
func (b *Builder) buildConstructor(ctx context.Context, middlewareName string, config config.Middleware) (alice.Constructor, error) {
var middleware alice.Constructor
2019-04-01 13:30:07 +00:00
badConf := errors.New("cannot create middleware: multi-types middleware not supported, consider declaring two different pieces of middleware instead")
2018-11-14 09:18:03 +00:00
// AddPrefix
if config.AddPrefix != nil {
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return addprefix.New(ctx, next, *config.AddPrefix, middlewareName)
2018-11-14 09:18:03 +00:00
}
}
// BasicAuth
if config.BasicAuth != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return auth.NewBasic(ctx, next, *config.BasicAuth, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// Buffering
if config.Buffering != nil && config.MaxConn.Amount != 0 {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return buffering.New(ctx, next, *config.Buffering, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// Chain
if config.Chain != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return chain.New(ctx, next, *config.Chain, b, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// CircuitBreaker
if config.CircuitBreaker != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return circuitbreaker.New(ctx, next, *config.CircuitBreaker, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// Compress
if config.Compress != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return compress.New(ctx, next, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// CustomErrors
if config.Errors != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return customerrors.New(ctx, next, *config.Errors, b.serviceBuilder, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// DigestAuth
if config.DigestAuth != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return auth.NewDigest(ctx, next, *config.DigestAuth, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// ForwardAuth
if config.ForwardAuth != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return auth.NewForward(ctx, next, *config.ForwardAuth, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// Headers
if config.Headers != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return headers.New(ctx, next, *config.Headers, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// IPWhiteList
if config.IPWhiteList != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return ipwhitelist.New(ctx, next, *config.IPWhiteList, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// MaxConn
if config.MaxConn != nil && config.MaxConn.Amount != 0 {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return maxconnection.New(ctx, next, *config.MaxConn, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// PassTLSClientCert
if config.PassTLSClientCert != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return passtlsclientcert.New(ctx, next, *config.PassTLSClientCert, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// RateLimit
if config.RateLimit != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return ratelimiter.New(ctx, next, *config.RateLimit, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// RedirectRegex
if config.RedirectRegex != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return redirect.NewRedirectRegex(ctx, next, *config.RedirectRegex, middlewareName)
}
}
// RedirectScheme
if config.RedirectScheme != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return redirect.NewRedirectScheme(ctx, next, *config.RedirectScheme, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// ReplacePath
if config.ReplacePath != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return replacepath.New(ctx, next, *config.ReplacePath, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// ReplacePathRegex
if config.ReplacePathRegex != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return replacepathregex.New(ctx, next, *config.ReplacePathRegex, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// Retry
if config.Retry != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
// FIXME missing metrics / accessLog
return retry.New(ctx, next, *config.Retry, retry.Listeners{}, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// StripPrefix
if config.StripPrefix != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return stripprefix.New(ctx, next, *config.StripPrefix, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
// StripPrefixRegex
if config.StripPrefixRegex != nil {
2019-04-01 13:30:07 +00:00
if middleware != nil {
2018-11-14 09:18:03 +00:00
return nil, badConf
}
2019-04-01 13:30:07 +00:00
middleware = func(next http.Handler) (http.Handler, error) {
return stripprefixregex.New(ctx, next, *config.StripPrefixRegex, middlewareName)
}
2018-11-14 09:18:03 +00:00
}
if middleware == nil {
2019-04-01 13:30:07 +00:00
return nil, errors.New("middleware does not exist")
}
2018-11-14 09:18:03 +00:00
return tracing.Wrap(ctx, middleware), nil
}
func inSlice(element string, stack []string) bool {
for _, value := range stack {
if value == element {
return true
}
}
return false
}