Skip to content

Commit 107237c

Browse files
authored
[feature] Make client IP logging configurable (#1799)
1 parent 68e54cb commit 107237c

File tree

9 files changed

+40
-12
lines changed

9 files changed

+40
-12
lines changed

cmd/gotosocial/action/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
162162
middlewares = append(middlewares, []gin.HandlerFunc{
163163
// note: hooks adding ctx fields must be ABOVE
164164
// the logger, otherwise won't be accessible.
165-
middleware.Logger(),
165+
middleware.Logger(config.GetLogClientIP()),
166166
middleware.UserAgent(),
167167
middleware.CORS(),
168168
middleware.ExtraHeaders(),

cmd/gotosocial/action/testrig/testrig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
107107
middlewares = append(middlewares, tracing.InstrumentGin())
108108
}
109109
middlewares = append(middlewares, []gin.HandlerFunc{
110-
middleware.Logger(),
110+
middleware.Logger(config.GetLogClientIP()),
111111
middleware.UserAgent(),
112112
middleware.CORS(),
113113
middleware.ExtraHeaders(),

example/config.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ log-level: "info"
3030
# Default: false
3131
log-db-queries: false
3232

33+
# Bool. Include the client IP in the emitted log lines
34+
# Options: [true, false]
35+
# Default: true
36+
log-client-ip: true
37+
3338
# String. Application name to use internally.
3439
# Examples: ["My Application","gotosocial"]
3540
# Default: "gotosocial"
@@ -766,10 +771,6 @@ syslog-address: "localhost:514"
766771
##### OBSERVABILITY SETTINGS #####
767772
##################################
768773

769-
# Bool. Enable generation/parsing of a request ID for each received HTTP Request.
770-
# Default: true
771-
request-id-enabled: true
772-
773774
# String. Header name to use to extract a request or trace ID from. Typically set by a
774775
# loadbalancer or proxy.
775776
# Default: "X-Request-Id"

internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func fieldtag(field, tag string) string {
4646
type Configuration struct {
4747
LogLevel string `name:"log-level" usage:"Log level to run at: [trace, debug, info, warn, fatal]"`
4848
LogDbQueries bool `name:"log-db-queries" usage:"Log database queries verbosely when log-level is trace or debug"`
49+
LogClientIP bool `name:"log-client-ip" usage:"Include the client IP in logs"`
4950
ApplicationName string `name:"application-name" usage:"Name of the application, used in various places internally"`
5051
LandingPageUser string `name:"landing-page-user" usage:"the user that should be shown on the instance's landing page"`
5152
ConfigPath string `name:"config-path" usage:"Path to a file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments"`

internal/config/defaults.go

+2
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,6 @@ var Defaults = Configuration{
198198
AdminMediaPruneDryRun: true,
199199

200200
RequestIDHeader: "X-Request-Id",
201+
202+
LogClientIP: true,
201203
}

internal/config/helpers.gen.go

+25
Original file line numberDiff line numberDiff line change
@@ -3679,3 +3679,28 @@ func GetRequestIDHeader() string { return global.GetRequestIDHeader() }
36793679

36803680
// SetRequestIDHeader safely sets the value for global configuration 'RequestIDHeader' field
36813681
func SetRequestIDHeader(v string) { global.SetRequestIDHeader(v) }
3682+
3683+
// GetLogClientIP safely fetches the Configuration value for state's 'LogClientIP' field
3684+
func (st *ConfigState) GetLogClientIP() (v bool) {
3685+
st.mutex.Lock()
3686+
v = st.config.LogClientIP
3687+
st.mutex.Unlock()
3688+
return
3689+
}
3690+
3691+
// SetLogClientIP safely sets the Configuration value for state's 'LogClientIP' field
3692+
func (st *ConfigState) SetLogClientIP(v bool) {
3693+
st.mutex.Lock()
3694+
defer st.mutex.Unlock()
3695+
st.config.LogClientIP = v
3696+
st.reloadToViper()
3697+
}
3698+
3699+
// LogClientIPFlag returns the flag name for the 'LogClientIP' field
3700+
func LogClientIPFlag() string { return "log-client-ip" }
3701+
3702+
// GetLogClientIP safely fetches the value for global configuration 'LogClientIP' field
3703+
func GetLogClientIP() bool { return global.GetLogClientIP() }
3704+
3705+
// SetLogClientIP safely sets the value for global configuration 'LogClientIP' field
3706+
func SetLogClientIP(v bool) { global.SetLogClientIP(v) }

internal/gtscontext/log_hooks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func init() {
3434
}
3535
return kvs
3636
})
37-
// Client IP middleware hook.
37+
// Public Key ID middleware hook.
3838
log.Hook(func(ctx context.Context, kvs []kv.Field) []kv.Field {
3939
if id := PublicKeyID(ctx); id != "" {
4040
return append(kvs, kv.Field{K: "pubKeyID", V: id})

internal/middleware/logger.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
// Logger returns a gin middleware which provides request logging and panic recovery.
34-
func Logger() gin.HandlerFunc {
34+
func Logger(logClientIP bool) gin.HandlerFunc {
3535
return func(c *gin.Context) {
3636
// Initialize the logging fields
3737
fields := make(kv.Fields, 5, 7)
@@ -72,10 +72,7 @@ func Logger() gin.HandlerFunc {
7272
fields[2] = kv.Field{"method", c.Request.Method}
7373
fields[3] = kv.Field{"statusCode", code}
7474
fields[4] = kv.Field{"path", path}
75-
if includeClientIP := true; includeClientIP {
76-
// TODO: make this configurable.
77-
//
78-
// Include clientIP if enabled.
75+
if logClientIP {
7976
fields = append(fields, kv.Field{
8077
"clientIP", c.ClientIP(),
8178
})

test/envparsing.sh

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ EXPECT=$(cat <<"EOF"
9898
"letsencrypt-email-address": "",
9999
"letsencrypt-enabled": true,
100100
"letsencrypt-port": 80,
101+
"log-client-ip": false,
101102
"log-db-queries": true,
102103
"log-level": "info",
103104
"media-description-max-chars": 5000,
@@ -170,6 +171,7 @@ EOF
170171
# ensure that these are parsed without panic
171172
OUTPUT=$(GTS_LOG_LEVEL='info' \
172173
GTS_LOG_DB_QUERIES=true \
174+
GTS_LOG_CLIENT_IP=false \
173175
GTS_APPLICATION_NAME=gts \
174176
GTS_LANDING_PAGE_USER=admin \
175177
GTS_HOST=example.com \

0 commit comments

Comments
 (0)