-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
380 lines (333 loc) · 9.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*************************************************************************
* Copyright 2019 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"os"
"sync"
"time"
"github.com/gravwell/gravwell/v4/ingest"
"github.com/gravwell/gravwell/v4/ingest/entry"
"github.com/gravwell/gravwell/v4/ingest/log"
"github.com/gravwell/gravwell/v4/ingest/processors"
"github.com/gravwell/gravwell/v4/ingesters/base"
"github.com/gravwell/gravwell/v4/ingesters/utils"
"github.com/gravwell/gravwell/v4/timegrinder"
"github.com/open-networks/go-msgraph"
)
const (
defaultConfigLoc = `/opt/gravwell/etc/msgraph_ingest.conf`
defaultConfigDLoc = `/opt/gravwell/etc/msgraph_ingest.conf.d`
appName = `msgraph`
)
var (
lg *log.Logger
debugOn bool
tracker *stateTracker
running bool
src net.IP
ErrInvalidStateFile = errors.New("State file exists and is not a regular file")
ErrFailedSeek = errors.New("Failed to seek to the start of the states file")
)
type event struct {
Id string
}
func main() {
var cfg *cfgType
ibc := base.IngesterBaseConfig{
IngesterName: appName,
AppName: appName,
DefaultConfigLocation: defaultConfigLoc,
DefaultConfigOverlayLocation: defaultConfigDLoc,
GetConfigFunc: GetConfig,
}
ib, err := base.Init(ibc)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get configuration %v\n", err)
return
} else if err = ib.AssignConfig(&cfg); err != nil || cfg == nil {
fmt.Fprintf(os.Stderr, "failed to assign configuration %v %v\n", err, cfg == nil)
return
}
debugOn = ib.Verbose
lg = ib.Logger
igst, err := ib.GetMuxer()
if err != nil {
ib.Logger.FatalCode(0, "failed to get ingest connection", log.KVErr(err))
return
}
defer igst.Close()
ib.AnnounceStartup()
debugout("Started ingester muxer\n")
tracker, err = NewTracker(cfg.Global.State_Store_Location, 48*time.Hour, igst)
if err != nil {
lg.Fatal("failed to initialize state file", log.KVErr(err))
}
tracker.Start()
// get the src we'll attach to entries
if cfg.Global.Source_Override != `` {
// global override
src = net.ParseIP(cfg.Global.Source_Override)
if src == nil {
lg.FatalCode(0, "Global Source-Override is invalid")
}
}
var wg sync.WaitGroup
// Instantiate the client
graphClient, err := msgraph.NewGraphClient(cfg.Global.Tenant_Domain, cfg.Global.Client_ID, cfg.Global.Client_Secret)
if err != nil {
lg.FatalCode(0, "Failed to get new client", log.KVErr(err))
}
ctx, cancel := context.WithCancel(context.Background())
// For each content type we're interested in, launch a
// goroutine to read entries from the Graph API
running = true
for k, ct := range cfg.ContentType {
// figure out which tag we're using
tag, err := igst.GetTag(ct.Tag_Name)
if err != nil {
lg.Fatal("failed to resolve tag", log.KV("tag", ct.Tag_Name), log.KVErr(err))
}
procset, err := cfg.Preprocessor.ProcessorSet(igst, ct.Preprocessor)
if err != nil {
lg.Fatal("preprocessor failure", log.KVErr(err))
}
// set up time extraction rules
tcfg := timegrinder.Config{
EnableLeftMostSeed: true,
}
tg, err := timegrinder.NewTimeGrinder(tcfg)
if err != nil {
ct.Ignore_Timestamps = true
} else if err = cfg.TimeFormat.LoadFormats(tg); err != nil {
lg.FatalCode(0, "failed to set load custom time formats", log.KVErr(err))
return
}
if ct.Assume_Local_Timezone {
tg.SetLocalTime()
}
if ct.Timezone_Override != `` {
if err = tg.SetTimezone(ct.Timezone_Override); err != nil {
lg.FatalCode(0, "failed to set timezone", log.KV("timezone", ct.Timezone_Override), log.KVErr(err))
}
}
// build the config
rcfg := routineCfg{
name: k,
ct: ct,
igst: igst,
wg: &wg,
cfg: cfg,
graphClient: graphClient,
ctx: ctx,
tg: tg,
procset: procset,
tag: tag,
}
switch ct.Content_Type {
case "alerts":
go alertRoutine(rcfg)
case "secureScores":
go secureScoreRoutine(rcfg)
case "controlProfiles":
go secureScoreProfileRoutine(rcfg)
}
}
//register quit signals so we can die gracefully
utils.WaitForQuit()
ib.AnnounceShutdown()
go func() {
time.Sleep(2 * time.Second)
cancel()
}()
running = false
wg.Wait()
// Write the final state info
tracker.Close()
}
type routineCfg struct {
name string
ct *contentType
igst *ingest.IngestMuxer
wg *sync.WaitGroup
cfg *cfgType
graphClient *msgraph.GraphClient
ctx context.Context
tg *timegrinder.TimeGrinder
procset *processors.ProcessorSet
tag entry.EntryTag
}
func alertRoutine(c routineCfg) {
lg.Info("started reader for content type", log.KV("contenttype", c.ct.Content_Type))
c.wg.Add(1)
defer c.wg.Done()
for running {
debugout("Querying alerts\n")
alerts, err := c.graphClient.ListAlerts()
if err != nil {
lg.Error("failed to list alerts", log.KVErr(err))
time.Sleep(10 * time.Second)
continue
}
var ent *entry.Entry
// Attempt to ingest each alert
for _, item := range alerts {
// CHECK IF ALREADY SEEN
if tracker.IdExists(item.ID) {
debugout("skipping already-seen alert %v\n", item.ID)
continue
}
debugout("extracting %v\n", item.ID)
// Now re-pack this as json
packed, err := json.Marshal(item)
if err != nil {
lg.Warn("failed to re-pack entry", log.KV("id", item.ID), log.KVErr(err))
continue
}
ent = &entry.Entry{
Data: packed,
Tag: c.tag,
SRC: src,
}
if c.ct.Ignore_Timestamps {
ent.TS = entry.Now()
} else {
ent.TS = entry.FromStandard(item.CreatedDateTime)
}
// now write the entry
if err := c.procset.ProcessContext(ent, c.ctx); err != nil {
lg.Warn("failed to handle entry", log.KVErr(err))
}
// Mark down this alert as ingested
tracker.RecordId(item.ID, time.Now())
}
// Here's how we shut down quickly
for i := 0; i < 30; i++ {
if !running {
break
}
time.Sleep(time.Second)
}
}
if err := c.procset.Close(); err != nil {
lg.Error("failed to close processor set", log.KVErr(err))
}
}
func secureScoreRoutine(c routineCfg) {
lg.Info("started reader for content type", log.KV("contenttype", c.ct.Content_Type))
c.wg.Add(1)
defer c.wg.Done()
for running {
debugout("Querying secure scores\n")
scores, err := c.graphClient.ListSecureScores()
if err != nil {
lg.Error("failed to list secure scores", log.KVErr(err))
time.Sleep(10 * time.Second)
continue
}
var ent *entry.Entry
// Attempt to ingest each score
for _, item := range scores {
// CHECK IF ALREADY SEEN
if tracker.IdExists(item.ID) {
debugout("skipping already-seen score %v\n", item.ID)
continue
}
debugout("extracting %v\n", item.ID)
// Now re-pack this as json
packed, err := json.Marshal(item)
if err != nil {
lg.Warn("failed to re-pack secure score entry", log.KV("id", item.ID), log.KVErr(err))
continue
}
ent = &entry.Entry{
Data: packed,
Tag: c.tag,
SRC: src,
}
if c.ct.Ignore_Timestamps {
ent.TS = entry.Now()
} else {
ent.TS = entry.FromStandard(item.CreatedDateTime)
}
// now write the entry
if err := c.procset.ProcessContext(ent, c.ctx); err != nil {
lg.Warn("failed to handle entry", log.KVErr(err))
}
// Mark down this alert as ingested
tracker.RecordId(item.ID, time.Now())
}
// Here's how we shut down quickly
// Secure scores are created very infrequently, so we sleep for a long time.
for i := 0; i < 300; i++ {
if !running {
break
}
time.Sleep(time.Second)
}
}
if err := c.procset.Close(); err != nil {
lg.Error("failed to close processor set", log.KVErr(err))
}
}
func secureScoreProfileRoutine(c routineCfg) {
lg.Info("started reader for content type", log.KV("contenttype", c.ct.Content_Type))
c.wg.Add(1)
defer c.wg.Done()
for running {
debugout("Querying secure score profiles\n")
profiles, err := c.graphClient.ListSecureScoreControlProfiles()
if err != nil {
lg.Error("failed to list secure score profiles", log.KVErr(err))
time.Sleep(10 * time.Second)
continue
}
var ent *entry.Entry
// Attempt to ingest each profile
for _, item := range profiles {
debugout("extracting %v\n", item.ID)
// Re-pack this as json
packed, err := json.Marshal(item)
if err != nil {
lg.Warn("failed to re-pack secure score profile", log.KV("id", item.ID), log.KVErr(err))
continue
}
ent = &entry.Entry{
Data: packed,
Tag: c.tag,
SRC: src,
}
// They don't change very often, so always make it now
ent.TS = entry.Now()
// write the entry
if err := c.procset.ProcessContext(ent, c.ctx); err != nil {
lg.Warn("failed to handle entry", log.KVErr(err))
}
}
// Here's how we shut down quickly
// We poll the profiles every hour just so they exist in the system
for i := 0; i < 3600; i++ {
if !running {
break
}
time.Sleep(time.Second)
}
}
if err := c.procset.Close(); err != nil {
lg.Error("failed to close processor set", log.KVErr(err))
}
}
func debugout(format string, args ...interface{}) {
if debugOn {
fmt.Printf(format, args...)
}
}