traefik/pkg/server/router/router.go

251 lines
8.3 KiB
Go
Raw Permalink Normal View History

2018-11-14 09:18:03 +00:00
package router
import (
"context"
"errors"
"fmt"
"math"
2018-11-14 09:18:03 +00:00
"net/http"
"strings"
2018-11-14 09:18:03 +00:00
"github.com/containous/alice"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/runtime"
"github.com/traefik/traefik/v3/pkg/logs"
"github.com/traefik/traefik/v3/pkg/middlewares/accesslog"
2023-11-29 11:20:57 +00:00
"github.com/traefik/traefik/v3/pkg/middlewares/denyrouterrecursion"
2023-02-03 14:24:05 +00:00
metricsMiddle "github.com/traefik/traefik/v3/pkg/middlewares/metrics"
2024-03-12 08:48:04 +00:00
"github.com/traefik/traefik/v3/pkg/middlewares/observability"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/middlewares/recovery"
httpmuxer "github.com/traefik/traefik/v3/pkg/muxer/http"
"github.com/traefik/traefik/v3/pkg/server/middleware"
"github.com/traefik/traefik/v3/pkg/server/provider"
"github.com/traefik/traefik/v3/pkg/tls"
2018-11-14 09:18:03 +00:00
)
const maxUserPriority = math.MaxInt - 1000
type middlewareBuilder interface {
BuildChain(ctx context.Context, names []string) *alice.Chain
}
type serviceManager interface {
BuildHTTP(rootCtx context.Context, serviceName string) (http.Handler, error)
LaunchHealthCheck(ctx context.Context)
}
2020-05-11 10:06:07 +00:00
// Manager A route/router manager.
type Manager struct {
routerHandlers map[string]http.Handler
serviceManager serviceManager
observabilityMgr *middleware.ObservabilityMgr
middlewaresBuilder middlewareBuilder
conf *runtime.Configuration
tlsManager *tls.Manager
}
// NewManager creates a new Manager.
func NewManager(conf *runtime.Configuration, serviceManager serviceManager, middlewaresBuilder middlewareBuilder, observabilityMgr *middleware.ObservabilityMgr, tlsManager *tls.Manager) *Manager {
2018-11-14 09:18:03 +00:00
return &Manager{
routerHandlers: make(map[string]http.Handler),
serviceManager: serviceManager,
observabilityMgr: observabilityMgr,
2018-11-14 09:18:03 +00:00
middlewaresBuilder: middlewaresBuilder,
conf: conf,
tlsManager: tlsManager,
2018-11-14 09:18:03 +00:00
}
}
func (m *Manager) getHTTPRouters(ctx context.Context, entryPoints []string, tls bool) map[string]map[string]*runtime.RouterInfo {
if m.conf != nil {
return m.conf.GetRoutersByEntryPoints(ctx, entryPoints, tls)
}
return make(map[string]map[string]*runtime.RouterInfo)
2018-11-14 09:18:03 +00:00
}
2020-05-11 10:06:07 +00:00
// BuildHandlers Builds handler for all entry points.
func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string, tls bool) map[string]http.Handler {
2018-11-14 09:18:03 +00:00
entryPointHandlers := make(map[string]http.Handler)
for entryPointName, routers := range m.getHTTPRouters(rootCtx, entryPoints, tls) {
2022-11-21 17:36:05 +00:00
logger := log.Ctx(rootCtx).With().Str(logs.EntryPointName, entryPointName).Logger()
ctx := logger.WithContext(rootCtx)
2018-11-14 09:18:03 +00:00
handler, err := m.buildEntryPointHandler(ctx, entryPointName, routers)
2018-11-14 09:18:03 +00:00
if err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2018-11-14 09:18:03 +00:00
continue
}
entryPointHandlers[entryPointName] = handler
2018-11-14 09:18:03 +00:00
}
// Create default handlers.
for _, entryPointName := range entryPoints {
2022-11-21 17:36:05 +00:00
logger := log.Ctx(rootCtx).With().Str(logs.EntryPointName, entryPointName).Logger()
ctx := logger.WithContext(rootCtx)
handler, ok := entryPointHandlers[entryPointName]
if ok || handler != nil {
continue
}
handler, err := m.observabilityMgr.BuildEPChain(ctx, entryPointName, "").Then(BuildDefaultHTTPRouter())
if err != nil {
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
continue
}
entryPointHandlers[entryPointName] = handler
}
2018-11-14 09:18:03 +00:00
return entryPointHandlers
}
func (m *Manager) buildEntryPointHandler(ctx context.Context, entryPointName string, configs map[string]*runtime.RouterInfo) (http.Handler, error) {
2022-03-17 17:02:08 +00:00
muxer, err := httpmuxer.NewMuxer()
if err != nil {
return nil, err
}
2018-11-14 09:18:03 +00:00
defaultHandler, err := m.observabilityMgr.BuildEPChain(ctx, entryPointName, "defaultHandler").Then(http.NotFoundHandler())
if err != nil {
return nil, err
}
muxer.SetDefaultHandler(defaultHandler)
2018-11-14 09:18:03 +00:00
for routerName, routerConfig := range configs {
2022-11-21 17:36:05 +00:00
logger := log.Ctx(ctx).With().Str(logs.RouterName, routerName).Logger()
ctxRouter := logger.WithContext(provider.AddInContext(ctx, routerName))
2018-11-14 09:18:03 +00:00
if routerConfig.Priority == 0 {
routerConfig.Priority = httpmuxer.GetRulePriority(routerConfig.Rule)
}
if routerConfig.Priority > maxUserPriority && !strings.HasSuffix(routerName, "@internal") {
err = fmt.Errorf("the router priority %d exceeds the max user-defined priority %d", routerConfig.Priority, maxUserPriority)
routerConfig.AddError(err, true)
2024-04-03 15:44:16 +00:00
logger.Error().Err(err).Send()
continue
}
handler, err := m.buildRouterHandler(ctxRouter, routerName, routerConfig)
2018-11-14 09:18:03 +00:00
if err != nil {
routerConfig.AddError(err, true)
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2018-11-14 09:18:03 +00:00
continue
}
observabilityChain := m.observabilityMgr.BuildEPChain(ctx, entryPointName, routerConfig.Service)
handler, err = observabilityChain.Then(handler)
if err != nil {
routerConfig.AddError(err, true)
logger.Error().Err(err).Send()
continue
}
2024-01-23 10:34:05 +00:00
if err = muxer.AddRoute(routerConfig.Rule, routerConfig.RuleSyntax, routerConfig.Priority, handler); err != nil {
routerConfig.AddError(err, true)
2022-11-21 17:36:05 +00:00
logger.Error().Err(err).Send()
2018-11-14 09:18:03 +00:00
continue
}
}
chain := alice.New()
chain = chain.Append(func(next http.Handler) (http.Handler, error) {
return recovery.New(ctx, next)
2018-11-14 09:18:03 +00:00
})
2022-03-17 17:02:08 +00:00
return chain.Then(muxer)
2018-11-14 09:18:03 +00:00
}
func (m *Manager) buildRouterHandler(ctx context.Context, routerName string, routerConfig *runtime.RouterInfo) (http.Handler, error) {
2018-11-14 09:18:03 +00:00
if handler, ok := m.routerHandlers[routerName]; ok {
return handler, nil
}
if routerConfig.TLS != nil {
// Don't build the router if the TLSOptions configuration is invalid.
tlsOptionsName := tls.DefaultTLSConfigName
if len(routerConfig.TLS.Options) > 0 && routerConfig.TLS.Options != tls.DefaultTLSConfigName {
tlsOptionsName = provider.GetQualifiedName(ctx, routerConfig.TLS.Options)
}
if _, err := m.tlsManager.Get(tls.DefaultTLSStoreName, tlsOptionsName); err != nil {
return nil, fmt.Errorf("building router handler: %w", err)
}
}
handler, err := m.buildHTTPHandler(ctx, routerConfig, routerName)
2018-11-14 09:18:03 +00:00
if err != nil {
return nil, err
}
// Prevents from enabling observability for internal resources.
if !m.observabilityMgr.ShouldAddAccessLogs(provider.GetQualifiedName(ctx, routerConfig.Service)) {
m.routerHandlers[routerName] = handler
return m.routerHandlers[routerName], nil
}
2018-11-14 09:18:03 +00:00
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
return accesslog.NewFieldHandler(next, accesslog.RouterName, routerName, nil), nil
}).Then(handler)
if err != nil {
2022-11-21 17:36:05 +00:00
log.Ctx(ctx).Error().Err(err).Send()
2018-11-14 09:18:03 +00:00
m.routerHandlers[routerName] = handler
} else {
m.routerHandlers[routerName] = handlerWithAccessLog
}
return m.routerHandlers[routerName], nil
}
func (m *Manager) buildHTTPHandler(ctx context.Context, router *runtime.RouterInfo, routerName string) (http.Handler, error) {
var qualifiedNames []string
for _, name := range router.Middlewares {
qualifiedNames = append(qualifiedNames, provider.GetQualifiedName(ctx, name))
}
2019-09-10 14:12:05 +00:00
router.Middlewares = qualifiedNames
2018-11-14 09:18:03 +00:00
if router.Service == "" {
return nil, errors.New("the service is missing on the router")
}
sHandler, err := m.serviceManager.BuildHTTP(ctx, router.Service)
2018-11-14 09:18:03 +00:00
if err != nil {
return nil, err
}
mHandler := m.middlewaresBuilder.BuildChain(ctx, router.Middlewares)
2018-11-14 09:18:03 +00:00
2021-04-30 08:22:04 +00:00
chain := alice.New()
if m.observabilityMgr.MetricsRegistry() != nil && m.observabilityMgr.MetricsRegistry().IsRouterEnabled() &&
m.observabilityMgr.ShouldAddMetrics(provider.GetQualifiedName(ctx, router.Service)) {
chain = chain.Append(metricsMiddle.WrapRouterHandler(ctx, m.observabilityMgr.MetricsRegistry(), routerName, provider.GetQualifiedName(ctx, router.Service)))
}
// Prevents from enabling tracing for internal resources.
if !m.observabilityMgr.ShouldAddTracing(provider.GetQualifiedName(ctx, router.Service)) {
return chain.Extend(*mHandler).Then(sHandler)
}
2024-03-12 08:48:04 +00:00
chain = chain.Append(observability.WrapRouterHandler(ctx, routerName, router.Rule, provider.GetQualifiedName(ctx, router.Service)))
2024-01-08 08:10:06 +00:00
if m.observabilityMgr.MetricsRegistry() != nil && m.observabilityMgr.MetricsRegistry().IsRouterEnabled() {
metricsHandler := metricsMiddle.WrapRouterHandler(ctx, m.observabilityMgr.MetricsRegistry(), routerName, provider.GetQualifiedName(ctx, router.Service))
2024-03-12 08:48:04 +00:00
chain = chain.Append(observability.WrapMiddleware(ctx, metricsHandler))
2021-04-30 08:22:04 +00:00
}
if router.DefaultRule {
chain = chain.Append(denyrouterrecursion.WrapHandler(routerName))
}
2024-01-08 08:10:06 +00:00
return chain.Extend(*mHandler).Then(sHandler)
2018-11-14 09:18:03 +00:00
}
// BuildDefaultHTTPRouter creates a default HTTP router.
func BuildDefaultHTTPRouter() http.Handler {
return http.NotFoundHandler()
}