git.iko.soy/typeable/oauth2-proxy
on master help sign in
Use bool pointers for upstream options that default to true
commitd43b372ca9bb72a35fcdee72326de9374f9cb60c authorJoel Speed <joel.speed@hotmail.co.uk> dateSun 19 Jul 2020, 14:00 (6 years ago) parent6b27069
7 files changed+41−38
pkg/apis/options/legacy_options.go+4−4 pkg/apis/options/legacy_options_test.go+15−14 pkg/apis/options/upstreams.go+2−2 pkg/upstream/http.go+2−2 pkg/upstream/http_test.go+10−7 pkg/validation/upstreams.go+2−2 pkg/validation/upstreams_test.go+6−7
pkg/apis/options/legacy_options.go+4−4
@@ -77,8 +77,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
7777 Path: u.Path,
7878 URI: upstreamString,
7979 InsecureSkipTLSVerify: l.SSLUpstreamInsecureSkipVerify,
80− PassHostHeader: l.PassHostHeader,
81− ProxyWebSockets: l.ProxyWebSockets,
80+ PassHostHeader: &l.PassHostHeader,
81+ ProxyWebSockets: &l.ProxyWebSockets,
8282 FlushInterval: &l.FlushInterval,
8383 }
8484
@@ -104,8 +104,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
104104 // Force defaults compatible with static responses
105105 upstream.URI = ""
106106 upstream.InsecureSkipTLSVerify = false
107− upstream.PassHostHeader = true
108− upstream.ProxyWebSockets = false
107+ upstream.PassHostHeader = nil
108+ upstream.ProxyWebSockets = nil
109109 flush := 1 * time.Second
110110 upstream.FlushInterval = &flush
111111 }
pkg/apis/options/legacy_options_test.go+15−14
@@ -23,6 +23,7 @@ var _ = Describe("Legacy Options", func() {
2323 legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
2424 legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"}
2525
26+ truth := true
2627 opts.UpstreamServers = Upstreams{
2728 {
2829 ID: "/baz",
@@ -30,8 +31,8 @@ var _ = Describe("Legacy Options", func() {
3031 URI: "http://foo.bar/baz",
3132 FlushInterval: &flushInterval,
3233 InsecureSkipTLSVerify: true,
33− PassHostHeader: true,
34− ProxyWebSockets: true,
34+ PassHostHeader: &truth,
35+ ProxyWebSockets: &truth,
3536 },
3637 {
3738 ID: "/bar",
@@ -39,8 +40,8 @@ var _ = Describe("Legacy Options", func() {
3940 URI: "file://var/lib/website#/bar",
4041 FlushInterval: &flushInterval,
4142 InsecureSkipTLSVerify: true,
42− PassHostHeader: true,
43− ProxyWebSockets: true,
43+ PassHostHeader: &truth,
44+ ProxyWebSockets: &truth,
4445 },
4546 }
4647
@@ -72,8 +73,8 @@ var _ = Describe("Legacy Options", func() {
7273 Path: "/baz",
7374 URI: validHTTP,
7475 InsecureSkipTLSVerify: skipVerify,
75− PassHostHeader: passHostHeader,
76− ProxyWebSockets: proxyWebSockets,
76+ PassHostHeader: &passHostHeader,
77+ ProxyWebSockets: &proxyWebSockets,
7778 FlushInterval: &flushInterval,
7879 }
7980
@@ -84,8 +85,8 @@ var _ = Describe("Legacy Options", func() {
8485 Path: "/",
8586 URI: emptyPathHTTP,
8687 InsecureSkipTLSVerify: skipVerify,
87− PassHostHeader: passHostHeader,
88− ProxyWebSockets: proxyWebSockets,
88+ PassHostHeader: &passHostHeader,
89+ ProxyWebSockets: &proxyWebSockets,
8990 FlushInterval: &flushInterval,
9091 }
9192
@@ -95,8 +96,8 @@ var _ = Describe("Legacy Options", func() {
9596 Path: "/bar",
9697 URI: validFileWithFragment,
9798 InsecureSkipTLSVerify: skipVerify,
98− PassHostHeader: passHostHeader,
99− ProxyWebSockets: proxyWebSockets,
99+ PassHostHeader: &passHostHeader,
100+ ProxyWebSockets: &proxyWebSockets,
100101 FlushInterval: &flushInterval,
101102 }
102103
@@ -109,8 +110,8 @@ var _ = Describe("Legacy Options", func() {
109110 Static: true,
110111 StaticCode: &validStaticCode,
111112 InsecureSkipTLSVerify: false,
112− PassHostHeader: true,
113− ProxyWebSockets: false,
113+ PassHostHeader: nil,
114+ ProxyWebSockets: nil,
114115 FlushInterval: &defaultFlushInterval,
115116 }
116117
@@ -123,8 +124,8 @@ var _ = Describe("Legacy Options", func() {
123124 Static: true,
124125 StaticCode: &invalidStaticCode,
125126 InsecureSkipTLSVerify: false,
126− PassHostHeader: true,
127− ProxyWebSockets: false,
127+ PassHostHeader: nil,
128+ ProxyWebSockets: nil,
128129 FlushInterval: &defaultFlushInterval,
129130 }
130131
pkg/apis/options/upstreams.go+2−2
@@ -52,9 +52,9 @@ type Upstream struct {
5252 // PassHostHeader determines whether the request host header should be proxied
5353 // to the upstream server.
5454 // Defaults to true.
55− PassHostHeader bool `json:"passHostHeader"`
55+ PassHostHeader *bool `json:"passHostHeader"`
5656
5757 // ProxyWebSockets enables proxying of websockets to upstream servers
5858 // Defaults to true.
59− ProxyWebSockets bool `json:"proxyWebSockets"`
59+ ProxyWebSockets *bool `json:"proxyWebSockets"`
6060}
pkg/upstream/http.go+2−2
@@ -49,7 +49,7 @@ func newHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *option
4949
5050 // Set up a WebSocket proxy if required
5151 var wsProxy http.Handler
52− if upstream.ProxyWebSockets {
52+ if upstream.ProxyWebSockets == nil || *upstream.ProxyWebSockets {
5353 wsProxy = newWebSocketReverseProxy(u, upstream.InsecureSkipTLSVerify)
5454 }
5555
@@ -110,7 +110,7 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
110110 }
111111
112112 // Set the request director based on the PassHostHeader option
113− if !upstream.PassHostHeader {
113+ if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
114114 setProxyUpstreamHostHeader(proxy, target)
115115 } else {
116116 setProxyDirector(proxy)
pkg/upstream/http_test.go+10−7
@@ -24,6 +24,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
2424
2525 const flushInterval5s = 5 * time.Second
2626 const flushInterval1s = 1 * time.Second
27+ truth := true
28+ falsum := false
2729
2830 type httpUpstreamTableInput struct {
2931 id string
@@ -51,10 +53,11 @@ var _ = Describe("HTTP Upstream Suite", func() {
5153 rw := httptest.NewRecorder()
5254
5355 flush := 1 * time.Second
56+
5457 upstream := options.Upstream{
5558 ID: in.id,
56− PassHostHeader: true,
57− ProxyWebSockets: false,
59+ PassHostHeader: &truth,
60+ ProxyWebSockets: &falsum,
5861 InsecureSkipTLSVerify: false,
5962 FlushInterval: &flush,
6063 }
@@ -258,8 +261,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
258261 flush := 1 * time.Second
259262 upstream := options.Upstream{
260263 ID: "noPassHost",
261− PassHostHeader: false,
262− ProxyWebSockets: false,
264+ PassHostHeader: &falsum,
265+ ProxyWebSockets: &falsum,
263266 InsecureSkipTLSVerify: false,
264267 FlushInterval: &flush,
265268 }
@@ -302,7 +305,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
302305 ID: "foo123",
303306 FlushInterval: &in.flushInterval,
304307 InsecureSkipTLSVerify: in.skipVerify,
305− ProxyWebSockets: in.proxyWebSockets,
308+ ProxyWebSockets: &in.proxyWebSockets,
306309 }
307310
308311 handler := newHTTPUpstreamProxy(upstream, u, in.sigData, in.errorHandler)
@@ -370,8 +373,8 @@ var _ = Describe("HTTP Upstream Suite", func() {
370373 flush := 1 * time.Second
371374 upstream := options.Upstream{
372375 ID: "websocketProxy",
373− PassHostHeader: true,
374− ProxyWebSockets: true,
376+ PassHostHeader: &truth,
377+ ProxyWebSockets: &truth,
375378 InsecureSkipTLSVerify: false,
376379 FlushInterval: &flush,
377380 }
pkg/validation/upstreams.go+2−2
@@ -73,10 +73,10 @@ func validateStaticUpstream(upstream options.Upstream) []string {
7373 if upstream.FlushInterval != nil && *upstream.FlushInterval != time.Second {
7474 msgs = append(msgs, fmt.Sprintf("upstream %q has flushInterval, but is a static upstream, this will have no effect.", upstream.ID))
7575 }
76− if !upstream.PassHostHeader {
76+ if upstream.PassHostHeader != nil {
7777 msgs = append(msgs, fmt.Sprintf("upstream %q has passHostHeader, but is a static upstream, this will have no effect.", upstream.ID))
7878 }
79− if !upstream.ProxyWebSockets {
79+ if upstream.ProxyWebSockets != nil {
8080 msgs = append(msgs, fmt.Sprintf("upstream %q has proxyWebSockets, but is a static upstream, this will have no effect.", upstream.ID))
8181 }
8282
pkg/validation/upstreams_test.go+6−7
@@ -17,6 +17,7 @@ var _ = Describe("Upstreams", func() {
1717
1818 flushInterval := 5 * time.Second
1919 staticCode200 := 200
20+ truth := true
2021
2122 validHTTPUpstream := options.Upstream{
2223 ID: "validHTTPUpstream",
@@ -24,11 +25,9 @@ var _ = Describe("Upstreams", func() {
2425 URI: "http://localhost:8080",
2526 }
2627 validStaticUpstream := options.Upstream{
27− ID: "validStaticUpstream",
28− Path: "/validStaticUpstream",
29− Static: true,
30− PassHostHeader: true, // This would normally be defaulted
31− ProxyWebSockets: true, // this would normally be defaulted
28+ ID: "validStaticUpstream",
29+ Path: "/validStaticUpstream",
30+ Static: true,
3231 }
3332 validFileUpstream := options.Upstream{
3433 ID: "validFileUpstream",
@@ -134,8 +133,8 @@ var _ = Describe("Upstreams", func() {
134133 URI: "ftp://foo",
135134 Static: true,
136135 FlushInterval: &flushInterval,
137− PassHostHeader: false,
138− ProxyWebSockets: false,
136+ PassHostHeader: &truth,
137+ ProxyWebSockets: &truth,
139138 InsecureSkipTLSVerify: true,
140139 },
141140 },