From 9597f081b0d3daccf94e9de1fb3a0ef4ad6c3876 Mon Sep 17 00:00:00 2001 From: Brandon Palm Date: Wed, 28 Oct 2026 12:37:24 -0500 Subject: [PATCH] olm: guard empty webhooks slice in getWebhookCABundle getWebhookCABundle indexes existingWebhooks.Items[0].Webhooks[0] after checking only that Items is non-empty. A webhook configuration that matches the OLM owner and generate-name labels but carries no webhooks (hand-edited, partially applied, or left behind by an older OLM version) panics the OLM operator on the CSV reconcile path, turning a single stale object into a crashloop. Require a non-empty Webhooks slice before indexing so the function falls through to its existing "unable to find CA" error, matching the behavior for a missing configuration. Signed-off-by: Brandon Palm --- pkg/controller/operators/olm/apiservices.go | 4 +- pkg/controller/operators/olm/operator_test.go | 111 ++++++++++++++++++ 2 files changed, 113 insertions(+), 2 deletions(-) diff --git a/pkg/controller/operators/olm/apiservices.go b/pkg/controller/operators/olm/apiservices.go index f0deae1706..3486dd8b1a 100644 --- a/pkg/controller/operators/olm/apiservices.go +++ b/pkg/controller/operators/olm/apiservices.go @@ -401,7 +401,7 @@ func (a *Operator) getWebhookCABundle(csv *v1alpha1.ClusterServiceVersion, desc return nil, fmt.Errorf("could not retrieve generated MutatingWebhookConfiguration: %v", err) } - if len(existingWebhooks.Items) > 0 { + if len(existingWebhooks.Items) > 0 && len(existingWebhooks.Items[0].Webhooks) > 0 { return existingWebhooks.Items[0].Webhooks[0].ClientConfig.CABundle, nil } case v1alpha1.ValidatingAdmissionWebhook: @@ -410,7 +410,7 @@ func (a *Operator) getWebhookCABundle(csv *v1alpha1.ClusterServiceVersion, desc return nil, fmt.Errorf("could not retrieve generated ValidatingWebhookConfiguration: %v", err) } - if len(existingWebhooks.Items) > 0 { + if len(existingWebhooks.Items) > 0 && len(existingWebhooks.Items[0].Webhooks) > 0 { return existingWebhooks.Items[0].Webhooks[0].ClientConfig.CABundle, nil } case v1alpha1.ConversionWebhook: diff --git a/pkg/controller/operators/olm/operator_test.go b/pkg/controller/operators/olm/operator_test.go index 595f1cf3ca..e45316da66 100644 --- a/pkg/controller/operators/olm/operator_test.go +++ b/pkg/controller/operators/olm/operator_test.go @@ -3800,6 +3800,117 @@ func TestWebhookCABundleRetrieval(t *testing.T) { err: nil, }, }, + { + name: "MissingMutatingCAResource", + initial: initial{ + csvs: []*v1alpha1.ClusterServiceVersion{ + csv("csv1", + namespace, + "0.0.0", + "", + installStrategy("csv1-dep1", + nil, + []v1alpha1.StrategyDeploymentPermissions{}, + ), + []*apiextensionsv1.CustomResourceDefinition{crd("c1", "v1", "g1")}, + []*apiextensionsv1.CustomResourceDefinition{}, + v1alpha1.CSVPhaseInstalling, + ), + }, + desc: v1alpha1.WebhookDescription{ + GenerateName: "webhook", + Type: v1alpha1.MutatingAdmissionWebhook, + }, + }, + expected: expected{ + caBundle: nil, + err: missingCAError, + }, + }, + { + name: "FailFromValidatingAdmissionWebhookWithNoWebhooks", + initial: initial{ + csvs: []*v1alpha1.ClusterServiceVersion{ + csvWithValidatingAdmissionWebhook(csv("csv1", + namespace, + "0.0.0", + "", + installStrategy("csv1-dep1", + nil, + []v1alpha1.StrategyDeploymentPermissions{}, + ), + []*apiextensionsv1.CustomResourceDefinition{crd("c1", "v1", "g1")}, + []*apiextensionsv1.CustomResourceDefinition{}, + v1alpha1.CSVPhaseInstalling, + ), "csv1-dep1", []string{"c1.g1"}), + }, + objs: []runtime.Object{ + &admissionregistrationv1.ValidatingWebhookConfiguration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "webhook", + Namespace: namespace, + Labels: map[string]string{ + "olm.owner": "csv1", + "olm.owner.namespace": namespace, + "olm.owner.kind": v1alpha1.ClusterServiceVersionKind, + "olm.webhook-description-generate-name": "webhook", + }, + }, + Webhooks: []admissionregistrationv1.ValidatingWebhook{}, + }, + }, + desc: v1alpha1.WebhookDescription{ + GenerateName: "webhook", + Type: v1alpha1.ValidatingAdmissionWebhook, + }, + }, + expected: expected{ + caBundle: nil, + err: missingCAError, + }, + }, + { + name: "FailFromMutatingAdmissionWebhookWithNoWebhooks", + initial: initial{ + csvs: []*v1alpha1.ClusterServiceVersion{ + csvWithMutatingAdmissionWebhook(csv("csv1", + namespace, + "0.0.0", + "", + installStrategy("csv1-dep1", + nil, + []v1alpha1.StrategyDeploymentPermissions{}, + ), + []*apiextensionsv1.CustomResourceDefinition{crd("c1", "v1", "g1")}, + []*apiextensionsv1.CustomResourceDefinition{}, + v1alpha1.CSVPhaseInstalling, + ), "csv1-dep1", []string{"c1.g1"}), + }, + objs: []runtime.Object{ + &admissionregistrationv1.MutatingWebhookConfiguration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "webhook", + Namespace: namespace, + Labels: map[string]string{ + "olm.owner": "csv1", + "olm.owner.namespace": namespace, + "olm.owner.kind": v1alpha1.ClusterServiceVersionKind, + "olm.webhook-description-generate-name": "webhook", + }, + }, + Webhooks: []admissionregistrationv1.MutatingWebhook{}, + }, + }, + desc: v1alpha1.WebhookDescription{ + GenerateName: "webhook", + Type: v1alpha1.MutatingAdmissionWebhook, + }, + }, + expected: expected{ + caBundle: nil, + err: missingCAError, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {