Skip to content

Commit 54ad519

Browse files
Herbaerttuunit
authored andcommitted
fix: strip managed fields from cached Secrets as well
1 parent 2646281 commit 54ad519

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎cmd/manager/main.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,23 @@ func init() {
6363
// controllers watch Secrets — credentials, bootstrap data and the bastion
6464
// cloud-init — and a watch only needs an object's identity to enqueue a
6565
// reconcile, never its contents, so caching credential bytes in memory buys
66-
// nothing and exposes them to anything that can read the process.
66+
// nothing and exposes them to anything that can read the process. Managed
67+
// fields go with them: nothing here reads them, and they are usually the
68+
// largest part of what is left once the data is gone.
6769
//
6870
// Deliberately no label selector on the entry, unlike Cluster API's own
69-
// equivalent in internal/setup: the Secrets this provider watches carry no
70-
// common label, so restricting the cache by one would silently stop the
71-
// watches from firing rather than only hardening them.
71+
// equivalent in internal/setup. Cluster API only watches Secrets it stamps
72+
// itself, whereas the credentials and bastion cloud-init Secrets here are
73+
// created by the user and carry no labels, so a selector would silently stop
74+
// those watches from firing rather than only hardening the cache.
7275
func managerCacheOptions() cache.Options {
7376
return cache.Options{
7477
ByObject: map[client.Object]cache.ByObject{
7578
&corev1.Secret{}: {
7679
Transform: func(in any) (any, error) {
7780
if secret, ok := in.(*corev1.Secret); ok {
7881
secret.Data = nil
82+
secret.SetManagedFields(nil)
7983
}
8084
return in, nil
8185
},

‎cmd/manager/main_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
corev1 "k8s.io/api/core/v1"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718
"sigs.k8s.io/controller-runtime/pkg/cache"
1819
)
1920

@@ -40,6 +41,9 @@ func TestManagerCacheOptionsStripsSecretData(t *testing.T) {
4041
}
4142

4243
out, err := transform(&corev1.Secret{
44+
ObjectMeta: metav1.ObjectMeta{
45+
ManagedFields: []metav1.ManagedFieldsEntry{{Manager: "kubectl", Operation: metav1.ManagedFieldsOperationUpdate}},
46+
},
4347
Data: map[string][]byte{"credentials": []byte("service-account-key")},
4448
})
4549
if err != nil {
@@ -52,6 +56,9 @@ func TestManagerCacheOptionsStripsSecretData(t *testing.T) {
5256
if secret.Data != nil {
5357
t.Errorf("Transform left Secret data in the cache: %v", secret.Data)
5458
}
59+
if secret.ManagedFields != nil {
60+
t.Errorf("Transform left managed fields in the cache: %v", secret.ManagedFields)
61+
}
5562
}
5663

5764
// The Transform runs on everything committed to the Secret informer, including

0 commit comments

Comments
 (0)