2019-01-01 08:23:01 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-12-22 22:17:00 +00:00
|
|
|
"context"
|
2019-05-29 18:53:51 +00:00
|
|
|
"crypto"
|
2019-04-19 18:20:34 +00:00
|
|
|
"crypto/x509"
|
2021-04-21 22:56:20 +00:00
|
|
|
"fmt"
|
2019-04-19 18:20:34 +00:00
|
|
|
"net"
|
2019-01-01 08:23:01 +00:00
|
|
|
"net/http"
|
2020-12-22 22:17:00 +00:00
|
|
|
"os"
|
2019-03-18 18:50:20 +00:00
|
|
|
"path/filepath"
|
2019-01-26 04:56:12 +00:00
|
|
|
"strconv"
|
2019-04-23 17:53:06 +00:00
|
|
|
"strings"
|
2020-12-22 22:17:00 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-01-01 08:23:01 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/bootstrap"
|
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-04-27 09:09:58 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/generated/clientset/versioned/scheme"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/nodepassword"
|
2022-03-29 18:36:48 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2020-12-22 22:17:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-05-09 22:05:51 +00:00
|
|
|
certutil "github.com/rancher/dynamiclistener/cert"
|
2021-07-03 08:37:19 +00:00
|
|
|
coreclient "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
2019-04-19 18:20:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-27 09:09:58 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-03-29 18:36:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-04-27 09:09:58 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2019-01-09 16:54:15 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/json"
|
2022-04-04 21:53:12 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2022-04-27 09:09:58 +00:00
|
|
|
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
2019-01-26 04:56:12 +00:00
|
|
|
const (
|
2019-10-27 05:53:25 +00:00
|
|
|
staticURL = "/static/"
|
2019-01-26 04:56:12 +00:00
|
|
|
)
|
|
|
|
|
2021-07-28 20:56:59 +00:00
|
|
|
func router(ctx context.Context, config *Config, cfg *cmds.Server) http.Handler {
|
2020-12-22 22:17:00 +00:00
|
|
|
serverConfig := &config.ControlConfig
|
|
|
|
nodeAuth := passwordBootstrap(ctx, config)
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
prefix := "/v1-" + version.Program
|
2022-04-04 21:54:50 +00:00
|
|
|
authed := mux.NewRouter().SkipClean(true)
|
2020-05-05 22:09:04 +00:00
|
|
|
authed.Use(authMiddleware(serverConfig, version.Program+":agent"))
|
2020-12-22 22:17:00 +00:00
|
|
|
authed.Path(prefix + "/serving-kubelet.crt").Handler(servingKubeletCert(serverConfig, serverConfig.Runtime.ServingKubeletKey, nodeAuth))
|
|
|
|
authed.Path(prefix + "/client-kubelet.crt").Handler(clientKubeletCert(serverConfig, serverConfig.Runtime.ClientKubeletKey, nodeAuth))
|
2020-05-05 22:09:04 +00:00
|
|
|
authed.Path(prefix + "/client-kube-proxy.crt").Handler(fileHandler(serverConfig.Runtime.ClientKubeProxyCert, serverConfig.Runtime.ClientKubeProxyKey))
|
|
|
|
authed.Path(prefix + "/client-" + version.Program + "-controller.crt").Handler(fileHandler(serverConfig.Runtime.ClientK3sControllerCert, serverConfig.Runtime.ClientK3sControllerKey))
|
|
|
|
authed.Path(prefix + "/client-ca.crt").Handler(fileHandler(serverConfig.Runtime.ClientCA))
|
|
|
|
authed.Path(prefix + "/server-ca.crt").Handler(fileHandler(serverConfig.Runtime.ServerCA))
|
2022-03-29 18:36:48 +00:00
|
|
|
authed.Path(prefix + "/apiservers").Handler(apiserversHandler(serverConfig))
|
2021-07-28 20:56:59 +00:00
|
|
|
authed.Path(prefix + "/config").Handler(configHandler(serverConfig, cfg))
|
2021-07-27 21:56:05 +00:00
|
|
|
authed.Path(prefix + "/readyz").Handler(readyzHandler(serverConfig))
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2022-02-16 22:19:58 +00:00
|
|
|
if cfg.DisableAPIServer {
|
|
|
|
authed.NotFoundHandler = apiserverDisabled()
|
|
|
|
} else {
|
|
|
|
authed.NotFoundHandler = apiserver(serverConfig.Runtime)
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
nodeAuthed := mux.NewRouter().SkipClean(true)
|
|
|
|
nodeAuthed.NotFoundHandler = authed
|
2022-04-04 21:53:12 +00:00
|
|
|
nodeAuthed.Use(authMiddleware(serverConfig, user.NodesGroup))
|
2020-12-10 18:26:16 +00:00
|
|
|
nodeAuthed.Path(prefix + "/connect").Handler(serverConfig.Runtime.Tunnel)
|
2019-10-27 05:53:25 +00:00
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
serverAuthed := mux.NewRouter().SkipClean(true)
|
2019-10-27 05:53:25 +00:00
|
|
|
serverAuthed.NotFoundHandler = nodeAuthed
|
2022-04-04 21:54:50 +00:00
|
|
|
serverAuthed.Use(authMiddleware(serverConfig, version.Program+":server"))
|
2022-01-06 18:38:22 +00:00
|
|
|
serverAuthed.Path(prefix + "/encrypt/status").Handler(encryptionStatusHandler(serverConfig))
|
|
|
|
serverAuthed.Path(prefix + "/encrypt/config").Handler(encryptionConfigHandler(ctx, serverConfig))
|
2019-10-31 02:05:40 +00:00
|
|
|
serverAuthed.Path("/db/info").Handler(nodeAuthed)
|
2022-04-15 00:30:04 +00:00
|
|
|
serverAuthed.Path(prefix + "/server-bootstrap").Handler(bootstrapHandler(serverConfig.Runtime))
|
2019-10-27 05:53:25 +00:00
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
systemAuthed := mux.NewRouter().SkipClean(true)
|
|
|
|
systemAuthed.NotFoundHandler = serverAuthed
|
|
|
|
systemAuthed.MethodNotAllowedHandler = serverAuthed
|
|
|
|
systemAuthed.Use(authMiddleware(serverConfig, user.SystemPrivilegedGroup))
|
|
|
|
systemAuthed.Methods(http.MethodConnect).Handler(serverConfig.Runtime.Tunnel)
|
|
|
|
|
2019-03-18 18:50:20 +00:00
|
|
|
staticDir := filepath.Join(serverConfig.DataDir, "static")
|
2022-04-04 21:54:50 +00:00
|
|
|
router := mux.NewRouter().SkipClean(true)
|
|
|
|
router.NotFoundHandler = systemAuthed
|
2019-03-18 18:50:20 +00:00
|
|
|
router.PathPrefix(staticURL).Handler(serveStatic(staticURL, staticDir))
|
2020-12-10 18:26:16 +00:00
|
|
|
router.Path("/cacerts").Handler(cacerts(serverConfig.Runtime.ServerCA))
|
2019-01-31 22:42:27 +00:00
|
|
|
router.Path("/ping").Handler(ping())
|
2019-01-01 08:23:01 +00:00
|
|
|
|
2020-12-10 18:26:16 +00:00
|
|
|
return router
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 06:13:10 +00:00
|
|
|
func apiserver(runtime *config.ControlRuntime) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if runtime != nil && runtime.APIServer != nil {
|
|
|
|
runtime.APIServer.ServeHTTP(resp, req)
|
|
|
|
} else {
|
2022-04-27 09:09:58 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewServiceUnavailable("apiserver not ready"),
|
|
|
|
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
|
|
|
|
)
|
2021-10-12 06:13:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:19:58 +00:00
|
|
|
func apiserverDisabled() http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
2022-04-27 09:09:58 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewServiceUnavailable("apiserver disabled"),
|
|
|
|
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
|
|
|
|
)
|
2022-04-15 00:30:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func bootstrapHandler(runtime *config.ControlRuntime) http.Handler {
|
|
|
|
if runtime.HTTPBootstrap {
|
|
|
|
return bootstrap.Handler(&runtime.ControlRuntimeBootstrap)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
logrus.Warnf("Received HTTP bootstrap request from %s, but embedded etcd is not enabled.", req.RemoteAddr)
|
2022-04-27 09:09:58 +00:00
|
|
|
responsewriters.ErrorNegotiated(
|
|
|
|
apierrors.NewBadRequest("etcd disabled"),
|
|
|
|
scheme.Codecs.WithoutConversion(), schema.GroupVersion{}, resp, req,
|
|
|
|
)
|
2022-02-16 22:19:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-10 18:26:16 +00:00
|
|
|
func cacerts(serverCA string) http.Handler {
|
|
|
|
var ca []byte
|
2019-01-01 08:23:01 +00:00
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
2020-12-10 18:26:16 +00:00
|
|
|
if ca == nil {
|
|
|
|
var err error
|
2022-10-08 00:36:57 +00:00
|
|
|
ca, err = os.ReadFile(serverCA)
|
2020-12-10 18:26:16 +00:00
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
resp.Header().Set("content-type", "text/plain")
|
2019-10-27 05:53:25 +00:00
|
|
|
resp.Write(ca)
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
func getNodeInfo(req *http.Request) (string, string, error) {
|
2020-05-05 22:09:04 +00:00
|
|
|
nodeName := req.Header.Get(version.Program + "-Node-Name")
|
|
|
|
if nodeName == "" {
|
2019-05-29 18:53:51 +00:00
|
|
|
return "", "", errors.New("node name not set")
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
nodePassword := req.Header.Get(version.Program + "-Node-Password")
|
|
|
|
if nodePassword == "" {
|
2019-05-29 18:53:51 +00:00
|
|
|
return "", "", errors.New("node password not set")
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
return strings.ToLower(nodeName), nodePassword, nil
|
2019-05-29 18:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCACertAndKeys(caCertFile, caKeyFile, signingKeyFile string) ([]*x509.Certificate, crypto.Signer, crypto.Signer, error) {
|
2022-10-08 00:36:57 +00:00
|
|
|
keyBytes, err := os.ReadFile(signingKeyFile)
|
2019-05-29 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := certutil.ParsePrivateKeyPEM(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:36:57 +00:00
|
|
|
caKeyBytes, err := os.ReadFile(caKeyFile)
|
2019-05-29 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caKey, err := certutil.ParsePrivateKeyPEM(caKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:36:57 +00:00
|
|
|
caBytes, err := os.ReadFile(caCertFile)
|
2019-05-29 18:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := certutil.ParseCertsPEM(caBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return caCert, caKey.(crypto.Signer), key.(crypto.Signer), nil
|
|
|
|
}
|
|
|
|
|
2020-12-22 22:17:00 +00:00
|
|
|
func servingKubeletCert(server *config.Control, keyFile string, auth nodePassBootstrapper) http.Handler {
|
2019-01-01 08:23:01 +00:00
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 18:20:34 +00:00
|
|
|
|
2020-12-23 23:55:47 +00:00
|
|
|
nodeName, errCode, err := auth(req)
|
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp, errCode)
|
2019-04-23 17:53:06 +00:00
|
|
|
return
|
2019-04-19 18:20:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
caCert, caKey, key, err := getCACertAndKeys(server.Runtime.ServerCA, server.Runtime.ServerCAKey, server.Runtime.ServingKubeletKey)
|
2019-04-19 18:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-03 02:15:09 +00:00
|
|
|
ips := []net.IP{net.ParseIP("127.0.0.1")}
|
2021-04-21 22:56:20 +00:00
|
|
|
|
2020-09-04 01:48:30 +00:00
|
|
|
if nodeIP := req.Header.Get(version.Program + "-Node-IP"); nodeIP != "" {
|
2021-04-21 22:56:20 +00:00
|
|
|
for _, v := range strings.Split(nodeIP, ",") {
|
|
|
|
ip := net.ParseIP(v)
|
|
|
|
if ip == nil {
|
|
|
|
sendError(fmt.Errorf("invalid IP address %s", ip), resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
2020-09-03 02:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
cert, err := certutil.NewSignedCert(certutil.Config{
|
|
|
|
CommonName: nodeName,
|
|
|
|
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
AltNames: certutil.AltNames{
|
|
|
|
DNSNames: []string{nodeName, "localhost"},
|
2020-09-03 02:15:09 +00:00
|
|
|
IPs: ips,
|
2019-05-29 18:53:51 +00:00
|
|
|
},
|
|
|
|
}, key, caCert[0], caKey)
|
2019-04-19 18:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:36:57 +00:00
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
2019-11-15 23:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
resp.Write(append(certutil.EncodeCertPEM(cert), certutil.EncodeCertPEM(caCert[0])...))
|
2019-11-15 23:48:03 +00:00
|
|
|
resp.Write(keyBytes)
|
2019-05-29 18:53:51 +00:00
|
|
|
})
|
|
|
|
}
|
2019-04-19 18:20:34 +00:00
|
|
|
|
2020-12-22 22:17:00 +00:00
|
|
|
func clientKubeletCert(server *config.Control, keyFile string, auth nodePassBootstrapper) http.Handler {
|
2019-05-29 18:53:51 +00:00
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2019-04-19 18:20:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:55:47 +00:00
|
|
|
nodeName, errCode, err := auth(req)
|
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp, errCode)
|
2019-04-19 18:20:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
caCert, caKey, key, err := getCACertAndKeys(server.Runtime.ClientCA, server.Runtime.ClientCAKey, server.Runtime.ClientKubeletKey)
|
2019-04-19 18:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:53:51 +00:00
|
|
|
cert, err := certutil.NewSignedCert(certutil.Config{
|
|
|
|
CommonName: "system:node:" + nodeName,
|
2022-04-04 21:53:12 +00:00
|
|
|
Organization: []string{user.NodesGroup},
|
2019-05-29 18:53:51 +00:00
|
|
|
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
}, key, caCert[0], caKey)
|
2019-04-19 18:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
sendError(err, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:36:57 +00:00
|
|
|
keyBytes, err := os.ReadFile(keyFile)
|
2019-11-15 23:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 18:20:34 +00:00
|
|
|
resp.Write(append(certutil.EncodeCertPEM(cert), certutil.EncodeCertPEM(caCert[0])...))
|
2019-11-15 23:48:03 +00:00
|
|
|
resp.Write(keyBytes)
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:48:03 +00:00
|
|
|
func fileHandler(fileName ...string) http.Handler {
|
2019-01-01 08:23:01 +00:00
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2019-11-15 23:48:03 +00:00
|
|
|
resp.Header().Set("Content-Type", "text/plain")
|
|
|
|
|
|
|
|
if len(fileName) == 1 {
|
|
|
|
http.ServeFile(resp, req, fileName[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range fileName {
|
2022-10-08 00:36:57 +00:00
|
|
|
bytes, err := os.ReadFile(f)
|
2019-11-15 23:48:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to read %s: %v", f, err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Write(bytes)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:36:48 +00:00
|
|
|
func apiserversHandler(server *config.Control) http.Handler {
|
|
|
|
var endpointsClient coreclient.EndpointsClient
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
var endpoints []string
|
|
|
|
if endpointsClient == nil {
|
|
|
|
if server.Runtime.Core != nil {
|
|
|
|
endpointsClient = server.Runtime.Core.Core().V1().Endpoints()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if endpointsClient != nil {
|
|
|
|
if endpoint, _ := endpointsClient.Get("default", "kubernetes", metav1.GetOptions{}); endpoint != nil {
|
|
|
|
endpoints = util.GetAddresses(endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Header().Set("content-type", "application/json")
|
|
|
|
if err := json.NewEncoder(resp).Encode(endpoints); err != nil {
|
|
|
|
logrus.Errorf("Failed to encode apiserver endpoints: %v", err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-28 20:56:59 +00:00
|
|
|
func configHandler(server *config.Control, cfg *cmds.Server) http.Handler {
|
2019-01-01 08:23:01 +00:00
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-07-28 20:56:59 +00:00
|
|
|
// Startup hooks may read and modify cmds.Server in a goroutine, but as these are copied into
|
|
|
|
// config.Control before the startup hooks are called, any modifications need to be sync'd back
|
|
|
|
// into the struct before it is sent to agents.
|
|
|
|
// At this time we don't sync all the fields, just those known to be touched by startup hooks.
|
|
|
|
server.DisableKubeProxy = cfg.DisableKubeProxy
|
2019-01-01 08:23:01 +00:00
|
|
|
resp.Header().Set("content-type", "application/json")
|
2021-10-12 06:13:10 +00:00
|
|
|
if err := json.NewEncoder(resp).Encode(server); err != nil {
|
|
|
|
logrus.Errorf("Failed to encode agent config: %v", err)
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
2019-01-26 04:56:12 +00:00
|
|
|
|
2021-07-27 21:56:05 +00:00
|
|
|
func readyzHandler(server *config.Control) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
code := http.StatusOK
|
|
|
|
data := []byte("ok")
|
|
|
|
if server.Runtime.Core == nil {
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
data = []byte("runtime core not ready")
|
|
|
|
}
|
|
|
|
resp.WriteHeader(code)
|
|
|
|
resp.Header().Set("Content-Type", "text/plain")
|
|
|
|
resp.Header().Set("Content-length", strconv.Itoa(len(data)))
|
|
|
|
resp.Write(data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-31 22:42:27 +00:00
|
|
|
func ping() http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
data := []byte("pong")
|
|
|
|
resp.Header().Set("Content-Type", "text/plain")
|
|
|
|
resp.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
|
|
|
resp.Write(data)
|
|
|
|
})
|
|
|
|
}
|
2019-03-18 18:50:20 +00:00
|
|
|
|
|
|
|
func serveStatic(urlPrefix, staticDir string) http.Handler {
|
|
|
|
return http.StripPrefix(urlPrefix, http.FileServer(http.Dir(staticDir)))
|
|
|
|
}
|
2019-04-19 18:20:34 +00:00
|
|
|
|
2019-04-23 17:53:06 +00:00
|
|
|
func sendError(err error, resp http.ResponseWriter, status ...int) {
|
2020-12-23 23:55:47 +00:00
|
|
|
var code int
|
|
|
|
if len(status) == 1 {
|
2019-04-23 17:53:06 +00:00
|
|
|
code = status[0]
|
|
|
|
}
|
2020-12-23 23:55:47 +00:00
|
|
|
if code == 0 || code == http.StatusOK {
|
|
|
|
code = http.StatusInternalServerError
|
2020-12-22 22:17:00 +00:00
|
|
|
}
|
2019-04-19 18:20:34 +00:00
|
|
|
logrus.Error(err)
|
2019-04-23 17:53:06 +00:00
|
|
|
resp.WriteHeader(code)
|
2019-04-19 18:20:34 +00:00
|
|
|
resp.Write([]byte(err.Error()))
|
|
|
|
}
|
2020-12-22 22:17:00 +00:00
|
|
|
|
2020-12-23 23:55:47 +00:00
|
|
|
// nodePassBootstrapper returns a node name, or http error code and error
|
2020-12-22 22:17:00 +00:00
|
|
|
type nodePassBootstrapper func(req *http.Request) (string, int, error)
|
|
|
|
|
|
|
|
func passwordBootstrap(ctx context.Context, config *Config) nodePassBootstrapper {
|
|
|
|
runtime := config.ControlConfig.Runtime
|
2022-03-18 09:07:19 +00:00
|
|
|
deferredNodes := map[string]bool{}
|
2020-12-22 22:17:00 +00:00
|
|
|
var secretClient coreclient.SecretClient
|
2022-03-18 09:07:19 +00:00
|
|
|
var mu sync.Mutex
|
2020-12-22 22:17:00 +00:00
|
|
|
|
|
|
|
return nodePassBootstrapper(func(req *http.Request) (string, int, error) {
|
|
|
|
nodeName, nodePassword, err := getNodeInfo(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if secretClient == nil {
|
|
|
|
if runtime.Core != nil {
|
|
|
|
// initialize the client if we can
|
|
|
|
secretClient = runtime.Core.Core().V1().Secret()
|
|
|
|
} else if nodeName == os.Getenv("NODE_NAME") {
|
|
|
|
// or verify the password locally and ensure a secret later
|
2022-03-18 09:07:19 +00:00
|
|
|
return verifyLocalPassword(ctx, config, &mu, deferredNodes, nodeName, nodePassword)
|
|
|
|
} else if config.ControlConfig.DisableAPIServer {
|
|
|
|
// or defer node password verification until an apiserver joins the cluster
|
|
|
|
return verifyRemotePassword(ctx, config, &mu, deferredNodes, nodeName, nodePassword)
|
2020-12-22 22:17:00 +00:00
|
|
|
} else {
|
|
|
|
// or reject the request until the core is ready
|
|
|
|
return "", http.StatusServiceUnavailable, errors.New("runtime core not ready")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodepassword.Ensure(secretClient, nodeName, nodePassword); err != nil {
|
|
|
|
return "", http.StatusForbidden, err
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:55:47 +00:00
|
|
|
return nodeName, http.StatusOK, nil
|
2020-12-22 22:17:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-18 09:07:19 +00:00
|
|
|
func verifyLocalPassword(ctx context.Context, config *Config, mu *sync.Mutex, deferredNodes map[string]bool, nodeName, nodePassword string) (string, int, error) {
|
2020-12-22 22:17:00 +00:00
|
|
|
// use same password file location that the agent creates
|
|
|
|
nodePasswordRoot := "/"
|
2022-09-29 20:16:33 +00:00
|
|
|
if config.ControlConfig.Rootless {
|
2020-12-22 22:17:00 +00:00
|
|
|
nodePasswordRoot = filepath.Join(config.ControlConfig.DataDir, "agent")
|
|
|
|
}
|
|
|
|
nodeConfigPath := filepath.Join(nodePasswordRoot, "etc", "rancher", "node")
|
|
|
|
nodePasswordFile := filepath.Join(nodeConfigPath, "password")
|
|
|
|
|
2022-10-08 00:36:57 +00:00
|
|
|
passBytes, err := os.ReadFile(nodePasswordFile)
|
2020-12-22 22:17:00 +00:00
|
|
|
if err != nil {
|
2020-12-23 22:05:55 +00:00
|
|
|
return "", http.StatusInternalServerError, errors.Wrap(err, "unable to read node password file")
|
2020-12-22 22:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
password := strings.TrimSpace(string(passBytes))
|
|
|
|
if password != nodePassword {
|
|
|
|
return "", http.StatusForbidden, errors.Wrapf(err, "unable to verify local password for node '%s'", nodeName)
|
|
|
|
}
|
|
|
|
|
2022-03-18 09:07:19 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := deferredNodes[nodeName]; !ok {
|
|
|
|
deferredNodes[nodeName] = true
|
|
|
|
go ensureSecret(ctx, config, nodeName, nodePassword)
|
|
|
|
logrus.Debugf("Password verified locally for node '%s'", nodeName)
|
2020-12-22 22:17:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 09:07:19 +00:00
|
|
|
return nodeName, http.StatusOK, nil
|
|
|
|
}
|
2020-12-22 22:17:00 +00:00
|
|
|
|
2022-03-18 09:07:19 +00:00
|
|
|
func verifyRemotePassword(ctx context.Context, config *Config, mu *sync.Mutex, deferredNodes map[string]bool, nodeName, nodePassword string) (string, int, error) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := deferredNodes[nodeName]; !ok {
|
|
|
|
deferredNodes[nodeName] = true
|
|
|
|
go ensureSecret(ctx, config, nodeName, nodePassword)
|
|
|
|
logrus.Debugf("Password verification deferred for node '%s'", nodeName)
|
|
|
|
}
|
2020-12-22 22:17:00 +00:00
|
|
|
|
2020-12-23 23:55:47 +00:00
|
|
|
return nodeName, http.StatusOK, nil
|
2020-12-22 22:17:00 +00:00
|
|
|
}
|
2022-03-18 09:07:19 +00:00
|
|
|
|
|
|
|
func ensureSecret(ctx context.Context, config *Config, nodeName, nodePassword string) {
|
|
|
|
runtime := config.ControlConfig.Runtime
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if runtime.Core != nil {
|
|
|
|
logrus.Debugf("Runtime core has become available, ensuring password secret for node '%s'", nodeName)
|
|
|
|
secretClient := runtime.Core.Core().V1().Secret()
|
|
|
|
if err := nodepassword.Ensure(secretClient, nodeName, nodePassword); err != nil {
|
|
|
|
logrus.Warnf("Error ensuring node password secret for pre-validated node '%s': %v", nodeName, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|