|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package tenantsecret |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "sort" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/labels" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 17 | + "k8s.io/apimachinery/pkg/watch" |
| 18 | + "k8s.io/apiserver/pkg/endpoints/request" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 21 | + |
| 22 | + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" |
| 23 | +) |
| 24 | + |
| 25 | +const testNamespace = "tenant-root" |
| 26 | + |
| 27 | +func newTestREST(t *testing.T, secrets ...*corev1.Secret) *REST { |
| 28 | + t.Helper() |
| 29 | + |
| 30 | + scheme := runtime.NewScheme() |
| 31 | + if err := corev1.AddToScheme(scheme); err != nil { |
| 32 | + t.Fatalf("add corev1 to scheme: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + objs := make([]client.Object, 0, len(secrets)) |
| 36 | + for _, s := range secrets { |
| 37 | + objs = append(objs, s) |
| 38 | + } |
| 39 | + fc := fake.NewClientBuilder(). |
| 40 | + WithScheme(scheme). |
| 41 | + WithObjects(objs...). |
| 42 | + Build() |
| 43 | + |
| 44 | + return &REST{ |
| 45 | + c: fc, |
| 46 | + w: fc, |
| 47 | + gvr: schema.GroupVersionResource{ |
| 48 | + Group: corev1alpha1.GroupName, |
| 49 | + Version: "v1alpha1", |
| 50 | + Resource: "tenantsecrets", |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// makeTenantSecret produces a Secret already labeled as a tenant resource plus any extra labels. |
| 56 | +func makeTenantSecret(name string, extra map[string]string) *corev1.Secret { |
| 57 | + lbls := map[string]string{ |
| 58 | + corev1alpha1.TenantResourceLabelKey: corev1alpha1.TenantResourceLabelValue, |
| 59 | + } |
| 60 | + for k, v := range extra { |
| 61 | + lbls[k] = v |
| 62 | + } |
| 63 | + return &corev1.Secret{ |
| 64 | + ObjectMeta: metav1.ObjectMeta{ |
| 65 | + Name: name, |
| 66 | + Namespace: testNamespace, |
| 67 | + Labels: lbls, |
| 68 | + }, |
| 69 | + Type: corev1.SecretTypeOpaque, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func itemNames(items []corev1alpha1.TenantSecret) []string { |
| 74 | + out := make([]string, len(items)) |
| 75 | + for i, it := range items { |
| 76 | + out[i] = it.Name |
| 77 | + } |
| 78 | + sort.Strings(out) |
| 79 | + return out |
| 80 | +} |
| 81 | + |
| 82 | +func listTenantSecrets(t *testing.T, r *REST, opts *metainternal.ListOptions) *corev1alpha1.TenantSecretList { |
| 83 | + t.Helper() |
| 84 | + ctx := request.WithNamespace(context.Background(), testNamespace) |
| 85 | + out, err := r.List(ctx, opts) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("List returned error: %v", err) |
| 88 | + } |
| 89 | + list, ok := out.(*corev1alpha1.TenantSecretList) |
| 90 | + if !ok { |
| 91 | + t.Fatalf("expected *TenantSecretList, got %T", out) |
| 92 | + } |
| 93 | + return list |
| 94 | +} |
| 95 | + |
| 96 | +func TestList_NoSelector_ReturnsOnlyTenantSecrets(t *testing.T) { |
| 97 | + bucket := makeTenantSecret("bucket-creds", map[string]string{ |
| 98 | + "apps.cozystack.io/application.kind": "Bucket", |
| 99 | + "apps.cozystack.io/application.name": "test", |
| 100 | + }) |
| 101 | + monitoring := makeTenantSecret("monitoring-creds", map[string]string{ |
| 102 | + "apps.cozystack.io/application.kind": "Monitoring", |
| 103 | + "apps.cozystack.io/application.name": "monitoring", |
| 104 | + }) |
| 105 | + // Plain Secret without the tenant marker — must be excluded from the list. |
| 106 | + plain := &corev1.Secret{ |
| 107 | + ObjectMeta: metav1.ObjectMeta{ |
| 108 | + Name: "plain", |
| 109 | + Namespace: testNamespace, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + r := newTestREST(t, bucket, monitoring, plain) |
| 114 | + |
| 115 | + list := listTenantSecrets(t, r, &metainternal.ListOptions{}) |
| 116 | + |
| 117 | + got := itemNames(list.Items) |
| 118 | + want := []string{"bucket-creds", "monitoring-creds"} |
| 119 | + if !equalStrings(got, want) { |
| 120 | + t.Fatalf("unexpected items: got %v, want %v", got, want) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestList_WithLabelSelector_FiltersToMatchingApp(t *testing.T) { |
| 125 | + bucket := makeTenantSecret("bucket-creds", map[string]string{ |
| 126 | + "apps.cozystack.io/application.kind": "Bucket", |
| 127 | + "apps.cozystack.io/application.name": "test", |
| 128 | + }) |
| 129 | + monitoring := makeTenantSecret("monitoring-creds", map[string]string{ |
| 130 | + "apps.cozystack.io/application.kind": "Monitoring", |
| 131 | + "apps.cozystack.io/application.name": "monitoring", |
| 132 | + }) |
| 133 | + other := makeTenantSecret("other-bucket", map[string]string{ |
| 134 | + "apps.cozystack.io/application.kind": "Bucket", |
| 135 | + "apps.cozystack.io/application.name": "other", |
| 136 | + }) |
| 137 | + |
| 138 | + r := newTestREST(t, bucket, monitoring, other) |
| 139 | + |
| 140 | + sel, err := labels.Parse( |
| 141 | + "apps.cozystack.io/application.kind=Bucket,apps.cozystack.io/application.name=test", |
| 142 | + ) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("parse selector: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + list := listTenantSecrets(t, r, &metainternal.ListOptions{LabelSelector: sel}) |
| 148 | + |
| 149 | + got := itemNames(list.Items) |
| 150 | + want := []string{"bucket-creds"} |
| 151 | + if !equalStrings(got, want) { |
| 152 | + t.Fatalf("expected only bucket-creds, got %v", got) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestList_WithLabelSelector_NoMatch_ReturnsEmpty(t *testing.T) { |
| 157 | + r := newTestREST(t, makeTenantSecret("bucket-creds", map[string]string{ |
| 158 | + "apps.cozystack.io/application.kind": "Bucket", |
| 159 | + "apps.cozystack.io/application.name": "monitoring", |
| 160 | + })) |
| 161 | + |
| 162 | + sel, err := labels.Parse("apps.cozystack.io/application.name=does-not-exist") |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("parse selector: %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + list := listTenantSecrets(t, r, &metainternal.ListOptions{LabelSelector: sel}) |
| 168 | + |
| 169 | + if len(list.Items) != 0 { |
| 170 | + t.Fatalf("expected empty list, got %v", itemNames(list.Items)) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestList_WithLabelSelector_PreservesTenantFilter(t *testing.T) { |
| 175 | + // A non-tenant Secret carrying the same user labels must NOT leak through |
| 176 | + // the label-selector filter. |
| 177 | + leaked := &corev1.Secret{ |
| 178 | + ObjectMeta: metav1.ObjectMeta{ |
| 179 | + Name: "leaked", |
| 180 | + Namespace: testNamespace, |
| 181 | + Labels: map[string]string{ |
| 182 | + "apps.cozystack.io/application.kind": "Bucket", |
| 183 | + "apps.cozystack.io/application.name": "test", |
| 184 | + }, |
| 185 | + }, |
| 186 | + } |
| 187 | + bucket := makeTenantSecret("bucket-creds", map[string]string{ |
| 188 | + "apps.cozystack.io/application.kind": "Bucket", |
| 189 | + "apps.cozystack.io/application.name": "test", |
| 190 | + }) |
| 191 | + |
| 192 | + r := newTestREST(t, leaked, bucket) |
| 193 | + |
| 194 | + sel, _ := labels.Parse( |
| 195 | + "apps.cozystack.io/application.kind=Bucket,apps.cozystack.io/application.name=test", |
| 196 | + ) |
| 197 | + |
| 198 | + list := listTenantSecrets(t, r, &metainternal.ListOptions{LabelSelector: sel}) |
| 199 | + |
| 200 | + got := itemNames(list.Items) |
| 201 | + want := []string{"bucket-creds"} |
| 202 | + if !equalStrings(got, want) { |
| 203 | + t.Fatalf("non-tenant Secret leaked through filter: got %v, want %v", got, want) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func TestList_WithEverythingSelector_BehavesLikeNoSelector(t *testing.T) { |
| 208 | + bucket := makeTenantSecret("bucket-creds", map[string]string{ |
| 209 | + "apps.cozystack.io/application.name": "test", |
| 210 | + }) |
| 211 | + monitoring := makeTenantSecret("monitoring-creds", map[string]string{ |
| 212 | + "apps.cozystack.io/application.name": "monitoring", |
| 213 | + }) |
| 214 | + |
| 215 | + r := newTestREST(t, bucket, monitoring) |
| 216 | + |
| 217 | + list := listTenantSecrets(t, r, &metainternal.ListOptions{LabelSelector: labels.Everything()}) |
| 218 | + |
| 219 | + got := itemNames(list.Items) |
| 220 | + want := []string{"bucket-creds", "monitoring-creds"} |
| 221 | + if !equalStrings(got, want) { |
| 222 | + t.Fatalf("expected all tenant secrets, got %v", got) |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +// TestWatch_WithLabelSelector_FiltersEvents reproduces issue |
| 227 | +// cozystack/cozystack#2527: TenantSecret Watch ignored opts.LabelSelector |
| 228 | +// and streamed every tenant Secret in the namespace, regardless of the |
| 229 | +// user-provided selector. |
| 230 | +// |
| 231 | +// fake.Client.Watch does not emit initial ADDED events for objects already in |
| 232 | +// the tracker — only events for subsequent CREATE/UPDATE/DELETE. So we start |
| 233 | +// the watch first and then create two secrets; only the one matching the |
| 234 | +// selector should be observed. |
| 235 | +func TestWatch_WithLabelSelector_FiltersEvents(t *testing.T) { |
| 236 | + r := newTestREST(t) |
| 237 | + |
| 238 | + sel, err := labels.Parse( |
| 239 | + "apps.cozystack.io/application.kind=Harbor,apps.cozystack.io/application.name=test", |
| 240 | + ) |
| 241 | + if err != nil { |
| 242 | + t.Fatalf("parse selector: %v", err) |
| 243 | + } |
| 244 | + |
| 245 | + ctx, cancel := context.WithCancel(request.WithNamespace(context.Background(), testNamespace)) |
| 246 | + defer cancel() |
| 247 | + |
| 248 | + w, err := r.Watch(ctx, &metainternal.ListOptions{LabelSelector: sel}) |
| 249 | + if err != nil { |
| 250 | + t.Fatalf("Watch returned error: %v", err) |
| 251 | + } |
| 252 | + defer w.Stop() |
| 253 | + |
| 254 | + matching := makeTenantSecret("harbor-test-credentials", map[string]string{ |
| 255 | + "apps.cozystack.io/application.kind": "Harbor", |
| 256 | + "apps.cozystack.io/application.name": "test", |
| 257 | + }) |
| 258 | + other := makeTenantSecret("postgres-new-credentials", map[string]string{ |
| 259 | + "apps.cozystack.io/application.kind": "Postgres", |
| 260 | + "apps.cozystack.io/application.name": "new", |
| 261 | + }) |
| 262 | + if err := r.c.Create(ctx, matching); err != nil { |
| 263 | + t.Fatalf("create matching secret: %v", err) |
| 264 | + } |
| 265 | + if err := r.c.Create(ctx, other); err != nil { |
| 266 | + t.Fatalf("create other secret: %v", err) |
| 267 | + } |
| 268 | + |
| 269 | + got := collectAddedNames(t, w, 500*time.Millisecond) |
| 270 | + want := []string{"harbor-test-credentials"} |
| 271 | + |
| 272 | + if !equalStrings(got, want) { |
| 273 | + t.Fatalf("Watch ignored labelSelector: got %v, want %v", got, want) |
| 274 | + } |
| 275 | +} |
| 276 | + |
| 277 | +// collectAddedNames drains ADDED events from a watch until the timeout fires, |
| 278 | +// returning the sorted list of object names. Bookmarks are ignored. Used to |
| 279 | +// assert that nothing extra leaks past a label-selector filter. |
| 280 | +func collectAddedNames(t *testing.T, w watch.Interface, timeout time.Duration) []string { |
| 281 | + t.Helper() |
| 282 | + names := make([]string, 0) |
| 283 | + deadline := time.NewTimer(timeout) |
| 284 | + defer deadline.Stop() |
| 285 | + |
| 286 | + for { |
| 287 | + select { |
| 288 | + case ev, ok := <-w.ResultChan(): |
| 289 | + if !ok { |
| 290 | + sort.Strings(names) |
| 291 | + return names |
| 292 | + } |
| 293 | + if ev.Type != watch.Added { |
| 294 | + continue |
| 295 | + } |
| 296 | + ts, ok := ev.Object.(*corev1alpha1.TenantSecret) |
| 297 | + if !ok { |
| 298 | + t.Fatalf("expected *TenantSecret in event, got %T", ev.Object) |
| 299 | + } |
| 300 | + names = append(names, ts.Name) |
| 301 | + case <-deadline.C: |
| 302 | + sort.Strings(names) |
| 303 | + return names |
| 304 | + } |
| 305 | + } |
| 306 | +} |
| 307 | + |
| 308 | +func equalStrings(a, b []string) bool { |
| 309 | + if len(a) != len(b) { |
| 310 | + return false |
| 311 | + } |
| 312 | + for i := range a { |
| 313 | + if a[i] != b[i] { |
| 314 | + return false |
| 315 | + } |
| 316 | + } |
| 317 | + return true |
| 318 | +} |
0 commit comments