/
log.go
139 lines (115 loc) · 3.77 KB
/
log.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
package middleware
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/env"
utillog "github.com/Azure/ARO-RP/pkg/util/log"
"github.com/Azure/ARO-RP/pkg/util/log/audit"
)
type logResponseWriter struct {
http.ResponseWriter
statusCode int
bytes int
}
func (w *logResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker := w.ResponseWriter.(http.Hijacker)
return hijacker.Hijack()
}
func (w *logResponseWriter) Write(b []byte) (int, error) {
n, err := w.ResponseWriter.Write(b)
w.bytes += n
return n, err
}
func (w *logResponseWriter) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
w.statusCode = statusCode
}
type logReadCloser struct {
io.ReadCloser
bytes int
}
func (rc *logReadCloser) Read(b []byte) (int, error) {
n, err := rc.ReadCloser.Read(b)
rc.bytes += n
return n, err
}
func Log(env env.Core, auditLog, baseLog *logrus.Entry) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t := time.Now()
r.Body = &logReadCloser{ReadCloser: r.Body}
w = &logResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
log := baseLog
log = utillog.EnrichWithPath(log, r.URL.Path)
username, _ := r.Context().Value(ContextKeyUsername).(string)
log = log.WithFields(logrus.Fields{
"request_method": r.Method,
"request_path": r.URL.Path,
"request_proto": r.Proto,
"request_remote_addr": r.RemoteAddr,
"request_user_agent": r.UserAgent(),
"username": username,
})
log.Print("read request")
auditEntry := auditLog.WithFields(logrus.Fields{
audit.MetadataAdminOperation: true,
audit.MetadataCreatedTime: time.Now().UTC().Format(time.RFC3339),
audit.MetadataLogKind: audit.IFXAuditLogKind,
audit.MetadataSource: audit.SourceAdminPortal,
audit.EnvKeyAppID: audit.SourceAdminPortal,
audit.EnvKeyCloudRole: audit.CloudRoleRP,
audit.EnvKeyEnvironment: env.Environment().Name,
audit.EnvKeyHostname: env.Hostname(),
audit.EnvKeyLocation: env.Location(),
audit.PayloadKeyCategory: audit.CategoryResourceManagement,
audit.PayloadKeyOperationName: fmt.Sprintf("%s %s", r.Method, r.URL.Path),
audit.PayloadKeyCallerIdentities: []audit.CallerIdentity{
{
CallerIdentityType: audit.CallerIdentityTypeUsername,
CallerIdentityValue: username,
CallerIPAddress: r.RemoteAddr,
},
},
audit.PayloadKeyTargetResources: []audit.TargetResource{
{
TargetResourceName: r.URL.Path,
TargetResourceType: auditTargetResourceType(r),
},
},
})
defer func() {
statusCode := w.(*logResponseWriter).statusCode
log.WithFields(logrus.Fields{
"body_read_bytes": r.Body.(*logReadCloser).bytes,
"body_written_bytes": w.(*logResponseWriter).bytes,
"duration": time.Since(t).Seconds(),
"response_status_code": statusCode,
}).Print("sent response")
resultType := audit.ResultTypeSuccess
if statusCode >= http.StatusBadRequest {
resultType = audit.ResultTypeFail
}
auditEntry.WithFields(logrus.Fields{
audit.PayloadKeyResult: audit.Result{
ResultType: resultType,
ResultDescription: fmt.Sprintf("Status code: %d", statusCode),
},
}).Info(audit.DefaultLogMessage)
}()
h.ServeHTTP(w, r)
})
}
}
func auditTargetResourceType(r *http.Request) string {
if matches := utillog.RXTolerantSubResourceID.FindStringSubmatch(r.URL.Path); matches != nil {
return matches[len(matches)-1]
}
return ""
}