1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-04-23 12:18:50 +02:00

Use bool pointers for upstream options that default to true

This commit is contained in:
Joel Speed 2020-07-19 14:00:52 +01:00
parent 6b27069812
commit d43b372ca9
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
7 changed files with 41 additions and 38 deletions

View File

@ -77,8 +77,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
Path: u.Path, Path: u.Path,
URI: upstreamString, URI: upstreamString,
InsecureSkipTLSVerify: l.SSLUpstreamInsecureSkipVerify, InsecureSkipTLSVerify: l.SSLUpstreamInsecureSkipVerify,
PassHostHeader: l.PassHostHeader, PassHostHeader: &l.PassHostHeader,
ProxyWebSockets: l.ProxyWebSockets, ProxyWebSockets: &l.ProxyWebSockets,
FlushInterval: &l.FlushInterval, FlushInterval: &l.FlushInterval,
} }
@ -104,8 +104,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
// Force defaults compatible with static responses // Force defaults compatible with static responses
upstream.URI = "" upstream.URI = ""
upstream.InsecureSkipTLSVerify = false upstream.InsecureSkipTLSVerify = false
upstream.PassHostHeader = true upstream.PassHostHeader = nil
upstream.ProxyWebSockets = false upstream.ProxyWebSockets = nil
flush := 1 * time.Second flush := 1 * time.Second
upstream.FlushInterval = &flush upstream.FlushInterval = &flush
} }

View File

@ -23,6 +23,7 @@ var _ = Describe("Legacy Options", func() {
legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"} legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"}
truth := true
opts.UpstreamServers = Upstreams{ opts.UpstreamServers = Upstreams{
{ {
ID: "/baz", ID: "/baz",
@ -30,8 +31,8 @@ var _ = Describe("Legacy Options", func() {
URI: "http://foo.bar/baz", URI: "http://foo.bar/baz",
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
InsecureSkipTLSVerify: true, InsecureSkipTLSVerify: true,
PassHostHeader: true, PassHostHeader: &truth,
ProxyWebSockets: true, ProxyWebSockets: &truth,
}, },
{ {
ID: "/bar", ID: "/bar",
@ -39,8 +40,8 @@ var _ = Describe("Legacy Options", func() {
URI: "file://var/lib/website#/bar", URI: "file://var/lib/website#/bar",
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
InsecureSkipTLSVerify: true, InsecureSkipTLSVerify: true,
PassHostHeader: true, PassHostHeader: &truth,
ProxyWebSockets: true, ProxyWebSockets: &truth,
}, },
} }
@ -72,8 +73,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/baz", Path: "/baz",
URI: validHTTP, URI: validHTTP,
InsecureSkipTLSVerify: skipVerify, InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader, PassHostHeader: &passHostHeader,
ProxyWebSockets: proxyWebSockets, ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
} }
@ -84,8 +85,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/", Path: "/",
URI: emptyPathHTTP, URI: emptyPathHTTP,
InsecureSkipTLSVerify: skipVerify, InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader, PassHostHeader: &passHostHeader,
ProxyWebSockets: proxyWebSockets, ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
} }
@ -95,8 +96,8 @@ var _ = Describe("Legacy Options", func() {
Path: "/bar", Path: "/bar",
URI: validFileWithFragment, URI: validFileWithFragment,
InsecureSkipTLSVerify: skipVerify, InsecureSkipTLSVerify: skipVerify,
PassHostHeader: passHostHeader, PassHostHeader: &passHostHeader,
ProxyWebSockets: proxyWebSockets, ProxyWebSockets: &proxyWebSockets,
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
} }
@ -109,8 +110,8 @@ var _ = Describe("Legacy Options", func() {
Static: true, Static: true,
StaticCode: &validStaticCode, StaticCode: &validStaticCode,
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
PassHostHeader: true, PassHostHeader: nil,
ProxyWebSockets: false, ProxyWebSockets: nil,
FlushInterval: &defaultFlushInterval, FlushInterval: &defaultFlushInterval,
} }
@ -123,8 +124,8 @@ var _ = Describe("Legacy Options", func() {
Static: true, Static: true,
StaticCode: &invalidStaticCode, StaticCode: &invalidStaticCode,
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
PassHostHeader: true, PassHostHeader: nil,
ProxyWebSockets: false, ProxyWebSockets: nil,
FlushInterval: &defaultFlushInterval, FlushInterval: &defaultFlushInterval,
} }

View File

@ -52,9 +52,9 @@ type Upstream struct {
// PassHostHeader determines whether the request host header should be proxied // PassHostHeader determines whether the request host header should be proxied
// to the upstream server. // to the upstream server.
// Defaults to true. // Defaults to true.
PassHostHeader bool `json:"passHostHeader"` PassHostHeader *bool `json:"passHostHeader"`
// ProxyWebSockets enables proxying of websockets to upstream servers // ProxyWebSockets enables proxying of websockets to upstream servers
// Defaults to true. // Defaults to true.
ProxyWebSockets bool `json:"proxyWebSockets"` ProxyWebSockets *bool `json:"proxyWebSockets"`
} }

View File

