-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChirpForwarder.go
277 lines (259 loc) · 8.56 KB
/
ChirpForwarder.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
package canarytools
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"os"
"os/user"
"path"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v7"
log "github.com/sirupsen/logrus"
)
// ChirpForwarder is the main struct of the forwarder
// it contains the configurations, and various components of the forwarder
type ChirpForwarder struct {
// configs
cfg ChirpForwarderConfig
// Work chans
incidentsChan chan Incident
filteredIncidentsChan chan Incident
outChan chan []byte
incidentAckerChan chan []byte
// interfaces
feeder Feeder
incidentAcker IncidentAcker
filter Filter
mapper Mapper
forwarder Forwarder
// logger
l *log.Logger
}
// NewChirpForwarder creates a new chirp forwarder
func NewChirpForwarder(cfg ChirpForwarderConfig, l *log.Logger) (cf *ChirpForwarder, err error) {
cf = &ChirpForwarder{}
cf.cfg = cfg
// create work chans
cf.incidentsChan = make(chan Incident)
cf.filteredIncidentsChan = make(chan Incident)
cf.outChan = make(chan []byte)
cf.incidentAckerChan = make(chan []byte)
// set logger
cf.l = l
return
}
func (cf *ChirpForwarder) setFeeder() {
var err error
switch cf.cfg.FeederModule {
case "consoleapi":
// did you specify both token file && manually using apikey+domain?
if cf.cfg.ImConsoleTokenFile != "" && (cf.cfg.ImConsoleAPIDomain != "" || cf.cfg.ImConsoleAPIKey != "") {
cf.l.Fatal("look, you either use 'tokenfile' or 'apikey+domain', not both")
}
// so, what if token file is not specfied, but neither apikey+domain?
// we'll look for the "canarytools.config" file in user's home directory
if cf.cfg.ImConsoleTokenFile == "" && cf.cfg.ImConsoleAPIDomain == "" && cf.cfg.ImConsoleAPIKey == "" {
cf.l.Warn("none of 'tokenfile', 'apikey' & 'domain' has been provided! will look for 'canarytools.config' file in user's home directory")
u, err := user.Current()
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error getting current user")
}
cf.cfg.ImConsoleTokenFile = path.Join(u.HomeDir, "canarytools.config")
cf.l.WithField("path", cf.cfg.ImConsoleTokenFile).Warn("automatically looking for canarytools.config")
if _, err := os.Stat(cf.cfg.ImConsoleTokenFile); os.IsNotExist(err) {
cf.l.Fatal("couldn't get apikey+domain! provide using environment variables, command line flags, or path to token file")
}
}
// tokenfile specified? get values from there
if cf.cfg.ImConsoleTokenFile != "" {
cf.cfg.ImConsoleAPIKey, cf.cfg.ImConsoleAPIDomain, err = LoadTokenFile(cf.cfg.ImConsoleTokenFile)
if err != nil || cf.cfg.ImConsoleAPIDomain == "" || cf.cfg.ImConsoleAPIKey == "" {
cf.l.WithFields(log.Fields{
"err": err,
"api": cf.cfg.ImConsoleAPIKey,
"domain": cf.cfg.ImConsoleAPIDomain,
}).Fatal("error parsing token file")
}
cf.l.WithFields(log.Fields{
"path": cf.cfg.ImConsoleTokenFile,
"api": cf.cfg.ImConsoleAPIKey,
"domain": cf.cfg.ImConsoleAPIDomain,
}).Info("successfully parsed token file, using values from there")
}
// few checks
if len(cf.cfg.ImConsoleAPIKey) != 32 {
cf.l.Fatal("invalid API Key (length != 32)")
}
if cf.cfg.ImConsoleAPIDomain == "" {
cf.l.Fatal("domain must be provided")
}
////////////////////
// start...
cf.l.WithFields(log.Fields{
"domain": cf.cfg.ImConsoleAPIDomain,
"cf.cfg.ImConsoleAPIKey": (cf.cfg.ImConsoleAPIKey)[0:4] + "..." + (cf.cfg.ImConsoleAPIKey)[len(cf.cfg.ImConsoleAPIKey)-4:len(cf.cfg.ImConsoleAPIKey)],
}).Info("ChirpForwarder Configs")
// building a new clint, testing connection...
cf.l.Debug("building new client and pinging console")
c, err := NewConsoleAPIFeeder(cf.cfg.ImConsoleAPIDomain, cf.cfg.ImConsoleAPIKey, cf.cfg.ThenWhat, cf.cfg.SinceWhenString, cf.cfg.WhichIncidents, cf.cfg.ImConsoleAPIFetchInterval, cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error during creating client, or pinging console")
}
cf.l.Debug("ping successful! we're good to go")
cf.feeder = c
cf.incidentAcker = c
default:
cf.l.WithField("feeder", cf.cfg.FeederModule).Fatal("unsupported feeder module specified")
}
}
func (cf *ChirpForwarder) setFilter() {
var err error
switch cf.cfg.IncidentFilter {
case "none":
cf.filter, err = NewFilterNone(cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error creating None filter")
}
case "dropevents":
cf.filter, err = NewFilterDropEvents(cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error creating DropEvents filter")
}
default:
cf.l.WithFields(log.Fields{
"filter": cf.cfg.IncidentFilter,
}).Fatal("unsupported filter")
}
}
func (cf *ChirpForwarder) setTLSConfig() {
var tlsConfig = &tls.Config{}
if cf.cfg.SSLUseSSL {
// ignore cert verification errors?
tlsConfig.InsecureSkipVerify = cf.cfg.SSLSkipInsecure
// custom CA?
if cf.cfg.SSLCA != "" {
// Get the SystemCertPool, continue with an empty pool on error
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
// Read in the cert file
certs, err := ioutil.ReadFile(cf.cfg.SSLCA)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
"cafile": cf.cfg.SSLCA,
}).Fatal("Failed to read CA file")
}
// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
cf.l.Fatal("couldn't add CA cert! (file might be improperly formatted)")
}
tlsConfig.RootCAs = rootCAs
}
// custom key + cert?
if cf.cfg.SSLKey != "" && cf.cfg.SSLCert != "" {
// Load client cert
clientCert, err := tls.LoadX509KeyPair(cf.cfg.SSLCert, cf.cfg.SSLKey)
if err != nil {
cf.l.Fatal(err)
}
tlsConfig.Certificates = []tls.Certificate{clientCert}
}
}
cf.cfg.TLSConfig = tlsConfig
}
func (cf *ChirpForwarder) setForwarder() {
switch cf.cfg.ForwarderModule {
case "tcp":
// bulding new TCP out
t, err := NewTCPForwarder(cf.cfg.OmTCPUDPHost, cf.cfg.OmTCPUDPPort, cf.cfg.TLSConfig, cf.cfg.SSLUseSSL, cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error during creating TCP Out client")
}
cf.forwarder = t
case "file":
// bulding new file out
ff, err := NewFileForwader(cf.cfg.OmFileName, cf.cfg.OmFileMaxSize, cf.cfg.OmFileMaxBackups, cf.cfg.OmFileMaxAge, cf.cfg.OmFileCompress, cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error during creating File Out client")
}
cf.forwarder = ff
case "elastic":
// bulding new elastic out
cfg := elasticsearch.Config{
Addresses: []string{cf.cfg.OmElasticHost}, // A list of Elasticsearch nodes to use.
Username: cf.cfg.OmElasticUser, // Username for HTTP Basic Authentication.
Password: cf.cfg.OmElasticPass, // Password for HTTP Basic Authentication.
CloudID: cf.cfg.OmElasticCloudID,
APIKey: cf.cfg.OmElasticCloudAPIKey,
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Duration(10) * time.Second,
TLSClientConfig: cf.cfg.TLSConfig,
},
}
ef, err := NewElasticForwarder(cfg, cf.cfg.OmElasticIndex, cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error during creating Elastic Out client")
}
cf.forwarder = ef
case "kafka":
// bulding new kafka out
if cf.cfg.OmKafkaTopic == "" || cf.cfg.OmKafkaBrokers == "" {
cf.l.Fatal("missing kafka brokers or topic")
}
brokers := strings.Split(cf.cfg.OmKafkaBrokers, ";")
var kf = &KafkaForwarder{}
if cf.cfg.SSLUseSSL {
kf, _ = NewKafkaForwarder(brokers, cf.cfg.OmKafkaTopic, cf.cfg.TLSConfig, cf.l)
} else {
kf, _ = NewKafkaForwarder(brokers, cf.cfg.OmKafkaTopic, nil, cf.l)
}
cf.forwarder = kf
case "":
cf.l.Fatal("you have to provide an output module! ('-output' flag, or CANARY_OUTPUT env variable)")
default:
cf.l.Fatal("invalid output module specified!")
}
}
func (cf *ChirpForwarder) setMapper() {
var err error
// only JSON mapper is implemented
cf.mapper, err = NewMapperJSON(false, cf.l)
if err != nil {
cf.l.WithFields(log.Fields{
"err": err,
}).Fatal("error creating JON Mapper")
}
}
// Run starts forwarding incidents
func (cf *ChirpForwarder) Run() {
cf.setFeeder()
cf.setFilter()
cf.setTLSConfig()
cf.setMapper()
cf.setForwarder()
// All good, let's roll...
go cf.feeder.Feed(cf.incidentsChan)
go cf.incidentAcker.AckIncidents(cf.incidentAckerChan)
go cf.filter.Filter(cf.incidentsChan, cf.filteredIncidentsChan)
go cf.mapper.Map(cf.filteredIncidentsChan, cf.outChan)
cf.forwarder.Forward(cf.outChan, cf.incidentAckerChan)
}