|
| 1 | +package executor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/tidwall/gjson" |
| 11 | + |
| 12 | + "github.com/utmstack/utmstack/backend/modules/soar/domain" |
| 13 | +) |
| 14 | + |
| 15 | +// Conditional evaluates a list of predicates against the execution's merged |
| 16 | +// context bag and returns success only when all of them are true (AND). A |
| 17 | +// failing predicate returns an error so the dispatcher routes the flow down |
| 18 | +// the node's onError branch — no bespoke edge kind needed. |
| 19 | +// ponytail: reuses domain.FilterType and gjson (already vendored via variable |
| 20 | +// + execution interpolation); OnSuccess/OnError already model the true/false |
| 21 | +// exits of a conditional. |
| 22 | +type Conditional struct{} |
| 23 | + |
| 24 | +func NewConditional() *Conditional { return &Conditional{} } |
| 25 | + |
| 26 | +func (Conditional) Type() string { return "conditional" } |
| 27 | + |
| 28 | +type conditionalParams struct { |
| 29 | + Conditions []domain.FilterType `json:"conditions"` |
| 30 | +} |
| 31 | + |
| 32 | +func (c *Conditional) Execute(_ context.Context, exec *domain.SoarExecution) (json.RawMessage, error) { |
| 33 | + var p conditionalParams |
| 34 | + if len(exec.Params) > 0 { |
| 35 | + if err := json.Unmarshal(exec.Params, &p); err != nil { |
| 36 | + return nil, fmt.Errorf("soar conditional: params: %w", err) |
| 37 | + } |
| 38 | + } |
| 39 | + if len(p.Conditions) == 0 { |
| 40 | + return nil, errors.New("soar conditional: at least one condition is required") |
| 41 | + } |
| 42 | + src := string(exec.Context) |
| 43 | + if src == "" { |
| 44 | + src = "{}" |
| 45 | + } |
| 46 | + for _, cond := range p.Conditions { |
| 47 | + if !evaluateFilter(src, cond) { |
| 48 | + exec.Result = fmt.Sprintf("condition failed: %s %s %v", cond.Field, cond.Operator, cond.Value) |
| 49 | + return nil, errors.New(exec.Result) |
| 50 | + } |
| 51 | + } |
| 52 | + exec.Result = "all conditions matched" |
| 53 | + return nil, nil |
| 54 | +} |
| 55 | + |
| 56 | +func evaluateFilter(src string, cond domain.FilterType) bool { |
| 57 | + val := gjson.Get(src, cond.Field) |
| 58 | + switch cond.Operator { |
| 59 | + case domain.OperatorExists: |
| 60 | + return val.Exists() |
| 61 | + case domain.OperatorNotExists: |
| 62 | + return !val.Exists() |
| 63 | + } |
| 64 | + got := val.String() |
| 65 | + switch cond.Operator { |
| 66 | + case domain.OperatorIS: |
| 67 | + return got == asString(cond.Value) |
| 68 | + case domain.OperatorISNot: |
| 69 | + return got != asString(cond.Value) |
| 70 | + case domain.OperatorContains: |
| 71 | + return strings.Contains(got, asString(cond.Value)) |
| 72 | + case domain.OperatorNotContains: |
| 73 | + return !strings.Contains(got, asString(cond.Value)) |
| 74 | + case domain.OperatorStartWith: |
| 75 | + return strings.HasPrefix(got, asString(cond.Value)) |
| 76 | + case domain.OperatorNotStartWith: |
| 77 | + return !strings.HasPrefix(got, asString(cond.Value)) |
| 78 | + case domain.OperatorEndsWith: |
| 79 | + return strings.HasSuffix(got, asString(cond.Value)) |
| 80 | + case domain.OperatorNotEndsWith: |
| 81 | + return !strings.HasSuffix(got, asString(cond.Value)) |
| 82 | + case domain.OperatorIsOneOf: |
| 83 | + return oneOf(asStringSlice(cond.Value), got) |
| 84 | + case domain.OperatorIsNotOneOf: |
| 85 | + return !oneOf(asStringSlice(cond.Value), got) |
| 86 | + } |
| 87 | + return false |
| 88 | +} |
| 89 | + |
| 90 | +func asString(v any) string { |
| 91 | + switch t := v.(type) { |
| 92 | + case string: |
| 93 | + return t |
| 94 | + case nil: |
| 95 | + return "" |
| 96 | + default: |
| 97 | + b, _ := json.Marshal(t) |
| 98 | + return string(b) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func asStringSlice(v any) []string { |
| 103 | + switch t := v.(type) { |
| 104 | + case []string: |
| 105 | + return t |
| 106 | + case []any: |
| 107 | + out := make([]string, 0, len(t)) |
| 108 | + for _, x := range t { |
| 109 | + out = append(out, asString(x)) |
| 110 | + } |
| 111 | + return out |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func oneOf(hay []string, needle string) bool { |
| 117 | + for _, h := range hay { |
| 118 | + if h == needle { |
| 119 | + return true |
| 120 | + } |
| 121 | + } |
| 122 | + return false |
| 123 | +} |
0 commit comments