@ -49,7 +49,7 @@ func newHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *option
// Set up a WebSocket proxy if required // Set up a WebSocket proxy if required
var wsProxy http.Handler var wsProxy http.Handler
if upstream.ProxyWebSockets { if upstream.ProxyWebSockets == nil || *upstream.ProxyWebSockets {
wsProxy = newWebSocketReverseProxy(u, upstream.InsecureSkipTLSVerify) wsProxy = newWebSocketReverseProxy(u, upstream.InsecureSkipTLSVerify)
} }
@ -110,7 +110,7 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
} }
// Set the request director based on the PassHostHeader option // Set the request director based on the PassHostHeader option
if !upstream.PassHostHeader { if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
setProxyUpstreamHostHeader(proxy, target) setProxyUpstreamHostHeader(proxy, target)
} else { } else {
setProxyDirector(proxy) setProxyDirector(proxy)

View File

@ -24,6 +24,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
const flushInterval5s = 5 * time.Second const flushInterval5s = 5 * time.Second
const flushInterval1s = 1 * time.Second const flushInterval1s = 1 * time.Second
truth := true
falsum := false
type httpUpstreamTableInput struct { type httpUpstreamTableInput struct {
id string id string
@ -51,10 +53,11 @@ var _ = Describe("HTTP Upstream Suite", func() {
rw := httptest.NewRecorder() rw := httptest.NewRecorder()
flush := 1 * time.Second flush := 1 * time.Second
upstream := options.Upstream{ upstream := options.Upstream{
ID: in.id, ID: in.id,
PassHostHeader: true, PassHostHeader: &truth,
ProxyWebSockets: false, ProxyWebSockets: &falsum,
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
FlushInterval: &flush, FlushInterval: &flush,
} }
@ -258,8 +261,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
flush := 1 * time.Second flush := 1 * time.Second
upstream := options.Upstream{ upstream := options.Upstream{
ID: "noPassHost", ID: "noPassHost",
PassHostHeader: false, PassHostHeader: &falsum,
ProxyWebSockets: false, ProxyWebSockets: &falsum,
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
FlushInterval: &flush, FlushInterval: &flush,
} }
@ -302,7 +305,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
ID: "foo123", ID: "foo123",
FlushInterval: &in.flushInterval, FlushInterval: &in.flushInterval,
InsecureSkipTLSVerify: in.skipVerify, InsecureSkipTLSVerify: in.skipVerify,
ProxyWebSockets: in.proxyWebSockets, ProxyWebSockets: &in.proxyWebSockets,
} }
handler := newHTTPUpstreamProxy(upstream, u, in.sigData, in.errorHandler) handler := newHTTPUpstreamProxy(upstream, u, in.sigData, in.errorHandler)
@ -370,8 +373,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
flush := 1 * time.Second flush := 1 * time.Second
upstream := options.Upstream{ upstream := options.Upstream{
ID: "websocketProxy", ID: "websocketProxy",
PassHostHeader: true, PassHostHeader: &truth,
ProxyWebSockets: true, ProxyWebSockets: &truth,
InsecureSkipTLSVerify: false, InsecureSkipTLSVerify: false,
FlushInterval: &flush, FlushInterval: &flush,
} }

View File

@ -73,10 +73,10 @@ func validateStaticUpstream(upstream options.Upstream) []string {
if upstream.FlushInterval != nil && *upstream.FlushInterval != time.Second { if upstream.FlushInterval != nil && *upstream.FlushInterval != time.Second {
msgs = append(msgs, fmt.Sprintf("upstream %q has flushInterval, but is a static upstream, this will have no effect.", upstream.ID)) msgs = append(msgs, fmt.Sprintf("upstream %q has flushInterval, but is a static upstream, this will have no effect.", upstream.ID))
} }
if !upstream.PassHostHeader { if upstream.PassHostHeader != nil {
msgs = append(msgs, fmt.Sprintf("upstream %q has passHostHeader, but is a static upstream, this will have no effect.", upstream.ID)) msgs = append(msgs, fmt.Sprintf("upstream %q has passHostHeader, but is a static upstream, this will have no effect.", upstream.ID))
} }
if !upstream.ProxyWebSockets { if upstream.ProxyWebSockets != nil {
msgs = append(msgs, fmt.Sprintf("upstream %q has proxyWebSockets, but is a static upstream, this will have no effect.", upstream.ID)) msgs = append(msgs, fmt.Sprintf("upstream %q has proxyWebSockets, but is a static upstream, this will have no effect.", upstream.ID))
} }

View File

@ -17,6 +17,7 @@ var _ = Describe("Upstreams", func() {
flushInterval := 5 * time.Second flushInterval := 5 * time.Second
staticCode200 := 200 staticCode200 := 200
truth := true
validHTTPUpstream := options.Upstream{ validHTTPUpstream := options.Upstream{
ID: "validHTTPUpstream", ID: "validHTTPUpstream",
@ -27,8 +28,6 @@ var _ = Describe("Upstreams", func() {
ID: "validStaticUpstream", ID: "validStaticUpstream",
Path: "/validStaticUpstream", Path: "/validStaticUpstream",
Static: true, Static: true,
PassHostHeader: true, // This would normally be defaulted
ProxyWebSockets: true, // this would normally be defaulted
} }
validFileUpstream := options.Upstream{ validFileUpstream := options.Upstream{
ID: "validFileUpstream", ID: "validFileUpstream",
@ -134,8 +133,8 @@ var _ = Describe("Upstreams", func() {
URI: "ftp://foo", URI: "ftp://foo",
Static: true, Static: true,
FlushInterval: &flushInterval, FlushInterval: &flushInterval,
PassHostHeader: false, PassHostHeader: &truth,
ProxyWebSockets: false, ProxyWebSockets: &truth,
InsecureSkipTLSVerify: true, InsecureSkipTLSVerify: true,
}, },
}, },