git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #746 from oauth2-proxy/fix-static

Fix conversion of static responses in upstreams

commit841bf77f7f22358e93b786b6711da01446a01454 authorJoel Speed <Joel.speed@hotmail.co.uk> dateMon 31 Aug 2020, 16:58 (6 years ago) parent73f0094bd619ab
5 files changed+39−19
CHANGELOG.md+1 pkg/apis/options/legacy_options.go+8−4 pkg/apis/options/legacy_options_test.go+20−10 pkg/upstream/proxy.go+1 pkg/upstream/static.go+9−5
CHANGELOG.md+1
@@ -9,6 +9,7 @@
99## Changes since v6.1.0
1010
1111- [#729](https://github.com/oauth2-proxy/oauth2-proxy/pull/729) Use X-Forwarded-Host consistently when set (@NickMeves)
12+- [#746](https://github.com/oauth2-proxy/oauth2-proxy/pull/746) Fix conversion of static responses in upstreams (@JoelSpeed)
1213
1314# v6.1.0
1415
pkg/apis/options/legacy_options.go+8−4
@@ -4,6 +4,7 @@ import (
44 "fmt"
55 "net/url"
66 "strconv"
7+ "strings"
78 "time"
89
910 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
@@ -87,6 +88,8 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
8788 if u.Fragment != "" {
8889 upstream.ID = u.Fragment
8990 upstream.Path = u.Fragment
91+ // Trim the fragment from the end of the URI
92+ upstream.URI = strings.SplitN(upstreamString, "#", 2)[0]
9093 }
9194 case "static":
9295 responseCode, err := strconv.Atoi(u.Host)
@@ -97,17 +100,18 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
97100 upstream.Static = true
98101 upstream.StaticCode = &responseCode
99102
100− // These are not allowed to be empty and must be unique
103+ // This is not allowed to be empty and must be unique
101104 upstream.ID = upstreamString
102− upstream.Path = upstreamString
105+
106+ // We only support the root path in the legacy config
107+ upstream.Path = "/"
103108
104109 // Force defaults compatible with static responses
105110 upstream.URI = ""
106111 upstream.InsecureSkipTLSVerify = false
107112 upstream.PassHostHeader = nil
108113 upstream.ProxyWebSockets = nil
109− flush := 1 * time.Second
110− upstream.FlushInterval = &flush
114+ upstream.FlushInterval = nil
111115 }
112116
113117 upstreams = append(upstreams, upstream)
pkg/apis/options/legacy_options_test.go+20−10
@@ -21,9 +21,10 @@ var _ = Describe("Legacy Options", func() {
2121 legacyOpts.LegacyUpstreams.PassHostHeader = true
2222 legacyOpts.LegacyUpstreams.ProxyWebSockets = true
2323 legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
24− legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"}
24+ legacyOpts.LegacyUpstreams.Upstreams = []string{"http://foo.bar/baz", "file:///var/lib/website#/bar", "static://204"}
2525
2626 truth := true
27+ staticCode := 204
2728 opts.UpstreamServers = Upstreams{
2829 {
2930 ID: "/baz",
@@ -37,12 +38,23 @@ var _ = Describe("Legacy Options", func() {
3738 {
3839 ID: "/bar",
3940 Path: "/bar",
40− URI: "file://var/lib/website#/bar",
41+ URI: "file:///var/lib/website",
4142 FlushInterval: &flushInterval,
4243 InsecureSkipTLSVerify: true,
4344 PassHostHeader: &truth,
4445 ProxyWebSockets: &truth,
4546 },
47+ {
48+ ID: "static://204",
49+ Path: "/",
50+ URI: "",
51+ Static: true,
52+ StaticCode: &staticCode,
53+ FlushInterval: nil,
54+ InsecureSkipTLSVerify: false,
55+ PassHostHeader: nil,
56+ ProxyWebSockets: nil,
57+ },
4658 }
4759
4860 converted, err := legacyOpts.ToOptions()
@@ -58,8 +70,6 @@ var _ = Describe("Legacy Options", func() {
5870 errMsg string
5971 }
6072
61− defaultFlushInterval := 1 * time.Second
62−
6373 // Non defaults for these options
6474 skipVerify := true
6575 passHostHeader := false
@@ -90,11 +100,11 @@ var _ = Describe("Legacy Options", func() {
90100 FlushInterval: &flushInterval,
91101 }
92102
93− validFileWithFragment := "file://var/lib/website#/bar"
103+ validFileWithFragment := "file:///var/lib/website#/bar"
94104 validFileWithFragmentUpstream := Upstream{
95105 ID: "/bar",
96106 Path: "/bar",
97− URI: validFileWithFragment,
107+ URI: "file:///var/lib/website",
98108 InsecureSkipTLSVerify: skipVerify,
99109 PassHostHeader: &passHostHeader,
100110 ProxyWebSockets: &proxyWebSockets,
@@ -105,28 +115,28 @@ var _ = Describe("Legacy Options", func() {
105115 validStaticCode := 204
106116 validStaticUpstream := Upstream{
107117 ID: validStatic,
108− Path: validStatic,
118+ Path: "/",
109119 URI: "",
110120 Static: true,
111121 StaticCode: &validStaticCode,
112122 InsecureSkipTLSVerify: false,
113123 PassHostHeader: nil,
114124 ProxyWebSockets: nil,
115− FlushInterval: &defaultFlushInterval,
125+ FlushInterval: nil,
116126 }
117127
118128 invalidStatic := "static://abc"
119129 invalidStaticCode := 200
120130 invalidStaticUpstream := Upstream{
121131 ID: invalidStatic,
122− Path: invalidStatic,
132+ Path: "/",
123133 URI: "",
124134 Static: true,
125135 StaticCode: &invalidStaticCode,
126136 InsecureSkipTLSVerify: false,
127137 PassHostHeader: nil,
128138 ProxyWebSockets: nil,
129− FlushInterval: &defaultFlushInterval,
139+ FlushInterval: nil,
130140 }
131141
132142 invalidHTTP := ":foo"
pkg/upstream/proxy.go+1
@@ -56,6 +56,7 @@ func (m *multiUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request
5656
5757// registerStaticResponseHandler registers a static response handler with at the given path.
5858func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
59+ logger.Printf("mapping path %q => static response %d", upstream.Path, derefStaticCode(upstream.StaticCode))
5960 m.serveMux.Handle(upstream.Path, newStaticResponseHandler(upstream.ID, upstream.StaticCode))
6061}
6162
pkg/upstream/static.go+9−5
@@ -10,12 +10,8 @@ const defaultStaticResponseCode = 200
1010// newStaticResponseHandler creates a new staticResponseHandler that serves a
1111// a static response code.
1212func newStaticResponseHandler(upstream string, code *int) http.Handler {
13− if code == nil {
14− c := defaultStaticResponseCode
15− code = &c
16− }
1713 return &staticResponseHandler{
18− code: *code,
14+ code: derefStaticCode(code),
1915 upstream: upstream,
2016 }
2117}
@@ -32,3 +28,11 @@ func (s *staticResponseHandler) ServeHTTP(rw http.ResponseWriter, req *http.Requ
3228 rw.WriteHeader(s.code)
3329 fmt.Fprintf(rw, "Authenticated")
3430}
31+
32+// derefStaticCode returns the derefenced value, or the default if the value is nil
33+func derefStaticCode(code *int) int {
34+ if code != nil {
35+ return *code
36+ }
37+ return defaultStaticResponseCode
38+}