git.iko.soy/typeable/oauth2-proxy
on 597ffeb help sign in
typeable/oauth2-proxy/main_test.go
215 lines 6.4 kB history raw
1package main
3import (
4 "errors"
5 "io/ioutil"
6 "os"
7 "time"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10 . "github.com/onsi/ginkgo"
11 . "github.com/onsi/ginkgo/extensions/table"
12 . "github.com/onsi/gomega"
13 "github.com/spf13/pflag"
16var _ = Describe("Configuration Loading Suite", func() {
17 const testLegacyConfig = `
18upstreams="http://httpbin"
19set_basic_auth="true"
20basic_auth_password="super-secret-password"
23 const testAlphaConfig = `
24upstreams:
25 - id: /
26 path: /
27 uri: http://httpbin
28 flushInterval: 1s
29 passHostHeader: true
30 proxyWebSockets: true
31injectRequestHeaders:
32- name: Authorization
33 values:
34 - claim: user
35 prefix: "Basic "
36 basicAuthPassword:
37 value: c3VwZXItc2VjcmV0LXBhc3N3b3Jk
38- name: X-Forwarded-Groups
39 values:
40 - claim: groups
41- name: X-Forwarded-User
42 values:
43 - claim: user
44- name: X-Forwarded-Email
45 values:
46 - claim: email
47- name: X-Forwarded-Preferred-Username
48 values:
49 - claim: preferred_username
50injectResponseHeaders:
51- name: Authorization
52 values:
53 - claim: user
54 prefix: "Basic "
55 basicAuthPassword:
56 value: c3VwZXItc2VjcmV0LXBhc3N3b3Jk
59 const testCoreConfig = `
60http_address="0.0.0.0:4180"
61cookie_secret="OQINaROshtE9TcZkNAm-5Zs2Pv3xaWytBmc5W7sPX7w="
62provider="oidc"
63email_domains="example.com"
64oidc_issuer_url="http://dex.localhost:4190/dex"
65client_secret="b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK"
66client_id="oauth2-proxy"
67cookie_secure="false"
69redirect_url="http://localhost:4180/oauth2/callback"
72 boolPtr := func(b bool) *bool {
73 return &b
74 }
76 durationPtr := func(d time.Duration) *options.Duration {
77 du := options.Duration(d)
78 return &du
79 }
81 testExpectedOptions := func() *options.Options {
82 opts, err := options.NewLegacyOptions().ToOptions()
83 Expect(err).ToNot(HaveOccurred())
85 opts.HTTPAddress = "0.0.0.0:4180"
86 opts.Cookie.Secret = "OQINaROshtE9TcZkNAm-5Zs2Pv3xaWytBmc5W7sPX7w="
87 opts.ProviderType = "oidc"
88 opts.EmailDomains = []string{"example.com"}
89 opts.OIDCIssuerURL = "http://dex.localhost:4190/dex"
90 opts.ClientSecret = "b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK"
91 opts.ClientID = "oauth2-proxy"
92 opts.Cookie.Secure = false
93 opts.RawRedirectURL = "http://localhost:4180/oauth2/callback"
95 opts.UpstreamServers = options.Upstreams{
96 {
97 ID: "/",
98 Path: "/",
99 URI: "http://httpbin",
100 FlushInterval: durationPtr(options.DefaultUpstreamFlushInterval),
101 PassHostHeader: boolPtr(true),
102 ProxyWebSockets: boolPtr(true),
103 },
104 }
106 authHeader := options.Header{
107 Name: "Authorization",
108 Values: []options.HeaderValue{
109 {
110 ClaimSource: &options.ClaimSource{
111 Claim: "user",
112 Prefix: "Basic ",
113 BasicAuthPassword: &options.SecretSource{
114 Value: []byte("super-secret-password"),
115 },
116 },
117 },
118 },
119 }
121 opts.InjectRequestHeaders = append([]options.Header{authHeader}, opts.InjectRequestHeaders...)
122 opts.InjectResponseHeaders = append(opts.InjectResponseHeaders, authHeader)
123 return opts
124 }
126 type loadConfigurationTableInput struct {
127 configContent string
128 alphaConfigContent string
129 args []string
130 extraFlags func() *pflag.FlagSet
131 expectedOptions func() *options.Options
132 expectedErr error
133 }
135 DescribeTable("LoadConfiguration",
136 func(in loadConfigurationTableInput) {
137 var configFileName, alphaConfigFileName string
139 defer func() {
140 if configFileName != "" {
141 Expect(os.Remove(configFileName)).To(Succeed())
142 }
143 if alphaConfigFileName != "" {
144 Expect(os.Remove(alphaConfigFileName)).To(Succeed())
145 }
146 }()
148 if in.configContent != "" {
149 By("Writing the config to a temporary file", func() {
150 file, err := ioutil.TempFile("", "oauth2-proxy-test-config-XXXX.cfg")
151 Expect(err).ToNot(HaveOccurred())
152 defer file.Close()
154 configFileName = file.Name()
156 _, err = file.WriteString(in.configContent)
157 Expect(err).ToNot(HaveOccurred())
158 })
159 }
161 if in.alphaConfigContent != "" {
162 By("Writing the config to a temporary file", func() {
163 file, err := ioutil.TempFile("", "oauth2-proxy-test-alpha-config-XXXX.yaml")
164 Expect(err).ToNot(HaveOccurred())
165 defer file.Close()
167 alphaConfigFileName = file.Name()
169 _, err = file.WriteString(in.alphaConfigContent)
170 Expect(err).ToNot(HaveOccurred())
171 })
172 }
174 extraFlags := pflag.NewFlagSet("test-flagset", pflag.ExitOnError)
175 if in.extraFlags != nil {
176 extraFlags = in.extraFlags()
177 }
179 opts, err := loadConfiguration(configFileName, alphaConfigFileName, extraFlags, in.args)
180 if in.expectedErr != nil {
181 Expect(err).To(MatchError(in.expectedErr.Error()))
182 } else {
183 Expect(err).ToNot(HaveOccurred())
184 }
185 Expect(in.expectedOptions).ToNot(BeNil())
186 Expect(opts).To(Equal(in.expectedOptions()))
187 },
188 Entry("with legacy configuration", loadConfigurationTableInput{
189 configContent: testCoreConfig + testLegacyConfig,
190 expectedOptions: testExpectedOptions,
191 }),
192 Entry("with alpha configuration", loadConfigurationTableInput{
193 configContent: testCoreConfig,
194 alphaConfigContent: testAlphaConfig,
195 expectedOptions: testExpectedOptions,
196 }),
197 Entry("with bad legacy configuration", loadConfigurationTableInput{
198 configContent: testCoreConfig + "unknown_field=\"something\"",
199 expectedOptions: func() *options.Options { return nil },
200 expectedErr: errors.New("failed to load config: error unmarshalling config: 1 error(s) decoding:\n\n* '' has invalid keys: unknown_field"),
201 }),
202 Entry("with bad alpha configuration", loadConfigurationTableInput{
203 configContent: testCoreConfig,
204 alphaConfigContent: testAlphaConfig + ":",
205 expectedOptions: func() *options.Options { return nil },
206 expectedErr: errors.New("failed to load alpha options: error unmarshalling config: error converting YAML to JSON: yaml: line 34: did not find expected key"),
207 }),
208 Entry("with alpha configuration and bad core configuration", loadConfigurationTableInput{
209 configContent: testCoreConfig + "unknown_field=\"something\"",
210 alphaConfigContent: testAlphaConfig,
211 expectedOptions: func() *options.Options { return nil },
212 expectedErr: errors.New("failed to load core options: failed to load config: error unmarshalling config: 1 error(s) decoding:\n\n* '' has invalid keys: unknown_field"),
213 }),
214 )
215})