git.iko.soy/typeable/oauth2-proxy
on master help sign in
229 lines 6.6 kB history raw
1package validation
3import (
4 "time"
6 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
7 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util/ptr"
8 . "github.com/onsi/ginkgo/v2"
9 . "github.com/onsi/gomega"
12var _ = Describe("Upstreams", func() {
13 type validateUpstreamTableInput struct {
14 upstreams options.UpstreamConfig
15 errStrings []string
16 }
18 flushInterval := 5 * time.Second
19 staticCode200 := 200
21 validHTTPUpstream := options.Upstream{
22 ID: "validHTTPUpstream",
23 Path: "/validHTTPUpstream",
24 URI: "http://localhost:8080",
25 }
26 validStaticUpstream := options.Upstream{
27 ID: "validStaticUpstream",
28 Path: "/validStaticUpstream",
29 Static: ptr.To(true),
30 }
31 validFileUpstream := options.Upstream{
32 ID: "validFileUpstream",
33 Path: "/validFileUpstream",
34 URI: "file://var/lib/foo",
35 }
37 emptyIDMsg := "upstream has empty id: ids are required for all upstreams"
38 emptyPathMsg := "upstream \"foo\" has empty path: paths are required for all upstreams"
39 emptyURIMsg := "upstream \"foo\" has empty uri: uris are required for all non-static upstreams"
40 invalidURIMsg := "upstream \"foo\" has invalid uri: parse \":\": missing protocol scheme"
41 invalidURISchemeMsg := "upstream \"foo\" has invalid scheme: \"ftp\""
42 staticWithURIMsg := "upstream \"foo\" has uri, but is a static upstream, this will have no effect."
43 staticWithInsecureMsg := "upstream \"foo\" has insecureSkipTLSVerify, but is a static upstream, this will have no effect."
44 staticWithFlushIntervalMsg := "upstream \"foo\" has flushInterval, but is a static upstream, this will have no effect."
45 staticWithPassHostHeaderMsg := "upstream \"foo\" has passHostHeader, but is a static upstream, this will have no effect."
46 staticWithProxyWebSocketsMsg := "upstream \"foo\" has proxyWebSockets, but is a static upstream, this will have no effect."
47 multipleIDsMsg := "multiple upstreams found with id \"foo\": upstream ids must be unique"
48 multiplePathsMsg := "multiple upstreams found with path \"/foo\": upstream paths must be unique"
49 staticCodeMsg := "upstream \"foo\" has staticCode (200), but is not a static upstream, set 'static' for a static response"
51 DescribeTable("validateUpstreams",
52 func(o *validateUpstreamTableInput) {
53 Expect(validateUpstreams(o.upstreams)).To(ConsistOf(o.errStrings))
54 },
55 Entry("with no upstreams", &validateUpstreamTableInput{
56 upstreams: options.UpstreamConfig{},
57 errStrings: []string{},
58 }),
59 Entry("with valid upstreams", &validateUpstreamTableInput{
60 upstreams: options.UpstreamConfig{
61 Upstreams: []options.Upstream{
62 validHTTPUpstream,
63 validStaticUpstream,
64 validFileUpstream,
65 },
66 },
67 errStrings: []string{},
68 }),
69 Entry("with an empty ID", &validateUpstreamTableInput{
70 upstreams: options.UpstreamConfig{
71 Upstreams: []options.Upstream{
72 {
73 ID: "",
74 Path: "/foo",
75 URI: "http://localhost:8080",
76 },
77 },
78 },
79 errStrings: []string{emptyIDMsg},
80 }),
81 Entry("with an empty Path", &validateUpstreamTableInput{
82 upstreams: options.UpstreamConfig{
83 Upstreams: []options.Upstream{
84 {
85 ID: "foo",
86 Path: "",
87 URI: "http://localhost:8080",
88 },
89 },
90 },
91 errStrings: []string{emptyPathMsg},
92 }),
93 Entry("with an empty Path", &validateUpstreamTableInput{
94 upstreams: options.UpstreamConfig{
95 Upstreams: []options.Upstream{
96 {
97 ID: "foo",
98 Path: "",
99 URI: "http://localhost:8080",
100 },
101 },
102 },
103 errStrings: []string{emptyPathMsg},
104 }),
105 Entry("with an empty URI", &validateUpstreamTableInput{
106 upstreams: options.UpstreamConfig{
107 Upstreams: []options.Upstream{
108 {
109 ID: "foo",
110 Path: "/foo",
111 URI: "",
112 },
113 },
114 },
115 errStrings: []string{emptyURIMsg},
116 }),
117 Entry("with an invalid URI", &validateUpstreamTableInput{
118 upstreams: options.UpstreamConfig{
119 Upstreams: []options.Upstream{
120 {
121 ID: "foo",
122 Path: "/foo",
123 URI: ":",
124 },
125 },
126 },
127 errStrings: []string{invalidURIMsg},
128 }),
129 Entry("with an invalid URI scheme", &validateUpstreamTableInput{
130 upstreams: options.UpstreamConfig{
131 Upstreams: []options.Upstream{
132 {
133 ID: "foo",
134 Path: "/foo",
135 URI: "ftp://foo",
136 },
137 },
138 },
139 errStrings: []string{invalidURISchemeMsg},
140 }),
141 Entry("with a static upstream and invalid options", &validateUpstreamTableInput{
142 upstreams: options.UpstreamConfig{
143 Upstreams: []options.Upstream{
144 {
145 ID: "foo",
146 Path: "/foo",
147 URI: "ftp://foo",
148 Static: ptr.To(true),
149 FlushInterval: &flushInterval,
150 PassHostHeader: ptr.To(true),
151 ProxyWebSockets: ptr.To(true),
152 InsecureSkipTLSVerify: ptr.To(true),
153 },
154 },
155 },
156 errStrings: []string{
157 staticWithURIMsg,
158 staticWithInsecureMsg,
159 staticWithFlushIntervalMsg,
160 staticWithPassHostHeaderMsg,
161 staticWithProxyWebSocketsMsg,
162 },
163 }),
164 Entry("with a static upstream and sane default options", &validateUpstreamTableInput{
165 upstreams: options.UpstreamConfig{
166 Upstreams: []options.Upstream{
167 {
168 ID: "foo",
169 Path: "/foo",
170 URI: "ftp://foo",
171 Static: ptr.To(true),
172 PassHostHeader: ptr.To(false),
173 ProxyWebSockets: ptr.To(false),
174 InsecureSkipTLSVerify: ptr.To(false),
175 },
176 },
177 },
178 errStrings: []string{
179 staticWithURIMsg,
180 },
181 }),
182 Entry("with duplicate IDs", &validateUpstreamTableInput{
183 upstreams: options.UpstreamConfig{
184 Upstreams: []options.Upstream{
185 {
186 ID: "foo",
187 Path: "/foo1",
188 URI: "http://foo",
189 },
190 {
191 ID: "foo",
192 Path: "/foo2",
193 URI: "http://foo",
194 },
195 },
196 },
197 errStrings: []string{multipleIDsMsg},
198 }),
199 Entry("with duplicate Paths", &validateUpstreamTableInput{
200 upstreams: options.UpstreamConfig{
201 Upstreams: []options.Upstream{
202 {
203 ID: "foo1",
204 Path: "/foo",
205 URI: "http://foo",
206 },
207 {
208 ID: "foo2",
209 Path: "/foo",
210 URI: "http://foo",
211 },
212 },
213 },
214 errStrings: []string{multiplePathsMsg},
215 }),
216 Entry("when a static code is supplied without static", &validateUpstreamTableInput{
217 upstreams: options.UpstreamConfig{
218 Upstreams: []options.Upstream{
219 {
220 ID: "foo",
221 Path: "/foo",
222 StaticCode: &staticCode200,
223 },
224 },
225 },
226 errStrings: []string{emptyURIMsg, staticCodeMsg},
227 }),
228 )
229})