2019-11-11 22:18:26 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-06-22 20:42:34 +00:00
|
|
|
"errors"
|
2023-04-05 00:52:14 +00:00
|
|
|
"time"
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/bootstrap"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2023-10-10 01:06:32 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2020-11-30 23:45:22 +00:00
|
|
|
"github.com/k3s-io/kine/pkg/client"
|
2021-03-11 20:07:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-04-05 00:52:14 +00:00
|
|
|
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2019-11-11 22:18:26 +00:00
|
|
|
)
|
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
// maxBootstrapWaitAttempts is the number of iterations to wait for another node to populate an empty bootstrap key.
|
|
|
|
// After this many attempts, the lock is deleted and the counter reset.
|
|
|
|
const maxBootstrapWaitAttempts = 5
|
|
|
|
|
2023-10-09 17:58:49 +00:00
|
|
|
func RotateBootstrapToken(ctx context.Context, config *config.Control, oldToken string) error {
|
|
|
|
|
2023-10-10 01:06:32 +00:00
|
|
|
token, err := util.ReadTokenFromFile(config.Runtime.ServerToken, config.Runtime.ServerCA, config.DataDir)
|
2023-10-09 17:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:06:32 +00:00
|
|
|
normalizedToken, err := util.NormalizeToken(token)
|
2023-10-09 17:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
storageClient, err := client.New(config.Runtime.EtcdConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer storageClient.Close()
|
|
|
|
|
|
|
|
tokenKey := storageKey(normalizedToken)
|
|
|
|
|
|
|
|
var bootstrapList []client.Value
|
|
|
|
if err := wait.PollImmediateUntilWithContext(ctx, 5*time.Second, func(ctx context.Context) (bool, error) {
|
|
|
|
bootstrapList, err = storageClient.List(ctx, "/bootstrap", 0)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:06:32 +00:00
|
|
|
normalizedOldToken, err := util.NormalizeToken(oldToken)
|
2023-10-09 17:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// reuse the existing migration function to reencrypt bootstrap data with new token
|
|
|
|
if err := migrateTokens(ctx, bootstrapList, storageClient, "", tokenKey, normalizedToken, normalizedOldToken); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-04-05 00:52:14 +00:00
|
|
|
logrus.Info("Saving cluster bootstrap data to datastore")
|
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 == "" {
|
2023-10-10 01:06:32 +00:00
|
|
|
tokenFromFile, err := util.ReadTokenFromFile(config.Runtime.ServerToken, config.Runtime.ServerCA, config.DataDir)
|
2021-06-22 20:42:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token = tokenFromFile
|
|
|
|
}
|
2023-10-10 01:06:32 +00:00
|
|
|
normalizedToken, err := util.NormalizeToken(token)
|
2021-06-22 20:42:34 +00:00
|
|
|
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
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
currentKey, _, err := getBootstrapKeyFromStorage(ctx, storageClient, normalizedToken, token)
|
|
|
|
if err != nil {
|
2021-06-22 20:42:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
// If there's an empty bootstrap key, then we've locked it and can override.
|
|
|
|
if currentKey != nil && len(currentKey.Data) == 0 {
|
|
|
|
logrus.Info("Bootstrap key lock is held")
|
|
|
|
override = true
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:42:34 +00:00
|
|
|
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
|
|
|
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)
|
|
|
|
}
|
2023-04-05 00:52:14 +00:00
|
|
|
logrus.Warn("Bootstrap key already exists")
|
2021-03-11 20:07:40 +00:00
|
|
|
return nil
|
2023-04-05 00:52:14 +00:00
|
|
|
} else if errors.Is(err, rpctypes.ErrGPRCNotSupportedForLearner) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
// storageBootstrap loads data from the datastore's bootstrap key into the
|
|
|
|
// ControlRuntimeBootstrap struct. The storage key and encryption passphrase are both derived
|
|
|
|
// from the join token. If no bootstrap key exists, indicating that data needs to be written
|
|
|
|
// back to the datastore, this function will set c.saveBootstrap to true and create an empty
|
|
|
|
// bootstrap key as a lock. This function will not return successfully until either the
|
|
|
|
// bootstrap key has been locked, or data is read into the struct.
|
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 == "" {
|
2023-10-10 01:06:32 +00:00
|
|
|
tokenFromFile, err := util.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 == "" {
|
2023-04-05 00:52:14 +00:00
|
|
|
// No token on disk or from CLI, but we don't know if there's data in the datastore.
|
|
|
|
// Return here and generate new CA certs and tokens. Note that startup will fail
|
|
|
|
// later when saving to the datastore if there's already a bootstrap key - but
|
|
|
|
// that's AFTER generating CA certs and tokens. If the config is updated to set the
|
|
|
|
// matching key, further startups will still be blocked pending cleanup of the
|
|
|
|
// "newer" files as per the bootstrap reconciliation code.
|
2021-06-22 20:42:34 +00:00
|
|
|
c.saveBootstrap = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
token = tokenFromFile
|
|
|
|
}
|
2023-10-10 01:06:32 +00:00
|
|
|
normalizedToken, err := util.NormalizeToken(token)
|
2021-06-22 20:42:34 +00:00
|
|
|
if err != nil {
|
2019-11-11 22:18:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
attempts := 0
|
|
|
|
tokenKey := storageKey(normalizedToken)
|
|
|
|
return wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
value, saveBootstrap, err := getBootstrapKeyFromStorage(ctx, storageClient, normalizedToken, token)
|
|
|
|
c.saveBootstrap = saveBootstrap
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-07-21 17:21:35 +00:00
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
if value == nil {
|
|
|
|
// No bootstrap keys found in the datastore - create an empty bootstrap key as a lock to
|
|
|
|
// ensure that no other node races us to populate it. If we fail to create the key, then
|
|
|
|
// some other node beat us to it and we should just wait for them to finish.
|
|
|
|
if err := storageClient.Create(ctx, tokenKey, []byte{}); err != nil {
|
|
|
|
if err.Error() == "key exists" {
|
|
|
|
logrus.Info("Bootstrap key already locked - waiting for data to be populated by another server")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
logrus.Info("Bootstrap key locked for initial create")
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value.Data) == 0 {
|
|
|
|
// Empty (locked) bootstrap key found - check to see if we should continue waiting, or
|
|
|
|
// delete it and attempt to retake the lock on the next iteration (assuming that the
|
|
|
|
// other node failed while holding the lock).
|
|
|
|
if attempts >= maxBootstrapWaitAttempts {
|
|
|
|
logrus.Info("Bootstrap key lock timed out - deleting lock and retrying")
|
|
|
|
attempts = 0
|
|
|
|
if err := storageClient.Delete(ctx, tokenKey, value.Modified); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Bootstrap key is locked - waiting for data to be populated by another server")
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := decrypt(normalizedToken, value.Data)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-11-11 22:18:26 +00:00
|
|
|
|
2023-04-05 00:52:14 +00:00
|
|
|
return true, 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
|
2023-04-05 00:52:14 +00:00
|
|
|
// value if it finds the key hashed by passed token or empty string.
|
|
|
|
// Upon receiving a "not supported for learner" error from etcd, this function will retry until the context is cancelled.
|
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)
|
2023-04-05 00:52:14 +00:00
|
|
|
|
|
|
|
var bootstrapList []client.Value
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err := wait.PollImmediateUntilWithContext(ctx, 5*time.Second, func(ctx context.Context) (bool, error) {
|
|
|
|
bootstrapList, err = storageClient.List(ctx, "/bootstrap", 0)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, rpctypes.ErrGPRCNotSupportedForLearner) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}); err != nil {
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil, false, err
|
2021-06-22 20:42:34 +00:00
|
|
|
}
|
2023-04-05 00:52:14 +00:00
|
|
|
|
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
|
2023-10-09 17:58:49 +00:00
|
|
|
if err := migrateTokens(ctx, bootstrapList, storageClient, emptyStringKey, tokenKey, normalizedToken, oldToken); err != nil {
|
2021-12-07 22:31:32 +00:00
|
|
|
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
|
2023-10-09 17:58:49 +00:00
|
|
|
logrus.Debugf("checking bootstrap key %s against %s", string(bootstrapKV.Key), tokenKey)
|
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
|
|
|
}
|
|
|
|
|
2023-10-09 17:58:49 +00:00
|
|
|
// migrateTokens will list all keys that has prefix /bootstrap and will check for key that is
|
2021-07-21 18:59:57 +00:00
|
|
|
// 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
|
2023-10-09 17:58:49 +00:00
|
|
|
func migrateTokens(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
|
2023-10-09 17:58:49 +00:00
|
|
|
logrus.Debug("Comparing ", string(bootstrapKV.Key), " to ", oldTokenKey)
|
2021-07-21 17:21:35 +00:00
|
|
|
if string(bootstrapKV.Key) == emptyStringKey {
|
2023-05-01 21:17:30 +00:00
|
|
|
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 {
|
2023-10-09 17:58:49 +00:00
|
|
|
if emptyStringKey != "" {
|
|
|
|
logrus.Warn("bootstrap data encrypted with old token format string, deleting and resaving with token")
|
|
|
|
}
|
2021-07-21 18:59:57 +00:00
|
|
|
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")
|
2023-04-05 00:52:14 +00:00
|
|
|
} else if errors.Is(err, rpctypes.ErrGPRCNotSupportedForLearner) {
|
2021-07-21 18:59:57 +00:00
|
|
|
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
|
|
|
}
|