-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlogging.go
More file actions
194 lines (157 loc) · 4.42 KB
/
logging.go
File metadata and controls
194 lines (157 loc) · 4.42 KB
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
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/elazarl/goproxy"
"github.com/sirupsen/logrus"
"github.com/dependabot/proxy/internal/logging"
)
type requestLogger struct {
}
func NewRequestLogger() *requestLogger {
return &requestLogger{}
}
func (l *requestLogger) logRequest(req *http.Request, p *goproxy.ProxyCtx) (*http.Request, *http.Response) {
logging.RequestLogf(p, "%s %s", req.Method, urlWithoutCredentials(req.URL))
return req, nil
}
func (l *requestLogger) logResponse(rsp *http.Response, p *goproxy.ProxyCtx) *http.Response {
if rsp == nil {
logging.RequestLogf(p, "No response from server")
return rsp
}
if rsp.Request == nil {
logging.RequestLogf(p, "%d (No request on response object)", rsp.StatusCode)
return rsp
}
if rsp.Request.URL == nil {
logging.RequestLogf(p, "%d (No URL on response.Request object)", rsp.StatusCode)
return rsp
}
logging.RequestLogf(p, "%d %s", rsp.StatusCode, urlWithoutCredentials(rsp.Request.URL))
if _, logForStatusCode := responseBodyLoggingStatusCodes[rsp.StatusCode]; logForStatusCode {
logResponseBody(rsp, p, 1024)
}
return rsp
}
var responseBodyLoggingStatusCodes = map[int]bool{
http.StatusBadRequest: true,
http.StatusUnauthorized: true,
http.StatusForbidden: true,
http.StatusProxyAuthRequired: true,
}
type multiCloser struct {
closers []io.Closer
}
func (mc *multiCloser) Close() (err error) {
for _, c := range mc.closers {
if e := c.Close(); e != nil && err == nil {
err = e
}
}
return
}
type multiReadCloser struct {
mr io.Reader
mc io.Closer
}
func (mrc *multiReadCloser) Read(p []byte) (int, error) {
return mrc.mr.Read(p)
}
func (mrc *multiReadCloser) Close() error {
return mrc.mc.Close()
}
func newMultiReadCloser(rcs ...io.ReadCloser) io.ReadCloser {
readers := make([]io.Reader, 0, len(rcs))
closers := make([]io.Closer, 0, len(rcs))
for _, r := range rcs {
readers = append(readers, r)
closers = append(closers, r)
}
return &multiReadCloser{
mr: io.MultiReader(readers...),
mc: &multiCloser{closers: closers},
}
}
func logResponseBody(rsp *http.Response, p *goproxy.ProxyCtx, maxBytes int) {
if maxBytes > 0 {
bodyBytes := make([]byte, maxBytes)
n, err := io.ReadFull(rsp.Body, bodyBytes)
if n > 0 {
logging.RequestMultilineLogf(p, "Remote response: %s", string(bodyBytes[:n]))
if err == io.EOF || err == io.ErrUnexpectedEOF {
rsp.Body.Close()
rsp.Body = io.NopCloser(bytes.NewReader(bodyBytes[:n]))
} else {
rsp.Body = newMultiReadCloser(io.NopCloser(bytes.NewReader(bodyBytes[:n])), rsp.Body)
}
}
} else {
bodyBytes, _ := io.ReadAll(rsp.Body)
rsp.Body.Close()
logging.RequestMultilineLogf(p, "Remote response: %s", string(bodyBytes))
rsp.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}
}
const placeholder = "xxx"
func urlWithoutCredentials(u *url.URL) string {
cloned := *u
if u.User != nil {
cloned.User = userWithoutCredentials(u.User)
}
if shouldConcealHost(u) {
cloned.Scheme = ""
cloned.Host = ""
}
return cloned.String()
}
// Since logs are rendered to the public, we should obfuscate the Dependabot's
// API hostnames. We can make an exception for AWS runners as those logs are
// scrubbed at runtime.
func shouldConcealHost(url *url.URL) bool {
// pass through calls to the backend from AWS runners
if url.Hostname() == "dependabot-api.githubapp.com" {
return false
}
if strings.HasPrefix(url.Hostname(), "dependabot-api") || strings.HasPrefix(url.Hostname(), "dependabot-actions") {
return true
}
return false
}
func userWithoutCredentials(user *url.Userinfo) *url.Userinfo {
username := ""
if user.Username() != "" {
username = placeholder
}
password, ok := user.Password()
if !ok {
return url.User(username)
}
if password != "" {
return url.UserPassword(username, placeholder)
}
return url.UserPassword(username, "")
}
type Formatter struct{}
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(fmt.Sprintf("%s %s\n", entry.Time.UTC().Format("2006/01/02 15:04:05"), entry.Message)), nil
}
func setupLogging() *os.File {
logrus.SetFormatter(&Formatter{})
if logfilePath != nil && *logfilePath != "" && *logfilePath != "-" {
file, err := os.OpenFile(*logfilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
log.Fatal("Failed to open log file: ", err)
}
log.SetOutput(file)
logrus.SetOutput(file)
return file
}
return nil
}