Skip to content

Commit dee4f55

Browse files
authored
Merge pull request #90 from DCSO/alertify-add-fields
Ensure that alertified events also contain added fields
2 parents dfbc596 + 5cea4fa commit dee4f55

File tree

6 files changed

+110
-25
lines changed

6 files changed

+110
-25
lines changed

cmd/fever/cmds/alertify.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ func alertify(cmd *cobra.Command, args []string) {
140140
limit := viper.GetUint("alert-limit")
141141
extrakey := viper.GetString("extra-key")
142142

143+
addFields := viper.GetStringMapString("add-fields")
143144
a := makeAlertifyAlertifier(prefix, extrakey)
145+
if err := a.SetAddedFields(addFields); err != nil {
146+
log.Fatal(err)
147+
}
144148
for e := range eventChan {
145149
err := emitAlertsForEvent(a, e, ioc, os.Stdout, uint64(limit))
146150
if err != nil {

db/slurper_ejdb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build ignore
12
// +build ignore
23

34
package db

processing/forward_handler.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package processing
55

66
import (
77
"crypto/tls"
8-
"fmt"
98
"sync"
109
"time"
1110

@@ -103,31 +102,11 @@ func (fh *ForwardHandler) EnableRDNS(expiryPeriod time.Duration) {
103102
// AddFields enables the addition of a custom set of top-level fields to the
104103
// forwarded JSON.
105104
func (fh *ForwardHandler) AddFields(fields map[string]string) error {
106-
j := ""
107-
// We preprocess the JSON to be able to only use fast string operations
108-
// later. This code progressively builds a JSON snippet by adding JSON
109-
// key-value pairs for each added field, e.g. `, "foo":"bar"`.
110-
for k, v := range fields {
111-
// Escape the fields to make sure we do not mess up the JSON when
112-
// encountering weird symbols in field names or values.
113-
kval, err := util.EscapeJSON(k)
114-
if err != nil {
115-
fh.Logger.Warningf("cannot escape value: %s", v)
116-
return err
117-
}
118-
vval, err := util.EscapeJSON(v)
119-
if err != nil {
120-
fh.Logger.Warningf("cannot escape value: %s", v)
121-
return err
122-
}
123-
j += fmt.Sprintf(",%s:%s", kval, vval)
105+
addedFields, err := util.PreprocessAddedFields(fields)
106+
if err != nil {
107+
return err
124108
}
125-
// We finish the list of key-value pairs with a final brace:
126-
// `, "foo":"bar"}`. This string can now just replace the final brace in a
127-
// given JSON string. If there were no added fields, we just leave the
128-
// output at the final brace.
129-
j += "}"
130-
fh.AddedFields = j
109+
fh.AddedFields = addedFields
131110
return nil
132111
}
133112

util/add_fields_preprocess.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
// PreprocessAddedFields preprocesses the added fields to be able to only use
10+
// fast string operations to add them to JSON text later. This code
11+
// progressively builds a JSON snippet by adding JSON key-value pairs for each
12+
// added field, e.g. `, "foo":"bar"`.
13+
func PreprocessAddedFields(fields map[string]string) (string, error) {
14+
j := ""
15+
for k, v := range fields {
16+
// Escape the fields to make sure we do not mess up the JSON when
17+
// encountering weird symbols in field names or values.
18+
kval, err := EscapeJSON(k)
19+
if err != nil {
20+
log.Warningf("cannot escape value: %s", v)
21+
return "", err
22+
}
23+
vval, err := EscapeJSON(v)
24+
if err != nil {
25+
log.Warningf("cannot escape value: %s", v)
26+
return "", err
27+
}
28+
j += fmt.Sprintf(",%s:%s", kval, vval)
29+
}
30+
// We finish the list of key-value pairs with a final brace:
31+
// `, "foo":"bar"}`. This string can now just replace the final brace in a
32+
// given JSON string. If there were no added fields, we just leave the
33+
// output at the final brace.
34+
j += "}"
35+
return j, nil
36+
}

util/add_fields_preprocess_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package util
2+
3+
import "testing"
4+
5+
func TestPreprocessAddedFields(t *testing.T) {
6+
type args struct {
7+
fields map[string]string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want string
13+
wantErr bool
14+
}{
15+
{
16+
name: "empty fieldset",
17+
args: args{
18+
fields: map[string]string{},
19+
},
20+
want: "}",
21+
},
22+
{
23+
name: "fieldset present",
24+
args: args{
25+
fields: map[string]string{
26+
"foo": "bar",
27+
"baz": "quux",
28+
},
29+
},
30+
want: `,"foo":"bar","baz":"quux"}`,
31+
},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
got, err := PreprocessAddedFields(tt.args.fields)
36+
if (err != nil) != tt.wantErr {
37+
t.Errorf("PreprocessAddedFields() error = %v, wantErr %v", err, tt.wantErr)
38+
return
39+
}
40+
if got != tt.want {
41+
t.Errorf("PreprocessAddedFields() = %v, want %v", got, tt.want)
42+
}
43+
})
44+
}
45+
}

util/alertifier.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type AlertJSONProvider interface {
3232
type Alertifier struct {
3333
alertPrefix string
3434
extraModifier ExtraModifier
35+
addedFields string
3536
matchTypes map[string]AlertJSONProvider
3637
}
3738

@@ -66,6 +67,17 @@ func (a *Alertifier) SetExtraModifier(em ExtraModifier) {
6667
a.extraModifier = em
6768
}
6869

70+
// SetAddedFields adds string key-value pairs to be added as extra JSON
71+
// values.
72+
func (a *Alertifier) SetAddedFields(fields map[string]string) error {
73+
af, err := PreprocessAddedFields(fields)
74+
if err != nil {
75+
return err
76+
}
77+
a.addedFields = af
78+
return nil
79+
}
80+
6981
// MakeAlert generates a new Entry representing an `alert` event based on the
7082
// given input metadata event. It uses the information from the Alertifier as
7183
// well as the given IoC to craft an `alert` sub-object in the resulting
@@ -141,6 +153,14 @@ func (a *Alertifier) MakeAlert(inputEvent types.Entry, ioc string,
141153
if err != nil {
142154
return nil, err
143155
}
156+
// Append added fields string, if present
157+
if len(a.addedFields) > 1 {
158+
j := l
159+
jlen := len(j)
160+
j = j[:jlen-1]
161+
j = append(j, a.addedFields...)
162+
l = j
163+
}
144164
// update returned entry
145165
newEntry.Timestamp = eventTimestampFormatted
146166
newEntry.JSONLine = string(l)

0 commit comments

Comments
 (0)