Commit 2dc4972e authored by qiuqunfeng's avatar qiuqunfeng
Browse files

commit

parent dc1f4c96
...@@ -9,7 +9,7 @@ CREATE TABLE waf_services ( ...@@ -9,7 +9,7 @@ CREATE TABLE waf_services (
mode VARCHAR(50) NOT NULL, mode VARCHAR(50) NOT NULL,
rule_num INTEGER DEFAULT 0, rule_num INTEGER DEFAULT 0,
attack_num INTEGER DEFAULT 0, attack_num INTEGER DEFAULT 0,
rule_category_status JSONB NOT NULL rule_category_status JSON NOT NULL
); );
-- Create waf_rules table -- Create waf_rules table
...@@ -33,7 +33,7 @@ CREATE TABLE waf_rule_categories ( ...@@ -33,7 +33,7 @@ CREATE TABLE waf_rule_categories (
description_en TEXT, description_en TEXT,
description_zh TEXT, description_zh TEXT,
status INTEGER NOT NULL, status INTEGER NOT NULL,
rules JSONB NOT NULL rules JSON NOT NULL
); );
-- Add indexes for better query performance -- Add indexes for better query performance
......
...@@ -18,8 +18,16 @@ func (h *HostList) Scan(src interface{}) error { ...@@ -18,8 +18,16 @@ func (h *HostList) Scan(src interface{}) error {
} }
type RuleCategoryStatus struct { type RuleCategoryStatus struct {
CategoryID string `json:"category_id"` CategoryID []string `json:"category_id"`
Status int `json:"status"` Status int `json:"status"`
}
func (r *RuleCategoryStatus) Scan(src interface{}) error {
return json.Unmarshal(src.([]byte), r)
}
func (r RuleCategoryStatus) Value() (driver.Value, error) {
return json.Marshal(r)
} }
type RuleCategoryStatusList []RuleCategoryStatus type RuleCategoryStatusList []RuleCategoryStatus
...@@ -33,16 +41,16 @@ func (r *RuleCategoryStatusList) Scan(src interface{}) error { ...@@ -33,16 +41,16 @@ func (r *RuleCategoryStatusList) Scan(src interface{}) error {
} }
type WafService struct { type WafService struct {
ID uint `gorm:"column:id;primaryKey;autoIncrement"` ID uint `gorm:"column:id;primaryKey;autoIncrement"`
GatewayName string `gorm:"column:gateway_name"` GatewayName string `gorm:"column:gateway_name"`
Port int `gorm:"column:port"` Port int `gorm:"column:port"`
Namespace string `gorm:"column:namespace"` Namespace string `gorm:"column:namespace"`
RegionCode string `gorm:"column:region_code"` RegionCode string `gorm:"column:region_code"`
Host HostList `gorm:"column:host"` Host HostList `gorm:"column:host"`
Mode string `gorm:"column:mode"` Mode string `gorm:"column:mode"`
RuleNum int `gorm:"column:rule_num"` RuleNum int `gorm:"column:rule_num"`
AttackNum int `gorm:"column:attack_num"` AttackNum int `gorm:"column:attack_num"`
RuleCategoryStatus RuleCategoryStatusList `gorm:"column:rule_category_status;type:json"` RuleCategoryStatus *RuleCategoryStatus `gorm:"column:rule_category_status;type:json"`
} }
func (WafService) TableName() string { func (WafService) TableName() string {
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"slices"
"gitlab.com/tensorsecurity-rd/waf-console/internal/model" "gitlab.com/tensorsecurity-rd/waf-console/internal/model"
"gitlab.com/tensorsecurity-rd/waf-console/pkg/apis/waf.security.io/v1alpha1" "gitlab.com/tensorsecurity-rd/waf-console/pkg/apis/waf.security.io/v1alpha1"
...@@ -33,6 +34,7 @@ func (s *wafService) GetWaf(ctx context.Context, gatewayName string) (*Waf, erro ...@@ -33,6 +34,7 @@ func (s *wafService) GetWaf(ctx context.Context, gatewayName string) (*Waf, erro
} }
func (s *wafService) CreateWaf(ctx context.Context, req *CreateWafReq) (*Waf, error) { func (s *wafService) CreateWaf(ctx context.Context, req *CreateWafReq) (*Waf, error) {
// Create the WAF service resource
service := &v1alpha1.Service{ service := &v1alpha1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: req.GatewayName, Name: req.GatewayName,
...@@ -49,9 +51,52 @@ func (s *wafService) CreateWaf(ctx context.Context, req *CreateWafReq) (*Waf, er ...@@ -49,9 +51,52 @@ func (s *wafService) CreateWaf(ctx context.Context, req *CreateWafReq) (*Waf, er
}, },
}, },
} }
_, err := s.client.WafV1alpha1().Services(req.Namespace).Create(context.Background(), service, metav1.CreateOptions{})
if err != nil { // Get enabled rule categories from DB
return nil, err var ruleCategories []model.WafRuleCategory
if err := s.db.Model(&model.WafRuleCategory{}).Where("status = ?", 1).Find(&ruleCategories).Error; err != nil {
return nil, fmt.Errorf("failed to get rule categories: %v", err)
}
// Get existing WAF service config if any
wafService := &model.WafService{}
err := s.db.Model(&model.WafService{}).Where("gateway_name = ?", req.GatewayName).First(wafService).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, fmt.Errorf("failed to get WAF service: %v", err)
}
// Determine which rule categories to enable
var enabledCategories []model.WafRuleCategory
if len(wafService.RuleCategoryStatus.CategoryID) > 0 {
// Only include categories not already enabled
for _, category := range ruleCategories {
if !slices.Contains(wafService.RuleCategoryStatus.CategoryID, category.CategoryID) {
enabledCategories = append(enabledCategories, category)
}
}
} else {
// Enable all categories if none specified
enabledCategories = ruleCategories
}
// Add rules from enabled categories
for _, category := range enabledCategories {
for _, rule := range category.Rules {
service.Spec.Rules = append(service.Spec.Rules, v1alpha1.Rule{
ID: rule.ID,
Level: rule.Level,
Name: rule.Name,
Type: rule.Type,
Description: rule.Description,
Expr: rule.Expr,
Mode: rule.Mode,
})
}
}
// Create the WAF service in Kubernetes
if _, err := s.client.WafV1alpha1().Services(req.Namespace).Create(ctx, service, metav1.CreateOptions{}); err != nil {
return nil, fmt.Errorf("failed to create WAF service: %v", err)
} }
return nil, nil return 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