git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add reverse proxy setting (#331)

* Add reverse proxy setting (#321)

commitd9362d3bb92e610ace7edff59db686783f379a54 authorMartin Campbell <martin@campbellsoftware.co.uk> dateFri 24 Jan 2020, 17:54 (7 years ago) parent79a0759
7 files changed+36−8
CHANGELOG.md+3 contrib/oauth2_proxy.cfg.example+3 docs/4_tls.md+1 docs/configuration/configuration.md+3−2 main.go+1 options.go+2 pkg/logger/logger.go+23−6
CHANGELOG.md+3
@@ -4,6 +4,9 @@
44
55## Breaking Changes
66
7+- [#321](https://github.com/pusher/oauth2_proxy/pull/331) Add reverse proxy boolean flag to control whether headers like `X-Real-Ip` are accepted.
8+ This defaults to false. **Usage behind a reverse proxy will require this flag to be set to avoid logging the reverse proxy IP address**.
9+
710## Changes since v4.1.0
811
912- [#339](https://github.com/pusher/oauth2_proxy/pull/339) Add configuration for cookie 'SameSite' value. (@pgroudas)
contrib/oauth2_proxy.cfg.example+3
@@ -5,6 +5,9 @@
55# http_address = "127.0.0.1:4180"
66# https_address = ":443"
77
8+## Are we running behind a reverse proxy? Will not accept headers like X-Real-Ip unless this is set.
9+# reverse_proxy = true
10+
811## TLS Settings
912# tls_cert_file = ""
1013# tls_key_file = ""
docs/4_tls.md+1
@@ -68,6 +68,7 @@ There are two recommended configurations.
6868 --cookie-secret=... \
6969 --cookie-secure=true \
7070 --provider=... \
71+ --reverse-proxy=true \
7172 --client-id=... \
7273 --client-secret=...
7374 ```
docs/configuration/configuration.md+3−2
@@ -91,6 +91,7 @@ An example [oauth2_proxy.cfg]({{ site.gitweb }}/contrib/oauth2_proxy.cfg.example
9191| `-request-logging` | bool | Log requests | true |
9292| `-request-logging-format` | string | Template for request log lines | see [Logging Configuration](#logging-configuration) |
9393| `-resource` | string | The resource that is protected (Azure AD only) | |
94+| `-reverse-proxy` | bool | are we running behind a reverse proxy, controls whether headers like X-Real-Ip are accepted | false |
9495| `-scope` | string | OAuth scope specification | |
9596| `-session-store-type` | string | Session data storage backend | cookie |
9697| `-set-xauthrequest` | bool | set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode) | false |
@@ -174,7 +175,7 @@ Available variables for auth logging:
174175
175176| Variable | Example | Description |
176177| --- | --- | --- |
177−| Client | 74.125.224.72 | The client/remote IP address. Will use the X-Real-IP header it if exists. |
178+| Client | 74.125.224.72 | The client/remote IP address. Will use the X-Real-IP header it if exists & reverse-proxy is set to true. |
178179| Host | domain.com | The value of the Host header. |
179180| Protocol | HTTP/1.0 | The request protocol. |
180181| RequestMethod | GET | The request method. |
@@ -202,7 +203,7 @@ Available variables for request logging:
202203
203204| Variable | Example | Description |
204205| --- | --- | --- |
205−| Client | 74.125.224.72 | The client/remote IP address. Will use the X-Real-IP header it if exists. |
206+| Client | 74.125.224.72 | The client/remote IP address. Will use the X-Real-IP header it if exists & reverse-proxy is set to true. |
206207| Host | domain.com | The value of the Host header. |
207208| Protocol | HTTP/1.0 | The request protocol. |
208209| RequestDuration | 0.001 | The time in seconds that a request took to process. |
main.go+1
@@ -32,6 +32,7 @@ func main() {
3232
3333 flagSet.String("http-address", "127.0.0.1:4180", "[http://]<addr>:<port> or unix://<path> to listen on for HTTP clients")
3434 flagSet.String("https-address", ":443", "<addr>:<port> to listen on for HTTPS clients")
35+ flagSet.Bool("reverse-proxy", false, "are we running behind a reverse proxy, controls whether headers like X-Real-Ip are accepted")
3536 flagSet.Bool("force-https", false, "force HTTPS redirect for HTTP requests")
3637 flagSet.String("tls-cert-file", "", "path to certificate file")
3738 flagSet.String("tls-key-file", "", "path to private key file")
options.go+2
@@ -34,6 +34,7 @@ type Options struct {
3434 ProxyWebSockets bool `flag:"proxy-websockets" cfg:"proxy_websockets" env:"OAUTH2_PROXY_PROXY_WEBSOCKETS"`
3535 HTTPAddress string `flag:"http-address" cfg:"http_address" env:"OAUTH2_PROXY_HTTP_ADDRESS"`
3636 HTTPSAddress string `flag:"https-address" cfg:"https_address" env:"OAUTH2_PROXY_HTTPS_ADDRESS"`
37+ ReverseProxy bool `flag:"reverse-proxy" cfg:"reverse_proxy" env:"OAUTH2_PROXY_REVERSE_PROXY"`
3738 ForceHTTPS bool `flag:"force-https" cfg:"force_https" env:"OAUTH2_PROXY_FORCE_HTTPS"`
3839 RedirectURL string `flag:"redirect-url" cfg:"redirect_url" env:"OAUTH2_PROXY_REDIRECT_URL"`
3940 ClientID string `flag:"client-id" cfg:"client_id" env:"OAUTH2_PROXY_CLIENT_ID"`
@@ -617,6 +618,7 @@ func setupLogger(o *Options, msgs []string) []string {
617618 logger.SetStandardTemplate(o.StandardLoggingFormat)
618619 logger.SetAuthTemplate(o.AuthLoggingFormat)
619620 logger.SetReqTemplate(o.RequestLoggingFormat)
621+ logger.SetReverseProxy(o.ReverseProxy)
620622
621623 excludePaths := make([]string, 0)
622624 excludePaths = append(excludePaths, strings.Split(o.ExcludeLoggingPaths, ",")...)
pkg/logger/logger.go+23−6
@@ -88,6 +88,7 @@ type Logger struct {
8888 stdEnabled bool
8989 authEnabled bool
9090 reqEnabled bool
91+ reverseProxy bool
9192 excludePaths map[string]struct{}
9293 stdLogTemplate *template.Template
9394 authTemplate *template.Template
@@ -102,6 +103,7 @@ func New(flag int) *Logger {
102103 stdEnabled: true,
103104 authEnabled: true,
104105 reqEnabled: true,
106+ reverseProxy: false,
105107 excludePaths: nil,
106108 stdLogTemplate: template.Must(template.New("std-log").Parse(DefaultStandardLoggingFormat)),
107109 authTemplate: template.Must(template.New("auth-log").Parse(DefaultAuthLoggingFormat)),
@@ -151,7 +153,7 @@ func (l *Logger) PrintAuth(username string, req *http.Request, status AuthStatus
151153 username = "-"
152154 }
153155
154− client := GetClient(req)
156+ client := GetClient(req, l.reverseProxy)
155157
156158 l.mu.Lock()
157159 defer l.mu.Unlock()
@@ -199,7 +201,7 @@ func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.
199201 }
200202 }
201203
202− client := GetClient(req)
204+ client := GetClient(req, l.reverseProxy)
203205
204206 l.mu.Lock()
205207 defer l.mu.Unlock()
@@ -251,10 +253,12 @@ func (l *Logger) GetFileLineString(calldepth int) string {
251253}
252254
253255// GetClient parses an HTTP request for the client/remote IP address.
254−func GetClient(req *http.Request) string {
255− client := req.Header.Get("X-Real-IP")
256− if client == "" {
257− client = req.RemoteAddr
256+func GetClient(req *http.Request, reverseProxy bool) string {
257+ client := req.RemoteAddr
258+ if reverseProxy {
259+ if ip := req.Header.Get("X-Real-IP"); ip != "" {
260+ client = ip
261+ }
258262 }
259263
260264 if c, _, err := net.SplitHostPort(client); err == nil {
@@ -308,6 +312,13 @@ func (l *Logger) SetReqEnabled(e bool) {
308312 l.reqEnabled = e
309313}
310314
315+// SetReverseProxy controls whether logging will trust headers that can be set by a reverse proxy.
316+func (l *Logger) SetReverseProxy(e bool) {
317+ l.mu.Lock()
318+ defer l.mu.Unlock()
319+ l.reverseProxy = e
320+}
321+
311322// SetExcludePaths sets the paths to exclude from logging.
312323func (l *Logger) SetExcludePaths(s []string) {
313324 l.mu.Lock()
@@ -381,6 +392,12 @@ func SetReqEnabled(e bool) {
381392 std.SetReqEnabled(e)
382393}
383394
395+// SetReverseProxy controls whether logging will trust headers that can be set
396+// by a reverse proxy for the standard logger.
397+func SetReverseProxy(e bool) {
398+ std.SetReverseProxy(e)
399+}
400+
384401// SetExcludePaths sets the path to exclude from logging, eg: health checks
385402func SetExcludePaths(s []string) {
386403 std.SetExcludePaths(s)