2021-12-02 21:19:16 +00:00
|
|
|
package cert
|
|
|
|
|
|
|
|
import (
|
2022-12-10 00:20:51 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-12-02 21:19:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2024-03-23 00:13:08 +00:00
|
|
|
"strings"
|
2021-12-02 21:19:16 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/erikdubbelboer/gspt"
|
2023-08-01 15:55:34 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent/util"
|
2022-12-10 00:20:51 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/bootstrap"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
2022-12-10 00:20:51 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/clientaccess"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/control/deps"
|
|
|
|
"github.com/k3s-io/k3s/pkg/datadir"
|
|
|
|
"github.com/k3s-io/k3s/pkg/server"
|
2024-03-23 00:13:08 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util/services"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2021-12-02 21:19:16 +00:00
|
|
|
"github.com/otiai10/copy"
|
2022-12-10 00:20:51 +00:00
|
|
|
"github.com/pkg/errors"
|
2024-03-23 00:13:08 +00:00
|
|
|
certutil "github.com/rancher/dynamiclistener/cert"
|
2021-12-02 21:19:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2022-12-10 00:20:51 +00:00
|
|
|
func commandSetup(app *cli.Context, cfg *cmds.Server, sc *server.Config) (string, error) {
|
2021-12-02 21:19:16 +00:00
|
|
|
gspt.SetProcTitle(os.Args[0])
|
|
|
|
|
|
|
|
dataDir, err := datadir.Resolve(cfg.DataDir)
|
|
|
|
if err != nil {
|
2022-12-10 00:20:51 +00:00
|
|
|
return "", err
|
2021-12-02 21:19:16 +00:00
|
|
|
}
|
2022-12-10 00:20:51 +00:00
|
|
|
sc.ControlConfig.DataDir = filepath.Join(dataDir, "server")
|
|
|
|
|
|
|
|
if cfg.Token == "" {
|
|
|
|
fp := filepath.Join(sc.ControlConfig.DataDir, "token")
|
|
|
|
tokenByte, err := os.ReadFile(fp)
|
2024-03-23 00:13:08 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2022-12-10 00:20:51 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cfg.Token = string(bytes.TrimRight(tokenByte, "\n"))
|
|
|
|
}
|
|
|
|
sc.ControlConfig.Token = cfg.Token
|
2023-02-08 00:37:10 +00:00
|
|
|
sc.ControlConfig.Runtime = config.NewRuntime(nil)
|
2022-12-10 00:20:51 +00:00
|
|
|
|
|
|
|
return dataDir, nil
|
2021-12-02 21:19:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
func Check(app *cli.Context) error {
|
2021-12-02 21:19:16 +00:00
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-23 00:13:08 +00:00
|
|
|
return check(app, &cmds.ServerConfig)
|
2021-12-02 21:19:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
func check(app *cli.Context, cfg *cmds.Server) error {
|
2021-12-02 21:19:16 +00:00
|
|
|
var serverConfig server.Config
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
_, err := commandSetup(app, cfg, &serverConfig)
|
2021-12-02 21:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:01:14 +00:00
|
|
|
deps.CreateRuntimeCertFiles(&serverConfig.ControlConfig)
|
2021-12-02 21:19:16 +00:00
|
|
|
|
2021-12-09 16:57:13 +00:00
|
|
|
if err := validateCertConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
if len(cmds.ServicesList) == 0 {
|
|
|
|
// detecting if the command is being run on an agent or server based on presence of the server data-dir
|
|
|
|
_, err := os.Stat(serverConfig.ControlConfig.DataDir)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("Agent detected, checking agent certificates")
|
|
|
|
cmds.ServicesList = services.Agent
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Server detected, checking agent and server certificates")
|
|
|
|
cmds.ServicesList = services.All
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMap, err := services.FilesForServices(serverConfig.ControlConfig, cmds.ServicesList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
warn := now.Add(time.Hour * 24 * config.CertificateRenewDays)
|
|
|
|
|
|
|
|
for service, files := range fileMap {
|
|
|
|
logrus.Info("Checking certificates for " + service)
|
|
|
|
for _, file := range files {
|
|
|
|
// ignore errors, as some files may not exist, or may not contain certs.
|
|
|
|
// Only check whatever exists and has certs.
|
|
|
|
certs, _ := certutil.CertsFromFile(file)
|
|
|
|
for _, cert := range certs {
|
|
|
|
if now.Before(cert.NotBefore) {
|
|
|
|
logrus.Errorf("%s: certificate %s is not valid before %s", file, cert.Subject, cert.NotBefore.Format(time.RFC3339))
|
|
|
|
} else if now.After(cert.NotAfter) {
|
|
|
|
logrus.Errorf("%s: certificate %s expired at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339))
|
|
|
|
} else if warn.After(cert.NotAfter) {
|
|
|
|
logrus.Warnf("%s: certificate %s will expire within %d days at %s", file, cert.Subject, config.CertificateRenewDays, cert.NotAfter.Format(time.RFC3339))
|
|
|
|
} else {
|
|
|
|
logrus.Infof("%s: certificate %s is ok, expires at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Rotate(app *cli.Context) error {
|
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rotate(app, &cmds.ServerConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rotate(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
var serverConfig server.Config
|
|
|
|
|
|
|
|
dataDir, err := commandSetup(app, cfg, &serverConfig)
|
2021-12-02 21:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
deps.CreateRuntimeCertFiles(&serverConfig.ControlConfig)
|
|
|
|
|
|
|
|
if err := validateCertConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-02 21:19:16 +00:00
|
|
|
if len(cmds.ServicesList) == 0 {
|
2024-03-23 00:13:08 +00:00
|
|
|
// detecting if the command is being run on an agent or server based on presence of the server data-dir
|
2022-12-10 00:20:51 +00:00
|
|
|
_, err := os.Stat(serverConfig.ControlConfig.DataDir)
|
2021-12-02 21:19:16 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("Agent detected, rotating agent certificates")
|
2024-03-23 00:13:08 +00:00
|
|
|
cmds.ServicesList = services.Agent
|
2021-12-02 21:19:16 +00:00
|
|
|
} else {
|
2024-03-23 00:13:08 +00:00
|
|
|
logrus.Infof("Server detected, rotating agent and server certificates")
|
|
|
|
cmds.ServicesList = services.All
|
2021-12-02 21:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 00:13:08 +00:00
|
|
|
|
|
|
|
fileMap, err := services.FilesForServices(serverConfig.ControlConfig, cmds.ServicesList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// back up all the files
|
|
|
|
agentDataDir := filepath.Join(dataDir, "agent")
|
|
|
|
tlsBackupDir, err := backupCertificates(serverConfig.ControlConfig.DataDir, agentDataDir, fileMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dynamiclistener cache file can't be simply deleted, we need to create a trigger
|
|
|
|
// file to indicate that the cert needs to be regenerated on startup.
|
2021-12-02 21:19:16 +00:00
|
|
|
for _, service := range cmds.ServicesList {
|
2024-03-23 00:13:08 +00:00
|
|
|
if service == version.Program+services.ProgramServer {
|
2022-12-10 00:20:51 +00:00
|
|
|
dynamicListenerRegenFilePath := filepath.Join(serverConfig.ControlConfig.DataDir, "tls", "dynamic-cert-regenerate")
|
2022-10-08 00:36:57 +00:00
|
|
|
if err := os.WriteFile(dynamicListenerRegenFilePath, []byte{}, 0600); err != nil {
|
2021-12-02 21:19:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("Rotating dynamic listener certificate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
// remove all files
|
|
|
|
for service, files := range fileMap {
|
|
|
|
logrus.Info("Rotating certificates for " + service)
|
|
|
|
for _, file := range files {
|
|
|
|
if err := os.Remove(file); err == nil {
|
|
|
|
logrus.Debugf("file %s is deleted", file)
|
|
|
|
}
|
2021-12-02 21:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 00:13:08 +00:00
|
|
|
logrus.Infof("Successfully backed up certificates to %s, please restart %s server or agent to rotate certificates", tlsBackupDir, version.Program)
|
2021-12-02 21:19:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
func backupCertificates(serverDataDir, agentDataDir string, fileMap map[string][]string) (string, error) {
|
2021-12-02 21:19:16 +00:00
|
|
|
serverTLSDir := filepath.Join(serverDataDir, "tls")
|
|
|
|
tlsBackupDir := filepath.Join(serverDataDir, "tls-"+strconv.Itoa(int(time.Now().Unix())))
|
|
|
|
|
|
|
|
if _, err := os.Stat(serverTLSDir); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := copy.Copy(serverTLSDir, tlsBackupDir); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-12-09 16:57:13 +00:00
|
|
|
|
2024-03-23 00:13:08 +00:00
|
|
|
for _, files := range fileMap {
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasPrefix(file, agentDataDir) {
|
|
|
|
cert := filepath.Base(file)
|
|
|
|
tlsBackupCert := filepath.Join(tlsBackupDir, cert)
|
|
|
|
if err := util.CopyFile(file, tlsBackupCert, true); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 16:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 00:13:08 +00:00
|
|
|
|
|
|
|
return tlsBackupDir, nil
|
2021-12-09 16:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateCertConfig() error {
|
|
|
|
for _, s := range cmds.ServicesList {
|
2024-03-23 00:13:08 +00:00
|
|
|
if !services.IsValid(s) {
|
|
|
|
return errors.New("service " + s + " is not recognized")
|
2021-12-09 16:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-10 00:20:51 +00:00
|
|
|
|
|
|
|
func RotateCA(app *cli.Context) error {
|
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rotateCA(app, &cmds.ServerConfig, &cmds.CertRotateCAConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rotateCA(app *cli.Context, cfg *cmds.Server, sync *cmds.CertRotateCA) error {
|
|
|
|
var serverConfig server.Config
|
|
|
|
|
|
|
|
_, err := commandSetup(app, cfg, &serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-08 23:59:21 +00:00
|
|
|
info, err := clientaccess.ParseAndValidateToken(cmds.ServerConfig.ServerURL, serverConfig.ControlConfig.Token, clientaccess.WithUser("server"))
|
2022-12-10 00:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up dummy server config for reading new bootstrap data from disk.
|
|
|
|
tmpServer := &config.Control{
|
2023-02-08 00:37:10 +00:00
|
|
|
Runtime: config.NewRuntime(nil),
|
2023-02-13 22:43:22 +00:00
|
|
|
DataDir: sync.CACertPath,
|
2022-12-10 00:20:51 +00:00
|
|
|
}
|
|
|
|
deps.CreateRuntimeCertFiles(tmpServer)
|
|
|
|
|
|
|
|
// Override these paths so that we don't get warnings when they don't exist, as the user is not expected to provide them.
|
|
|
|
tmpServer.Runtime.PasswdFile = "/dev/null"
|
|
|
|
tmpServer.Runtime.IPSECKey = "/dev/null"
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := bootstrap.ReadFromDisk(buf, &tmpServer.Runtime.ControlRuntimeBootstrap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("/v1-%s/cert/cacerts?force=%t", version.Program, sync.Force)
|
|
|
|
if err = info.Put(url, buf.Bytes()); err != nil {
|
|
|
|
return errors.Wrap(err, "see server log for details")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("certificates saved to datastore")
|
|
|
|
return nil
|
|
|
|
}
|