forked from falcosecurity/falcosidekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
429 lines (385 loc) · 14.7 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/DataDog/datadog-go/statsd"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/falcosecurity/falcosidekick/outputs"
"github.com/falcosecurity/falcosidekick/types"
)
// Globale variables
var (
nullClient *outputs.Client
slackClient *outputs.Client
rocketchatClient *outputs.Client
mattermostClient *outputs.Client
teamsClient *outputs.Client
datadogClient *outputs.Client
discordClient *outputs.Client
alertmanagerClient *outputs.Client
elasticsearchClient *outputs.Client
influxdbClient *outputs.Client
lokiClient *outputs.Client
natsClient *outputs.Client
stanClient *outputs.Client
awsClient *outputs.Client
smtpClient *outputs.Client
opsgenieClient *outputs.Client
webhookClient *outputs.Client
cloudeventsClient *outputs.Client
azureClient *outputs.Client
gcpClient *outputs.Client
googleChatClient *outputs.Client
kafkaClient *outputs.Client
pagerdutyClient *outputs.Client
kubelessClient *outputs.Client
openfaasClient *outputs.Client
webUIClient *outputs.Client
rabbitmqClient *outputs.Client
wavefrontClient *outputs.Client
statsdClient, dogstatsdClient *statsd.Client
config *types.Configuration
stats *types.Statistics
promStats *types.PromStatistics
)
func init() {
config = getConfig()
stats = getInitStats()
promStats = getInitPromStats()
nullClient = &outputs.Client{
OutputType: "null",
Config: config,
Stats: stats,
PromStats: promStats,
StatsdClient: statsdClient,
DogstatsdClient: dogstatsdClient,
}
if config.Statsd.Forwarder != "" {
var err error
statsdClient, err = outputs.NewStatsdClient("StatsD", config, stats)
if err != nil {
config.Statsd.Forwarder = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "StatsD")
nullClient.DogstatsdClient = statsdClient
}
}
if config.Dogstatsd.Forwarder != "" {
var err error
dogstatsdClient, err = outputs.NewStatsdClient("DogStatsD", config, stats)
if err != nil {
config.Statsd.Forwarder = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "DogStatsD")
nullClient.DogstatsdClient = dogstatsdClient
}
}
if config.Slack.WebhookURL != "" {
var err error
slackClient, err = outputs.NewClient("Slack", config.Slack.WebhookURL, config.Slack.MutualTLS, config.Slack.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Slack.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Slack")
}
}
if config.Rocketchat.WebhookURL != "" {
var err error
rocketchatClient, err = outputs.NewClient("Rocketchat", config.Rocketchat.WebhookURL, config.Rocketchat.MutualTLS, config.Rocketchat.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Rocketchat.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Rocketchat")
}
}
if config.Mattermost.WebhookURL != "" {
var err error
mattermostClient, err = outputs.NewClient("Mattermost", config.Mattermost.WebhookURL, config.Mattermost.MutualTLS, config.Mattermost.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Mattermost.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Mattermost")
}
}
if config.Teams.WebhookURL != "" {
var err error
teamsClient, err = outputs.NewClient("Teams", config.Teams.WebhookURL, config.Teams.MutualTLS, config.Teams.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Teams.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Teams")
}
}
if config.Datadog.APIKey != "" {
var err error
datadogClient, err = outputs.NewClient("Datadog", config.Datadog.Host+outputs.DatadogPath+"?api_key="+config.Datadog.APIKey, config.Datadog.MutualTLS, config.Datadog.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Datadog.APIKey = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Datadog")
}
}
if config.Discord.WebhookURL != "" {
var err error
discordClient, err = outputs.NewClient("Discord", config.Discord.WebhookURL, config.Discord.MutualTLS, config.Discord.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Discord.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Discord")
}
}
if config.Alertmanager.HostPort != "" {
var err error
alertmanagerClient, err = outputs.NewClient("AlertManager", config.Alertmanager.HostPort+outputs.AlertmanagerURI, config.Alertmanager.MutualTLS, config.Alertmanager.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Alertmanager.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AlertManager")
}
}
if config.Elasticsearch.HostPort != "" {
var err error
elasticsearchClient, err = outputs.NewClient("Elasticsearch", config.Elasticsearch.HostPort+"/"+config.Elasticsearch.Index+"/"+config.Elasticsearch.Type, config.Elasticsearch.MutualTLS, config.Elasticsearch.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Elasticsearch.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Elasticsearch")
}
}
if config.Loki.HostPort != "" {
var err error
lokiClient, err = outputs.NewClient("Loki", config.Loki.HostPort+"/api/prom/push", config.Loki.MutualTLS, config.Loki.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Loki.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Loki")
}
}
if config.Nats.HostPort != "" {
var err error
natsClient, err = outputs.NewClient("NATS", config.Nats.HostPort, config.Nats.MutualTLS, config.Nats.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Nats.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "NATS")
}
}
if config.Stan.HostPort != "" && config.Stan.ClusterID != "" && config.Stan.ClientID != "" {
var err error
stanClient, err = outputs.NewClient("STAN", config.Stan.HostPort, config.Stan.MutualTLS, config.Stan.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Stan.HostPort = ""
config.Stan.ClusterID = ""
config.Stan.ClientID = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "STAN")
}
}
if config.Influxdb.HostPort != "" {
var credentials string
if config.Influxdb.User != "" && config.Influxdb.Password != "" {
credentials = "&u=" + config.Influxdb.User + "&p=" + config.Influxdb.Password
}
var err error
influxdbClient, err = outputs.NewClient("Influxdb", config.Influxdb.HostPort+"/write?db="+config.Influxdb.Database+credentials, config.Influxdb.MutualTLS, config.Influxdb.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Influxdb.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Influxdb")
}
}
if config.AWS.Lambda.FunctionName != "" || config.AWS.SQS.URL != "" ||
config.AWS.SNS.TopicArn != "" || config.AWS.CloudWatchLogs.LogGroup != "" || config.AWS.S3.Bucket != "" {
var err error
awsClient, err = outputs.NewAWSClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.AWS.AccessKeyID = ""
config.AWS.SecretAccessKey = ""
config.AWS.Region = ""
config.AWS.Lambda.FunctionName = ""
config.AWS.SQS.URL = ""
config.AWS.S3.Bucket = ""
config.AWS.SNS.TopicArn = ""
config.AWS.CloudWatchLogs.LogGroup = ""
config.AWS.CloudWatchLogs.LogStream = ""
} else {
if config.AWS.Lambda.FunctionName != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AWSLambda")
}
if config.AWS.SQS.URL != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AWSSQS")
}
if config.AWS.SNS.TopicArn != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AWSSNS")
}
if config.AWS.CloudWatchLogs.LogGroup != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AWSCloudWatchLogs")
}
if config.AWS.S3.Bucket != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "AWSS3")
}
}
}
if config.SMTP.HostPort != "" && config.SMTP.From != "" && config.SMTP.To != "" {
var err error
smtpClient, err = outputs.NewSMTPClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.SMTP.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "SMTP")
}
}
if config.Opsgenie.APIKey != "" {
var err error
url := "https://api.opsgenie.com/v2/alerts"
if strings.ToLower(config.Opsgenie.Region) == "eu" {
url = "https://api.eu.opsgenie.com/v2/alerts"
}
opsgenieClient, err = outputs.NewClient("Opsgenie", url, config.Opsgenie.MutualTLS, config.Opsgenie.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Opsgenie.APIKey = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Opsgenie")
}
}
if config.Webhook.Address != "" {
var err error
webhookClient, err = outputs.NewClient("Webhook", config.Webhook.Address, config.Webhook.MutualTLS, config.Webhook.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Webhook.Address = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Webhook")
}
}
if config.CloudEvents.Address != "" {
var err error
cloudeventsClient, err = outputs.NewClient("CloudEvents", config.CloudEvents.Address, config.CloudEvents.MutualTLS, config.CloudEvents.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.CloudEvents.Address = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "CloudEvents")
}
}
if config.Azure.EventHub.Name != "" {
var err error
azureClient, err = outputs.NewEventHubClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Azure.EventHub.Name = ""
config.Azure.EventHub.Namespace = ""
} else {
if config.Azure.EventHub.Name != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "EventHub")
}
}
}
if (config.GCP.PubSub.ProjectID != "" && config.GCP.PubSub.Topic != "") || config.GCP.Storage.Bucket != "" {
var err error
gcpClient, err = outputs.NewGCPClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.GCP.PubSub.ProjectID = ""
config.GCP.PubSub.Topic = ""
config.GCP.Storage.Bucket = ""
} else {
if config.GCP.PubSub.Topic != "" && config.GCP.PubSub.ProjectID != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "GCPPubSub")
}
if config.GCP.Storage.Bucket != "" {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "GCPStorage")
}
}
}
if config.Googlechat.WebhookURL != "" {
var err error
googleChatClient, err = outputs.NewClient("Googlechat", config.Googlechat.WebhookURL, config.Googlechat.MutualTLS, config.Googlechat.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Googlechat.WebhookURL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Google Chat")
}
}
if config.Kafka.HostPort != "" && config.Kafka.Topic != "" {
var err error
kafkaClient, err = outputs.NewKafkaClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Kafka.HostPort = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Kafka")
}
}
if config.Pagerduty.RoutingKey != "" {
var err error
var url = "https://events.pagerduty.com/v2/enqueue"
var outputName = "Pagerduty"
pagerdutyClient, err = outputs.NewClient(outputName, url, config.Pagerduty.MutualTLS, config.Pagerduty.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Pagerduty.RoutingKey = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, outputName)
}
}
if config.Kubeless.Namespace != "" && config.Kubeless.Function != "" {
var err error
kubelessClient, err = outputs.NewKubelessClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
log.Printf("[ERROR] : Kubeless - %v\n", err)
config.Kubeless.Namespace = ""
config.Kubeless.Function = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Kubeless")
}
}
if config.WebUI.URL != "" {
var err error
webUIClient, err = outputs.NewClient("WebUI", config.WebUI.URL, config.WebUI.MutualTLS, config.WebUI.CheckCert, config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.WebUI.URL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "WebUI")
}
}
if config.Openfaas.FunctionName != "" {
var err error
openfaasClient, err = outputs.NewOpenfaasClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
log.Printf("[ERROR] : OpenFaaS - %v\n", err)
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "OpenFaaS")
}
}
if config.Rabbitmq.URL != "" && config.Rabbitmq.Queue != "" {
var err error
rabbitmqClient, err = outputs.NewRabbitmqClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
config.Rabbitmq.URL = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "RabbitMQ")
}
}
if config.Wavefront.EndpointType != "" && config.Wavefront.EndpointHost != "" {
var err error
wavefrontClient, err = outputs.NewWavefrontClient(config, stats, promStats, statsdClient, dogstatsdClient)
if err != nil {
log.Printf("[ERROR] : Wavefront - %v\n", err)
config.Wavefront.EndpointHost = ""
} else {
outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Wavefront")
}
}
log.Printf("[INFO] : Enabled Outputs : %s\n", outputs.EnabledOutputs)
}
func main() {
http.HandleFunc("/", mainHandler)
http.HandleFunc("/ping", pingHandler)
http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/test", testHandler)
http.Handle("/metrics", promhttp.Handler())
log.Printf("[INFO] : Falco Sidekick is up and listening on %s:%d", config.ListenAddress, config.ListenPort)
if config.Debug {
log.Printf("[INFO] : Debug mode : %v", config.Debug)
}
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort), nil); err != nil {
log.Fatalf("[ERROR] : %v", err.Error())
}
}