Commit b78a1d03 authored by Stan Hu's avatar Stan Hu

Strip port and include remote IP in access logs

The port number is meaningless when the X-Forwarded-For header
is used, so let's just remove it.

Closes https://gitlab.com/gitlab-org/gitlab-workhorse/issues/201
parent 0acf5a23
......@@ -94,8 +94,11 @@ func (l *statsCollectingResponseWriter) writeAccessLog(r *http.Request) {
func (l *statsCollectingResponseWriter) accessLogFields(r *http.Request) log.Fields {
duration := time.Since(l.started)
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
return log.Fields{
"host": r.Host,
"remoteIp": ip,
"remoteAddr": r.RemoteAddr,
"method": r.Method,
"uri": ScrubURLParams(r.RequestURI),
......
......@@ -10,6 +10,40 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_statsCollectingResponseWriter_remoteIp_accessLogFields(t *testing.T) {
testCases := []struct {
remoteAddr string
expected string
}{
{remoteAddr: "", expected: ""},
{remoteAddr: "bogus", expected: ""},
{remoteAddr: "8.8.8.8:1234", expected: "8.8.8.8"},
{remoteAddr: "8.8.8.8", expected: ""},
{remoteAddr: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]", expected: ""},
{remoteAddr: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443", expected: "2001:db8:85a3:8d3:1319:8a2e:370:7348"},
}
for _, tc := range testCases {
req, err := http.NewRequest("GET", "/blah", nil)
req.RemoteAddr = tc.remoteAddr
assert.Nil(t, err)
l := &statsCollectingResponseWriter{
rw: nil,
status: 200,
wroteHeader: true,
written: 50,
started: time.Now(),
}
fields := l.accessLogFields(req)
ip := fields["remoteIp"].(string)
assert.Equal(t, tc.expected, ip)
}
}
func Test_statsCollectingResponseWriter_accessLogFields(t *testing.T) {
passwords := []string{
"should_be_filtered", // Basic case
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment