2021-12-07 22:31:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-08 00:36:57 +00:00
|
|
|
"io"
|
2021-12-07 22:31:32 +00:00
|
|
|
"math/big"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cluster"
|
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
|
|
|
"github.com/k3s-io/k3s/pkg/secretsencrypt"
|
2023-08-01 00:51:46 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2021-12-07 22:31:32 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/core"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-08-15 21:58:18 +00:00
|
|
|
"golang.org/x/mod/semver"
|
2021-12-07 22:31:32 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-03-24 19:23:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2021-12-07 22:31:32 +00:00
|
|
|
apiserverconfigv1 "k8s.io/apiserver/pkg/apis/config/v1"
|
|
|
|
"k8s.io/utils/pointer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const aescbcKeySize = 32
|
|
|
|
|
|
|
|
type EncryptionState struct {
|
|
|
|
Stage string `json:"stage"`
|
|
|
|
ActiveKey string `json:"activekey"`
|
|
|
|
Enable *bool `json:"enable,omitempty"`
|
|
|
|
HashMatch bool `json:"hashmatch,omitempty"`
|
|
|
|
HashError string `json:"hasherror,omitempty"`
|
|
|
|
InactiveKeys []string `json:"inactivekeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type EncryptionRequest struct {
|
|
|
|
Stage *string `json:"stage,omitempty"`
|
|
|
|
Enable *bool `json:"enable,omitempty"`
|
|
|
|
Force bool `json:"force"`
|
|
|
|
Skip bool `json:"skip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEncryptionRequest(req *http.Request) (EncryptionRequest, error) {
|
2022-10-08 00:36:57 +00:00
|
|
|
b, err := io.ReadAll(req.Body)
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return EncryptionRequest{}, err
|
|
|
|
}
|
|
|
|
result := EncryptionRequest{}
|
|
|
|
err = json.Unmarshal(b, &result)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionStatusHandler(server *config.Control) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
status, err := encryptionStatus(server)
|
|
|
|
if err != nil {
|
2022-12-10 00:20:51 +00:00
|
|
|
genErrorMessage(resp, http.StatusInternalServerError, err, "secrets-encrypt")
|
2021-12-07 22:31:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(status)
|
|
|
|
if err != nil {
|
2022-12-10 00:20:51 +00:00
|
|
|
genErrorMessage(resp, http.StatusInternalServerError, err, "secrets-encrypt")
|
2021-12-07 22:31:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Write(b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionStatus(server *config.Control) (EncryptionState, error) {
|
|
|
|
state := EncryptionState{}
|
|
|
|
providers, err := secretsencrypt.GetEncryptionProviders(server.Runtime)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return state, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
if providers[1].Identity != nil && providers[0].AESCBC != nil {
|
|
|
|
state.Enable = pointer.Bool(true)
|
|
|
|
} else if providers[0].Identity != nil && providers[1].AESCBC != nil || !server.EncryptSecrets {
|
|
|
|
state.Enable = pointer.Bool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := verifyEncryptionHashAnnotation(server.Runtime, server.Runtime.Core.Core(), ""); err != nil {
|
|
|
|
state.HashMatch = false
|
|
|
|
state.HashError = err.Error()
|
|
|
|
} else {
|
|
|
|
state.HashMatch = true
|
|
|
|
}
|
|
|
|
stage, _, err := getEncryptionHashAnnotation(server.Runtime.Core.Core())
|
|
|
|
if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
state.Stage = stage
|
|
|
|
active := true
|
|
|
|
for _, p := range providers {
|
|
|
|
if p.AESCBC != nil {
|
|
|
|
for _, aesKey := range p.AESCBC.Keys {
|
|
|
|
if active {
|
|
|
|
active = false
|
|
|
|
state.ActiveKey = aesKey.Name
|
|
|
|
} else {
|
|
|
|
state.InactiveKeys = append(state.InactiveKeys, aesKey.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Identity != nil {
|
|
|
|
active = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionEnable(ctx context.Context, server *config.Control, enable bool) error {
|
|
|
|
providers, err := secretsencrypt.GetEncryptionProviders(server.Runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(providers) > 2 {
|
|
|
|
return fmt.Errorf("more than 2 providers (%d) found in secrets encryption", len(providers))
|
|
|
|
}
|
|
|
|
curKeys, err := secretsencrypt.GetEncryptionKeys(server.Runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if providers[1].Identity != nil && providers[0].AESCBC != nil && !enable {
|
|
|
|
logrus.Infoln("Disabling secrets encryption")
|
|
|
|
if err := secretsencrypt.WriteEncryptionConfig(server.Runtime, curKeys, enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if !enable {
|
|
|
|
logrus.Infoln("Secrets encryption already disabled")
|
|
|
|
return nil
|
|
|
|
} else if providers[0].Identity != nil && providers[1].AESCBC != nil && enable {
|
|
|
|
logrus.Infoln("Enabling secrets encryption")
|
|
|
|
if err := secretsencrypt.WriteEncryptionConfig(server.Runtime, curKeys, enable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if enable {
|
|
|
|
logrus.Infoln("Secrets encryption already enabled")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unable to enable/disable secrets encryption, unknown configuration")
|
|
|
|
}
|
2023-08-01 17:20:21 +00:00
|
|
|
if err := cluster.Save(ctx, server, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.EncryptSkip = true
|
|
|
|
return setReencryptAnnotation(server)
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
func encryptionConfigHandler(ctx context.Context, server *config.Control) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.TLS == nil {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.Method != http.MethodPut {
|
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
encryptReq, err := getEncryptionRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
|
|
|
resp.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if encryptReq.Stage != nil {
|
|
|
|
switch *encryptReq.Stage {
|
|
|
|
case secretsencrypt.EncryptionPrepare:
|
|
|
|
err = encryptionPrepare(ctx, server, encryptReq.Force)
|
|
|
|
case secretsencrypt.EncryptionRotate:
|
|
|
|
err = encryptionRotate(ctx, server, encryptReq.Force)
|
2023-08-01 17:20:21 +00:00
|
|
|
case secretsencrypt.EncryptionRotateKeys:
|
|
|
|
err = encryptionRotateKeys(ctx, server)
|
2021-12-07 22:31:32 +00:00
|
|
|
case secretsencrypt.EncryptionReencryptActive:
|
|
|
|
err = encryptionReencrypt(ctx, server, encryptReq.Force, encryptReq.Skip)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown stage %s requested", *encryptReq.Stage)
|
|
|
|
}
|
|
|
|
} else if encryptReq.Enable != nil {
|
|
|
|
err = encryptionEnable(ctx, server, *encryptReq.Enable)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-12-10 00:20:51 +00:00
|
|
|
genErrorMessage(resp, http.StatusBadRequest, err, "secrets-encrypt")
|
2021-12-07 22:31:32 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-15 16:00:52 +00:00
|
|
|
// If a user kills the k3s server immediately after this call, we run into issues where the files
|
|
|
|
// have not yet been written. This sleep ensures that things have time to sync to disk before
|
|
|
|
// the request completes.
|
|
|
|
time.Sleep(1 * time.Second)
|
2021-12-07 22:31:32 +00:00
|
|
|
resp.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionPrepare(ctx context.Context, server *config.Control, force bool) error {
|
|
|
|
states := secretsencrypt.EncryptionStart + "-" + secretsencrypt.EncryptionReencryptFinished
|
|
|
|
if err := verifyEncryptionHashAnnotation(server.Runtime, server.Runtime.Core.Core(), states); err != nil && !force {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
curKeys, err := secretsencrypt.GetEncryptionKeys(server.Runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AppendNewEncryptionKey(&curKeys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infoln("Adding secrets-encryption key: ", curKeys[len(curKeys)-1])
|
|
|
|
|
|
|
|
if err := secretsencrypt.WriteEncryptionConfig(server.Runtime, curKeys, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
node, err := server.Runtime.Core.Core().V1().Node().Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = secretsencrypt.WriteEncryptionHashAnnotation(server.Runtime, node, secretsencrypt.EncryptionPrepare); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-25 17:26:13 +00:00
|
|
|
return cluster.Save(ctx, server, true)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionRotate(ctx context.Context, server *config.Control, force bool) error {
|
|
|
|
if err := verifyEncryptionHashAnnotation(server.Runtime, server.Runtime.Core.Core(), secretsencrypt.EncryptionPrepare); err != nil && !force {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
curKeys, err := secretsencrypt.GetEncryptionKeys(server.Runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right rotate elements
|
|
|
|
rotatedKeys := append(curKeys[len(curKeys)-1:], curKeys[:len(curKeys)-1]...)
|
|
|
|
|
|
|
|
if err = secretsencrypt.WriteEncryptionConfig(server.Runtime, rotatedKeys, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infoln("Encryption keys right rotated")
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
node, err := server.Runtime.Core.Core().V1().Node().Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := secretsencrypt.WriteEncryptionHashAnnotation(server.Runtime, node, secretsencrypt.EncryptionRotate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-25 17:26:13 +00:00
|
|
|
return cluster.Save(ctx, server, true)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encryptionReencrypt(ctx context.Context, server *config.Control, force bool, skip bool) error {
|
|
|
|
if err := verifyEncryptionHashAnnotation(server.Runtime, server.Runtime.Core.Core(), secretsencrypt.EncryptionRotate); err != nil && !force {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.EncryptForce = force
|
|
|
|
server.EncryptSkip = skip
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
node, err := server.Runtime.Core.Core().V1().Node().Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reencryptHash, err := secretsencrypt.GenReencryptHash(server.Runtime, secretsencrypt.EncryptionReencryptRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ann := secretsencrypt.EncryptionReencryptRequest + "-" + reencryptHash
|
|
|
|
node.Annotations[secretsencrypt.EncryptionHashAnnotation] = ann
|
|
|
|
if _, err = server.Runtime.Core.Core().V1().Node().Update(node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("encryption hash annotation set successfully on node: %s\n", node.ObjectMeta.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:20:21 +00:00
|
|
|
func addAndRotateKeys(server *config.Control) error {
|
|
|
|
curKeys, err := secretsencrypt.GetEncryptionKeys(server.Runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := AppendNewEncryptionKey(&curKeys); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infoln("Adding secrets-encryption key: ", curKeys[len(curKeys)-1])
|
|
|
|
|
|
|
|
if err := secretsencrypt.WriteEncryptionConfig(server.Runtime, curKeys, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right rotate elements
|
|
|
|
rotatedKeys := append(curKeys[len(curKeys)-1:], curKeys[:len(curKeys)-1]...)
|
|
|
|
|
|
|
|
return secretsencrypt.WriteEncryptionConfig(server.Runtime, rotatedKeys, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptionRotateKeys is both adds and rotates keys, and sets the annotaiton that triggers the
|
|
|
|
// reencryption process. It is the preferred way to rotate keys, starting with v1.28
|
|
|
|
func encryptionRotateKeys(ctx context.Context, server *config.Control) error {
|
|
|
|
states := secretsencrypt.EncryptionStart + "-" + secretsencrypt.EncryptionReencryptFinished
|
|
|
|
if err := verifyEncryptionHashAnnotation(server.Runtime, server.Runtime.Core.Core(), states); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-15 21:58:18 +00:00
|
|
|
if err := verifyRotateKeysSupport(server.Runtime.Core.Core()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:20:21 +00:00
|
|
|
if err := addAndRotateKeys(server); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return setReencryptAnnotation(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReencryptAnnotation(server *config.Control) error {
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
node, err := server.Runtime.Core.Core().V1().Node().Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reencryptHash, err := secretsencrypt.GenReencryptHash(server.Runtime, secretsencrypt.EncryptionReencryptRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ann := secretsencrypt.EncryptionReencryptRequest + "-" + reencryptHash
|
|
|
|
node.Annotations[secretsencrypt.EncryptionHashAnnotation] = ann
|
|
|
|
if _, err = server.Runtime.Core.Core().V1().Node().Update(node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("encryption hash annotation set successfully on node: %s\n", node.ObjectMeta.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
func AppendNewEncryptionKey(keys *[]apiserverconfigv1.Key) error {
|
|
|
|
aescbcKey := make([]byte, aescbcKeySize)
|
|
|
|
_, err := rand.Read(aescbcKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encodedKey := base64.StdEncoding.EncodeToString(aescbcKey)
|
|
|
|
|
|
|
|
newKey := []apiserverconfigv1.Key{
|
|
|
|
{
|
|
|
|
Name: "aescbckey-" + time.Now().Format(time.RFC3339),
|
|
|
|
Secret: encodedKey,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
*keys = append(*keys, newKey...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEncryptionHashAnnotation(core core.Interface) (string, string, error) {
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
node, err := core.V1().Node().Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2023-08-01 00:51:46 +00:00
|
|
|
if _, ok := node.Labels[util.ControlPlaneRoleLabelKey]; !ok {
|
2022-03-24 19:23:59 +00:00
|
|
|
return "", "", fmt.Errorf("cannot manage secrets encryption on non control-plane node %s", nodeName)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
2022-03-24 19:23:59 +00:00
|
|
|
if ann, ok := node.Annotations[secretsencrypt.EncryptionHashAnnotation]; ok {
|
|
|
|
split := strings.Split(ann, "-")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return "", "", fmt.Errorf("invalid annotation %s found on node %s", ann, nodeName)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
2022-03-24 19:23:59 +00:00
|
|
|
return split[0], split[1], nil
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
2022-03-24 19:23:59 +00:00
|
|
|
return "", "", fmt.Errorf("missing annotation on node %s", nodeName)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 21:58:18 +00:00
|
|
|
// verifyRotateKeysSupport checks that the k3s version is at least v1.28.0 on all control-plane nodes
|
|
|
|
func verifyRotateKeysSupport(core core.Interface) error {
|
|
|
|
labelSelector := labels.Set{util.ControlPlaneRoleLabelKey: "true"}.String()
|
|
|
|
nodes, err := core.V1().Node().List(metav1.ListOptions{LabelSelector: labelSelector})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, node := range nodes.Items {
|
|
|
|
kubver := node.Status.NodeInfo.KubeletVersion
|
|
|
|
if semver.Compare(kubver, "v1.28.0") < 0 {
|
|
|
|
return fmt.Errorf("node %s is running k3s version %s that does not support rotate-keys", node.ObjectMeta.Name, kubver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
// verifyEncryptionHashAnnotation checks that all nodes are on the same stage,
|
|
|
|
// and that a request for new stage is valid
|
|
|
|
func verifyEncryptionHashAnnotation(runtime *config.ControlRuntime, core core.Interface, prevStage string) error {
|
|
|
|
var firstHash string
|
|
|
|
var firstNodeName string
|
|
|
|
first := true
|
2023-08-01 00:51:46 +00:00
|
|
|
labelSelector := labels.Set{util.ControlPlaneRoleLabelKey: "true"}.String()
|
2022-03-24 19:23:59 +00:00
|
|
|
nodes, err := core.V1().Node().List(metav1.ListOptions{LabelSelector: labelSelector})
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-24 19:23:59 +00:00
|
|
|
for _, node := range nodes.Items {
|
2021-12-07 22:31:32 +00:00
|
|
|
hash, ok := node.Annotations[secretsencrypt.EncryptionHashAnnotation]
|
|
|
|
if ok && first {
|
|
|
|
firstHash = hash
|
|
|
|
first = false
|
|
|
|
firstNodeName = node.ObjectMeta.Name
|
|
|
|
} else if ok && hash != firstHash {
|
|
|
|
return fmt.Errorf("hash does not match between %s and %s", firstNodeName, node.ObjectMeta.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if prevStage == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
oldStage, oldHash, err := getEncryptionHashAnnotation(core)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptionConfigHash, err := secretsencrypt.GenEncryptionConfigHash(runtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.Contains(prevStage, oldStage) {
|
2022-03-24 19:23:59 +00:00
|
|
|
return fmt.Errorf("incorrect stage: %s found on node %s", oldStage, nodes.Items[0].ObjectMeta.Name)
|
2021-12-07 22:31:32 +00:00
|
|
|
} else if oldHash != encryptionConfigHash {
|
2022-03-24 19:23:59 +00:00
|
|
|
return fmt.Errorf("invalid hash: %s found on node %s", oldHash, nodes.Items[0].ObjectMeta.Name)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:23:59 +00:00
|
|
|
// genErrorMessage sends and logs a random error ID so that logs can be correlated
|
|
|
|
// between the REST API (which does not provide any detailed error output, to avoid
|
|
|
|
// information disclosure) and the server logs.
|
2022-12-10 00:20:51 +00:00
|
|
|
func genErrorMessage(resp http.ResponseWriter, statusCode int, passedErr error, component string) {
|
2021-12-07 22:31:32 +00:00
|
|
|
errID, err := rand.Int(rand.Reader, big.NewInt(99999))
|
|
|
|
if err != nil {
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
resp.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2022-12-10 00:20:51 +00:00
|
|
|
logrus.Warnf("%s error ID %05d: %s", component, errID, passedErr.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
resp.WriteHeader(statusCode)
|
2022-12-10 00:20:51 +00:00
|
|
|
resp.Write([]byte(fmt.Sprintf("%s error ID %05d", component, errID)))
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|