2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-09-08 17:56:18 +00:00
|
|
|
"errors"
|
2022-03-29 07:35:54 +00:00
|
|
|
"fmt"
|
2022-10-08 00:36:57 +00:00
|
|
|
"io"
|
2020-09-14 21:14:30 +00:00
|
|
|
"log"
|
2019-10-27 05:53:25 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-06-14 05:06:55 +00:00
|
|
|
"net/http/pprof"
|
2021-06-22 20:42:34 +00:00
|
|
|
"os"
|
2019-10-27 05:53:25 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-06-14 05:06:55 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/dynamiclistener"
|
|
|
|
"github.com/rancher/dynamiclistener/factory"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/file"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/kubernetes"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/memory"
|
2021-07-03 08:37:19 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/core"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-09-24 06:06:00 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-03-29 07:35:54 +00:00
|
|
|
utilsnet "k8s.io/utils/net"
|
2019-10-27 05:53:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-22 20:35:58 +00:00
|
|
|
// newListener returns a new TCP listener and HTTP request handler using dynamiclistener.
|
2020-09-24 06:29:25 +00:00
|
|
|
// dynamiclistener will use the cluster's Server CA to sign the dynamically generate certificate,
|
|
|
|
// and will sync the certs into the Kubernetes datastore, with a local disk cache.
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) newListener(ctx context.Context) (net.Listener, http.Handler, error) {
|
2021-06-22 20:42:34 +00:00
|
|
|
if c.managedDB != nil {
|
2023-09-21 18:53:50 +00:00
|
|
|
resetDone, err := c.managedDB.IsReset()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if resetDone {
|
|
|
|
// delete the dynamic listener TLS secret cache after restoring,
|
|
|
|
// to ensure that dynamiclistener doesn't sync the old secret over the top
|
|
|
|
// of whatever was just restored.
|
2021-06-22 20:42:34 +00:00
|
|
|
os.Remove(filepath.Join(c.config.DataDir, "tls/dynamic-cert.json"))
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 07:35:54 +00:00
|
|
|
ip := c.config.BindAddress
|
|
|
|
if utilsnet.IsIPv6String(ip) {
|
|
|
|
ip = fmt.Sprintf("[%s]", ip)
|
|
|
|
}
|
|
|
|
tcp, err := dynamiclistener.NewTCPListener(ip, c.config.SupervisorPort)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-08-15 18:06:24 +00:00
|
|
|
certs, key, err := factory.LoadCertsChain(c.config.Runtime.ServerCA, c.config.Runtime.ServerCAKey)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-08-01 00:51:46 +00:00
|
|
|
c.config.SANs = append(c.config.SANs, "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc."+c.config.ClusterDomain)
|
2023-08-28 20:39:21 +00:00
|
|
|
if c.config.SANSecurity {
|
|
|
|
c.config.Runtime.ClusterControllerStarts["server-cn-filter"] = func(ctx context.Context) {
|
|
|
|
registerAddressHandlers(ctx, c)
|
|
|
|
}
|
2023-08-01 00:51:46 +00:00
|
|
|
}
|
2022-02-24 19:01:14 +00:00
|
|
|
storage := tlsStorage(ctx, c.config.DataDir, c.config.Runtime)
|
2023-08-15 18:06:24 +00:00
|
|
|
return wrapHandler(dynamiclistener.NewListenerWithChain(tcp, storage, certs, key, dynamiclistener.Config{
|
2020-09-24 06:43:53 +00:00
|
|
|
ExpirationDaysCheck: config.CertificateRenewDays,
|
|
|
|
Organization: []string{version.Program},
|
2023-08-01 00:51:46 +00:00
|
|
|
SANs: c.config.SANs,
|
2020-09-24 06:43:53 +00:00
|
|
|
CN: version.Program,
|
2020-07-23 02:34:51 +00:00
|
|
|
TLSConfig: &tls.Config{
|
2020-05-06 17:43:15 +00:00
|
|
|
ClientAuth: tls.RequestClientCert,
|
|
|
|
MinVersion: c.config.TLSMinVersion,
|
|
|
|
CipherSuites: c.config.TLSCipherSuites,
|
2022-03-01 19:27:52 +00:00
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
2019-10-27 05:53:25 +00:00
|
|
|
},
|
2023-08-01 00:51:46 +00:00
|
|
|
FilterCN: c.filterCN,
|
2021-12-02 21:19:16 +00:00
|
|
|
RegenerateCerts: func() bool {
|
|
|
|
const regenerateDynamicListenerFile = "dynamic-cert-regenerate"
|
|
|
|
dynamicListenerRegenFilePath := filepath.Join(c.config.DataDir, "tls", regenerateDynamicListenerFile)
|
|
|
|
if _, err := os.Stat(dynamicListenerRegenFilePath); err == nil {
|
|
|
|
os.Remove(dynamicListenerRegenFilePath)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2022-04-04 21:54:50 +00:00
|
|
|
}))
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 00:51:46 +00:00
|
|
|
func (c *Cluster) filterCN(cn ...string) []string {
|
|
|
|
if c.cnFilterFunc != nil {
|
|
|
|
return c.cnFilterFunc(cn...)
|
|
|
|
}
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// initClusterAndHTTPS sets up the dynamic tls listener, request router,
|
|
|
|
// and cluster database. Once the database is up, it starts the supervisor http server.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
|
2020-09-24 06:43:53 +00:00
|
|
|
// Set up dynamiclistener TLS listener and request handler
|
|
|
|
listener, handler, err := c.newListener(ctx)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Get the base request handler
|
2019-10-27 05:53:25 +00:00
|
|
|
handler, err = c.getHandler(handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:53:50 +00:00
|
|
|
// Register database request handlers and controller callbacks
|
|
|
|
handler, err = c.registerDBHandlers(handler)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:06:55 +00:00
|
|
|
if c.config.EnablePProf {
|
2023-01-14 00:09:14 +00:00
|
|
|
mux := mux.NewRouter().SkipClean(true)
|
2022-06-14 05:06:55 +00:00
|
|
|
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
|
|
mux.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
|
|
|
|
mux.NotFoundHandler = handler
|
|
|
|
handler = mux
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Create a HTTP server with the registered request handlers, using logrus for logging
|
2019-10-27 05:53:25 +00:00
|
|
|
server := http.Server{
|
2021-10-22 22:56:01 +00:00
|
|
|
Handler: handler,
|
|
|
|
}
|
2021-10-12 22:00:28 +00:00
|
|
|
|
|
|
|
if logrus.IsLevelEnabled(logrus.DebugLevel) {
|
|
|
|
server.ErrorLog = log.New(logrus.StandardLogger().Writer(), "Cluster-Http-Server ", log.LstdFlags)
|
|
|
|
} else {
|
2022-10-08 00:36:57 +00:00
|
|
|
server.ErrorLog = log.New(io.Discard, "Cluster-Http-Server", 0)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Start the supervisor http server on the tls listener
|
2019-10-27 05:53:25 +00:00
|
|
|
go func() {
|
2020-09-24 06:43:53 +00:00
|
|
|
err := server.Serve(listener)
|
2021-09-08 17:56:18 +00:00
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logrus.Fatalf("server stopped: %v", err)
|
|
|
|
}
|
2019-10-27 05:53:25 +00:00
|
|
|
}()
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Shutdown the http server when the context is closed
|
2019-10-27 05:53:25 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// tlsStorage creates an in-memory cache for dynamiclistener's certificate, backed by a file on disk
|
|
|
|
// and the Kubernetes datastore.
|
2019-10-27 05:53:25 +00:00
|
|
|
func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage {
|
|
|
|
fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json"))
|
|
|
|
cache := memory.NewBacked(fileStorage)
|
|
|
|
return kubernetes.New(ctx, func() *core.Factory {
|
|
|
|
return runtime.Core
|
2020-09-24 06:06:00 +00:00
|
|
|
}, metav1.NamespaceSystem, version.Program+"-serving", cache)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
2022-04-04 21:54:50 +00:00
|
|
|
|
|
|
|
// wrapHandler wraps the dynamiclistener request handler, adding a User-Agent value to
|
|
|
|
// CONNECT requests that will prevent DynamicListener from adding the request's Host
|
|
|
|
// header to the SAN list. CONNECT requests set the Host header to the target of the
|
|
|
|
// proxy connection, so it is not correct to add this value to the certificate. It would
|
|
|
|
// be nice if we could do this with with the FilterCN callback, but unfortunately that
|
|
|
|
// callback does not offer access to the request that triggered the change.
|
|
|
|
func wrapHandler(listener net.Listener, handler http.Handler, err error) (net.Listener, http.Handler, error) {
|
|
|
|
return listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodConnect {
|
|
|
|
r.Header.Add("User-Agent", "mozilla")
|
|
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}), err
|
|
|
|
}
|