/
auditor.go
62 lines (54 loc) · 1.33 KB
/
auditor.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
package requests
import (
"net/http"
"net/http/httputil"
)
type Auditor interface {
// UserSuccess audits the requests and response when the http status
// code is <300.
UserSuccess(userID, action string, metadata map[string]interface{})
// UserFail audits the requests and response when the http status
// code is >=300.
UserFail(userID, action string, metadata map[string]interface{})
}
type reqAuditor struct {
auditor Auditor
req []byte
resp []byte
success bool
action string
userID string
}
func NewReqAuditor(auditor Auditor, userID, action string) *reqAuditor {
return &reqAuditor{
auditor: auditor,
action: action,
userID: userID,
}
}
// SetReq sets the HTTP request to the provider to be audited.
func (a *reqAuditor) SetReq(req *http.Request) {
d, _ := httputil.DumpRequestOut(req, true)
a.req = d
}
// SetResp sets the HTTP response from the provider to be audited.
func (a *reqAuditor) SetResp(resp *http.Response) {
a.success = resp.StatusCode < 300
d, _ := httputil.DumpResponse(resp, true)
a.resp = d
}
// Audit sends the audit trail to the Auditor.
func (a *reqAuditor) Audit() {
if a == nil {
return
}
md := map[string]interface{}{
"req": a.req,
"resp": a.resp,
}
if a.success {
a.auditor.UserSuccess(a.userID, a.action, md)
return
}
a.auditor.UserFail(a.userID, a.action, md)
}