-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
286 lines (243 loc) · 8.28 KB
/
logger.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
// Package traefik_middleware_request_logger Traefik middleware to log incoming requests and outgoing responses.
package traefik_middleware_request_logger //nolint:revive,stylecheck
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
"github.com/google/uuid" //nolint:depguard
)
// Config holds the plugin configuration.
type Config struct {
RequestIDHeaderName string `json:"RequestIDHeaderName,omitempty"` //nolint:tagliatelle // This is a configuration option
StatusCodes []int `json:"StatusCodes,omitempty"` //nolint:tagliatelle // This is a configuration option
ContentTypes []string `json:"ContentTypes,omitempty"` //nolint:tagliatelle // This is a configuration option
LogTarget string `json:"LogTarget,omitempty"` //nolint:tagliatelle // This is a configuration option
LogTargetURL string `json:"LogTargetUrl,omitempty"` //nolint:tagliatelle // This is a configuration option
SkipHeaders []string `json:"SkipHeaders,omitempty"` //nolint:tagliatelle // This is a configuration option
Limits ConfigLimit `json:"Limits,omitempty"` //nolint:tagliatelle // This is a configuration option
}
// ConfigLimit holds the plugin configuration.
type ConfigLimit struct {
MaxBodySize int `json:"MaxBodySize,omitempty"` //nolint:tagliatelle // This is a configuration option
}
// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// logRequest holds the plugin configuration.
type logRequest struct {
name string
next http.Handler
contentTypes []string
statusCodes []int
maxBodySize int
requestIDHeaderName string
logTarget string
logTargetURL string
skipHeaders []string
}
// RequestLog holds the plugin configuration.
type requestLog struct {
RequestID string `json:"request_id"` //nolint:tagliatelle
Request requestData `json:"request"` //nolint:tagliatelle
Response responseData `json:"response"` //nolint:tagliatelle
Direction string `json:"direction"` //nolint:tagliatelle
Metadata string `json:"metadata"` //nolint:tagliatelle
}
type requestData struct {
URI string `json:"uri"` //nolint:tagliatelle
Host string `json:"host"` //nolint:tagliatelle
Headers map[string]string `json:"headers"` //nolint:tagliatelle
Body interface{} `json:"body"` //nolint:tagliatelle
Verb string `json:"verb"` //nolint:tagliatelle
IPAddress string `json:"ip_address"` //nolint:tagliatelle
Time string `json:"time"` //nolint:tagliatelle
TransferEncoding string `json:"transfer_encoding"` //nolint:tagliatelle
}
type responseData struct {
Time string `json:"time"` //nolint:tagliatelle
Status int `json:"status"` //nolint:tagliatelle
Headers map[string]string `json:"headers"` //nolint:tagliatelle
Body interface{} `json:"body"` //nolint:tagliatelle
TransferEncoding string `json:"transfer_encoding"` //nolint:tagliatelle
}
// New creates and returns a new plugin instance.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &logRequest{
name: name,
next: next,
requestIDHeaderName: config.RequestIDHeaderName,
contentTypes: config.ContentTypes,
statusCodes: config.StatusCodes,
maxBodySize: config.Limits.MaxBodySize,
logTarget: config.LogTarget,
logTargetURL: config.LogTargetURL,
skipHeaders: config.SkipHeaders,
}, nil
}
func (p *logRequest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestID := uuid.NewString()
if r.Header.Get(p.requestIDHeaderName) != "" {
requestID = r.Header.Get(p.requestIDHeaderName)
}
r.Header.Set(p.requestIDHeaderName, requestID)
requestBody := []byte{}
if r.Body != nil {
requestBody, _ = io.ReadAll(r.Body)
}
r.Body = io.NopCloser(bytes.NewBuffer(requestBody))
resp := &wrappedResponseWriter{
w: w,
buf: &bytes.Buffer{},
code: 200,
}
p.next.ServeHTTP(resp, r)
defer resp.Flush()
respBodyBytes := resp.buf.Bytes()
headers := make(map[string]string)
for name, values := range r.Header {
if len(values) > 0 && allowedHeader(name, p.skipHeaders) {
headers[name] = values[0] // Take the first value of the header
}
}
reqData := requestData{
URI: r.URL.String(),
Host: r.Host,
Headers: headers,
Time: time.Now().Format(time.RFC3339),
Verb: r.Method,
}
reqData.Body = allowedBody(requestBody, r.Header.Get("Content-Type"), p.maxBodySize, p.contentTypes)
responseBody := io.NopCloser(bytes.NewBuffer(respBodyBytes))
responseBodyBytes, _ := io.ReadAll(responseBody)
respHeaders := make(map[string]string)
for name, values := range resp.Header() {
if len(values) > 0 && allowedHeader(name, p.skipHeaders) {
respHeaders[name] = values[0] // Take the first value of the header
}
}
resData := responseData{
Headers: respHeaders,
Status: resp.code,
Time: time.Now().Format(time.RFC3339),
}
resData.Body = allowedBody(responseBodyBytes, resp.Header().Get("Content-Type"), p.maxBodySize, p.contentTypes)
log := requestLog{
RequestID: requestID,
Request: reqData,
Response: resData,
Direction: "Incomming",
Metadata: "",
}
jsonData, err := json.Marshal(log)
if err != nil {
fmt.Println(err)
}
if allowStatusCode(resp.code, p.statusCodes) && p.logTarget == "stdout" {
_, err = os.Stdout.WriteString(string(jsonData) + "\n")
if err != nil {
fmt.Println(err)
}
}
if allowStatusCode(resp.code, p.statusCodes) && p.logTarget == "stderr" {
_, err = os.Stderr.WriteString(string(jsonData) + "\n")
if err != nil {
fmt.Println(err)
}
}
if allowStatusCode(resp.code, p.statusCodes) && p.logTarget == "url" && p.logTargetURL != "" {
go http.Post(p.logTargetURL, "application/json", bytes.NewBuffer(jsonData)) //nolint:errcheck
}
}
type wrappedResponseWriter struct {
w http.ResponseWriter
buf *bytes.Buffer
code int
}
func (w *wrappedResponseWriter) Header() http.Header {
return w.w.Header()
}
func (w *wrappedResponseWriter) Write(b []byte) (int, error) {
return w.buf.Write(b)
}
func (w *wrappedResponseWriter) WriteHeader(code int) {
w.code = code
}
func (w *wrappedResponseWriter) Flush() {
w.w.WriteHeader(w.code)
_, err := io.Copy(w.w, w.buf)
if err != nil {
fmt.Println(err)
}
}
func (w *wrappedResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := w.w.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("%T is not an http.Hijacker", w.w)
}
return hijacker.Hijack()
}
func allowContentType(contentType string, contentTypes []string) bool {
if len(contentTypes) == 0 {
return true
}
if contentType == "" {
return false
}
for _, ct := range contentTypes {
if ct == contentType {
return true
}
}
return false
}
func allowStatusCode(statusCode int, statusCodes []int) bool {
if len(statusCodes) == 0 {
return true
}
for _, sc := range statusCodes {
if sc == statusCode {
return true
}
}
return false
}
func allowBodySize(bodySize, maxBodySize int) bool {
return bodySize <= maxBodySize
}
func allowedBody(body []byte, contentType string, maxBodySize int, contentTypes []string) interface{} {
if len(body) == 0 {
return nil
}
if !allowBodySize(len(body), maxBodySize) || !allowContentType(contentType, contentTypes) {
return fmt.Sprintf("Request body too large to log or wrong content type. Size: %d bytes, Content-type: %s", len(body), contentType)
}
// Try to parse the body as JSON
var parsedBody interface{}
if contentType == "application/json" {
err := json.Unmarshal(body, &parsedBody)
if err == nil {
return parsedBody
}
}
// If not JSON, return as string
return string(body)
}
func allowedHeader(headerName string, skipHeaders []string) bool {
if len(skipHeaders) == 0 {
return true
}
for _, sh := range skipHeaders {
if sh == headerName {
return false
}
}
return true
}