(w http.ResponseWriter, r *http.Request)
| 20 | } |
| 21 | |
| 22 | func (a *API) handleAuditRules(w http.ResponseWriter, r *http.Request) { |
| 23 | if a.auditRuleStore == nil { |
| 24 | writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "audit rule store not configured"}) |
| 25 | return |
| 26 | } |
| 27 | switch r.Method { |
| 28 | case http.MethodGet: |
| 29 | rules, err := a.auditRuleStore.List() |
| 30 | if err != nil { |
| 31 | writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) |
| 32 | return |
| 33 | } |
| 34 | if rules == nil { |
| 35 | rules = []auditrules.Rule{} |
| 36 | } |
| 37 | writeJSON(w, http.StatusOK, map[string]any{"rules": rules}) |
| 38 | case http.MethodPost: |
| 39 | var req struct { |
| 40 | Type auditrules.RuleType `json:"type"` |
| 41 | Value string `json:"value"` |
| 42 | } |
| 43 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 44 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid json"}) |
| 45 | return |
| 46 | } |
| 47 | req.Value = strings.TrimSpace(req.Value) |
| 48 | if req.Value == "" { |
| 49 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "value is required"}) |
| 50 | return |
| 51 | } |
| 52 | if req.Type != auditrules.RuleTypeDomainKeyword && req.Type != auditrules.RuleTypePort && req.Type != auditrules.RuleTypeIP { |
| 53 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid type"}) |
| 54 | return |
| 55 | } |
| 56 | rule := auditrules.Rule{ |
| 57 | ID: idgen.NextString(), |
| 58 | Type: req.Type, |
| 59 | Value: req.Value, |
| 60 | Enabled: true, |
| 61 | CreatedAt: time.Now(), |
| 62 | } |
| 63 | if err := a.auditRuleStore.Insert(rule); err != nil { |
| 64 | writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()}) |
| 65 | return |
| 66 | } |
| 67 | writeJSON(w, http.StatusOK, rule) |
| 68 | default: |
| 69 | writeMethodNotAllowed(w, http.MethodGet+", "+http.MethodPost) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (a *API) handleAuditRule(w http.ResponseWriter, r *http.Request, id string) { |
| 74 | if a.auditRuleStore == nil { |
nothing calls this directly
no test coverage detected