node.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. package parser
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/antonmedv/expr"
  9. "github.com/antonmedv/expr/vm"
  10. "github.com/davecgh/go-spew/spew"
  11. "github.com/prometheus/client_golang/prometheus"
  12. log "github.com/sirupsen/logrus"
  13. yaml "gopkg.in/yaml.v2"
  14. "github.com/crowdsecurity/grokky"
  15. "github.com/crowdsecurity/crowdsec/pkg/cache"
  16. "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
  17. "github.com/crowdsecurity/crowdsec/pkg/types"
  18. )
  19. type Node struct {
  20. FormatVersion string `yaml:"format"`
  21. //Enable config + runtime debug of node via config o/
  22. Debug bool `yaml:"debug,omitempty"`
  23. //If enabled, the node (and its child) will report their own statistics
  24. Profiling bool `yaml:"profiling,omitempty"`
  25. //Name, author, description and reference(s) for parser pattern
  26. Name string `yaml:"name,omitempty"`
  27. Author string `yaml:"author,omitempty"`
  28. Description string `yaml:"description,omitempty"`
  29. References []string `yaml:"references,omitempty"`
  30. //if debug is present in the node, keep its specific Logger in runtime structure
  31. Logger *log.Entry `yaml:"-"`
  32. //This is mostly a hack to make writing less repetitive.
  33. //relying on stage, we know which field to parse, and we
  34. //can also promote log to next stage on success
  35. Stage string `yaml:"stage,omitempty"`
  36. //OnSuccess allows to tag a node to be able to move log to next stage on success
  37. OnSuccess string `yaml:"onsuccess,omitempty"`
  38. rn string //this is only for us in debug, a random generated name for each node
  39. //Filter is executed at runtime (with current log line as context)
  40. //and must succeed or node is exited
  41. Filter string `yaml:"filter,omitempty"`
  42. RunTimeFilter *vm.Program `yaml:"-" json:"-"` //the actual compiled filter
  43. ExprDebugger *exprhelpers.ExprDebugger `yaml:"-" json:"-"` //used to debug expression by printing the content of each variable of the expression
  44. //If node has leafs, execute all of them until one asks for a 'break'
  45. LeavesNodes []Node `yaml:"nodes,omitempty"`
  46. //Flag used to describe when to 'break' or return an 'error'
  47. EnrichFunctions EnricherCtx
  48. /* If the node is actually a leaf, it can have : grok, enrich, statics */
  49. //pattern_syntax are named grok patterns that are re-utilized over several grok patterns
  50. SubGroks yaml.MapSlice `yaml:"pattern_syntax,omitempty"`
  51. //Holds a grok pattern
  52. Grok GrokPattern `yaml:"grok,omitempty"`
  53. //Statics can be present in any type of node and is executed last
  54. Statics []ExtraField `yaml:"statics,omitempty"`
  55. //Stash allows to capture data from the log line and store it in an accessible cache
  56. Stash []DataCapture `yaml:"stash,omitempty"`
  57. //Whitelists
  58. Whitelist Whitelist `yaml:"whitelist,omitempty"`
  59. Data []*types.DataSource `yaml:"data,omitempty"`
  60. }
  61. func (n *Node) validate(pctx *UnixParserCtx, ectx EnricherCtx) error {
  62. //stage is being set automagically
  63. if n.Stage == "" {
  64. return fmt.Errorf("stage needs to be an existing stage")
  65. }
  66. /* "" behaves like continue */
  67. if n.OnSuccess != "continue" && n.OnSuccess != "next_stage" && n.OnSuccess != "" {
  68. return fmt.Errorf("onsuccess '%s' not continue,next_stage", n.OnSuccess)
  69. }
  70. if n.Filter != "" && n.RunTimeFilter == nil {
  71. return fmt.Errorf("non-empty filter '%s' was not compiled", n.Filter)
  72. }
  73. if n.Grok.RunTimeRegexp != nil || n.Grok.TargetField != "" {
  74. if n.Grok.TargetField == "" && n.Grok.ExpValue == "" {
  75. return fmt.Errorf("grok requires 'expression' or 'apply_on'")
  76. }
  77. if n.Grok.RegexpName == "" && n.Grok.RegexpValue == "" {
  78. return fmt.Errorf("grok needs 'pattern' or 'name'")
  79. }
  80. }
  81. for idx, static := range n.Statics {
  82. if static.Method != "" {
  83. if static.ExpValue == "" {
  84. return fmt.Errorf("static %d : when method is set, expression must be present", idx)
  85. }
  86. if _, ok := ectx.Registered[static.Method]; !ok {
  87. log.Warningf("the method '%s' doesn't exist or the plugin has not been initialized", static.Method)
  88. }
  89. } else {
  90. if static.Meta == "" && static.Parsed == "" && static.TargetByName == "" {
  91. return fmt.Errorf("static %d : at least one of meta/event/target must be set", idx)
  92. }
  93. if static.Value == "" && static.RunTimeValue == nil {
  94. return fmt.Errorf("static %d value or expression must be set", idx)
  95. }
  96. }
  97. }
  98. for idx, stash := range n.Stash {
  99. if stash.Name == "" {
  100. return fmt.Errorf("stash %d : name must be set", idx)
  101. }
  102. if stash.Value == "" {
  103. return fmt.Errorf("stash %s : value expression must be set", stash.Name)
  104. }
  105. if stash.Key == "" {
  106. return fmt.Errorf("stash %s : key expression must be set", stash.Name)
  107. }
  108. if stash.TTL == "" {
  109. return fmt.Errorf("stash %s : ttl must be set", stash.Name)
  110. }
  111. if stash.Strategy == "" {
  112. stash.Strategy = "LRU"
  113. }
  114. //should be configurable
  115. if stash.MaxMapSize == 0 {
  116. stash.MaxMapSize = 100
  117. }
  118. }
  119. return nil
  120. }
  121. func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[string]interface{}) (bool, error) {
  122. var NodeState bool
  123. var NodeHasOKGrok bool
  124. clog := n.Logger
  125. cachedExprEnv := expressionEnv
  126. clog.Tracef("Event entering node")
  127. if n.RunTimeFilter != nil {
  128. //Evaluate node's filter
  129. output, err := expr.Run(n.RunTimeFilter, cachedExprEnv)
  130. if err != nil {
  131. clog.Warningf("failed to run filter : %v", err)
  132. clog.Debugf("Event leaving node : ko")
  133. return false, nil
  134. }
  135. switch out := output.(type) {
  136. case bool:
  137. if n.Debug {
  138. n.ExprDebugger.Run(clog, out, cachedExprEnv)
  139. }
  140. if !out {
  141. clog.Debugf("Event leaving node : ko (failed filter)")
  142. return false, nil
  143. }
  144. default:
  145. clog.Warningf("Expr '%s' returned non-bool, abort : %T", n.Filter, output)
  146. clog.Debugf("Event leaving node : ko")
  147. return false, nil
  148. }
  149. NodeState = true
  150. } else {
  151. clog.Tracef("Node has not filter, enter")
  152. NodeState = true
  153. }
  154. if n.Name != "" {
  155. NodesHits.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  156. }
  157. isWhitelisted := false
  158. hasWhitelist := false
  159. var srcs []net.IP
  160. /*overflow and log don't hold the source ip in the same field, should be changed */
  161. /* perform whitelist checks for ips, cidr accordingly */
  162. /* TODO move whitelist elsewhere */
  163. if p.Type == types.LOG {
  164. if _, ok := p.Meta["source_ip"]; ok {
  165. srcs = append(srcs, net.ParseIP(p.Meta["source_ip"]))
  166. }
  167. } else if p.Type == types.OVFLW {
  168. for k := range p.Overflow.Sources {
  169. srcs = append(srcs, net.ParseIP(k))
  170. }
  171. }
  172. for _, src := range srcs {
  173. if isWhitelisted {
  174. break
  175. }
  176. for _, v := range n.Whitelist.B_Ips {
  177. if v.Equal(src) {
  178. clog.Debugf("Event from [%s] is whitelisted by IP (%s), reason [%s]", src, v, n.Whitelist.Reason)
  179. isWhitelisted = true
  180. } else {
  181. clog.Tracef("whitelist: %s is not eq [%s]", src, v)
  182. }
  183. hasWhitelist = true
  184. }
  185. for _, v := range n.Whitelist.B_Cidrs {
  186. if v.Contains(src) {
  187. clog.Debugf("Event from [%s] is whitelisted by CIDR (%s), reason [%s]", src, v, n.Whitelist.Reason)
  188. isWhitelisted = true
  189. } else {
  190. clog.Tracef("whitelist: %s not in [%s]", src, v)
  191. }
  192. hasWhitelist = true
  193. }
  194. }
  195. if isWhitelisted {
  196. p.Whitelisted = true
  197. }
  198. /* run whitelist expression tests anyway */
  199. for eidx, e := range n.Whitelist.B_Exprs {
  200. output, err := expr.Run(e.Filter, cachedExprEnv)
  201. if err != nil {
  202. clog.Warningf("failed to run whitelist expr : %v", err)
  203. clog.Debug("Event leaving node : ko")
  204. return false, nil
  205. }
  206. switch out := output.(type) {
  207. case bool:
  208. if n.Debug {
  209. e.ExprDebugger.Run(clog, out, cachedExprEnv)
  210. }
  211. if out {
  212. clog.Debugf("Event is whitelisted by expr, reason [%s]", n.Whitelist.Reason)
  213. p.Whitelisted = true
  214. isWhitelisted = true
  215. }
  216. hasWhitelist = true
  217. default:
  218. log.Errorf("unexpected type %t (%v) while running '%s'", output, output, n.Whitelist.Exprs[eidx])
  219. }
  220. }
  221. if isWhitelisted {
  222. p.WhitelistReason = n.Whitelist.Reason
  223. /*huglily wipe the ban order if the event is whitelisted and it's an overflow */
  224. if p.Type == types.OVFLW { /*don't do this at home kids */
  225. ips := []string{}
  226. for _, src := range srcs {
  227. ips = append(ips, src.String())
  228. }
  229. clog.Infof("Ban for %s whitelisted, reason [%s]", strings.Join(ips, ","), n.Whitelist.Reason)
  230. p.Overflow.Whitelisted = true
  231. }
  232. }
  233. //Process grok if present, should be exclusive with nodes :)
  234. gstr := ""
  235. if n.Grok.RunTimeRegexp != nil {
  236. clog.Tracef("Processing grok pattern : %s : %p", n.Grok.RegexpName, n.Grok.RunTimeRegexp)
  237. //for unparsed, parsed etc. set sensible defaults to reduce user hassle
  238. if n.Grok.TargetField != "" {
  239. //it's a hack to avoid using real reflect
  240. if n.Grok.TargetField == "Line.Raw" {
  241. gstr = p.Line.Raw
  242. } else if val, ok := p.Parsed[n.Grok.TargetField]; ok {
  243. gstr = val
  244. } else {
  245. clog.Debugf("(%s) target field '%s' doesn't exist in %v", n.rn, n.Grok.TargetField, p.Parsed)
  246. NodeState = false
  247. }
  248. } else if n.Grok.RunTimeValue != nil {
  249. output, err := expr.Run(n.Grok.RunTimeValue, cachedExprEnv)
  250. if err != nil {
  251. clog.Warningf("failed to run RunTimeValue : %v", err)
  252. NodeState = false
  253. }
  254. switch out := output.(type) {
  255. case string:
  256. gstr = out
  257. default:
  258. clog.Errorf("unexpected return type for RunTimeValue : %T", output)
  259. }
  260. }
  261. var groklabel string
  262. if n.Grok.RegexpName == "" {
  263. groklabel = fmt.Sprintf("%5.5s...", n.Grok.RegexpValue)
  264. } else {
  265. groklabel = n.Grok.RegexpName
  266. }
  267. grok := n.Grok.RunTimeRegexp.Parse(gstr)
  268. if len(grok) > 0 {
  269. /*tag explicitly that the *current* node had a successful grok pattern. it's important to know success state*/
  270. NodeHasOKGrok = true
  271. clog.Debugf("+ Grok '%s' returned %d entries to merge in Parsed", groklabel, len(grok))
  272. //We managed to grok stuff, merged into parse
  273. for k, v := range grok {
  274. clog.Debugf("\t.Parsed['%s'] = '%s'", k, v)
  275. p.Parsed[k] = v
  276. }
  277. // if the grok succeed, process associated statics
  278. err := n.ProcessStatics(n.Grok.Statics, p)
  279. if err != nil {
  280. clog.Errorf("(%s) Failed to process statics : %v", n.rn, err)
  281. return false, err
  282. }
  283. } else {
  284. //grok failed, node failed
  285. clog.Debugf("+ Grok '%s' didn't return data on '%s'", groklabel, gstr)
  286. NodeState = false
  287. }
  288. } else {
  289. clog.Tracef("! No grok pattern : %p", n.Grok.RunTimeRegexp)
  290. }
  291. //Process the stash (data collection) if : a grok was present and succeeded, or if there is no grok
  292. if NodeHasOKGrok || n.Grok.RunTimeRegexp == nil {
  293. for idx, stash := range n.Stash {
  294. var value string
  295. var key string
  296. if stash.ValueExpression == nil {
  297. clog.Warningf("Stash %d has no value expression, skipping", idx)
  298. continue
  299. }
  300. if stash.KeyExpression == nil {
  301. clog.Warningf("Stash %d has no key expression, skipping", idx)
  302. continue
  303. }
  304. //collect the data
  305. output, err := expr.Run(stash.ValueExpression, cachedExprEnv)
  306. if err != nil {
  307. clog.Warningf("Error while running stash val expression : %v", err)
  308. }
  309. //can we expect anything else than a string ?
  310. switch output := output.(type) {
  311. case string:
  312. value = output
  313. default:
  314. clog.Warningf("unexpected type %t (%v) while running '%s'", output, output, stash.Value)
  315. continue
  316. }
  317. //collect the key
  318. output, err = expr.Run(stash.KeyExpression, cachedExprEnv)
  319. if err != nil {
  320. clog.Warningf("Error while running stash key expression : %v", err)
  321. }
  322. //can we expect anything else than a string ?
  323. switch output := output.(type) {
  324. case string:
  325. key = output
  326. default:
  327. clog.Warningf("unexpected type %t (%v) while running '%s'", output, output, stash.Key)
  328. continue
  329. }
  330. cache.SetKey(stash.Name, key, value, &stash.TTLVal)
  331. }
  332. }
  333. //Iterate on leafs
  334. for _, leaf := range n.LeavesNodes {
  335. ret, err := leaf.process(p, ctx, cachedExprEnv)
  336. if err != nil {
  337. clog.Tracef("\tNode (%s) failed : %v", leaf.rn, err)
  338. clog.Debugf("Event leaving node : ko")
  339. return false, err
  340. }
  341. clog.Tracef("\tsub-node (%s) ret : %v (strategy:%s)", leaf.rn, ret, n.OnSuccess)
  342. if ret {
  343. NodeState = true
  344. /* if child is successful, stop processing */
  345. if n.OnSuccess == "next_stage" {
  346. clog.Debugf("child is success, OnSuccess=next_stage, skip")
  347. break
  348. }
  349. } else if !NodeHasOKGrok {
  350. /*
  351. If the parent node has a successful grok pattern, it's state will stay successful even if one or more chil fails.
  352. If the parent node is a skeleton node (no grok pattern), then at least one child must be successful for it to be a success.
  353. */
  354. NodeState = false
  355. }
  356. }
  357. /*todo : check if a node made the state change ?*/
  358. /* should the childs inherit the on_success behavior */
  359. clog.Tracef("State after nodes : %v", NodeState)
  360. //grok or leafs failed, don't process statics
  361. if !NodeState {
  362. if n.Name != "" {
  363. NodesHitsKo.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  364. }
  365. clog.Debugf("Event leaving node : ko")
  366. return NodeState, nil
  367. }
  368. if n.Name != "" {
  369. NodesHitsOk.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  370. }
  371. /*
  372. This is to apply statics when the node *has* whitelists that successfully matched the node.
  373. */
  374. if len(n.Statics) > 0 && (isWhitelisted || !hasWhitelist) {
  375. clog.Debugf("+ Processing %d statics", len(n.Statics))
  376. // if all else is good in whitelist, process node's statics
  377. err := n.ProcessStatics(n.Statics, p)
  378. if err != nil {
  379. clog.Errorf("Failed to process statics : %v", err)
  380. return false, err
  381. }
  382. } else {
  383. clog.Tracef("! No node statics")
  384. }
  385. if NodeState {
  386. clog.Debugf("Event leaving node : ok")
  387. log.Tracef("node is successful, check strategy")
  388. if n.OnSuccess == "next_stage" {
  389. idx := stageidx(p.Stage, ctx.Stages)
  390. //we're at the last stage
  391. if idx+1 == len(ctx.Stages) {
  392. clog.Debugf("node reached the last stage : %s", p.Stage)
  393. } else {
  394. clog.Debugf("move Event from stage %s to %s", p.Stage, ctx.Stages[idx+1])
  395. p.Stage = ctx.Stages[idx+1]
  396. }
  397. } else {
  398. clog.Tracef("no strategy on success (%s), continue !", n.OnSuccess)
  399. }
  400. } else {
  401. clog.Debugf("Event leaving node : ko")
  402. }
  403. clog.Tracef("Node successful, continue")
  404. return NodeState, nil
  405. }
  406. func (n *Node) compile(pctx *UnixParserCtx, ectx EnricherCtx) error {
  407. var err error
  408. var valid bool
  409. valid = false
  410. dumpr := spew.ConfigState{MaxDepth: 1, DisablePointerAddresses: true}
  411. n.rn = seed.Generate()
  412. n.EnrichFunctions = ectx
  413. log.Tracef("compile, node is %s", n.Stage)
  414. /* if the node has debugging enabled, create a specific logger with debug
  415. that will be used only for processing this node ;) */
  416. if n.Debug {
  417. var clog = log.New()
  418. if err = types.ConfigureLogger(clog); err != nil {
  419. log.Fatalf("While creating bucket-specific logger : %s", err)
  420. }
  421. clog.SetLevel(log.DebugLevel)
  422. n.Logger = clog.WithFields(log.Fields{
  423. "id": n.rn,
  424. })
  425. n.Logger.Infof("%s has debug enabled", n.Name)
  426. } else {
  427. /* else bind it to the default one (might find something more elegant here)*/
  428. n.Logger = log.WithFields(log.Fields{
  429. "id": n.rn,
  430. })
  431. }
  432. /* display info about top-level nodes, they should be the only one with explicit stage name ?*/
  433. n.Logger = n.Logger.WithFields(log.Fields{"stage": n.Stage, "name": n.Name})
  434. n.Logger.Tracef("Compiling : %s", dumpr.Sdump(n))
  435. //compile filter if present
  436. if n.Filter != "" {
  437. n.RunTimeFilter, err = expr.Compile(n.Filter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  438. if err != nil {
  439. return fmt.Errorf("compilation of '%s' failed: %v", n.Filter, err)
  440. }
  441. if n.Debug {
  442. n.ExprDebugger, err = exprhelpers.NewDebugger(n.Filter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  443. if err != nil {
  444. log.Errorf("unable to build debug filter for '%s' : %s", n.Filter, err)
  445. }
  446. }
  447. }
  448. /* handle pattern_syntax and groks */
  449. for _, pattern := range n.SubGroks {
  450. n.Logger.Tracef("Adding subpattern '%s' : '%s'", pattern.Key, pattern.Value)
  451. if err = pctx.Grok.Add(pattern.Key.(string), pattern.Value.(string)); err != nil {
  452. if errors.Is(err, grokky.ErrAlreadyExist) {
  453. n.Logger.Warningf("grok '%s' already registred", pattern.Key)
  454. continue
  455. }
  456. n.Logger.Errorf("Unable to compile subpattern %s : %v", pattern.Key, err)
  457. return err
  458. }
  459. }
  460. /* load grok by name or compile in-place */
  461. if n.Grok.RegexpName != "" {
  462. n.Logger.Tracef("+ Regexp Compilation '%s'", n.Grok.RegexpName)
  463. n.Grok.RunTimeRegexp, err = pctx.Grok.Get(n.Grok.RegexpName)
  464. if err != nil {
  465. return fmt.Errorf("unable to find grok '%s' : %v", n.Grok.RegexpName, err)
  466. }
  467. if n.Grok.RunTimeRegexp == nil {
  468. return fmt.Errorf("empty grok '%s'", n.Grok.RegexpName)
  469. }
  470. n.Logger.Tracef("%s regexp: %s", n.Grok.RegexpName, n.Grok.RunTimeRegexp.String())
  471. valid = true
  472. } else if n.Grok.RegexpValue != "" {
  473. if strings.HasSuffix(n.Grok.RegexpValue, "\n") {
  474. n.Logger.Debugf("Beware, pattern ends with \\n : '%s'", n.Grok.RegexpValue)
  475. }
  476. n.Grok.RunTimeRegexp, err = pctx.Grok.Compile(n.Grok.RegexpValue)
  477. if err != nil {
  478. return fmt.Errorf("failed to compile grok '%s': %v", n.Grok.RegexpValue, err)
  479. }
  480. if n.Grok.RunTimeRegexp == nil {
  481. // We shouldn't be here because compilation succeeded, so regexp shouldn't be nil
  482. return fmt.Errorf("grok compilation failure: %s", n.Grok.RegexpValue)
  483. }
  484. n.Logger.Tracef("%s regexp : %s", n.Grok.RegexpValue, n.Grok.RunTimeRegexp.String())
  485. valid = true
  486. }
  487. /*if grok source is an expression*/
  488. if n.Grok.ExpValue != "" {
  489. n.Grok.RunTimeValue, err = expr.Compile(n.Grok.ExpValue,
  490. exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  491. if err != nil {
  492. return fmt.Errorf("while compiling grok's expression: %w", err)
  493. }
  494. }
  495. /* load grok statics */
  496. //compile expr statics if present
  497. for idx := range n.Grok.Statics {
  498. if n.Grok.Statics[idx].ExpValue != "" {
  499. n.Grok.Statics[idx].RunTimeValue, err = expr.Compile(n.Grok.Statics[idx].ExpValue,
  500. exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  501. if err != nil {
  502. return err
  503. }
  504. }
  505. valid = true
  506. }
  507. /* load data capture (stash) */
  508. for i, stash := range n.Stash {
  509. n.Stash[i].ValueExpression, err = expr.Compile(stash.Value,
  510. exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  511. if err != nil {
  512. return fmt.Errorf("while compiling stash value expression: %w", err)
  513. }
  514. n.Stash[i].KeyExpression, err = expr.Compile(stash.Key,
  515. exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  516. if err != nil {
  517. return fmt.Errorf("while compiling stash key expression: %w", err)
  518. }
  519. n.Stash[i].TTLVal, err = time.ParseDuration(stash.TTL)
  520. if err != nil {
  521. return fmt.Errorf("while parsing stash ttl: %w", err)
  522. }
  523. logLvl := n.Logger.Logger.GetLevel()
  524. //init the cache, does it make sense to create it here just to be sure everything is fine ?
  525. if err = cache.CacheInit(cache.CacheCfg{
  526. Size: n.Stash[i].MaxMapSize,
  527. TTL: n.Stash[i].TTLVal,
  528. Name: n.Stash[i].Name,
  529. Strategy: n.Stash[i].Strategy,
  530. LogLevel: &logLvl,
  531. }); err != nil {
  532. return fmt.Errorf("while initializing cache: %w", err)
  533. }
  534. }
  535. /* compile leafs if present */
  536. for idx := range n.LeavesNodes {
  537. if n.LeavesNodes[idx].Name == "" {
  538. n.LeavesNodes[idx].Name = fmt.Sprintf("child-%s", n.Name)
  539. }
  540. /*propagate debug/stats to child nodes*/
  541. if !n.LeavesNodes[idx].Debug && n.Debug {
  542. n.LeavesNodes[idx].Debug = true
  543. }
  544. if !n.LeavesNodes[idx].Profiling && n.Profiling {
  545. n.LeavesNodes[idx].Profiling = true
  546. }
  547. n.LeavesNodes[idx].Stage = n.Stage
  548. err = n.LeavesNodes[idx].compile(pctx, ectx)
  549. if err != nil {
  550. return err
  551. }
  552. valid = true
  553. }
  554. /* load statics if present */
  555. for idx := range n.Statics {
  556. if n.Statics[idx].ExpValue != "" {
  557. n.Statics[idx].RunTimeValue, err = expr.Compile(n.Statics[idx].ExpValue, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  558. if err != nil {
  559. n.Logger.Errorf("Statics Compilation failed %v.", err)
  560. return err
  561. }
  562. }
  563. valid = true
  564. }
  565. /* compile whitelists if present */
  566. for _, v := range n.Whitelist.Ips {
  567. n.Whitelist.B_Ips = append(n.Whitelist.B_Ips, net.ParseIP(v))
  568. n.Logger.Debugf("adding ip %s to whitelists", net.ParseIP(v))
  569. valid = true
  570. }
  571. for _, v := range n.Whitelist.Cidrs {
  572. _, tnet, err := net.ParseCIDR(v)
  573. if err != nil {
  574. n.Logger.Fatalf("Unable to parse cidr whitelist '%s' : %v.", v, err)
  575. }
  576. n.Whitelist.B_Cidrs = append(n.Whitelist.B_Cidrs, tnet)
  577. n.Logger.Debugf("adding cidr %s to whitelists", tnet)
  578. valid = true
  579. }
  580. for _, filter := range n.Whitelist.Exprs {
  581. expression := &ExprWhitelist{}
  582. expression.Filter, err = expr.Compile(filter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  583. if err != nil {
  584. n.Logger.Fatalf("Unable to compile whitelist expression '%s' : %v.", filter, err)
  585. }
  586. expression.ExprDebugger, err = exprhelpers.NewDebugger(filter, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
  587. if err != nil {
  588. log.Errorf("unable to build debug filter for '%s' : %s", filter, err)
  589. }
  590. n.Whitelist.B_Exprs = append(n.Whitelist.B_Exprs, expression)
  591. n.Logger.Debugf("adding expression %s to whitelists", filter)
  592. valid = true
  593. }
  594. if !valid {
  595. /* node is empty, error force return */
  596. n.Logger.Error("Node is empty or invalid, abort")
  597. n.Stage = ""
  598. return fmt.Errorf("Node is empty")
  599. }
  600. if err := n.validate(pctx, ectx); err != nil {
  601. return err
  602. }
  603. return nil
  604. }