2019-01-15 13:28:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-02-19 14:44:03 +00:00
|
|
|
"slices"
|
|
|
|
|
2020-10-29 14:40:04 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/tlsalpn01"
|
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/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/logs"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/server/provider"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/tls"
|
2019-01-15 13:28:04 +00:00
|
|
|
)
|
|
|
|
|
2020-03-09 10:12:05 +00:00
|
|
|
func mergeConfiguration(configurations dynamic.Configurations, defaultEntryPoints []string) dynamic.Configuration {
|
2022-02-07 10:58:04 +00:00
|
|
|
// TODO: see if we can use DeepCopies inside, so that the given argument is left
|
|
|
|
// untouched, and the modified copy is returned.
|
2019-07-10 07:26:04 +00:00
|
|
|
conf := dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2020-09-11 13:40:03 +00:00
|
|
|
Routers: make(map[string]*dynamic.Router),
|
|
|
|
Middlewares: make(map[string]*dynamic.Middleware),
|
|
|
|
Services: make(map[string]*dynamic.Service),
|
|
|
|
Models: make(map[string]*dynamic.Model),
|
|
|
|
ServersTransports: make(map[string]*dynamic.ServersTransport),
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
TCP: &dynamic.TCPConfiguration{
|
2022-12-09 08:58:05 +00:00
|
|
|
Routers: make(map[string]*dynamic.TCPRouter),
|
|
|
|
Services: make(map[string]*dynamic.TCPService),
|
|
|
|
Middlewares: make(map[string]*dynamic.TCPMiddleware),
|
2024-01-23 10:34:05 +00:00
|
|
|
Models: make(map[string]*dynamic.TCPModel),
|
2022-12-09 08:58:05 +00:00
|
|
|
ServersTransports: make(map[string]*dynamic.TCPServersTransport),
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
2020-02-11 00:26:04 +00:00
|
|
|
UDP: &dynamic.UDPConfiguration{
|
|
|
|
Routers: make(map[string]*dynamic.UDPRouter),
|
|
|
|
Services: make(map[string]*dynamic.UDPService),
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
TLS: &dynamic.TLSConfiguration{
|
2019-06-27 21:58:03 +00:00
|
|
|
Stores: make(map[string]tls.Store),
|
|
|
|
Options: make(map[string]tls.Options),
|
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:18:05 +00:00
|
|
|
var defaultTLSOptionProviders []string
|
2020-02-24 16:14:06 +00:00
|
|
|
var defaultTLSStoreProviders []string
|
2020-01-27 09:40:05 +00:00
|
|
|
for pvd, configuration := range configurations {
|
2019-03-14 08:30:04 +00:00
|
|
|
if configuration.HTTP != nil {
|
|
|
|
for routerName, router := range configuration.HTTP.Routers {
|
2020-03-05 11:46:05 +00:00
|
|
|
if len(router.EntryPoints) == 0 {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().
|
|
|
|
Str(logs.RouterName, routerName).
|
|
|
|
Strs(logs.EntryPointName, defaultEntryPoints).
|
|
|
|
Msg("No entryPoint defined for this router, using the default one(s) instead")
|
2020-03-09 10:12:05 +00:00
|
|
|
router.EntryPoints = defaultEntryPoints
|
2020-03-05 11:46:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 09:40:05 +00:00
|
|
|
conf.HTTP.Routers[provider.MakeQualifiedName(pvd, routerName)] = router
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
for middlewareName, middleware := range configuration.HTTP.Middlewares {
|
2020-01-27 09:40:05 +00:00
|
|
|
conf.HTTP.Middlewares[provider.MakeQualifiedName(pvd, middlewareName)] = middleware
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
for serviceName, service := range configuration.HTTP.Services {
|
2020-01-27 09:40:05 +00:00
|
|
|
conf.HTTP.Services[provider.MakeQualifiedName(pvd, serviceName)] = service
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2020-03-05 11:46:05 +00:00
|
|
|
for modelName, model := range configuration.HTTP.Models {
|
|
|
|
conf.HTTP.Models[provider.MakeQualifiedName(pvd, modelName)] = model
|
|
|
|
}
|
2020-09-11 13:40:03 +00:00
|
|
|
for serversTransportName, serversTransport := range configuration.HTTP.ServersTransports {
|
|
|
|
conf.HTTP.ServersTransports[provider.MakeQualifiedName(pvd, serversTransportName)] = serversTransport
|
|
|
|
}
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
if configuration.TCP != nil {
|
|
|
|
for routerName, router := range configuration.TCP.Routers {
|
2021-05-11 14:46:14 +00:00
|
|
|
if len(router.EntryPoints) == 0 {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().
|
|
|
|
Str(logs.RouterName, routerName).
|
|
|
|
Msgf("No entryPoint defined for this TCP router, using the default one(s) instead: %+v", defaultEntryPoints)
|
2021-05-11 14:46:14 +00:00
|
|
|
router.EntryPoints = defaultEntryPoints
|
|
|
|
}
|
2020-01-27 09:40:05 +00:00
|
|
|
conf.TCP.Routers[provider.MakeQualifiedName(pvd, routerName)] = router
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2021-06-11 13:30:05 +00:00
|
|
|
for middlewareName, middleware := range configuration.TCP.Middlewares {
|
|
|
|
conf.TCP.Middlewares[provider.MakeQualifiedName(pvd, middlewareName)] = middleware
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
for serviceName, service := range configuration.TCP.Services {
|
2020-01-27 09:40:05 +00:00
|
|
|
conf.TCP.Services[provider.MakeQualifiedName(pvd, serviceName)] = service
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2024-05-14 07:42:04 +00:00
|
|
|
for modelName, model := range configuration.TCP.Models {
|
|
|
|
conf.TCP.Models[provider.MakeQualifiedName(pvd, modelName)] = model
|
|
|
|
}
|
2022-12-09 08:58:05 +00:00
|
|
|
for serversTransportName, serversTransport := range configuration.TCP.ServersTransports {
|
|
|
|
conf.TCP.ServersTransports[provider.MakeQualifiedName(pvd, serversTransportName)] = serversTransport
|
|
|
|
}
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2020-02-11 00:26:04 +00:00
|
|
|
if configuration.UDP != nil {
|
|
|
|
for routerName, router := range configuration.UDP.Routers {
|
|
|
|
conf.UDP.Routers[provider.MakeQualifiedName(pvd, routerName)] = router
|
|
|
|
}
|
|
|
|
for serviceName, service := range configuration.UDP.Services {
|
|
|
|
conf.UDP.Services[provider.MakeQualifiedName(pvd, serviceName)] = service
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
if configuration.TLS != nil {
|
2020-10-29 14:40:04 +00:00
|
|
|
for _, cert := range configuration.TLS.Certificates {
|
2024-02-19 14:44:03 +00:00
|
|
|
if slices.Contains(cert.Stores, tlsalpn01.ACMETLS1Protocol) && pvd != "tlsalpn.acme" {
|
2020-10-29 14:40:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.TLS.Certificates = append(conf.TLS.Certificates, cert)
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
for key, store := range configuration.TLS.Stores {
|
2021-06-14 08:06:05 +00:00
|
|
|
if key != tls.DefaultTLSStoreName {
|
2020-02-24 16:14:06 +00:00
|
|
|
key = provider.MakeQualifiedName(pvd, key)
|
|
|
|
} else {
|
|
|
|
defaultTLSStoreProviders = append(defaultTLSStoreProviders, pvd)
|
|
|
|
}
|
2019-06-27 21:58:03 +00:00
|
|
|
conf.TLS.Stores[key] = store
|
2019-06-21 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 21:58:03 +00:00
|
|
|
for tlsOptionsName, options := range configuration.TLS.Options {
|
|
|
|
if tlsOptionsName != "default" {
|
2020-01-27 09:40:05 +00:00
|
|
|
tlsOptionsName = provider.MakeQualifiedName(pvd, tlsOptionsName)
|
2019-06-27 21:58:03 +00:00
|
|
|
} else {
|
2020-01-27 09:40:05 +00:00
|
|
|
defaultTLSOptionProviders = append(defaultTLSOptionProviders, pvd)
|
2019-06-27 21:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf.TLS.Options[tlsOptionsName] = options
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 16:14:06 +00:00
|
|
|
if len(defaultTLSStoreProviders) > 1 {
|
2024-03-12 09:38:29 +00:00
|
|
|
log.Error().Msgf("Default TLS Store defined in multiple providers: %v", defaultTLSStoreProviders)
|
2021-06-14 08:06:05 +00:00
|
|
|
delete(conf.TLS.Stores, tls.DefaultTLSStoreName)
|
2020-02-24 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:18:05 +00:00
|
|
|
if len(defaultTLSOptionProviders) == 0 {
|
2021-06-14 08:06:05 +00:00
|
|
|
conf.TLS.Options[tls.DefaultTLSConfigName] = tls.DefaultTLSOptions
|
2019-06-21 15:18:05 +00:00
|
|
|
} else if len(defaultTLSOptionProviders) > 1 {
|
2024-03-12 09:38:29 +00:00
|
|
|
log.Error().Msgf("Default TLS Options defined in multiple providers %v", defaultTLSOptionProviders)
|
2019-06-21 15:18:05 +00:00
|
|
|
// We do not set an empty tls.TLS{} as above so that we actually get a "cascading failure" later on,
|
|
|
|
// i.e. routers depending on this missing TLS option will fail to initialize as well.
|
2021-06-14 08:06:05 +00:00
|
|
|
delete(conf.TLS.Options, tls.DefaultTLSConfigName)
|
2019-06-21 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:28:04 +00:00
|
|
|
return conf
|
|
|
|
}
|
2020-03-05 11:46:05 +00:00
|
|
|
|
|
|
|
func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
|
2024-05-14 07:42:04 +00:00
|
|
|
if cfg.HTTP != nil && len(cfg.HTTP.Models) > 0 {
|
|
|
|
rts := make(map[string]*dynamic.Router)
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
for name, rt := range cfg.HTTP.Routers {
|
|
|
|
router := rt.DeepCopy()
|
2020-03-09 10:12:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
if !router.DefaultRule && router.RuleSyntax == "" {
|
|
|
|
for _, model := range cfg.HTTP.Models {
|
|
|
|
router.RuleSyntax = model.DefaultRuleSyntax
|
|
|
|
break
|
|
|
|
}
|
2024-01-23 10:34:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
eps := router.EntryPoints
|
|
|
|
router.EntryPoints = nil
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
for _, epName := range eps {
|
|
|
|
m, ok := cfg.HTTP.Models[epName+"@internal"]
|
|
|
|
if ok {
|
|
|
|
cp := router.DeepCopy()
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
cp.EntryPoints = []string{epName}
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
if cp.TLS == nil {
|
|
|
|
cp.TLS = m.TLS
|
|
|
|
}
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
cp.Middlewares = append(m.Middlewares, cp.Middlewares...)
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
rtName := name
|
|
|
|
if len(eps) > 1 {
|
|
|
|
rtName = epName + "-" + name
|
|
|
|
}
|
|
|
|
rts[rtName] = cp
|
|
|
|
} else {
|
|
|
|
router.EntryPoints = append(router.EntryPoints, epName)
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
rts[name] = router
|
|
|
|
}
|
2020-03-05 11:46:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
cfg.HTTP.Routers = rts
|
|
|
|
}
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2024-01-23 10:34:05 +00:00
|
|
|
if cfg.TCP == nil || len(cfg.TCP.Models) == 0 {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpRouters := make(map[string]*dynamic.TCPRouter)
|
|
|
|
|
2024-05-14 07:42:04 +00:00
|
|
|
for name, rt := range cfg.TCP.Routers {
|
2024-01-23 10:34:05 +00:00
|
|
|
router := rt.DeepCopy()
|
|
|
|
|
|
|
|
if router.RuleSyntax == "" {
|
|
|
|
for _, model := range cfg.TCP.Models {
|
|
|
|
router.RuleSyntax = model.DefaultRuleSyntax
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-05-14 07:42:04 +00:00
|
|
|
|
|
|
|
tcpRouters[name] = router
|
2024-01-23 10:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.TCP.Routers = tcpRouters
|
|
|
|
|
2020-03-05 11:46:05 +00:00
|
|
|
return cfg
|
|
|
|
}
|