This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
traildash.go
504 lines (451 loc) · 13.5 KB
/
traildash.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
const version = "0.0.10"
const usage = `traildash: easy AWS CloudTrail dashboard
Usage:
traildash
traildash --version
Note: traildash uses Environment Vars rather than flags for Docker compatibility.
Required Environment Variables:
AWS_SQS_URL AWS SQS URL.
AWS credentials are sourced by (in order): Environment Variables, ~/.aws/credentials, IAM profiles.
AWS_ACCESS_KEY_ID AWS Key ID.
AWS_SECRET_ACCESS_KEY AWS Secret Key.
Optional Environment Variables:
AWS_REGION AWS Region (SQS and S3 regions must match. default: us-east-1).
ES_URL ElasticSearch URL (default: http://localhost:9200).
WEB_LISTEN Listen IP and port for HTTP/HTTPS interface (default: 0.0.0.0:7000).
SSL_MODE "off": disable HTTPS and use HTTP (default)
"custom": use custom key/cert stored stored in ".tdssl/key.pem" and ".tdssl/cert.pem"
"selfSigned": use key/cert in ".tdssl", generate an self-signed cert if empty
SQS_PERSIST Set to prevent deleting of finished SQS messages - for debugging.
DEBUG Enable debugging output.
`
const esPath = "cloudtrail/event"
type sslModeOption int
const (
SSLoff sslModeOption = 0
SSLcustom sslModeOption = 1
SSLselfSigned sslModeOption = 2
SSLcertDir = ".tdssl/"
SSLcertFile = SSLcertDir + "cert.pem"
SSLkeyFile = SSLcertDir + "key.pem"
)
var sslModeOptionMap = map[string]sslModeOption{
"off": SSLoff,
"custom": SSLcustom,
"selfSigned": SSLselfSigned,
}
type config struct {
awsKeyId string
awsSecret string
awsConfig aws.Config
region string
queueURL string
esURL string
listen string
authUser string
authPw string
sslMode sslModeOption
debugOn bool
sqsPersist bool
}
type sqsNotification struct {
Type string
MessageID string
TopicArn string
Message string
Timestamp string
SignatureVersion string
Signature string
SigningCertURL string
UnsubscribeURL string
}
type cloudtrailNotification struct {
S3Bucket string
S3ObjectKey []string
MessageID string
ReceiptHandle string
}
type cloudtrailLog struct {
Records []cloudtrailRecord
}
type cloudtrailRecord struct {
EventName string
UserAgent string
EventID string
EventSource string
SourceIPAddress string
EventType string
EventVersion string
EventTime string
AwsRegion string
RequestID string
RecipientAccountId string
UserIdentity map[string]interface{}
RequestParameters map[string]interface{}
//ResponseElements string
}
func main() {
c, err := parseArgs()
if err != nil {
fmt.Printf("Error parsing arguments: %s\n\n", err.Error())
fmt.Println(usage)
os.Exit(1)
}
go c.workLogs()
go c.serveKibana()
log.Print("Started")
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-sig:
log.Fatalf("Signal (%d) received, stopping", s)
}
}
log.Print("Exiting!")
}
// serveKibana runs a webserver for 1. kibana and 2. elasticsearch proxy
func (c *config) serveKibana() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
webStaticHandler(w, r)
})
http.HandleFunc("/es/", c.proxyHandler)
if c.sslMode == SSLoff {
http.ListenAndServe(c.listen, nil)
} else {
http.ListenAndServeTLS(c.listen, SSLcertFile, SSLkeyFile, nil)
}
log.Print("Web server exit")
}
// webStaticHandler serves embedded static web files (js&css)
func webStaticHandler(w http.ResponseWriter, r *http.Request) {
assetPath := "kibana/" + r.URL.Path[1:]
if assetPath == "kibana/" {
assetPath = "kibana/index.html"
}
staticAsset, err := Asset(assetPath)
if err != nil {
log.Printf("Kibana web error: %s", err.Error())
http.NotFound(w, r)
return
}
headers := w.Header()
if strings.HasSuffix(assetPath, ".js") {
headers["Content-Type"] = []string{"application/javascript"}
} else if strings.HasSuffix(assetPath, ".css") {
headers["Content-Type"] = []string{"text/css"}
} else if strings.HasSuffix(assetPath, ".html") {
headers["Content-Type"] = []string{"text/html"}
}
io.Copy(w, bytes.NewReader(staticAsset))
}
// proxyHandler securely proxies requests to the ElasticSearch instance
func (c *config) proxyHandler(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(c.esURL)
if err != nil {
log.Printf("URL err: %s", err.Error())
http.Error(w, err.Error(), 500)
return
}
if firewallES(r) {
c.debug("Permitting ES %s request: %s", r.Method, r.RequestURI)
} else {
c.debug("Refusing ES %s request: %s", r.Method, r.RequestURI)
http.Error(w, "Permission denied", 403)
return
}
client := &http.Client{}
req := r
req.RequestURI = ""
req.Host = u.Host
req.URL.Host = req.Host
req.URL.Scheme = u.Scheme
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/es")
resp, err := client.Do(req)
if err != nil {
c.debug("Proxy err: %s", err.Error())
http.Error(w, err.Error(), 500)
return
}
copyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
if err := resp.Body.Close(); err != nil {
c.debug("Can't close response body %v", err)
}
//c.debug("Copied %v bytes to client error=%v", nr, err)
}
// firewallES provides a basic "firewall" for ElasticSearch
func firewallES(r *http.Request) bool {
switch r.Method {
case "GET":
return true
case "POST":
parts := strings.SplitN(r.RequestURI, "?", 2)
if strings.HasSuffix(parts[0], "_search") {
return true
}
case "PUT":
if strings.HasPrefix(r.RequestURI, "/es/kibana-int/dashboard/") {
return true
}
default:
return false
}
return false
}
// workLogs fetches and loads logs from SQS
func (c *config) workLogs() {
for {
// fetch a message from SQS
m, err := c.dequeue()
if err != nil {
kerblowie("Error dequeing from SQS: %s", err.Error())
continue
} else if m == nil {
log.Printf("Empty queue... polling for 20 seconds.")
continue
}
if len(m.S3ObjectKey) < 1 {
kerblowie("Error dequeing from SQS: S3ObjectKey empty. Please grab the contents of SQS message ID sqs://%s and report in a GitHub issue. Thanks!!", m.MessageID)
continue
}
// download from S3
records, err := c.download(m)
if err != nil {
kerblowie("Error downloading from S3: %s", err.Error())
continue
}
c.debug("Downloaded %d records from sqs://%s [s3://%s/%s]", len(*records), m.MessageID, m.S3Bucket, m.S3ObjectKey[0])
// load into elasticsearch
if err = c.load(records); err != nil {
kerblowie("Error uploading to ElasticSearch: %s", err.Error())
continue
}
c.debug("Uploaded sqs://%s [s3://%s/%s] to es://%s", m.MessageID, m.S3Bucket, m.S3ObjectKey[0], esPath)
// delete message from sqs
if c.sqsPersist {
c.debug("NOT DELETING sqs://%s [s3://%s/%s]", m.MessageID, m.S3Bucket, m.S3ObjectKey[0])
} else {
if err = c.deleteSQS(m); err != nil {
kerblowie("Error deleting from SQS queue: %s", err.Error())
continue
}
c.debug("Deleted sqs://%s [s3://%s/%s]", m.MessageID, m.S3Bucket, m.S3ObjectKey[0])
}
log.Printf("Loaded CloudTrail file with %d records.", len(*records))
}
}
// dequeue fetches an item from SQS
func (c *config) dequeue() (*cloudtrailNotification, error) {
numRequested := 1
q := sqs.New(&c.awsConfig)
req := sqs.ReceiveMessageInput{
QueueURL: aws.String(c.queueURL),
MaxNumberOfMessages: aws.Int64(int64(numRequested)),
WaitTimeSeconds: aws.Int64(20), // max allowed
}
resp, err := q.ReceiveMessage(&req)
if err != nil {
return nil, fmt.Errorf("SQS ReceiveMessage error: %s", err.Error())
}
//c.debug("Received %d messsage from SQS.", len(resp.Messages))
if len(resp.Messages) > numRequested {
return nil, fmt.Errorf("Expected %d but got %d messages.", numRequested, len(resp.Messages))
} else if len(resp.Messages) == 0 {
return nil, nil
}
m := resp.Messages[0]
body := *m.Body
not := sqsNotification{}
if err := json.Unmarshal([]byte(body), ¬); err != nil {
return nil, fmt.Errorf("SQS message JSON error [id: %s]: %s", not.MessageID, err.Error())
}
n := cloudtrailNotification{}
n.MessageID = not.MessageID
n.ReceiptHandle = *m.ReceiptHandle
if not.Message == "CloudTrail validation message." { // swallow validation messages
if err = c.deleteSQS(&n); err != nil {
return nil, fmt.Errorf("Error deleting CloudTrail validation message [id: %s]: %s", not.MessageID, err.Error())
}
return nil, fmt.Errorf("Deleted CloudTrail validation message id %s", not.MessageID)
} else if err := json.Unmarshal([]byte(not.Message), &n); err != nil {
return nil, fmt.Errorf("CloudTrail JSON error [id: %s]: %s", not.MessageID, err.Error())
}
return &n, nil
}
// download fetches the CloudTrail logfile from S3 and parses it
func (c *config) download(m *cloudtrailNotification) (*[]cloudtrailRecord, error) {
if len(m.S3ObjectKey) != 1 {
return nil, fmt.Errorf("Expected one S3 key but got %d", len(m.S3ObjectKey[0]))
}
s := s3.New(&c.awsConfig)
q := s3.GetObjectInput{
Bucket: aws.String(m.S3Bucket),
Key: aws.String(m.S3ObjectKey[0]),
}
o, err := s.GetObject(&q)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(o.Body)
if err != nil {
return nil, err
}
logfile := cloudtrailLog{}
if err := json.Unmarshal(b, &logfile); err != nil {
return nil, fmt.Errorf("Error unmarshaling cloutrail JSON: %s", err.Error())
}
return &logfile.Records, nil
}
// load stores a group of cloudtrail records into ElasticSearch
func (c *config) load(records *[]cloudtrailRecord) error {
bulk := ""
for _, r := range *records { // build file for bulk upload to ES
j, err := json.Marshal(r)
if err != nil {
return err
}
bulk += fmt.Sprintf(`{ "index": { "_id" : "%s" }}`+"\n", r.EventID)
bulk += string(j) + "\n"
}
url := fmt.Sprintf("%s/%s/_bulk", c.esURL, esPath)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(bulk)))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.Status != "200 OK" {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Error response from Elasticsearch: %s %s", resp.Status, string(body))
}
return nil
}
// deleteSQS removes a completed notification from the queue
func (c *config) deleteSQS(m *cloudtrailNotification) error {
q := sqs.New(&c.awsConfig)
req := sqs.DeleteMessageInput{
QueueURL: aws.String(c.queueURL),
ReceiptHandle: aws.String(m.ReceiptHandle),
}
_, err := q.DeleteMessage(&req)
if err != nil {
return err
}
return nil
}
// parseArgs handles CLI flags and env vars
func parseArgs() (*config, error) {
c := config{}
var verPtr bool
var helpPtr bool
flag.BoolVar(&verPtr, "version", false, "print version")
flag.BoolVar(&verPtr, "v", false, "print version")
flag.BoolVar(&helpPtr, "help", false, "print usage")
flag.BoolVar(&helpPtr, "h", false, "print usage")
flag.Parse()
if verPtr {
fmt.Println(version)
os.Exit(0)
} else if helpPtr {
fmt.Println(usage)
os.Exit(0)
}
c.queueURL = os.Getenv("AWS_SQS_URL")
if len(c.queueURL) < 1 {
return nil, fmt.Errorf("Must specify SQS url with -Q flag or by setting AWS_SQS_URL env var.")
}
c.awsKeyId = os.Getenv("AWS_ACCESS_KEY_ID")
c.awsSecret = os.Getenv("AWS_SECRET_ACCESS_KEY")
c.region = os.Getenv("AWS_REGION")
if len(c.region) < 1 {
c.region = "us-east-1"
}
c.awsConfig = aws.Config{Region: aws.String(c.region)}
c.esURL = os.Getenv("ES_URL")
if len(c.esURL) < 1 {
c.esURL = "http://127.0.0.1:9200"
}
c.listen = os.Getenv("WEB_LISTEN")
if len(c.listen) < 1 {
c.listen = "0.0.0.0:7000"
}
if len(os.Getenv("DEBUG")) > 0 {
c.debugOn = true
}
if len(os.Getenv("SQS_PERSIST")) > 0 {
c.sqsPersist = true
}
c.sslMode = SSLoff
if len(os.Getenv("SSL_MODE")) > 0 {
var ok bool
c.sslMode, ok = sslModeOptionMap[os.Getenv("SSL_MODE")]
if !ok {
return nil, fmt.Errorf("Invalid SSL_MODE. Must be one of 'off', 'selfSigned', or 'custom'.")
}
}
if c.sslMode != SSLoff {
// look for existing ".tdssl/key.pem" and ".tdssl/cert.pem"
_, keyErr := os.Stat(SSLkeyFile)
_, certErr := os.Stat(SSLcertFile)
if os.IsNotExist(keyErr) && os.IsNotExist(certErr) && c.sslMode == SSLselfSigned {
if _, dirErr := os.Stat(SSLcertDir); os.IsNotExist(dirErr) {
if err := os.Mkdir(SSLcertDir, 0700); err != nil {
return nil, fmt.Errorf("Error creating SSL cert directory at %s: %s", SSLcertDir, err.Error())
}
}
if err := generateCert(SSLcertFile, SSLkeyFile); err != nil {
return nil, fmt.Errorf("Error generating a self-signed SSL cert: %s", err.Error())
}
log.Printf("Created new self-signed SSL cert in %s.", SSLcertDir)
} else if os.IsNotExist(keyErr) || os.IsNotExist(certErr) {
return nil, fmt.Errorf("SSL key or cert missing. Expected at %s and %s", SSLcertFile, SSLkeyFile)
}
}
return &c, nil
}
// debug reports stuff if debugging is on
func (c *config) debug(format string, m ...interface{}) {
if c.debugOn {
log.Printf(format, m...)
}
}
// kerblowie handles API failures "gracefully"... hah
func kerblowie(format string, s ...interface{}) {
log.Printf(format, s...)
time.Sleep(5 * time.Second)
}
// copyHeaders copies HTTP headers to proxy responses
func copyHeaders(dst, src http.Header) {
for k, _ := range dst {
dst.Del(k)
}
for k, vs := range src {
for _, v := range vs {
dst.Add(k, v)
}
}
}