Skip to content

Commit

Permalink
Merge pull request oauth2-proxy#1638 from jacksgt/configure-upstream-…
Browse files Browse the repository at this point in the history
…timeout

Configure upstream timeout
  • Loading branch information
JoelSpeed authored May 20, 2022
2 parents 27f4bb6 + 7a27cb0 commit 086b869
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ If you are using an architecture specific tag (ex: v7.2.1-arm64) you should move
- [#1286](https://github.com/oauth2-proxy/oauth2-proxy/pull/1286) Add the `allowed_email_domains` and the `allowed_groups` on the `auth_request` + support standard wildcard char for validation with sub-domain and email-domain. (@w3st3ry @armandpicard)
- [#1361](https://github.com/oauth2-proxy/oauth2-proxy/pull/1541) PKCE Code Challenge Support - RFC-7636 (@braunsonm)
- [#1594](https://github.com/oauth2-proxy/oauth2-proxy/pull/1594) Release ARMv8 docker images (@braunsonm)
- [#1638](https://github.com/oauth2-proxy/oauth2-proxy/pull/1638) Implement configurable upstream timeout (@jacksgt)

# V7.2.1

Expand Down
1 change: 1 addition & 0 deletions docs/docs/configuration/alpha_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ Requests will be proxied to this upstream if the path matches the request path.
| `flushInterval` | _[Duration](#duration)_ | FlushInterval is the period between flushing the response buffer when<br/>streaming response from the upstream.<br/>Defaults to 1 second. |
| `passHostHeader` | _bool_ | PassHostHeader determines whether the request host header should be proxied<br/>to the upstream server.<br/>Defaults to true. |
| `proxyWebSockets` | _bool_ | ProxyWebSockets enables proxying of websockets to upstream servers<br/>Defaults to true. |
| `timeout` | _[Duration](#duration)_ | Timeout is the maximum duration the server will wait for a response from the upstream server.<br/>Defaults to 30 seconds. |
### UpstreamConfig
Expand Down
1 change: 1 addition & 0 deletions docs/docs/configuration/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ An example [oauth2-proxy.cfg](https://github.com/oauth2-proxy/oauth2-proxy/blob/
| `--tls-key-file` | string | path to private key file | |
| `--tls-min-version` | string | minimum TLS version that is acceptable, either `"TLS1.2"` or `"TLS1.3"` | `"TLS1.2"` |
| `--upstream` | string \| list | the http url(s) of the upstream endpoint, file:// paths for static files or `static://<status_code>` for static response. Routing is based on the path | |
| `--upstream-timeout` | duration | maximum amount of time the server will wait for a response from the upstream | 30s |
| `--allowed-group` | string \| list | restrict logins to members of this group (may be given multiple times) | |
| `--allowed-role` | string \| list | restrict logins to users with this role (may be given multiple times). Only works with the keycloak-oidc provider. | |
| `--validate-url` | string | Access token validation endpoint | |
Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ upstreamConfig:
flushInterval: 1s
passHostHeader: true
proxyWebSockets: true
timeout: 30s
injectRequestHeaders:
- name: Authorization
values:
Expand Down Expand Up @@ -118,6 +119,7 @@ redirect_url="http://localhost:4180/oauth2/callback"
FlushInterval: durationPtr(options.DefaultUpstreamFlushInterval),
PassHostHeader: boolPtr(true),
ProxyWebSockets: boolPtr(true),
Timeout: durationPtr(options.DefaultUpstreamTimeout),
},
},
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/options/legacy_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewLegacyOptions() *LegacyOptions {
PassHostHeader: true,
ProxyWebSockets: true,
FlushInterval: DefaultUpstreamFlushInterval,
Timeout: DefaultUpstreamTimeout,
},

LegacyHeaders: LegacyHeaders{
Expand Down Expand Up @@ -101,6 +102,7 @@ type LegacyUpstreams struct {
ProxyWebSockets bool `flag:"proxy-websockets" cfg:"proxy_websockets"`
SSLUpstreamInsecureSkipVerify bool `flag:"ssl-upstream-insecure-skip-verify" cfg:"ssl_upstream_insecure_skip_verify"`
Upstreams []string `flag:"upstream" cfg:"upstreams"`
Timeout time.Duration `flag:"upstream-timeout" cfg:"upstream_timeout"`
}

func legacyUpstreamsFlagSet() *pflag.FlagSet {
Expand All @@ -111,6 +113,7 @@ func legacyUpstreamsFlagSet() *pflag.FlagSet {
flagSet.Bool("proxy-websockets", true, "enables WebSocket proxying")
flagSet.Bool("ssl-upstream-insecure-skip-verify", false, "skip validation of certificates presented when using HTTPS upstreams")
flagSet.StringSlice("upstream", []string{}, "the http url(s) of the upstream endpoint, file:// paths for static files or static://<status_code> for static response. Routing is based on the path")
flagSet.Duration("upstream-timeout", DefaultUpstreamTimeout, "maximum amount of time the server will wait for a response from the upstream")

return flagSet
}
Expand All @@ -129,6 +132,7 @@ func (l *LegacyUpstreams) convert() (UpstreamConfig, error) {
}

flushInterval := Duration(l.FlushInterval)
timeout := Duration(l.Timeout)
upstream := Upstream{
ID: u.Path,
Path: u.Path,
Expand All @@ -137,6 +141,7 @@ func (l *LegacyUpstreams) convert() (UpstreamConfig, error) {
PassHostHeader: &l.PassHostHeader,
ProxyWebSockets: &l.ProxyWebSockets,
FlushInterval: &flushInterval,
Timeout: &timeout,
}

switch u.Scheme {
Expand Down Expand Up @@ -168,6 +173,7 @@ func (l *LegacyUpstreams) convert() (UpstreamConfig, error) {
upstream.PassHostHeader = nil
upstream.ProxyWebSockets = nil
upstream.FlushInterval = nil
upstream.Timeout = nil
}

upstreams.Upstreams = append(upstreams.Upstreams, upstream)
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/options/legacy_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var _ = Describe("Legacy Options", func() {

// Set upstreams and related options to test their conversion
flushInterval := Duration(5 * time.Second)
timeout := Duration(5 * time.Second)
legacyOpts.LegacyUpstreams.FlushInterval = time.Duration(flushInterval)
legacyOpts.LegacyUpstreams.Timeout = time.Duration(timeout)
legacyOpts.LegacyUpstreams.PassHostHeader = true
legacyOpts.LegacyUpstreams.ProxyWebSockets = true
legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
Expand All @@ -36,6 +38,7 @@ var _ = Describe("Legacy Options", func() {
InsecureSkipTLSVerify: true,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
Timeout: &timeout,
},
{
ID: "/bar",
Expand All @@ -45,6 +48,7 @@ var _ = Describe("Legacy Options", func() {
InsecureSkipTLSVerify: true,
PassHostHeader: &truth,
ProxyWebSockets: &truth,
Timeout: &timeout,
},
{
ID: "static://204",
Expand All @@ -56,6 +60,7 @@ var _ = Describe("Legacy Options", func() {
InsecureSkipTLSVerify: false,
PassHostHeader: nil,
ProxyWebSockets: nil,
Timeout: nil,
},
},
}
Expand Down Expand Up @@ -140,6 +145,7 @@ var _ = Describe("Legacy Options", func() {
passHostHeader := false
proxyWebSockets := true
flushInterval := Duration(5 * time.Second)
timeout := Duration(5 * time.Second)

// Test cases and expected outcomes
validHTTP := "http://foo.bar/baz"
Expand All @@ -151,6 +157,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
Timeout: &timeout,
}

// Test cases and expected outcomes
Expand All @@ -163,6 +170,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
Timeout: &timeout,
}

validFileWithFragment := "file:///var/lib/website#/bar"
Expand All @@ -174,6 +182,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: &passHostHeader,
ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval,
Timeout: &timeout,
}

validStatic := "static://204"
Expand All @@ -188,6 +197,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: nil,
ProxyWebSockets: nil,
FlushInterval: nil,
Timeout: nil,
}

invalidStatic := "static://abc"
Expand All @@ -202,6 +212,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: nil,
ProxyWebSockets: nil,
FlushInterval: nil,
Timeout: nil,
}

invalidHTTP := ":foo"
Expand All @@ -215,6 +226,7 @@ var _ = Describe("Legacy Options", func() {
PassHostHeader: passHostHeader,
ProxyWebSockets: proxyWebSockets,
FlushInterval: time.Duration(flushInterval),
Timeout: time.Duration(timeout),
}

upstreams, err := legacyUpstreams.convert()
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/options/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var _ = Describe("Load", func() {
PassHostHeader: true,
ProxyWebSockets: true,
FlushInterval: DefaultUpstreamFlushInterval,
Timeout: DefaultUpstreamTimeout,
},

LegacyHeaders: LegacyHeaders{
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/options/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "time"
const (
// DefaultUpstreamFlushInterval is the default value for the Upstream FlushInterval.
DefaultUpstreamFlushInterval = 1 * time.Second

// DefaultUpstreamTimeout is the maximum duration a network dial to a upstream server for a response.
DefaultUpstreamTimeout = 30 * time.Second
)

// UpstreamConfig is a collection of definitions for upstream servers.
Expand Down Expand Up @@ -84,4 +87,8 @@ type Upstream struct {
// ProxyWebSockets enables proxying of websockets to upstream servers
// Defaults to true.
ProxyWebSockets *bool `json:"proxyWebSockets,omitempty"`

// Timeout is the maximum duration the server will wait for a response from the upstream server.
// Defaults to 30 seconds.
Timeout *Duration `json:"timeout,omitempty"`
}
29 changes: 22 additions & 7 deletions pkg/upstream/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package upstream

import (
"crypto/tls"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -100,6 +99,14 @@ func (h *httpUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)
func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler ProxyErrorHandler) http.Handler {
proxy := httputil.NewSingleHostReverseProxy(target)

// Inherit default transport options from Go's stdlib
transport := http.DefaultTransport.(*http.Transport).Clone()

// Change default duration for waiting for an upstream response
if upstream.Timeout != nil {
transport.ResponseHeaderTimeout = upstream.Timeout.Duration()
}

// Configure options on the SingleHostReverseProxy
if upstream.FlushInterval != nil {
proxy.FlushInterval = upstream.FlushInterval.Duration()
Expand All @@ -110,9 +117,7 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
// InsecureSkipVerify is a configurable option we allow
/* #nosec G402 */
if upstream.InsecureSkipTLSVerify {
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
transport.TLSClientConfig.InsecureSkipVerify = true
}

// Ensure we always pass the original request path
Expand All @@ -127,6 +132,10 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
if errorHandler != nil {
proxy.ErrorHandler = errorHandler
}

// Apply the customized transport to our proxy before returning it
proxy.Transport = transport

return proxy
}

Expand Down Expand Up @@ -156,11 +165,17 @@ func setProxyDirector(proxy *httputil.ReverseProxy) {
// newWebSocketReverseProxy creates a new reverse proxy for proxying websocket connections.
func newWebSocketReverseProxy(u *url.URL, skipTLSVerify bool) http.Handler {
wsProxy := httputil.NewSingleHostReverseProxy(u)

// Inherit default transport options from Go's stdlib
transport := http.DefaultTransport.(*http.Transport).Clone()

/* #nosec G402 */
if skipTLSVerify {
wsProxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
transport.TLSClientConfig.InsecureSkipVerify = true
}

// Apply the customized transport to our proxy before returning it
wsProxy.Transport = transport

return wsProxy
}
Loading

0 comments on commit 086b869

Please sign in to comment.