66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package actions
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.wayfinderak.com/wayfinderak/log-guardian/internal/store"
|
|
)
|
|
|
|
type Result struct {
|
|
Action string `json:"action"`
|
|
DryRun bool `json:"dry_run"`
|
|
Detail string `json:"detail"`
|
|
}
|
|
|
|
type Runner struct {
|
|
dryRun bool
|
|
}
|
|
|
|
func NewRunner(dryRun bool) *Runner { return &Runner{dryRun: dryRun} }
|
|
|
|
func (r *Runner) Run(ctx context.Context, rule store.Rule, action store.Action) (Result, error) {
|
|
_ = ctx
|
|
if !action.Enabled {
|
|
return Result{Action: action.Type, DryRun: r.effectiveDryRun(action), Detail: "disabled"}, nil
|
|
}
|
|
switch action.Type {
|
|
case "generic_webhook", "webhook":
|
|
url := action.Params["url"]
|
|
if url == "" {
|
|
return Result{}, fmt.Errorf("webhook action requires url")
|
|
}
|
|
return Result{Action: action.Type, DryRun: true, Detail: "would POST webhook for rule " + rule.Name}, nil
|
|
case "record_recommendation":
|
|
return Result{Action: action.Type, DryRun: false, Detail: "recommendation recorded for operator review"}, nil
|
|
case "allowlisted_command", "command":
|
|
id := action.Params["command_id"]
|
|
if id == "" {
|
|
id = action.Params["id"]
|
|
}
|
|
if id == "" {
|
|
return Result{}, fmt.Errorf("allowlisted command requires command_id")
|
|
}
|
|
return Result{Action: action.Type, DryRun: true, Detail: "would run allowlisted command " + id}, nil
|
|
case "portainer_restart":
|
|
service := action.Params["service"]
|
|
if service == "" {
|
|
return Result{}, fmt.Errorf("portainer_restart requires service")
|
|
}
|
|
return Result{Action: action.Type, DryRun: true, Detail: "would restart Portainer service " + service}, nil
|
|
default:
|
|
return Result{}, fmt.Errorf("unsupported action type %q", action.Type)
|
|
}
|
|
return Result{}, fmt.Errorf("unsupported action type %q", action.Type)
|
|
}
|
|
|
|
func (r *Runner) effectiveDryRun(action store.Action) bool {
|
|
if r.dryRun {
|
|
return true
|
|
}
|
|
if action.DryRun == nil {
|
|
return true
|
|
}
|
|
return *action.DryRun
|
|
}
|