2019-11-14 15:40:05 +00:00
|
|
|
package service
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-06 16:34:07 +00:00
|
|
|
"context"
|
2018-11-14 09:18:03 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2019-04-01 13:30:07 +00:00
|
|
|
"errors"
|
2020-09-11 13:40:03 +00:00
|
|
|
"fmt"
|
2018-11-14 09:18:03 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-09-11 13:40:03 +00:00
|
|
|
"reflect"
|
2024-02-19 14:44:03 +00:00
|
|
|
"slices"
|
2024-02-06 16:34:07 +00:00
|
|
|
"strings"
|
2020-09-11 13:40:03 +00:00
|
|
|
"sync"
|
2018-11-14 09:18:03 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-10-14 15:16:08 +00:00
|
|
|
"github.com/spiffe/go-spiffe/v2/bundle/x509bundle"
|
|
|
|
"github.com/spiffe/go-spiffe/v2/spiffeid"
|
|
|
|
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
|
|
|
|
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
traefiktls "github.com/traefik/traefik/v3/pkg/tls"
|
2024-01-11 16:06:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2022-10-14 15:16:08 +00:00
|
|
|
// SpiffeX509Source allows to retrieve a x509 SVID and bundle.
|
|
|
|
type SpiffeX509Source interface {
|
|
|
|
x509svid.Source
|
|
|
|
x509bundle.Source
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
// TransportManager handles transports for backend communication.
|
|
|
|
type TransportManager struct {
|
2020-09-11 13:40:03 +00:00
|
|
|
rtLock sync.RWMutex
|
|
|
|
roundTrippers map[string]http.RoundTripper
|
|
|
|
configs map[string]*dynamic.ServersTransport
|
2024-09-26 09:00:05 +00:00
|
|
|
tlsConfigs map[string]*tls.Config
|
2022-10-14 15:16:08 +00:00
|
|
|
|
|
|
|
spiffeX509Source SpiffeX509Source
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
// NewTransportManager creates a new TransportManager.
|
|
|
|
func NewTransportManager(spiffeX509Source SpiffeX509Source) *TransportManager {
|
|
|
|
return &TransportManager{
|
|
|
|
roundTrippers: make(map[string]http.RoundTripper),
|
|
|
|
configs: make(map[string]*dynamic.ServersTransport),
|
|
|
|
tlsConfigs: make(map[string]*tls.Config),
|
|
|
|
spiffeX509Source: spiffeX509Source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the transport configurations.
|
|
|
|
func (t *TransportManager) Update(newConfigs map[string]*dynamic.ServersTransport) {
|
|
|
|
t.rtLock.Lock()
|
|
|
|
defer t.rtLock.Unlock()
|
2020-09-11 13:40:03 +00:00
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
for configName, config := range t.configs {
|
2020-09-11 13:40:03 +00:00
|
|
|
newConfig, ok := newConfigs[configName]
|
|
|
|
if !ok {
|
2024-09-26 09:00:05 +00:00
|
|
|
delete(t.configs, configName)
|
|
|
|
delete(t.roundTrippers, configName)
|
|
|
|
delete(t.tlsConfigs, configName)
|
2020-09-11 13:40:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(newConfig, config) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-09-26 09:00:05 +00:00
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if tlsConfig, err = t.createTLSConfig(newConfig); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Could not configure HTTP Transport %s TLS configuration, fallback on default TLS config", configName)
|
|
|
|
}
|
|
|
|
t.tlsConfigs[configName] = tlsConfig
|
|
|
|
|
|
|
|
t.roundTrippers[configName], err = t.createRoundTripper(newConfig, tlsConfig)
|
2020-09-11 13:40:03 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Could not configure HTTP Transport %s, fallback on default transport", configName)
|
2024-09-26 09:00:05 +00:00
|
|
|
t.roundTrippers[configName] = http.DefaultTransport
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for newConfigName, newConfig := range newConfigs {
|
2024-09-26 09:00:05 +00:00
|
|
|
if _, ok := t.configs[newConfigName]; ok {
|
2020-09-11 13:40:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-09-26 09:00:05 +00:00
|
|
|
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if tlsConfig, err = t.createTLSConfig(newConfig); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Could not configure HTTP Transport %s TLS configuration, fallback on default TLS config", newConfigName)
|
|
|
|
}
|
|
|
|
t.tlsConfigs[newConfigName] = tlsConfig
|
|
|
|
|
|
|
|
t.roundTrippers[newConfigName], err = t.createRoundTripper(newConfig, tlsConfig)
|
2020-09-11 13:40:03 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Could not configure HTTP Transport %s, fallback on default transport", newConfigName)
|
2024-09-26 09:00:05 +00:00
|
|
|
t.roundTrippers[newConfigName] = http.DefaultTransport
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
t.configs = newConfigs
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
// GetRoundTripper gets a roundtripper corresponding to the given transport name.
|
|
|
|
func (t *TransportManager) GetRoundTripper(name string) (http.RoundTripper, error) {
|
2020-09-11 13:40:03 +00:00
|
|
|
if len(name) == 0 {
|
|
|
|
name = "default@internal"
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
t.rtLock.RLock()
|
|
|
|
defer t.rtLock.RUnlock()
|
2020-09-11 13:40:03 +00:00
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
if rt, ok := t.roundTrippers[name]; ok {
|
2020-09-11 13:40:03 +00:00
|
|
|
return rt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("servers transport not found %s", name)
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
// Get gets transport by name.
|
|
|
|
func (t *TransportManager) Get(name string) (*dynamic.ServersTransport, error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = "default@internal"
|
2018-11-27 16:42:04 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
t.rtLock.RLock()
|
|
|
|
defer t.rtLock.RUnlock()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
if rt, ok := t.configs[name]; ok {
|
|
|
|
return rt, nil
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
return nil, fmt.Errorf("servers transport not found %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSConfig gets a TLS config corresponding to the given transport name.
|
|
|
|
func (t *TransportManager) GetTLSConfig(name string) (*tls.Config, error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = "default@internal"
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
t.rtLock.RLock()
|
|
|
|
defer t.rtLock.RUnlock()
|
|
|
|
|
|
|
|
if rt, ok := t.tlsConfigs[name]; ok {
|
|
|
|
return rt, nil
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
return nil, fmt.Errorf("tls config not found %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TransportManager) createTLSConfig(cfg *dynamic.ServersTransport) (*tls.Config, error) {
|
|
|
|
var config *tls.Config
|
2022-10-14 15:16:08 +00:00
|
|
|
if cfg.Spiffe != nil {
|
2024-09-26 09:00:05 +00:00
|
|
|
if t.spiffeX509Source == nil {
|
2022-10-14 15:16:08 +00:00
|
|
|
return nil, errors.New("SPIFFE is enabled for this transport, but not configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
spiffeAuthorizer, err := buildSpiffeAuthorizer(cfg.Spiffe)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to build SPIFFE authorizer: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
config = tlsconfig.MTLSClientConfig(t.spiffeX509Source, t.spiffeX509Source, spiffeAuthorizer)
|
2022-10-14 15:16:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 12:02:11 +00:00
|
|
|
if cfg.InsecureSkipVerify || len(cfg.RootCAs) > 0 || len(cfg.ServerName) > 0 || len(cfg.Certificates) > 0 || cfg.PeerCertURI != "" {
|
2024-09-26 09:00:05 +00:00
|
|
|
if config != nil {
|
2022-10-14 15:16:08 +00:00
|
|
|
return nil, errors.New("TLS and SPIFFE configuration cannot be defined at the same time")
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
config = &tls.Config{
|
2020-09-11 13:40:03 +00:00
|
|
|
ServerName: cfg.ServerName,
|
|
|
|
InsecureSkipVerify: cfg.InsecureSkipVerify,
|
|
|
|
RootCAs: createRootCACertPool(cfg.RootCAs),
|
|
|
|
Certificates: cfg.Certificates.GetCertificates(),
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2021-07-15 12:02:11 +00:00
|
|
|
|
|
|
|
if cfg.PeerCertURI != "" {
|
2024-09-26 09:00:05 +00:00
|
|
|
config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
|
|
return traefiktls.VerifyPeerCertificate(cfg.PeerCertURI, config, rawCerts)
|
2021-07-15 12:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createRoundTripper creates an http.RoundTripper configured with the Transport configuration settings.
|
|
|
|
// For the settings that can't be configured in Traefik it uses the default http.Transport settings.
|
|
|
|
// An exception to this is the MaxIdleConns setting as we only provide the option MaxIdleConnsPerHost in Traefik at this point in time.
|
|
|
|
// Setting this value to the default of 100 could lead to confusing behavior and backwards compatibility issues.
|
|
|
|
func (t *TransportManager) createRoundTripper(cfg *dynamic.ServersTransport, tlsConfig *tls.Config) (http.RoundTripper, error) {
|
|
|
|
if cfg == nil {
|
|
|
|
return nil, errors.New("no transport configuration given")
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ForwardingTimeouts != nil {
|
|
|
|
dialer.Timeout = time.Duration(cfg.ForwardingTimeouts.DialTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: dialer.DialContext,
|
|
|
|
MaxIdleConnsPerHost: cfg.MaxIdleConnsPerHost,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
ReadBufferSize: 64 * 1024,
|
|
|
|
WriteBufferSize: 64 * 1024,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ForwardingTimeouts != nil {
|
|
|
|
transport.ResponseHeaderTimeout = time.Duration(cfg.ForwardingTimeouts.ResponseHeaderTimeout)
|
|
|
|
transport.IdleConnTimeout = time.Duration(cfg.ForwardingTimeouts.IdleConnTimeout)
|
|
|
|
}
|
|
|
|
|
2021-03-29 12:32:03 +00:00
|
|
|
// Return directly HTTP/1.1 transport when HTTP/2 is disabled
|
|
|
|
if cfg.DisableHTTP2 {
|
2024-09-26 09:00:05 +00:00
|
|
|
return &kerberosRoundTripper{
|
2024-02-06 16:34:07 +00:00
|
|
|
OriginalRoundTripper: transport,
|
|
|
|
new: func() http.RoundTripper {
|
|
|
|
return transport.Clone()
|
|
|
|
},
|
|
|
|
}, nil
|
2021-03-29 12:32:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 16:34:07 +00:00
|
|
|
rt, err := newSmartRoundTripper(transport, cfg.ForwardingTimeouts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-26 09:00:05 +00:00
|
|
|
return &kerberosRoundTripper{
|
2024-02-06 16:34:07 +00:00
|
|
|
OriginalRoundTripper: rt,
|
|
|
|
new: func() http.RoundTripper {
|
|
|
|
return rt.Clone()
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stickyRoundTripper struct {
|
|
|
|
RoundTripper http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
type transportKeyType string
|
|
|
|
|
|
|
|
var transportKey transportKeyType = "transport"
|
|
|
|
|
|
|
|
func AddTransportOnContext(ctx context.Context) context.Context {
|
|
|
|
return context.WithValue(ctx, transportKey, &stickyRoundTripper{})
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
type kerberosRoundTripper struct {
|
|
|
|
new func() http.RoundTripper
|
|
|
|
OriginalRoundTripper http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kerberosRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
|
2024-02-06 16:34:07 +00:00
|
|
|
value, ok := request.Context().Value(transportKey).(*stickyRoundTripper)
|
|
|
|
if !ok {
|
|
|
|
return k.OriginalRoundTripper.RoundTrip(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.RoundTripper != nil {
|
|
|
|
return value.RoundTripper.RoundTrip(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := k.OriginalRoundTripper.RoundTrip(request)
|
|
|
|
|
|
|
|
// If we found that we are authenticating with Kerberos (Negotiate) or NTLM.
|
|
|
|
// We put a dedicated roundTripper in the ConnContext.
|
|
|
|
// This will stick the next calls to the same connection with the backend.
|
|
|
|
if err == nil && containsNTLMorNegotiate(resp.Header.Values("WWW-Authenticate")) {
|
|
|
|
value.RoundTripper = k.new()
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsNTLMorNegotiate(h []string) bool {
|
2024-02-19 14:44:03 +00:00
|
|
|
return slices.ContainsFunc(h, func(s string) bool {
|
|
|
|
return strings.HasPrefix(s, "NTLM") || strings.HasPrefix(s, "Negotiate")
|
|
|
|
})
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 16:06:06 +00:00
|
|
|
func createRootCACertPool(rootCAs []types.FileOrContent) *x509.CertPool {
|
2020-03-23 15:48:06 +00:00
|
|
|
if len(rootCAs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
roots := x509.NewCertPool()
|
|
|
|
|
|
|
|
for _, cert := range rootCAs {
|
|
|
|
certContent, err := cert.Read()
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msg("Error while read RootCAs")
|
2018-11-14 09:18:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
roots.AppendCertsFromPEM(certContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return roots
|
|
|
|
}
|
2022-10-14 15:16:08 +00:00
|
|
|
|
|
|
|
func buildSpiffeAuthorizer(cfg *dynamic.Spiffe) (tlsconfig.Authorizer, error) {
|
|
|
|
switch {
|
|
|
|
case len(cfg.IDs) > 0:
|
|
|
|
spiffeIDs := make([]spiffeid.ID, 0, len(cfg.IDs))
|
|
|
|
for _, rawID := range cfg.IDs {
|
|
|
|
id, err := spiffeid.FromString(rawID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid SPIFFE ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spiffeIDs = append(spiffeIDs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsconfig.AuthorizeOneOf(spiffeIDs...), nil
|
|
|
|
|
|
|
|
case cfg.TrustDomain != "":
|
|
|
|
trustDomain, err := spiffeid.TrustDomainFromString(cfg.TrustDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid SPIFFE trust domain: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsconfig.AuthorizeMemberOf(trustDomain), nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return tlsconfig.AuthorizeAny(), nil
|
|
|
|
}
|
|
|
|
}
|