From 685c56ae691e7a307f787f5cb3412b7536cc724d Mon Sep 17 00:00:00 2001 From: Leah Leshchinsky Date: Thu, 3 Aug 2023 12:04:00 -0400 Subject: [PATCH] Add missing return statement in the webhook admissions func As the function is currently written without a return, for an unstructured object which is successfully unmashalled, it will fall through to the subsequent lines and fail to decode properly. Add return statement to successful unstructured decode. Signed-off-by: Leah Leshchinsky --- pkg/webhook/admission/decode.go | 1 + pkg/webhook/admission/decode_test.go | 33 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/webhook/admission/decode.go b/pkg/webhook/admission/decode.go index f14f130f7b..7e9c0a96bc 100644 --- a/pkg/webhook/admission/decode.go +++ b/pkg/webhook/admission/decode.go @@ -71,6 +71,7 @@ func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) er return err } unstructuredInto.SetUnstructuredContent(object) + return nil } deserializer := d.codecs.UniversalDeserializer() diff --git a/pkg/webhook/admission/decode_test.go b/pkg/webhook/admission/decode_test.go index 66586da790..4c3578d143 100644 --- a/pkg/webhook/admission/decode_test.go +++ b/pkg/webhook/admission/decode_test.go @@ -123,6 +123,8 @@ var _ = Describe("Admission Webhook Decoder", func() { })) }) + // NOTE: This will only pass if a GVK is provided. An unstructered object without a GVK may succeed + // in decoding to an alternate type. It("should fail to decode if the object in the request doesn't match the passed-in type", func() { By("trying to extract a pod from the quest into a node") Expect(decoder.Decode(req, &corev1.Node{})).NotTo(Succeed()) @@ -152,4 +154,35 @@ var _ = Describe("Admission Webhook Decoder", func() { "namespace": "default", })) }) + + req2 := Request{ + AdmissionRequest: admissionv1.AdmissionRequest{ + Operation: "CREATE", + Object: runtime.RawExtension{ + Raw: []byte(`{ + "metadata": { + "name": "foo", + "namespace": "default" + }, + "spec": { + "containers": [ + { + "image": "bar:v2", + "name": "bar" + } + ] + } + }`), + }, + OldObject: runtime.RawExtension{ + Object: nil, + }, + }, + } + + It("should decode a valid admission request without GVK", func() { + By("extracting the object from the request") + var target3 unstructured.Unstructured + Expect(decoder.DecodeRaw(req2.Object, &target3)).To(Succeed()) + }) })