|
@@ -14,6 +14,14 @@ import (
|
|
|
"github.com/smallstep/certificates/middleware/requestid"
|
|
"github.com/smallstep/certificates/middleware/requestid"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// Common headers used for identifying the originating IP address of a client
|
|
|
|
|
+// connecting to a web server through a proxy server
|
|
|
|
|
+var (
|
|
|
|
|
+ trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
|
|
|
|
|
+ xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
|
+ xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
// LoggerHandler creates a logger handler
|
|
// LoggerHandler creates a logger handler
|
|
|
type LoggerHandler struct {
|
|
type LoggerHandler struct {
|
|
|
name string
|
|
name string
|
|
@@ -28,16 +36,23 @@ type options struct {
|
|
|
// endpoint should only be logged at the TRACE level in the (expected) HTTP
|
|
// endpoint should only be logged at the TRACE level in the (expected) HTTP
|
|
|
// 200 case
|
|
// 200 case
|
|
|
onlyTraceHealthEndpoint bool
|
|
onlyTraceHealthEndpoint bool
|
|
|
|
|
+
|
|
|
|
|
+ // logRealIP determines if the real IP address of the client should be logged
|
|
|
|
|
+ // instead of the IP address of the proxy
|
|
|
|
|
+ logRealIP bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// NewLoggerHandler returns the given http.Handler with the logger integrated.
|
|
// NewLoggerHandler returns the given http.Handler with the logger integrated.
|
|
|
func NewLoggerHandler(name string, logger *Logger, next http.Handler) http.Handler {
|
|
func NewLoggerHandler(name string, logger *Logger, next http.Handler) http.Handler {
|
|
|
onlyTraceHealthEndpoint, _ := strconv.ParseBool(os.Getenv("STEP_LOGGER_ONLY_TRACE_HEALTH_ENDPOINT"))
|
|
onlyTraceHealthEndpoint, _ := strconv.ParseBool(os.Getenv("STEP_LOGGER_ONLY_TRACE_HEALTH_ENDPOINT"))
|
|
|
|
|
+ logRealIP, _ := strconv.ParseBool(os.Getenv("STEP_LOGGER_LOG_REAL_IP"))
|
|
|
|
|
+
|
|
|
return &LoggerHandler{
|
|
return &LoggerHandler{
|
|
|
name: name,
|
|
name: name,
|
|
|
logger: logger.GetImpl(),
|
|
logger: logger.GetImpl(),
|
|
|
options: options{
|
|
options: options{
|
|
|
onlyTraceHealthEndpoint: onlyTraceHealthEndpoint,
|
|
onlyTraceHealthEndpoint: onlyTraceHealthEndpoint,
|
|
|
|
|
+ logRealIP: logRealIP,
|
|
|
},
|
|
},
|
|
|
next: next,
|
|
next: next,
|
|
|
}
|
|
}
|
|
@@ -67,9 +82,12 @@ func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Tim
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Remote hostname
|
|
// Remote hostname
|
|
|
- addr, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- addr = r.RemoteAddr
|
|
|
|
|
|
|
+ addr := r.RemoteAddr
|
|
|
|
|
+ if l.options.logRealIP {
|
|
|
|
|
+ addr = realIP(r)
|
|
|
|
|
+ }
|
|
|
|
|
+ if host, _, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
|
+ addr = host
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// From https://github.com/gorilla/handlers
|
|
// From https://github.com/gorilla/handlers
|
|
@@ -125,3 +143,27 @@ func sanitizeLogEntry(s string) string {
|
|
|
escaped := strings.ReplaceAll(s, "\n", "")
|
|
escaped := strings.ReplaceAll(s, "\n", "")
|
|
|
return strings.ReplaceAll(escaped, "\r", "")
|
|
return strings.ReplaceAll(escaped, "\r", "")
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// realIP returns the real IP address of the client connecting to the server by
|
|
|
|
|
+// parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
|
|
|
|
|
+// (in that order). If the headers are not set or set to an invalid IP, it
|
|
|
|
|
+// returns the RemoteAddr of the request.
|
|
|
|
|
+func realIP(r *http.Request) string {
|
|
|
|
|
+ var ip string
|
|
|
|
|
+
|
|
|
|
|
+ if tcip := r.Header.Get(trueClientIP); tcip != "" {
|
|
|
|
|
+ ip = tcip
|
|
|
|
|
+ } else if xrip := r.Header.Get(xRealIP); xrip != "" {
|
|
|
|
|
+ ip = xrip
|
|
|
|
|
+ } else if xff := r.Header.Get(xForwardedFor); xff != "" {
|
|
|
|
|
+ i := strings.Index(xff, ",")
|
|
|
|
|
+ if i == -1 {
|
|
|
|
|
+ i = len(xff)
|
|
|
|
|
+ }
|
|
|
|
|
+ ip = xff[:i]
|
|
|
|
|
+ }
|
|
|
|
|
+ if ip == "" || net.ParseIP(ip) == nil {
|
|
|
|
|
+ return r.RemoteAddr
|
|
|
|
|
+ }
|
|
|
|
|
+ return ip
|
|
|
|
|
+}
|