2019-11-11 22:18:26 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-06-22 20:42:34 +00:00
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-03-16 16:14:43 +00:00
|
|
|
"strings"
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2020-11-30 23:45:22 +00:00
|
|
|
"github.com/k3s-io/kine/pkg/client"
|
2019-11-11 22:18:26 +00:00
|
|
|
"github.com/rancher/k3s/pkg/bootstrap"
|
2021-06-22 20:42:34 +00:00
|
|
|
"github.com/rancher/k3s/pkg/clientaccess"
|
2021-12-07 22:31:32 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2021-03-11 20:07:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-11-11 22:18:26 +00:00
|
|
|
)
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
// Save writes the current ControlRuntimeBootstrap data to the datastore. This contains a complete
|
2020-09-24 06:29:25 +00:00
|
|
|
// snapshot of the cluster's CA certs and keys, encryption passphrases, etc - encrypted with the join token.
|
|
|
|
// This is used when bootstrapping a cluster from a managed database or external etcd cluster.
|
|
|
|
// This is NOT used with embedded etcd, which bootstraps over HTTP.
|
2022-02-25 17:26:13 +00:00
|
|
|
func Save(ctx context.Context, config *config.Control, override bool) error {
|
2019-11-11 22:18:26 +00:00
|
|
|
buf := &bytes.Buffer{}
|
2021-12-07 22:31:32 +00:00
|
|
|
if err := bootstrap.ReadFromDisk(buf, &config.Runtime.ControlRuntimeBootstrap); err != nil {
|
2019-11-11 22:18:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-07 22:31:32 +00:00
|
|
|
token := config.Token
|
2021-06-22 20:42:34 +00:00
|
|
|
if token == "" {
|
2021-12-07 22:31:32 +00:00
|
|
|
tokenFromFile, err := readTokenFromFile(config.Runtime.ServerToken, config.Runtime.ServerCA, config.DataDir)
|
2021-06-22 20:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token = tokenFromFile
|
|
|
|
}
|
|
|
|
normalizedToken, err := normalizeToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
data, err := encrypt(normalizedToken, buf.Bytes())
|
2019-11-11 22:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-16 16:14:43 +00:00
|
|
|
|
2022-02-25 17:26:13 +00:00
|
|
|
storageClient, err := client.New(config.Runtime.EtcdConfig)
|
2021-03-11 20:07:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-14 21:58:45 +00:00
|
|
|
defer storageClient.Close()
|
2021-03-11 20:07:40 +00:00
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
if _, _, err = getBootstrapKeyFromStorage(ctx, storageClient, normalizedToken, token); err != nil {
|
2021-06-22 20:42:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storageClient.Create(ctx, storageKey(normalizedToken), data); err != nil {
|
2021-03-11 20:07:40 +00:00
|
|
|
if err.Error() == "key exists" {
|
2021-10-07 19:47:00 +00:00
|
|
|
logrus.Warn("bootstrap key already exists")
|
|
|
|
if override {
|
2021-12-07 22:31:32 +00:00
|
|
|
bsd, err := bootstrapKeyData(ctx, storageClient)
|
2021-10-07 19:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return storageClient.Update(ctx, storageKey(normalizedToken), bsd.Modified, data)
|
|
|
|
}
|
2021-03-11 20:07:40 +00:00
|
|
|
return nil
|
2021-03-16 16:14:43 +00:00
|
|
|
} else if strings.Contains(err.Error(), "not supported for learner") {
|
2021-06-22 20:42:34 +00:00
|
|
|
logrus.Debug("skipping bootstrap data save on learner")
|
2021-03-16 16:14:43 +00:00
|
|
|
return nil
|
2021-03-11 20:07:40 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2021-03-11 20:07:40 +00:00
|
|
|
return nil
|
2019-11-11 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 19:47:00 +00:00
|
|
|
// bootstrapKeyData lists keys stored in the datastore with the prefix "/bootstrap", and
|
|
|
|
// will return the first such key. It will return an error if not exactly one key is found.
|
2021-12-07 22:31:32 +00:00
|
|
|
func bootstrapKeyData(ctx context.Context, storageClient client.Client) (*client.Value, error) {
|
2021-10-07 19:47:00 +00:00
|
|
|
bootstrapList, err := storageClient.List(ctx, "/bootstrap", 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(bootstrapList) == 0 {
|
|
|
|
return nil, errors.New("no bootstrap data found")
|
|
|
|
}
|
|
|
|
if len(bootstrapList) > 1 {
|
|
|
|
return nil, errors.New("found multiple bootstrap keys in storage")
|
|
|
|
}
|
|
|
|
return &bootstrapList[0], nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// storageBootstrap loads data from the datastore into the ControlRuntimeBootstrap struct.
|
|
|
|
// The storage key and encryption passphrase are both derived from the join token.
|
2021-10-07 19:47:00 +00:00
|
|
|
// token is either passed.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) storageBootstrap(ctx context.Context) error {
|
2019-11-11 22:18:26 +00:00
|
|
|
if err := c.startStorage(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:26:13 +00:00
|
|
|
storageClient, err := client.New(c.config.Runtime.EtcdConfig)
|
2019-11-11 22:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-14 21:58:45 +00:00
|
|
|
defer storageClient.Close()
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
token := c.config.Token
|
|
|
|
if token == "" {
|
2022-02-24 19:01:14 +00:00
|
|
|
tokenFromFile, err := readTokenFromFile(c.config.Runtime.ServerToken, c.config.Runtime.ServerCA, c.config.DataDir)
|
2021-06-22 20:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tokenFromFile == "" {
|
|
|
|
// at this point this is a fresh start in a non managed environment
|
|
|
|
c.saveBootstrap = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
token = tokenFromFile
|
|
|
|
}
|
|
|
|
normalizedToken, err := normalizeToken(token)
|
|
|
|
if err != nil {
|
2019-11-11 22:18:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
value, saveBootstrap, err := getBootstrapKeyFromStorage(ctx, storageClient, normalizedToken, token)
|
|
|
|
c.saveBootstrap = saveBootstrap
|
2021-06-22 20:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-21 17:21:35 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
data, err := decrypt(normalizedToken, value.Data)
|
2019-11-11 22:18:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:35:08 +00:00
|
|
|
return c.ReconcileBootstrapData(ctx, bytes.NewReader(data), &c.config.Runtime.ControlRuntimeBootstrap, false)
|
2019-11-11 22:18:26 +00:00
|
|
|
}
|
2021-06-22 20:42:34 +00:00
|
|
|
|
|
|
|
// getBootstrapKeyFromStorage will list all keys that has prefix /bootstrap and will check for key that is
|
|
|
|
// hashed with empty string and will check for any key that is hashed by different token than the one
|
|
|
|
// passed to it, it will return error if it finds a key that is hashed with different token and will return
|
|
|
|
// value if it finds the key hashed by passed token or empty string
|
2021-12-07 22:31:32 +00:00
|
|
|
func getBootstrapKeyFromStorage(ctx context.Context, storageClient client.Client, normalizedToken, oldToken string) (*client.Value, bool, error) {
|
2021-06-22 20:42:34 +00:00
|
|
|
emptyStringKey := storageKey("")
|
2021-07-21 17:21:35 +00:00
|
|
|
tokenKey := storageKey(normalizedToken)
|
2021-06-22 20:42:34 +00:00
|
|
|
bootstrapList, err := storageClient.List(ctx, "/bootstrap", 0)
|
|
|
|
if err != nil {
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil, false, err
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
|
|
|
if len(bootstrapList) == 0 {
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil, true, nil
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
|
|
|
if len(bootstrapList) > 1 {
|
2021-07-21 00:50:36 +00:00
|
|
|
logrus.Warn("found multiple bootstrap keys in storage")
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
2021-07-21 18:59:57 +00:00
|
|
|
// check for empty string key and for old token format with k10 prefix
|
2021-12-07 22:31:32 +00:00
|
|
|
if err := migrateOldTokens(ctx, bootstrapList, storageClient, emptyStringKey, tokenKey, normalizedToken, oldToken); err != nil {
|
|
|
|
return nil, false, err
|
2021-07-21 17:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getting the list of bootstrap again after migrating the empty key
|
|
|
|
bootstrapList, err = storageClient.List(ctx, "/bootstrap", 0)
|
|
|
|
if err != nil {
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil, false, err
|
2021-07-21 17:21:35 +00:00
|
|
|
}
|
2021-07-21 00:50:36 +00:00
|
|
|
for _, bootstrapKV := range bootstrapList {
|
2021-07-21 20:42:05 +00:00
|
|
|
// ensure bootstrap is stored in the current token's key
|
2021-07-21 17:56:12 +00:00
|
|
|
if string(bootstrapKV.Key) == tokenKey {
|
2021-12-07 22:31:32 +00:00
|
|
|
return &bootstrapKV, false, nil
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil, false, errors.New("bootstrap data already found and encrypted with different token")
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readTokenFromFile will attempt to get the token from <data-dir>/token if it the file not found
|
|
|
|
// in case of fresh installation it will try to use the runtime serverToken saved in memory
|
|
|
|
// after stripping it from any additional information like the username or cahash, if the file
|
|
|
|
// found then it will still strip the token from any additional info
|
|
|
|
func readTokenFromFile(serverToken, certs, dataDir string) (string, error) {
|
|
|
|
tokenFile := filepath.Join(dataDir, "token")
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
b, err := ioutil.ReadFile(tokenFile)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
token, err := clientaccess.FormatToken(serverToken, certs)
|
|
|
|
if err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
// strip the token from any new line if its read from file
|
|
|
|
return string(bytes.TrimRight(b, "\n")), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeToken will normalize the token read from file or passed as a cli flag
|
|
|
|
func normalizeToken(token string) (string, error) {
|
|
|
|
_, password, ok := clientaccess.ParseUsernamePassword(token)
|
|
|
|
if !ok {
|
2021-10-11 17:00:22 +00:00
|
|
|
return password, errors.New("failed to normalize token; must be in format K10<CA-HASH>::<USERNAME>:<PASSWORD> or <PASSWORD>")
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
return password, nil
|
|
|
|
}
|
2021-07-21 17:21:35 +00:00
|
|
|
|
2021-07-21 18:59:57 +00:00
|
|
|
// migrateOldTokens will list all keys that has prefix /bootstrap and will check for key that is
|
|
|
|
// hashed with empty string and keys that is hashed with old token format before normalizing
|
|
|
|
// then migrate those and resave only with the normalized token
|
2021-12-07 22:31:32 +00:00
|
|
|
func migrateOldTokens(ctx context.Context, bootstrapList []client.Value, storageClient client.Client, emptyStringKey, tokenKey, token, oldToken string) error {
|
2021-07-21 18:59:57 +00:00
|
|
|
oldTokenKey := storageKey(oldToken)
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 17:21:35 +00:00
|
|
|
for _, bootstrapKV := range bootstrapList {
|
|
|
|
// checking for empty string bootstrap key
|
|
|
|
if string(bootstrapKV.Key) == emptyStringKey {
|
|
|
|
logrus.Warn("bootstrap data encrypted with empty string, deleting and resaving with token")
|
2021-07-21 18:59:57 +00:00
|
|
|
if err := doMigrateToken(ctx, storageClient, bootstrapKV, "", emptyStringKey, token, tokenKey); err != nil {
|
2021-07-21 17:21:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-21 18:59:57 +00:00
|
|
|
} else if string(bootstrapKV.Key) == oldTokenKey && oldTokenKey != tokenKey {
|
|
|
|
logrus.Warn("bootstrap data encrypted with old token format string, deleting and resaving with token")
|
|
|
|
if err := doMigrateToken(ctx, storageClient, bootstrapKV, oldToken, oldTokenKey, token, tokenKey); err != nil {
|
2021-07-21 17:21:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 17:21:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-21 18:59:57 +00:00
|
|
|
|
|
|
|
func doMigrateToken(ctx context.Context, storageClient client.Client, keyValue client.Value, oldToken, oldTokenKey, newToken, newTokenKey string) error {
|
|
|
|
// make sure that the process is non-destructive by decrypting/re-encrypting/storing the data before deleting the old key
|
|
|
|
data, err := decrypt(oldToken, keyValue.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 18:59:57 +00:00
|
|
|
encryptedData, err := encrypt(newToken, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 18:59:57 +00:00
|
|
|
// saving the new encrypted data with the right token key
|
|
|
|
if err := storageClient.Create(ctx, newTokenKey, encryptedData); err != nil {
|
|
|
|
if err.Error() == "key exists" {
|
2021-07-21 20:39:44 +00:00
|
|
|
logrus.Warn("bootstrap key exists")
|
2021-07-21 18:59:57 +00:00
|
|
|
} else if strings.Contains(err.Error(), "not supported for learner") {
|
|
|
|
logrus.Debug("skipping bootstrap data save on learner")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 18:59:57 +00:00
|
|
|
logrus.Infof("created bootstrap key %s", newTokenKey)
|
|
|
|
// deleting the old key
|
2021-07-21 20:18:56 +00:00
|
|
|
if err := storageClient.Delete(ctx, oldTokenKey, keyValue.Modified); err != nil {
|
|
|
|
logrus.Warnf("failed to delete old bootstrap key %s", oldTokenKey)
|
|
|
|
}
|
2021-10-07 19:47:00 +00:00
|
|
|
|
2021-07-21 20:18:56 +00:00
|
|
|
return nil
|
2021-07-21 18:59:57 +00:00
|
|
|
}
|