From dbed12a00ae22e1547a2973a9249b8c37c3c6bae Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Wed, 31 Aug 2022 15:20:01 +0200 Subject: [PATCH] fix: Prevent operator panic on wrong Kamelet binding --- .../kameletbinding/error_handler.go | 24 +++++++++++++------ .../kameletbinding/error_handler_test.go | 15 ++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/controller/kameletbinding/error_handler.go b/pkg/controller/kameletbinding/error_handler.go index f7dc600f49..bc9c470b62 100644 --- a/pkg/controller/kameletbinding/error_handler.go +++ b/pkg/controller/kameletbinding/error_handler.go @@ -46,11 +46,7 @@ func maybeErrorHandler(errHandlConf *v1alpha1.ErrorHandlerSpec, bindingContext b } } - err = setErrorHandlerConfiguration(errorHandlerBinding, errorHandlerSpec) - if err != nil { - return nil, errors.Wrap(err, "could not set integration error handler") - } - + _ = setErrorHandlerConfiguration(errorHandlerBinding, errorHandlerSpec) return errorHandlerBinding, nil } return nil, nil @@ -87,17 +83,31 @@ 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 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 { + _, err := verifyErrorHandler(errorHandler) if err != nil { return err } + 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 b6dab39238..818ca6ed8e 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) +}