2021-12-07 22:31:32 +00:00
|
|
|
package secretsencrypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
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/util"
|
2021-12-07 22:31:32 +00:00
|
|
|
coreclient "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-02-07 21:58:44 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-08-02 21:08:06 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
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
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2022-03-23 20:53:49 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2021-12-07 22:31:32 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2024-02-09 19:37:37 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2021-12-07 22:31:32 +00:00
|
|
|
"k8s.io/client-go/tools/pager"
|
|
|
|
"k8s.io/client-go/tools/record"
|
2022-04-22 23:32:10 +00:00
|
|
|
"k8s.io/client-go/util/retry"
|
2021-12-07 22:31:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
controllerAgentName string = "reencrypt-controller"
|
|
|
|
secretsUpdateStartEvent string = "SecretsUpdateStart"
|
|
|
|
secretsProgressEvent string = "SecretsProgress"
|
|
|
|
secretsUpdateCompleteEvent string = "SecretsUpdateComplete"
|
|
|
|
secretsUpdateErrorEvent string = "SecretsUpdateError"
|
|
|
|
)
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
ctx context.Context
|
|
|
|
controlConfig *config.Control
|
|
|
|
nodes coreclient.NodeController
|
|
|
|
secrets coreclient.SecretController
|
|
|
|
recorder record.EventRecorder
|
|
|
|
}
|
|
|
|
|
|
|
|
func Register(
|
|
|
|
ctx context.Context,
|
2024-02-09 19:37:37 +00:00
|
|
|
controllerName string,
|
2021-12-07 22:31:32 +00:00
|
|
|
controlConfig *config.Control,
|
|
|
|
nodes coreclient.NodeController,
|
|
|
|
secrets coreclient.SecretController,
|
|
|
|
) error {
|
2024-02-09 19:37:37 +00:00
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", controlConfig.Runtime.KubeConfigSupervisor)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k8s, err := kubernetes.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
h := &handler{
|
|
|
|
ctx: ctx,
|
|
|
|
controlConfig: controlConfig,
|
|
|
|
nodes: nodes,
|
|
|
|
secrets: secrets,
|
2022-03-23 17:26:21 +00:00
|
|
|
recorder: util.BuildControllerEventRecorder(k8s, controllerAgentName, metav1.NamespaceDefault),
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 19:37:37 +00:00
|
|
|
nodes.OnChange(ctx, controllerName, h.onChangeNode)
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// onChangeNode handles changes to Nodes. We are looking for a specific annotation change
|
2022-04-22 23:32:10 +00:00
|
|
|
func (h *handler) onChangeNode(nodeName string, node *corev1.Node) (*corev1.Node, error) {
|
2021-12-07 22:31:32 +00:00
|
|
|
if node == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ann, ok := node.Annotations[EncryptionHashAnnotation]
|
|
|
|
if !ok {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 20:53:49 +00:00
|
|
|
// This is consistent with events attached to the node generated by the kubelet
|
|
|
|
// https://github.com/kubernetes/kubernetes/blob/612130dd2f4188db839ea5c2dea07a96b0ad8d1c/pkg/kubelet/kubelet.go#L479-L485
|
|
|
|
nodeRef := &corev1.ObjectReference{
|
|
|
|
Kind: "Node",
|
|
|
|
Name: node.Name,
|
|
|
|
UID: types.UID(node.Name),
|
|
|
|
Namespace: "",
|
|
|
|
}
|
|
|
|
|
2021-12-07 22:31:32 +00:00
|
|
|
if valid, err := h.validateReencryptStage(node, ann); err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
} else if !valid {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reencryptHash, err := GenReencryptHash(h.controlConfig.Runtime, EncryptionReencryptActive)
|
|
|
|
if err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
ann = EncryptionReencryptActive + "-" + reencryptHash
|
2022-04-22 23:32:10 +00:00
|
|
|
|
|
|
|
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
node, err = h.nodes.Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.Annotations[EncryptionHashAnnotation] = ann
|
|
|
|
_, err = h.nodes.Update(node)
|
|
|
|
return err
|
|
|
|
})
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.updateSecrets(node); err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If skipping, revert back to the previous stage
|
|
|
|
if h.controlConfig.EncryptSkip {
|
2022-04-22 23:32:10 +00:00
|
|
|
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
node, err = h.nodes.Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
BootstrapEncryptionHashAnnotation(node, h.controlConfig.Runtime)
|
|
|
|
_, err = h.nodes.Update(node)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return node, err
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove last key
|
2024-02-09 19:37:37 +00:00
|
|
|
curKeys, err := GetEncryptionKeys(h.controlConfig.Runtime, false)
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
2024-02-09 19:37:37 +00:00
|
|
|
logrus.Infoln("Removing key: ", curKeys[len(curKeys)-1])
|
2021-12-07 22:31:32 +00:00
|
|
|
curKeys = curKeys[:len(curKeys)-1]
|
|
|
|
if err = WriteEncryptionConfig(h.controlConfig.Runtime, curKeys, true); err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
2024-02-09 19:37:37 +00:00
|
|
|
|
2022-04-22 23:32:10 +00:00
|
|
|
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
node, err = h.nodes.Get(nodeName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return WriteEncryptionHashAnnotation(h.controlConfig.Runtime, node, EncryptionReencryptFinished)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
2022-02-25 17:26:13 +00:00
|
|
|
if err := cluster.Save(h.ctx, h.controlConfig, true); err != nil {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Event(nodeRef, corev1.EventTypeWarning, secretsUpdateErrorEvent, err.Error())
|
2021-12-07 22:31:32 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateReencryptStage ensures that the request for reencryption is valid and
|
|
|
|
// that there is only one active reencryption at a time
|
|
|
|
func (h *handler) validateReencryptStage(node *corev1.Node, annotation string) (bool, error) {
|
|
|
|
split := strings.Split(annotation, "-")
|
|
|
|
if len(split) != 2 {
|
|
|
|
err := fmt.Errorf("invalid annotation %s found on node %s", annotation, node.ObjectMeta.Name)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
stage := split[0]
|
|
|
|
hash := split[1]
|
|
|
|
|
|
|
|
// Validate the specific stage and the request via sha256 hash
|
|
|
|
if stage != EncryptionReencryptRequest {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if reencryptRequestHash, err := GenReencryptHash(h.controlConfig.Runtime, EncryptionReencryptRequest); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if reencryptRequestHash != hash {
|
|
|
|
err = fmt.Errorf("invalid hash: %s found on node %s", hash, node.ObjectMeta.Name)
|
|
|
|
return false, err
|
|
|
|
}
|
2022-03-24 19:23:59 +00:00
|
|
|
reencryptActiveHash, err := GenReencryptHash(h.controlConfig.Runtime, EncryptionReencryptActive)
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
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 := h.nodes.List(metav1.ListOptions{LabelSelector: labelSelector})
|
2021-12-07 22:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes.Items {
|
|
|
|
if ann, ok := node.Annotations[EncryptionHashAnnotation]; ok {
|
|
|
|
split := strings.Split(ann, "-")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return false, fmt.Errorf("invalid annotation %s found on node %s", ann, node.ObjectMeta.Name)
|
|
|
|
}
|
|
|
|
stage := split[0]
|
|
|
|
hash := split[1]
|
|
|
|
if stage == EncryptionReencryptActive && hash == reencryptActiveHash {
|
|
|
|
return false, fmt.Errorf("another reencrypt is already active")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) updateSecrets(node *corev1.Node) error {
|
2022-03-23 20:53:49 +00:00
|
|
|
nodeRef := &corev1.ObjectReference{
|
|
|
|
Kind: "Node",
|
|
|
|
Name: node.Name,
|
|
|
|
UID: types.UID(node.Name),
|
|
|
|
Namespace: "",
|
|
|
|
}
|
2021-12-07 22:31:32 +00:00
|
|
|
secretPager := pager.New(pager.SimplePageFunc(func(opts metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
return h.secrets.List("", opts)
|
|
|
|
}))
|
2022-08-02 21:08:06 +00:00
|
|
|
secretsList, _, err := secretPager.List(h.ctx, metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-07 22:31:32 +00:00
|
|
|
i := 0
|
2022-08-02 21:08:06 +00:00
|
|
|
err = meta.EachListItem(secretsList, func(obj runtime.Object) error {
|
2021-12-07 22:31:32 +00:00
|
|
|
if secret, ok := obj.(*corev1.Secret); ok {
|
2023-02-07 21:58:44 +00:00
|
|
|
if _, err := h.secrets.Update(secret); err != nil && !apierrors.IsConflict(err) {
|
|
|
|
return fmt.Errorf("failed to update secret: %v", err)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
if i != 0 && i%10 == 0 {
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Eventf(nodeRef, corev1.EventTypeNormal, secretsProgressEvent, "reencrypted %d secrets", i)
|
2021-12-07 22:31:32 +00:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-08-02 21:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-23 20:53:49 +00:00
|
|
|
h.recorder.Eventf(nodeRef, corev1.EventTypeNormal, secretsUpdateCompleteEvent, "completed reencrypt of %d secrets", i)
|
2021-12-07 22:31:32 +00:00
|
|
|
return nil
|
|
|
|
}
|