Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion products/safeline/cmd/policyrule/policyrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func newCreateCmd() *cobra.Command {
enabled bool
expireTime int64
patternJSON string
websiteIDs []int
// simple pattern flags
target string
cmp string
Expand Down Expand Up @@ -254,18 +255,25 @@ Examples:
return fmt.Errorf("either --pattern-json or --target/--cmp/--value is required")
}

websiteIDs, err := normalizeWebsiteIDs(websiteIDs)
if err != nil {
return err
}
req := map[string]interface{}{
"comment": comment,
"pattern": pattern,
"action": action,
"risk_level": riskLevel,
"is_global": true,
"is_global": len(websiteIDs) == 0,
"is_enabled": enabled,
"rule_type": 1, // GENERAL_RULE
"expire_time": expireTime,
"log_option": "Persistence",
"attack_type": -1,
}
if len(websiteIDs) > 0 {
req["websites"] = websiteIDs
}

body, err := json.Marshal(req)
if err != nil {
Expand Down Expand Up @@ -315,13 +323,30 @@ Examples:
c.Flags().StringVar(&target, "target", "", "Target key (simple mode)")
c.Flags().StringVar(&cmp, "cmp", "", "Comparison operator (simple mode)")
c.Flags().StringVar(&value, "value", "", "Match value (simple mode)")
c.Flags().IntSliceVar(&websiteIDs, "website-id", nil, "Site ID to bind; repeatable or comma-separated")

c.MarkFlagRequired("comment")
c.MarkFlagRequired("action")

return c
}

func normalizeWebsiteIDs(ids []int) ([]int, error) {
seen := make(map[int]struct{}, len(ids))
result := make([]int, 0, len(ids))
for _, id := range ids {
if id <= 0 {
return nil, fmt.Errorf("--website-id must be greater than 0")
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
result = append(result, id)
}
return result, nil
}

func newDeleteCmd() *cobra.Command {
return &cobra.Command{
Use: "delete <id>",
Expand Down
Loading