Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rule-engine): Revamp rule engine #452

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion cmd/fibratus/app/rules/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/rabbitstack/fibratus/internal/bootstrap"
"github.com/rabbitstack/fibratus/pkg/filter"
"github.com/rabbitstack/fibratus/pkg/filter/fields"
"github.com/rabbitstack/fibratus/pkg/rules"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func validateRules() error {
f := filter.New(rule.Condition, cfg)
err := f.Compile()
if err != nil {
return fmt.Errorf("%v %v", emoji.DisappointedFace, filter.ErrInvalidFilter(rule.Name, err))
return fmt.Errorf("%v %v", emoji.DisappointedFace, rules.ErrInvalidFilter(rule.Name, err))
}

w := warning{rule: rule.Name}
Expand Down
4 changes: 4 additions & 0 deletions configs/fibratus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ filament:
# For local file system rule paths, it is possible to use the glob expression to load the
# rules from different directory locations.
filters:
# Indicates if the rule engine match all strategy is enabled. When the match all strategy
# is enabled, a single event can trigger multiple rules.
match-all: true

rules:
# Indicates if the rule engine is enabled and rules loaded
enabled: true
Expand Down
27 changes: 14 additions & 13 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/rabbitstack/fibratus/pkg/handle"
"github.com/rabbitstack/fibratus/pkg/kcap"
"github.com/rabbitstack/fibratus/pkg/ps"
"github.com/rabbitstack/fibratus/pkg/rules"
"github.com/rabbitstack/fibratus/pkg/symbolize"
"github.com/rabbitstack/fibratus/pkg/sys"
"github.com/rabbitstack/fibratus/pkg/util/multierror"
Expand All @@ -52,7 +53,7 @@ type App struct {
config *config.Config
evs *EventSourceControl
symbolizer *symbolize.Symbolizer
rules *filter.Rules
engine *rules.Engine
hsnap handle.Snapshotter
psnap ps.Snapshotter
filament filament.Filament
Expand Down Expand Up @@ -134,34 +135,34 @@ func NewApp(cfg *config.Config, options ...Option) (*App, error) {
hsnap := handle.NewSnapshotter(cfg, opts.handleSnapshotFn)
psnap := ps.NewSnapshotter(hsnap, cfg)

var (
rules *filter.Rules
res *config.RulesCompileResult
)
var engine *rules.Engine
var rs *config.RulesCompileResult

if cfg.Filters.Rules.Enabled && !cfg.ForwardMode && !cfg.IsCaptureSet() {
rules = filter.NewRules(psnap, cfg)
engine = rules.NewEngine(psnap, cfg)
var err error
res, err = rules.Compile()
rs, err = engine.Compile()
if err != nil {
return nil, err
}
if res != nil {
log.Infof("rules compile summary: %s", res)
if rs != nil {
log.Infof("rules compile summary: %s", rs)
}
} else {
log.Info("rule engine is disabled")
}

evs := NewEventSourceControl(psnap, hsnap, cfg, res)
evs := NewEventSourceControl(psnap, hsnap, cfg, rs)

app := &App{
config: cfg,
evs: evs,
rules: rules,
engine: engine,
hsnap: hsnap,
psnap: psnap,
signals: sigs,
}

return app, nil
}

Expand Down Expand Up @@ -234,8 +235,8 @@ func (f *App) Run(args []string) error {
f.evs.RegisterEventListener(f.symbolizer)
}
// register rule engine
if f.rules != nil {
f.evs.RegisterEventListener(f.rules)
if f.engine != nil {
f.evs.RegisterEventListener(f.engine)
}
// register YARA scanner
if cfg.Yara.Enabled {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ func (c *Config) addFlags() {
c.flags.StringSlice(rulesFromPaths, []string{filepath.Join(dir, "*")}, "Comma-separated list of rules files")
c.flags.StringSlice(macrosFromPaths, []string{filepath.Join(dir, "Macros", "*")}, "Comma-separated list of macro files")
c.flags.StringSlice(rulesFromURLs, []string{}, "Comma-separated list of rules URL resources")
c.flags.Bool(matchAll, true, "Indicates if the match all strategy is enabled for the rule engine. If the match all strategy is enabled, a single event can trigger multiple rules")
}
if c.opts.capture {
c.flags.StringP(kcapFile, "o", "", "The path of the output kcap file")
Expand Down
13 changes: 9 additions & 4 deletions pkg/config/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ func (f FilterConfig) HasLabel(l string) bool { return f.Labels[l] != "" }

// Filters contains references to rule and macro definitions.
type Filters struct {
Rules Rules `json:"rules" yaml:"rules"`
Macros Macros `json:"macros" yaml:"macros"`
macros map[string]*Macro
filters []*FilterConfig
Rules Rules `json:"rules" yaml:"rules"`
Macros Macros `json:"macros" yaml:"macros"`
// MatchAll indicates if the match all strategy is enabled for the rule engine.
// If the match all strategy is enabled, a single event can trigger multiple rules.
MatchAll bool `json:"match-all" yaml:"match-all"`
macros map[string]*Macro
filters []*FilterConfig
}

// FiltersWithMacros builds the filter config with the map of
Expand Down Expand Up @@ -241,13 +244,15 @@ const (
rulesFromPaths = "filters.rules.from-paths"
rulesFromURLs = "filters.rules.from-urls"
macrosFromPaths = "filters.macros.from-paths"
matchAll = "filters.match-all"
)

func (f *Filters) initFromViper(v *viper.Viper) {
f.Rules.Enabled = v.GetBool(rulesEnabled)
f.Rules.FromPaths = v.GetStringSlice(rulesFromPaths)
f.Rules.FromURLs = v.GetStringSlice(rulesFromURLs)
f.Macros.FromPaths = v.GetStringSlice(macrosFromPaths)
f.MatchAll = v.GetBool(matchAll)
}

func (f Filters) HasMacros() bool { return len(f.macros) > 0 }
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestLoadRulesFromPaths(t *testing.T) {
},
},
Macros{FromPaths: nil},
false,
map[string]*Macro{},
[]*FilterConfig{},
}
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestLoadRulesFromPathsWithTemplate(t *testing.T) {
},
},
Macros{FromPaths: nil},
false,
map[string]*Macro{},
[]*FilterConfig{},
}
Expand Down Expand Up @@ -116,6 +118,7 @@ func TestLoadGroupsFromURLs(t *testing.T) {
},
},
Macros{FromPaths: nil},
false,
map[string]*Macro{},
[]*FilterConfig{},
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/schema_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ var schema = `
"filters": {
"type": "object",
"properties": {
"match-all": {"type": "boolean"},
"rules": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -510,7 +511,7 @@ var rulesSchema = `
"id": {"type": "string", "minLength": 36, "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"},
"version": {"type": "string", "minLength": 5, "pattern": "^([0-9]+.)([0-9]+.)([0-9]+)$"},
"name": {"type": "string", "minLength": 3},
"description": {"type": "string"},
"description": {"type": "string"},
"output": {"type": "string", "minLength": 5},
"notes": {"type": "string"},
"severity": {"type": "string", "enum": ["low", "medium", "high", "critical"]},
Expand Down
16 changes: 0 additions & 16 deletions pkg/filter/_fixtures/sequence_gc.yml

This file was deleted.

15 changes: 0 additions & 15 deletions pkg/filter/_fixtures/sequence_rule_bound_fields.yml

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/filter/_fixtures/sequence_rule_bound_fields_with_functions.yml

This file was deleted.

12 changes: 0 additions & 12 deletions pkg/filter/_fixtures/sequence_rule_expire.yml

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/filter/_fixtures/sequence_rule_out_of_order.yml

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/filter/_fixtures/sequence_rule_simple_max_span.yml

This file was deleted.

Loading
Loading