From 81b167d373c12d2a2c9949caeb0329c058a5aebd Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Fri, 22 May 2020 14:39:36 -0700 Subject: [PATCH 1/2] support/http: log user agent header at request start ### What Log the `User-Agent` header at request start. ### Why Identifying which agent sent a request can be useful in the reference implementations that accept inbound requests. It can be useful for debugging strange issues, understanding broad usage patterns, and if a service is deployed in use with a specific client and that client uses the User-Agent header to communicate its version it can help an operator understand what versions of its client are accessing the service. This can help make decisions about upgrade plans, etc. The specific use case I have is that we are deploying webauth and recoverysigner with a client and I want to be able to know which version of the client is accessing these services. Horizon and the Horizon SDKs actually use some custom headers to achieve this specific goal but I am not repeating that pattern since it is more common to communicate this information in the user agent header. The header does not normally contain any personally identifiable information or sensitive information and so I believe there are no concerns on that front. --- support/http/logging_middleware.go | 11 ++++++----- support/http/logging_middleware_test.go | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/support/http/logging_middleware.go b/support/http/logging_middleware.go index cb889969ab..adeab3cdcd 100644 --- a/support/http/logging_middleware.go +++ b/support/http/logging_middleware.go @@ -49,11 +49,12 @@ func logStartOfRequest( r *stdhttp.Request, ) { log.Ctx(r.Context()).WithFields(log.F{ - "subsys": "http", - "path": r.URL.String(), - "method": r.Method, - "ip": r.RemoteAddr, - "host": r.Host, + "subsys": "http", + "path": r.URL.String(), + "method": r.Method, + "ip": r.RemoteAddr, + "host": r.Host, + "useragent": r.Header.Get("User-Agent"), }).Info("starting request") } diff --git a/support/http/logging_middleware_test.go b/support/http/logging_middleware_test.go index e5ac8e371b..64eea40367 100644 --- a/support/http/logging_middleware_test.go +++ b/support/http/logging_middleware_test.go @@ -36,6 +36,7 @@ func TestHTTPMiddleware(t *testing.T) { assert.Equal(t, "GET", logged[0].Data["method"]) assert.NotEmpty(t, logged[0].Data["req"]) assert.NotEmpty(t, logged[0].Data["path"]) + assert.Equal(t, "Go-http-client/1.1", logged[0].Data["useragent"]) req1 := logged[0].Data["req"] assert.Equal(t, "handler log line", logged[1].Message) @@ -52,6 +53,7 @@ func TestHTTPMiddleware(t *testing.T) { assert.Equal(t, "GET", logged[3].Data["method"]) assert.NotEmpty(t, logged[3].Data["req"]) assert.NotEmpty(t, logged[3].Data["path"]) + assert.Equal(t, "Go-http-client/1.1", logged[3].Data["useragent"]) req2 := logged[3].Data["req"] assert.Equal(t, "finished request", logged[4].Message) @@ -89,6 +91,7 @@ func TestHTTPMiddlewareWithLoggerSet(t *testing.T) { assert.Equal(t, "GET", logged[0].Data["method"]) assert.NotEmpty(t, logged[0].Data["req"]) assert.NotEmpty(t, logged[0].Data["path"]) + assert.Equal(t, "Go-http-client/1.1", logged[0].Data["useragent"]) req1 := logged[0].Data["req"] assert.Equal(t, "handler log line", logged[1].Message) @@ -105,6 +108,7 @@ func TestHTTPMiddlewareWithLoggerSet(t *testing.T) { assert.Equal(t, "GET", logged[3].Data["method"]) assert.NotEmpty(t, logged[3].Data["req"]) assert.NotEmpty(t, logged[3].Data["path"]) + assert.Equal(t, "Go-http-client/1.1", logged[3].Data["useragent"]) req2 := logged[3].Data["req"] assert.Equal(t, "finished request", logged[4].Message) From 859c915bc348cf75d81ac330c717154993c567bf Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Fri, 22 May 2020 14:55:12 -0700 Subject: [PATCH 2/2] Update CHANGELOGs --- services/federation/CHANGELOG.md | 1 + services/friendbot/CHANGELOG.md | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/services/federation/CHANGELOG.md b/services/federation/CHANGELOG.md index 06e6654edb..95da62e8b9 100644 --- a/services/federation/CHANGELOG.md +++ b/services/federation/CHANGELOG.md @@ -9,6 +9,7 @@ bumps. A breaking change will get clearly notified in this log. ## Unreleased * Dropped support for Go 1.12. +* Log User-Agent header in request logs. ## [v0.3.0] - 2019-11-20 diff --git a/services/friendbot/CHANGELOG.md b/services/friendbot/CHANGELOG.md index 87b6499b4f..05d09ac778 100644 --- a/services/friendbot/CHANGELOG.md +++ b/services/friendbot/CHANGELOG.md @@ -6,6 +6,10 @@ file. This project adheres to [Semantic Versioning](http://semver.org/). As this project is pre 1.0, breaking changes may happen for minor version bumps. A breaking change will get clearly notified in this log. +## Unreleased + +* Log User-Agent header in request logs. + ## [v0.0.2] - 2019-11-20 ### Changed