-
Notifications
You must be signed in to change notification settings - Fork 22
/
audit.go
100 lines (86 loc) · 2.44 KB
/
audit.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
package middleware
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
gabi "github.com/app-sre/gabi/pkg"
"github.com/app-sre/gabi/pkg/audit"
"github.com/app-sre/gabi/pkg/models"
)
func Audit(cfg *gabi.Config) Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
now := time.Now()
var (
b bytes.Buffer
request models.QueryRequest
user string
)
if s := r.Header.Get(contentLengthHeader); s == "" {
l := fmt.Sprintf("Request without required header: %s", contentLengthHeader)
http.Error(w, l, http.StatusBadRequest)
return
}
base64DecodeQuery := false
if s := r.URL.Query().Get("base64_query"); s != "" {
if ok, err := strconv.ParseBool(s); err == nil && ok {
base64DecodeQuery = true
}
}
if ctxUser := ctx.Value(ContextKeyUser); ctxUser != nil {
if s, ok := ctxUser.(string); ok {
user = s
}
} else {
user = r.Header.Get(forwardedUserHeader)
}
if user == "" {
l := fmt.Sprintf("Request without required header: %s", forwardedUserHeader)
http.Error(w, l, http.StatusBadRequest)
return
}
if _, err := io.Copy(&b, r.Body); err != nil {
cfg.Logger.Errorf("Unable to copy request body: %s", err)
http.Error(w, "An internal error has occurred", http.StatusInternalServerError)
return
}
_ = r.Body.Close()
r.Body = io.NopCloser(bytes.NewReader(b.Bytes()))
err := json.Unmarshal(b.Bytes(), &request)
if err != nil {
cfg.Logger.Debugf("Unable to unmarshal request body: %s", err)
h.ServeHTTP(w, r)
return
}
if base64DecodeQuery {
bytes, err := cfg.Encoder.DecodeString(request.Query)
if err != nil {
l := "Unable to decode Base64-encoded query"
cfg.Logger.Errorf("%s: %s", l, err)
http.Error(w, l, http.StatusBadRequest)
return
}
request.Query = string(bytes)
}
query := &audit.QueryData{
Query: request.Query,
User: user,
Timestamp: now.Unix(),
}
_ = cfg.LoggerAudit.Write(ctx, query)
if err := cfg.SplunkAudit.Write(ctx, query); err != nil {
cfg.Logger.Errorf("Unable to send audit to Splunk: %s", err)
http.Error(w, "An internal error has occurred", http.StatusInternalServerError)
return
}
ctx = context.WithValue(ctx, ContextKeyQuery, request.Query)
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}