From 5691acfeece097780f5c355d716d5f20a8d162e9 Mon Sep 17 00:00:00 2001 From: Levi Blackstone Date: Wed, 6 Mar 2019 16:21:33 -0700 Subject: [PATCH] Check for required name parameter in dynamic client The Create, Delete, Get, Patch, Update and UpdateStatus methods in the dynamic client all expect the name parameter to be non-empty, but did not validate this requirement, which could lead to a panic. Add explicit checks to these methods. Kubernetes-commit: a9cba032dedbed9d04828c917a79a8371305d058 --- dynamic/simple.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/dynamic/simple.go b/dynamic/simple.go index 65c96dcc..4e0ef5a7 100644 --- a/dynamic/simple.go +++ b/dynamic/simple.go @@ -17,6 +17,7 @@ limitations under the License. package dynamic import ( + "fmt" "io" "k8s.io/apimachinery/pkg/api/meta" @@ -102,6 +103,9 @@ func (c *dynamicResourceClient) Create(obj *unstructured.Unstructured, opts meta return nil, err } name = accessor.GetName() + if len(name) == 0 { + return nil, fmt.Errorf("name is required") + } } result := c.client.client. @@ -130,6 +134,10 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta if err != nil { return nil, err } + name := accessor.GetName() + if len(name) == 0 { + return nil, fmt.Errorf("name is required") + } outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj) if err != nil { return nil, err @@ -137,7 +145,7 @@ func (c *dynamicResourceClient) Update(obj *unstructured.Unstructured, opts meta result := c.client.client. Put(). - AbsPath(append(c.makeURLSegments(accessor.GetName()), subresources...)...). + AbsPath(append(c.makeURLSegments(name), subresources...)...). Body(outBytes). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). Do() @@ -161,6 +169,10 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt if err != nil { return nil, err } + name := accessor.GetName() + if len(name) == 0 { + return nil, fmt.Errorf("name is required") + } outBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj) if err != nil { @@ -169,7 +181,7 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt result := c.client.client. Put(). - AbsPath(append(c.makeURLSegments(accessor.GetName()), "status")...). + AbsPath(append(c.makeURLSegments(name), "status")...). Body(outBytes). SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1). Do() @@ -189,6 +201,9 @@ func (c *dynamicResourceClient) UpdateStatus(obj *unstructured.Unstructured, opt } func (c *dynamicResourceClient) Delete(name string, opts *metav1.DeleteOptions, subresources ...string) error { + if len(name) == 0 { + return fmt.Errorf("name is required") + } if opts == nil { opts = &metav1.DeleteOptions{} } @@ -224,6 +239,9 @@ func (c *dynamicResourceClient) DeleteCollection(opts *metav1.DeleteOptions, lis } func (c *dynamicResourceClient) Get(name string, opts metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) { + if len(name) == 0 { + return nil, fmt.Errorf("name is required") + } result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do() if err := result.Error(); err != nil { return nil, err @@ -292,6 +310,9 @@ func (c *dynamicResourceClient) Watch(opts metav1.ListOptions) (watch.Interface, } func (c *dynamicResourceClient) Patch(name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) { + if len(name) == 0 { + return nil, fmt.Errorf("name is required") + } result := c.client.client. Patch(pt). AbsPath(append(c.makeURLSegments(name), subresources...)...).