This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch 3586/avoid-panic-on-wrong-kamelet-binding
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 12795356d713a5f3e4b47e94781c8d009bb77ecd
Author: Nicolas Filotto <nfilo...@talend.com>
AuthorDate: Wed Aug 31 15:20:01 2022 +0200

    fix: Prevent panic on wrong Kamelet binding
---
 pkg/controller/kameletbinding/error_handler.go      | 16 +++++++++++++---
 pkg/controller/kameletbinding/error_handler_test.go | 15 +++++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/pkg/controller/kameletbinding/error_handler.go 
b/pkg/controller/kameletbinding/error_handler.go
index f7dc600f4..643e9d843 100644
--- a/pkg/controller/kameletbinding/error_handler.go
+++ b/pkg/controller/kameletbinding/error_handler.go
@@ -87,17 +87,27 @@ func parseErrorHandler(rawMessage v1alpha1.RawMessage) 
(v1alpha1.ErrorHandler, e
                        return nil, err
                }
 
-               return dst, nil
+               return verifyErrorHandler(dst)
        }
 
        return nil, errors.New("You must provide any supported error handler")
 }
 
-func setErrorHandlerConfiguration(errorHandlerBinding *bindings.Binding, 
errorHandler v1alpha1.ErrorHandler) error {
+func verifyErrorHandler(errorHandler v1alpha1.ErrorHandler) 
(v1alpha1.ErrorHandler, error) {
        properties, err := errorHandler.Configuration()
        if err != nil {
-               return err
+               return nil, err
+       } else if properties == nil {
+               return nil, errors.New("The properties of the error handler 
could not be found")
+       }
+       if errorHandler.Type() == v1alpha1.ErrorHandlerTypeSink && 
errorHandler.Endpoint() == nil {
+               return nil, errors.New("The endpoint of the error handler could 
not be found")
        }
+       return errorHandler, nil
+}
+
+func setErrorHandlerConfiguration(errorHandlerBinding *bindings.Binding, 
errorHandler v1alpha1.ErrorHandler) error {
+       properties, _ := errorHandler.Configuration()
        // initialize map if not yet initialized
        if errorHandlerBinding.ApplicationProperties == nil {
                errorHandlerBinding.ApplicationProperties = 
make(map[string]string)
diff --git a/pkg/controller/kameletbinding/error_handler_test.go 
b/pkg/controller/kameletbinding/error_handler_test.go
index b6dab3923..818ca6ed8 100644
--- a/pkg/controller/kameletbinding/error_handler_test.go
+++ b/pkg/controller/kameletbinding/error_handler_test.go
@@ -101,3 +101,18 @@ func TestParseErrorHandlerSinkWithParametersDoesSucceed(t 
*testing.T) {
        assert.Equal(t, "value1", 
parameters["camel.beans.defaultErrorHandler.param1"])
        assert.Equal(t, "value2", 
parameters["camel.beans.defaultErrorHandler.param2"])
 }
+
+func TestParseErrorHandlerWithWrongDefinition(t *testing.T) {
+       _, err := parseErrorHandler(
+               []byte(`{
+                       "sink": {
+                               "ref": {
+                                       "kind": "Kamelet",
+                                       "apiVersion": 
"camel.apache.org/v1alpha1",
+                                       "name": "err-sink"
+                               }
+                       }
+               }`),
+       )
+       assert.NotNil(t, err)
+}

Reply via email to