MCPcopy
hub / github.com/crowdsecurity/crowdsec / NewAlertContext

Function NewAlertContext

pkg/alertcontext/alertcontext.go:46–90  ·  view source on GitHub ↗
(contextToSend map[string][]string, valueLength int)

Source from the content-addressed store, hash-verified

44}
45
46func NewAlertContext(contextToSend map[string][]string, valueLength int) error {
47 if valueLength == 0 {
48 log.Debugf("No console context value length provided, using default: %d", MaxContextValueLen)
49 valueLength = MaxContextValueLen
50 }
51
52 if valueLength > MaxContextValueLen {
53 log.Debugf("Provided console context value length (%d) is higher than the maximum, using default: %d", valueLength, MaxContextValueLen)
54 valueLength = MaxContextValueLen
55 }
56
57 ac := Context{
58 ContextToSend: contextToSend,
59 ContextValueLen: valueLength,
60 ContextToSendCompiled: make(map[string][]*vm.Program),
61 }
62
63 for key, values := range contextToSend {
64 if _, ok := ac.ContextToSend[key]; !ok {
65 ac.ContextToSend[key] = make([]string, 0)
66 }
67
68 if _, ok := ac.ContextToSendCompiled[key]; !ok {
69 ac.ContextToSendCompiled[key] = make([]*vm.Program, 0)
70 }
71
72 for _, value := range values {
73 valueCompiled, err := expr.Compile(value, exprhelpers.GetExprOptions(map[string]any{
74 "evt": &pipeline.Event{},
75 "match": &pipeline.MatchedRule{},
76 "req": &http.Request{},
77 })...)
78 if err != nil {
79 return fmt.Errorf("compilation of '%s' context value failed: %w", value, err)
80 }
81
82 ac.ContextToSendCompiled[key] = append(ac.ContextToSendCompiled[key], valueCompiled)
83 ac.ContextToSend[key] = append(ac.ContextToSend[key], value)
84 }
85 }
86
87 alertContext.Store(&ac)
88
89 return nil
90}
91
92func getAlertContext() *Context {
93 if ac := alertContext.Load(); ac != nil {

Callers 5

LoadBucketsFunction · 0.92
TestNewAlertContextFunction · 0.85
TestEventToContextFunction · 0.85
TestAppsecEventToContextFunction · 0.85

Calls 2

GetExprOptionsFunction · 0.92
CompileMethod · 0.45

Tested by 4

TestNewAlertContextFunction · 0.68
TestEventToContextFunction · 0.68
TestAppsecEventToContextFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…