Commit 0228ee23 authored by qiuqunfeng's avatar qiuqunfeng
Browse files

Add getEnabledRuleNum function to WafService for calculating enabled rule count

This new function retrieves the number of enabled WAF rule categories and adjusts the count based on the WAF service's rule category status. The RuleNum field in the GetWaf method is updated to use this new function, enhancing the accuracy of the WAF service response.
parent 90e9b022
...@@ -61,6 +61,21 @@ func NewWafService(clusterClientManager *utils.ClusterClientManager, db *gorm.DB ...@@ -61,6 +61,21 @@ func NewWafService(clusterClientManager *utils.ClusterClientManager, db *gorm.DB
return &wafService{clusterClientManager: clusterClientManager, db: db, gatewayUrl: gatewayUrl, elasticClient: elasticClient} return &wafService{clusterClientManager: clusterClientManager, db: db, gatewayUrl: gatewayUrl, elasticClient: elasticClient}
} }
func getEnabledRuleNum(db *gorm.DB, wafService *model.WafService) int {
ruleNum := 0
var ruleCategoryNum int64
if err := db.Model(&model.WafRuleCategory{}).Where("status = ?", 0).Count(&ruleCategoryNum).Error; err != nil {
log.Error().Msgf("failed to get rule categories: %v", err)
return 0
}
if wafService.RuleCategoryStatus != nil && len(wafService.RuleCategoryStatus.CategoryID) == 1 {
ruleNum = len(wafService.RuleCategoryStatus.CategoryID)
}
ruleCategoryNum = ruleCategoryNum - int64(ruleNum)
return int(ruleCategoryNum)
}
func (s *wafService) GetWaf(ctx context.Context, regionCode, namespace, gatewayName string) (*WafService, error) { func (s *wafService) GetWaf(ctx context.Context, regionCode, namespace, gatewayName string) (*WafService, error) {
wafService := &model.WafService{} wafService := &model.WafService{}
err := s.db.Model(&model.WafService{}).Where("gateway_name = ? AND region_code = ? AND namespace = ?", gatewayName, regionCode, namespace).First(wafService).Error err := s.db.Model(&model.WafService{}).Where("gateway_name = ? AND region_code = ? AND namespace = ?", gatewayName, regionCode, namespace).First(wafService).Error
...@@ -97,7 +112,7 @@ func (s *wafService) GetWaf(ctx context.Context, regionCode, namespace, gatewayN ...@@ -97,7 +112,7 @@ func (s *wafService) GetWaf(ctx context.Context, regionCode, namespace, gatewayN
return &WafService{ return &WafService{
GatewayName: wafService.GatewayName, GatewayName: wafService.GatewayName,
Mode: wafService.Mode, Mode: wafService.Mode,
RuleNum: wafService.RuleNum, RuleNum: getEnabledRuleNum(s.db, wafService),
AttackNum: wafService.AttackNum, AttackNum: wafService.AttackNum,
Listeners: listeners, Listeners: listeners,
}, nil }, nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment