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"
20basic_auth_password="super-secret-password"
23 const testAlphaConfig = `
37 value: c3VwZXItc2VjcmV0LXBhc3N3b3Jk
38- name: X-Forwarded-Groups
41- name: X-Forwarded-User
44- name: X-Forwarded-Email
47- name: X-Forwarded-Preferred-Username
49 - claim: preferred_username
56 value: c3VwZXItc2VjcmV0LXBhc3N3b3Jk
59 const testCoreConfig = `
60http_address="0.0.0.0:4180"
61cookie_secret="OQINaROshtE9TcZkNAm-5Zs2Pv3xaWytBmc5W7sPX7w="
63email_domains="example.com"
64oidc_issuer_url="http://dex.localhost:4190/dex"
65client_secret="b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK"
66client_id="oauth2-proxy"
69redirect_url="http://localhost:4180/oauth2/callback"
72 boolPtr := func(b bool) *bool {
76 durationPtr := func(d time.Duration) *options.Duration {
77 du := options.Duration(d)
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{
99 URI: "http://httpbin",
100 FlushInterval: durationPtr(options.DefaultUpstreamFlushInterval),
101 PassHostHeader: boolPtr(true),
102 ProxyWebSockets: boolPtr(true),
106 authHeader := options.Header{
107 Name: "Authorization",
108 Values: []options.HeaderValue{
110 ClaimSource: &options.ClaimSource{
113 BasicAuthPassword: &options.SecretSource{
114 Value: []byte("super-secret-password"),
121 opts.InjectRequestHeaders = append([]options.Header{authHeader}, opts.InjectRequestHeaders...)
122 opts.InjectResponseHeaders = append(opts.InjectResponseHeaders, authHeader)
126 type loadConfigurationTableInput struct {
128 alphaConfigContent string
130 extraFlags func() *pflag.FlagSet
131 expectedOptions func() *options.Options
135 DescribeTable("LoadConfiguration",
136 func(in loadConfigurationTableInput) {
137 var configFileName, alphaConfigFileName string
140 if configFileName != "" {
141 Expect(os.Remove(configFileName)).To(Succeed())
143 if alphaConfigFileName != "" {
144 Expect(os.Remove(alphaConfigFileName)).To(Succeed())
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())
154 configFileName = file.Name()
156 _, err = file.WriteString(in.configContent)
157 Expect(err).ToNot(HaveOccurred())
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())
167 alphaConfigFileName = file.Name()
169 _, err = file.WriteString(in.alphaConfigContent)
170 Expect(err).ToNot(HaveOccurred())
174 extraFlags := pflag.NewFlagSet("test-flagset", pflag.ExitOnError)
175 if in.extraFlags != nil {
176 extraFlags = in.extraFlags()
179 opts, err := loadConfiguration(configFileName, alphaConfigFileName, extraFlags, in.args)
180 if in.expectedErr != nil {
181 Expect(err).To(MatchError(in.expectedErr.Error()))
183 Expect(err).ToNot(HaveOccurred())
185 Expect(in.expectedOptions).ToNot(BeNil())
186 Expect(opts).To(Equal(in.expectedOptions()))
188 Entry("with legacy configuration", loadConfigurationTableInput{
189 configContent: testCoreConfig + testLegacyConfig,
190 expectedOptions: testExpectedOptions,
192 Entry("with alpha configuration", loadConfigurationTableInput{
193 configContent: testCoreConfig,
194 alphaConfigContent: testAlphaConfig,
195 expectedOptions: testExpectedOptions,
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"),
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"),
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"),
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list