From 46ba76120fe12127f01344d43a89209e05dca49f Mon Sep 17 00:00:00 2001 From: Prateek Date: Thu, 28 Nov 2024 23:05:01 +0530 Subject: [PATCH] fix CodeQL failure due to incorrect int conversion handling Signed-off-by: Prateek --- KubeArmor/config/config.go | 12 ++++++------ KubeArmor/core/kubeUpdate.go | 16 +++++++++++----- KubeArmor/enforcer/bpflsm/enforcer.go | 4 ++-- KubeArmor/feeder/feeder.go | 4 ++-- KubeArmor/monitor/logUpdate.go | 4 ++-- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/KubeArmor/config/config.go b/KubeArmor/config/config.go index 22ccb3b505..4a71d8700c 100644 --- a/KubeArmor/config/config.go +++ b/KubeArmor/config/config.go @@ -56,10 +56,10 @@ type KubearmorConfig struct { StateAgent bool // enable KubeArmor state agent - AlertThrottling bool // Enable/Disable Alert Throttling - MaxAlertPerSec int // Maximum alerts allowed per second - ThrottleSec int // Number of seconds for which subsequent alerts will be dropped - AnnotateResources bool // enable annotations by kubearmor if kubearmor-controller is not present + AlertThrottling bool // Enable/Disable Alert Throttling + MaxAlertPerSec int32 // Maximum alerts allowed per second + ThrottleSec int32 // Number of seconds for which subsequent alerts will be dropped + AnnotateResources bool // enable annotations by kubearmor if kubearmor-controller is not present ProcFsMount string // path where procfs is hosted } @@ -325,8 +325,8 @@ func LoadConfig() error { GlobalCfg.StateAgent = viper.GetBool(ConfigStateAgent) GlobalCfg.AlertThrottling = viper.GetBool(ConfigAlertThrottling) - GlobalCfg.MaxAlertPerSec = viper.GetInt(ConfigMaxAlertPerSec) - GlobalCfg.ThrottleSec = viper.GetInt(ConfigThrottleSec) + GlobalCfg.MaxAlertPerSec = int32(viper.GetInt(ConfigMaxAlertPerSec)) + GlobalCfg.ThrottleSec = int32(viper.GetInt(ConfigThrottleSec)) GlobalCfg.AnnotateResources = viper.GetBool(ConfigAnnotateResources) GlobalCfg.ProcFsMount = viper.GetString(ConfigProcFsMount) diff --git a/KubeArmor/core/kubeUpdate.go b/KubeArmor/core/kubeUpdate.go index 491ba6cc3a..882b9b5b61 100644 --- a/KubeArmor/core/kubeUpdate.go +++ b/KubeArmor/core/kubeUpdate.go @@ -2740,16 +2740,18 @@ func (dm *KubeArmorDaemon) WatchConfigMap() cache.InformerSynced { cfg.GlobalCfg.AlertThrottling = (cm.Data[cfg.ConfigAlertThrottling] == "true") } if _, ok := cm.Data[cfg.ConfigMaxAlertPerSec]; ok { - cfg.GlobalCfg.MaxAlertPerSec, err = strconv.Atoi(cm.Data[cfg.ConfigMaxAlertPerSec]) + maxAlertPerSec, err := strconv.ParseInt(cm.Data[cfg.ConfigMaxAlertPerSec], 10, 32) if err != nil { dm.Logger.Warnf("Error: %s", err) } + cfg.GlobalCfg.MaxAlertPerSec = int32(maxAlertPerSec) } - if _, ok := cm.Data[cfg.ConfigMaxAlertPerSec]; ok { - cfg.GlobalCfg.ThrottleSec, err = strconv.Atoi(cm.Data[cfg.ConfigThrottleSec]) + if _, ok := cm.Data[cfg.ConfigThrottleSec]; ok { + throttleSec, err := strconv.ParseInt(cm.Data[cfg.ConfigThrottleSec], 10, 32) if err != nil { dm.Logger.Warnf("Error: %s", err) } + cfg.GlobalCfg.ThrottleSec = int32(throttleSec) } dm.SystemMonitor.UpdateThrottlingConfig() @@ -2790,14 +2792,18 @@ func (dm *KubeArmorDaemon) WatchConfigMap() cache.InformerSynced { if _, ok := cm.Data[cfg.ConfigAlertThrottling]; ok { cfg.GlobalCfg.AlertThrottling = (cm.Data[cfg.ConfigAlertThrottling] == "true") } - cfg.GlobalCfg.MaxAlertPerSec, err = strconv.Atoi(cm.Data[cfg.ConfigMaxAlertPerSec]) + + maxAlertPerSec, err := strconv.ParseInt(cm.Data[cfg.ConfigMaxAlertPerSec], 10, 32) if err != nil { dm.Logger.Warnf("Error: %s", err) } - cfg.GlobalCfg.ThrottleSec, err = strconv.Atoi(cm.Data[cfg.ConfigThrottleSec]) + cfg.GlobalCfg.MaxAlertPerSec = int32(maxAlertPerSec) + + throttleSec, err := strconv.ParseInt(cm.Data[cfg.ConfigThrottleSec], 10, 32) if err != nil { dm.Logger.Warnf("Error: %s", err) } + cfg.GlobalCfg.ThrottleSec = int32(throttleSec) dm.SystemMonitor.UpdateThrottlingConfig() } }, diff --git a/KubeArmor/enforcer/bpflsm/enforcer.go b/KubeArmor/enforcer/bpflsm/enforcer.go index 963eb0f3e6..7c3fbc1c40 100644 --- a/KubeArmor/enforcer/bpflsm/enforcer.go +++ b/KubeArmor/enforcer/bpflsm/enforcer.go @@ -375,8 +375,8 @@ func (be *BPFEnforcer) TraceEvents() { case mon.DropAlert: log.Operation = "AlertThreshold" log.Type = "SystemEvent" - log.MaxAlertsPerSec = int32(cfg.GlobalCfg.MaxAlertPerSec) - log.DroppingAlertsInterval = int32(cfg.GlobalCfg.ThrottleSec) + log.MaxAlertsPerSec = cfg.GlobalCfg.MaxAlertPerSec + log.DroppingAlertsInterval = cfg.GlobalCfg.ThrottleSec } // fallback logic if we don't receive source from BuildLogBase() if log.Operation != "Process" && len(log.Source) == 0 { diff --git a/KubeArmor/feeder/feeder.go b/KubeArmor/feeder/feeder.go index 9be7e5ecde..c1c4203c1b 100644 --- a/KubeArmor/feeder/feeder.go +++ b/KubeArmor/feeder/feeder.go @@ -589,8 +589,8 @@ func (fd *Feeder) PushLog(log tp.Log) { } else if alert && !throttle { log.Operation = "AlertThreshold" log.Type = "SystemEvent" - log.MaxAlertsPerSec = int32(cfg.GlobalCfg.MaxAlertPerSec) - log.DroppingAlertsInterval = int32(cfg.GlobalCfg.ThrottleSec) + log.MaxAlertsPerSec = cfg.GlobalCfg.MaxAlertPerSec + log.DroppingAlertsInterval = cfg.GlobalCfg.ThrottleSec } } pbAlert := pb.Alert{} diff --git a/KubeArmor/monitor/logUpdate.go b/KubeArmor/monitor/logUpdate.go index a1d4951fe6..1462249dcb 100644 --- a/KubeArmor/monitor/logUpdate.go +++ b/KubeArmor/monitor/logUpdate.go @@ -517,8 +517,8 @@ func (mon *SystemMonitor) UpdateLogs() { case DropAlert: // throttling alert log.Operation = "AlertThreshold" log.Type = "SystemEvent" - log.MaxAlertsPerSec = int32(cfg.GlobalCfg.MaxAlertPerSec) - log.DroppingAlertsInterval = int32(cfg.GlobalCfg.ThrottleSec) + log.MaxAlertsPerSec = cfg.GlobalCfg.MaxAlertPerSec + log.DroppingAlertsInterval = cfg.GlobalCfg.ThrottleSec default: continue