From c232606253f6e9e7d30f2a9850dbe83e1554a985 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Mon, 28 Oct 2024 16:35:38 +0200 Subject: [PATCH] events: fix for G104 (CWE-703) Reported by gosec. G104 (CWE-703): Errors unhandled. (Confidence: HIGH, Severity: LOW) This commit handles the previously ignored errors. --- cmd/event-logger/app/options.go | 5 +++- pkg/events/events_logger.go | 36 ++++++++++++++++------------ pkg/events/gardener_event_watcher.go | 28 ++++++++++++---------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/cmd/event-logger/app/options.go b/cmd/event-logger/app/options.go index e37b23b2..0cdb43ec 100644 --- a/cmd/event-logger/app/options.go +++ b/cmd/event-logger/app/options.go @@ -182,7 +182,10 @@ func (o *Options) Run(stopCh <-chan struct{}) error { return err } - eventLogger := config.New() + eventLogger, err := config.New() + if err != nil { + return err + } eventLogger.Run(stopCh) diff --git a/pkg/events/events_logger.go b/pkg/events/events_logger.go index b4e0335a..ac6ad761 100644 --- a/pkg/events/events_logger.go +++ b/pkg/events/events_logger.go @@ -17,7 +17,7 @@ import ( ) // NewEventInformerFuncForNamespace returns function which creates new event informer for a given namespace. -func NewEventInformerFuncForNamespace(origin, namespace string) kubeinformersinterfaces.NewInformerFunc { +func NewEventInformerFuncForNamespace(namespace string) kubeinformersinterfaces.NewInformerFunc { return func(clientset kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { watchlist := cache.NewListWatchFromClient( clientset.CoreV1().RESTClient(), @@ -31,24 +31,30 @@ func NewEventInformerFuncForNamespace(origin, namespace string) kubeinformersint resyncPeriod, cache.Indexers{}, ) - informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ - AddFunc: func(obj interface{}) { - if isV1Event(obj) { - v1Event := obj.(*v1.Event) - printV1Event(v1Event, origin) - } - }, - UpdateFunc: func(_ interface{}, newObject interface{}) { - if isV1Event(newObject) { - v1Event := newObject.(*v1.Event) - printV1Event(v1Event, origin) - } - }, - }) + return informer } } +func addEventHandler(informer cache.SharedIndexInformer, origin string) error { + _, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + if isV1Event(obj) { + v1Event := obj.(*v1.Event) + printV1Event(v1Event, origin) + } + }, + UpdateFunc: func(_ interface{}, newObject interface{}) { + if isV1Event(newObject) { + v1Event := newObject.(*v1.Event) + printV1Event(v1Event, origin) + } + }, + }) + + return err +} + func isV1Event(obj interface{}) bool { _, ok := obj.(*v1.Event) return ok diff --git a/pkg/events/gardener_event_watcher.go b/pkg/events/gardener_event_watcher.go index e7e3ea44..d0904171 100644 --- a/pkg/events/gardener_event_watcher.go +++ b/pkg/events/gardener_event_watcher.go @@ -30,31 +30,35 @@ type GardenerEventWatcher struct { } // New returns new GardenerEventWatcherConfig -func (e *GardenerEventWatcherConfig) New() *GardenerEventWatcher { +func (e *GardenerEventWatcherConfig) New() (*GardenerEventWatcher, error) { for indx, namespace := range e.SeedEventWatcherConfig.Namespaces { - _ = e.SeedKubeInformerFactories[indx].InformerFor(&v1.Event{}, - NewEventInformerFuncForNamespace( - "seed", - namespace, - ), + informer := e.SeedKubeInformerFactories[indx].InformerFor( + &v1.Event{}, + NewEventInformerFuncForNamespace(namespace), ) + if err := addEventHandler(informer, "seed"); err != nil { + return nil, err + } } if e.ShootEventWatcherConfig.Kubeconfig != "" { for indx, namespace := range e.ShootEventWatcherConfig.Namespaces { - _ = e.ShootKubeInformerFactories[indx].InformerFor(&v1.Event{}, - NewEventInformerFuncForNamespace( - "shoot", - namespace, - ), + informer := e.ShootKubeInformerFactories[indx].InformerFor( + &v1.Event{}, + NewEventInformerFuncForNamespace(namespace), ) + if err := addEventHandler(informer, "shoot"); err != nil { + return nil, err + } } } - return &GardenerEventWatcher{ + watcher := &GardenerEventWatcher{ SeedKubeInformerFactories: e.SeedKubeInformerFactories, ShootKubeInformerFactories: e.ShootKubeInformerFactories, } + + return watcher, nil } // Run start the GardenerEventWatcher lifecycle