Skip to content

Commit

Permalink
enhancement: remove clientip from log
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed May 19, 2021
1 parent 7a5d106 commit ca6e8a4
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/handlers/apiv1/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func SendInvites() web.HandlerFunc {
return c.HandleValidation(result)
}

log.Warnf(c, "Sending @{TotalInvites:magenta} invites by @{ClientIP:magenta}", dto.Props{
log.Warnf(c, "@{Tenant:magenta} sent @{TotalInvites:magenta} invites", dto.Props{
"Tenant": c.Tenant().Subdomain,
"TotalInvites": len(action.Invitations),
"ClientIP": c.Request.ClientIP,
})
c.Enqueue(tasks.SendInvites(action.Subject, action.Message, action.Invitations))

Expand Down
3 changes: 1 addition & 2 deletions app/middlewares/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ func WorkerSetup() worker.MiddlewareFunc {
func WebSetup() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c *web.Context) error {
log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} started for @{ClientIP:magenta}", dto.Props{
log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} started", dto.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ClientIP": c.Request.ClientIP,
})

logFinish := func(state string, err error) {
Expand Down
6 changes: 3 additions & 3 deletions app/middlewares/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestWebSetup_Logging_Success(t *testing.T) {
Expect(status).Equals(http.StatusOK)

Expect(infoLogs).HasLen(2)
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started for @{ClientIP:magenta}")
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started")
Expect(infoLogs[1].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms (@{State})")
Expect(infoLogs[1].Props["StatusCode"]).Equals(200)
Expect(infoLogs[1].Props["State"]).Equals("committed")
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestWebSetup_Logging_RequestCanceled(t *testing.T) {
Expect(status).Equals(http.StatusOK)

Expect(infoLogs).HasLen(2)
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started for @{ClientIP:magenta}")
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started")
Expect(infoLogs[1].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} was canceled after @{ElapsedMs:magenta}ms")

Expect(errorLogs).HasLen(0)
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestWebSetup_Logging_Error(t *testing.T) {
Expect(status).Equals(http.StatusInternalServerError)

Expect(infoLogs).HasLen(2)
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started for @{ClientIP:magenta}")
Expect(infoLogs[0].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} started")
Expect(infoLogs[1].Message).Equals("@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms (@{State})")
Expect(infoLogs[1].Props["StatusCode"]).Equals(500)
Expect(infoLogs[1].Props["State"]).Equals("next_error")
Expand Down
6 changes: 0 additions & 6 deletions app/pkg/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ func (s *Server) AddCookie(name string, value string) *Server {
return s
}

// WithClientIP set current ClientIP address
func (s *Server) WithClientIP(clientIP string) *Server {
s.context.Request.ClientIP = clientIP
return s
}

// WithURL set current context Request URL
func (s *Server) WithURL(fullURL string) *Server {
u, _ := url.Parse(fullURL)
Expand Down
15 changes: 0 additions & 15 deletions app/pkg/web/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package web

import (
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
Expand All @@ -16,7 +15,6 @@ import (
type Request struct {
instance *http.Request
Method string
ClientIP string
ContentLength int64
Body string
IsSecure bool
Expand Down Expand Up @@ -50,12 +48,9 @@ func WrapRequest(request *http.Request) Request {
}
}

clientIP := getClientIP(request)

return Request{
instance: request,
Method: request.Method,
ClientIP: strings.TrimSpace(clientIP),
ContentLength: request.ContentLength,
Body: string(bodyBytes),
URL: u,
Expand All @@ -64,16 +59,6 @@ func WrapRequest(request *http.Request) Request {
}
}

// getClientIP returns the IP of the original requestor.
func getClientIP(request *http.Request) (clientIP string) {
if forwardedHosts := request.Header.Get("X-Forwarded-For"); forwardedHosts != "" {
clientIP = strings.Split(forwardedHosts, ",")[0]
} else {
clientIP, _, _ = net.SplitHostPort(request.RemoteAddr)
}
return
}

// GetHeader returns the value of HTTP header from given key
func (r *Request) GetHeader(key string) string {
return r.instance.Header.Get(key)
Expand Down
5 changes: 0 additions & 5 deletions app/pkg/web/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ func TestRequest_Basic(t *testing.T) {
&http.Request{
Method: "GET",
Header: header,
RemoteAddr: "172.10.10.10:5555",
Host: "helloworld.com",
},
)

Expect(req.Method).Equals("GET")
Expect(req.GetHeader("Content-Type")).Equals("application/json")
Expect(req.ClientIP).Equals("172.10.10.10")
Expect(req.URL.Hostname()).Equals("helloworld.com")
Expect(req.URL.Scheme).Equals("http")
Expect(req.URL.RequestURI()).Equals("/")
Expand All @@ -51,7 +49,6 @@ func TestRequest_WithPort(t *testing.T) {

Expect(req.Method).Equals("GET")
Expect(req.GetHeader("Content-Type")).Equals("application/json")
Expect(req.ClientIP).Equals("")
Expect(req.URL.Hostname()).Equals("helloworld.com")
Expect(req.URL.Scheme).Equals("http")
Expect(req.URL.Port()).Equals("3000")
Expand All @@ -65,7 +62,6 @@ func TestRequest_BehindTLSTerminationProxy(t *testing.T) {
header := make(http.Header)
header.Set("X-Forwarded-Host", "feedback.mycompany.com")
header.Set("X-Forwarded-Proto", "https")
header.Set("X-Forwarded-For", "127.5.5.5, 129.2.2.2, 121.2.2.5")

req := web.WrapRequest(
&http.Request{
Expand All @@ -78,7 +74,6 @@ func TestRequest_BehindTLSTerminationProxy(t *testing.T) {
Expect(req.Method).Equals("GET")
Expect(req.URL.Hostname()).Equals("feedback.mycompany.com")
Expect(req.URL.Scheme).Equals("https")
Expect(req.ClientIP).Equals("127.5.5.5")
Expect(req.IsSecure).Equals(true)
Expect(req.IsAPI()).IsFalse()
}
Expand Down

0 comments on commit ca6e8a4

Please sign in to comment.