Skip to content

Commit ff8d403

Browse files
committed
refactor(core): updagrade to antrl 4.9.2; refactored grammar to parse actions as lists
Signed-off-by: Frederico Araujo <[email protected]>
1 parent 012bbd9 commit ff8d403

File tree

11 files changed

+760
-459
lines changed

11 files changed

+760
-459
lines changed

core/policyengine/engine/actionhandler.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ func NewActionHandler(conf Config) *ActionHandler {
9494
return ah
9595
}
9696

97+
// CheckActions checks whether actions rules definitions have known implementations.
98+
func (ah *ActionHandler) CheckActions(rules []Rule) {
99+
for _, r := range rules {
100+
for _, a := range r.Actions {
101+
if _, ok := ah.BuiltInActions[a]; !ok {
102+
if _, ok = ah.UserDefinedActions[a]; !ok {
103+
logger.Warn.Printf("Unknown action identifier '%s' found in rule '%s'", a, r.Name)
104+
}
105+
}
106+
}
107+
}
108+
}
109+
97110
// HandleAction handles actions defined in rule.
98111
func (ah *ActionHandler) HandleActions(rule Rule, r *Record) {
99112
for _, a := range rule.Actions {
@@ -102,10 +115,8 @@ func (ah *ActionHandler) HandleActions(rule Rule, r *Record) {
102115
action, ok = ah.UserDefinedActions[a]
103116
}
104117
if !ok {
105-
logger.Error.Println("Unknown action: '" + a + "'")
106118
continue
107119
}
108-
109120
if err := action(r); err != nil {
110121
logger.Error.Println("Error in action: " + err.Error())
111122
}

core/policyengine/engine/interpreter.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (pi *PolicyInterpreter) Compile(paths ...string) error {
158158
return err
159159
}
160160
}
161+
pi.ah.CheckActions(pi.rules)
161162
return nil
162163
}
163164

@@ -347,18 +348,19 @@ func (pi *PolicyInterpreter) getPriority(ctx *parser.PruleContext) Priority {
347348

348349
func (pi *PolicyInterpreter) getActions(ctx *parser.PruleContext) []string {
349350
var actions []string
350-
if ctx.ACTION(0) != nil || ctx.OUTPUT(0) != nil {
351-
astr := ctx.Text(2).GetText()
352-
l := pi.extractList(astr)
353-
for _, v := range l {
354-
actions = append(actions, strings.ToLower(v))
355-
}
351+
ictx := ctx.Actions(0)
352+
if ictx != nil {
353+
return append(actions, pi.extractActions(ictx)...)
356354
}
357355
return actions
358356
}
359357

360358
func (pi *PolicyInterpreter) extractList(str string) []string {
361-
return strings.Split(itemsre.ReplaceAllString(str, "$2"), LISTSEP)
359+
var items []string
360+
for _, i := range strings.Split(itemsre.ReplaceAllString(str, "$2"), LISTSEP) {
361+
items = append(items, trimBoundingQuotes(i))
362+
}
363+
return items
362364
}
363365

364366
func (pi *PolicyInterpreter) extractListFromItems(ctx parser.IItemsContext) []string {
@@ -375,6 +377,13 @@ func (pi *PolicyInterpreter) extractTags(ctx parser.ITagsContext) []string {
375377
return []string{}
376378
}
377379

380+
func (pi *PolicyInterpreter) extractActions(ctx parser.IActionsContext) []string {
381+
if ctx != nil {
382+
return pi.extractList(ctx.GetText())
383+
}
384+
return []string{}
385+
}
386+
378387
func (pi *PolicyInterpreter) extractListFromAtoms(ctxs []parser.IAtomContext) []string {
379388
s := []string{}
380389
for _, v := range ctxs {

core/policyengine/lang/Sfpl.g4

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ defs
2828
: (srule | sfilter | pmacro | plist | preq)* EOF
2929
;
3030

31-
prule
32-
: DECL RULE DEF text DESC DEF text COND DEF expression ((ACTION|OUTPUT) DEF text | PRIORITY DEF severity | TAGS DEF tags | PREFILTER DEF prefilter | ENABLED DEF enabled | WARNEVTTYPE DEF warnevttype | SKIPUNKNOWN DEF skipunknown)*
31+
prule
32+
: DECL RULE DEF text DESC DEF text COND DEF expression (OUTPUT DEF text | ACTION DEF actions | PRIORITY DEF severity | TAGS DEF tags | PREFILTER DEF prefilter | ENABLED DEF enabled | WARNEVTTYPE DEF warnevttype | SKIPUNKNOWN DEF skipunknown)*
3333
;
3434

3535
srule
36-
: DECL RULE DEF text DESC DEF text COND DEF expression ((ACTION|OUTPUT) DEF text | PRIORITY DEF severity | TAGS DEF tags | PREFILTER DEF prefilter | ENABLED DEF enabled | WARNEVTTYPE DEF warnevttype | SKIPUNKNOWN DEF skipunknown)*
36+
: DECL RULE DEF text DESC DEF text COND DEF expression (OUTPUT DEF text | ACTION DEF actions | PRIORITY DEF severity | TAGS DEF tags | PREFILTER DEF prefilter | ENABLED DEF enabled | WARNEVTTYPE DEF warnevttype | SKIPUNKNOWN DEF skipunknown)*
3737
;
3838

3939
pfilter
@@ -85,6 +85,10 @@ items
8585
: LBRACK (atom (LISTSEP atom)*)? (LISTSEP)? RBRACK
8686
;
8787

88+
actions
89+
: LBRACK (atom (LISTSEP atom)*)? (LISTSEP)? RBRACK
90+
;
91+
8892
tags
8993
: LBRACK (atom (LISTSEP atom)*)? (LISTSEP)? RBRACK
9094
;
@@ -138,9 +142,9 @@ text
138142
p.GetCurrentToken().GetText() == "enabled" ||
139143
p.GetCurrentToken().GetText() == "warn_evttypes" ||
140144
p.GetCurrentToken().GetText() == "skip-if-unknown-filter" ||
141-
p.GetCurrentToken().GetText() == "append")}? .)+
145+
p.GetCurrentToken().GetText() == "append" )}? .)+
142146
;
143-
147+
144148
binary_operator
145149
: LT
146150
| LE

core/policyengine/lang/generate.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
antlr4='java -Xmx500M -cp ".:/usr/local/lib/antlr-4.8-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
4-
grun='java -Xmx500M -cp ".:/usr/local/lib/antlr-4.8-complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'
3+
antlr4='java -Xmx500M -cp ".:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
4+
grun='java -Xmx500M -cp ".:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig'
55

66
$antlr4 -Dlanguage=Go -o parser -package parser -visitor Sfpl.g4

core/policyengine/lang/parser/Sfpl.interp

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

core/policyengine/lang/parser/sfpl_base_listener.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/policyengine/lang/parser/sfpl_base_visitor.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/policyengine/lang/parser/sfpl_lexer.go

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/policyengine/lang/parser/sfpl_listener.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)