-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
268 lines (235 loc) · 6.99 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
/*************************************************************************
* 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/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingest/log"
"github.com/gravwell/gravwell/v3/ingesters/base"
"github.com/gravwell/gravwell/v3/ingesters/utils"
"github.com/gravwell/gravwell/v3/timegrinder"
"github.com/gravwell/o365"
)
const (
defaultConfigLoc = `/opt/gravwell/etc/o365_ingest.conf`
defaultConfigDLoc = `/opt/gravwell/etc/o365_ingest.conf.d`
appName string = `o365`
ingesterName string = "Office 365"
)
var (
lg *log.Logger
debugOn bool
tracker *stateTracker
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: ingesterName,
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
// get the src we'll attach to entries
var src net.IP
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")
}
}
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("Successfully connected to ingesters\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()
var wg sync.WaitGroup
// Instantiate the client
ocfg := o365.DefaultConfig
ocfg.ClientID = cfg.Global.Client_ID
ocfg.ClientSecret = cfg.Global.Client_Secret
ocfg.DirectoryID = cfg.Global.Directory_ID
ocfg.TenantDomain = cfg.Global.Tenant_Domain
ocfg.ContentTypes = cfg.ContentTypes()
ocfg.PlanName = cfg.Global.Plan_Name
o, err := o365.New(ocfg)
if err != nil {
lg.FatalCode(0, "Failed to get new client", log.KVErr(err))
}
// Make sure there are subscriptions for all our requested content types
err = o.EnableSubscriptions()
if err != nil {
lg.FatalCode(0, "Failed to enable subscriptions", log.KVErr(err))
}
ctx, cancel := context.WithCancel(context.Background())
// For each content type we're interested in, launch a
// goroutine to read entries from Office 365 maintenance API
running := true
for k, v := range cfg.ContentType {
//get timegrinder stood up
tcfg := timegrinder.Config{
EnableLeftMostSeed: true,
}
tgr, err := timegrinder.NewTimeGrinder(tcfg)
if err != nil {
lg.FatalCode(0, "failed to create timegrinder", log.KVErr(err))
return
} else if err := cfg.TimeFormat.LoadFormats(tgr); err != nil {
lg.FatalCode(0, "failed to set load custom time formats", log.KVErr(err))
return
}
if v.Assume_Local_Timezone {
tgr.SetLocalTime()
}
if v.Timezone_Override != `` {
if err = tgr.SetTimezone(v.Timezone_Override); err != nil {
lg.FatalCode(0, "failed to set timezone", log.KV("timezone", v.Timezone_Override), log.KVErr(err))
return
}
}
go func(name string, ct contentType, tg *timegrinder.TimeGrinder) {
debugout("Started reader for content type %v\n", ct.Content_Type)
wg.Add(1)
defer wg.Done()
// 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.KV("handler", name), log.KVErr(err))
}
procset, err := cfg.Preprocessor.ProcessorSet(igst, ct.Preprocessor)
if err != nil {
lg.Fatal("preprocessor failure", log.KVErr(err))
}
// we'll do a sliding window, they warn it can take a long time for some logs to show up
for running {
end := time.Now()
start := end.Add(-24 * time.Hour)
content, err := o.ListAvailableContent(ct.Content_Type, start, end)
if err != nil {
lg.Error("failed to list content type", log.KV("contenttype", ct.Content_Type), log.KVErr(err))
time.Sleep(10 * time.Second)
continue
}
var uri, contentId string
var ok bool
var ent *entry.Entry
var events []json.RawMessage
var eventUnpacked event
for _, item := range content {
contentId, ok = item["contentId"]
if !ok {
continue
}
// CHECK IF ALREADY SEEN
if tracker.IdExists(contentId) {
continue
}
debugout("extracting %v\n", contentId)
uri, ok = item["contentUri"]
if !ok {
continue
}
result, err := o.GetContent(uri)
if err != nil {
continue
}
// Dumb fact: each item may have multiple events
err = json.Unmarshal(result, &events)
if err != nil {
continue
}
for _, evt := range events {
err = json.Unmarshal(evt, &eventUnpacked)
if err != nil {
continue
}
if tracker.IdExists(eventUnpacked.Id) {
continue
}
ent = &entry.Entry{
Data: []byte(evt),
Tag: tag,
SRC: src,
}
if ct.Ignore_Timestamps {
ent.TS = entry.Now()
} else {
ts, ok, err := tg.Extract(ent.Data)
if !ok || err != nil {
// something went wrong, switch to using the current time
ct.Ignore_Timestamps = true
ent.TS = entry.Now()
} else {
ent.TS = entry.FromStandard(ts)
}
}
// now write the entry
if err := procset.ProcessContext(ent, ctx); err != nil {
lg.Warn("failed to handle entry", log.KVErr(err))
}
// Add the Id to the temporary map
tracker.RecordId(eventUnpacked.Id, time.Now())
}
// Add the contentId to the temporary map
tracker.RecordId(contentId, time.Now())
}
time.Sleep(5 * time.Second)
}
if err := procset.Close(); err != nil {
lg.Error("failed to close processor set", log.KVErr(err))
}
}(k, *v, tgr)
}
//register quit signals so we can die gracefully
utils.WaitForQuit()
ib.AnnounceShutdown()
go func() {
time.Sleep(time.Second)
cancel()
}()
running = false
wg.Wait()
// Write the final state info
tracker.Close()
}
func debugout(format string, args ...interface{}) {
if debugOn {
fmt.Printf(format, args...)
}
}