11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util/ptr"
13 "github.com/stretchr/testify/assert"
17 cookieSecret = "secretthirtytwobytes+abcdefghijk"
19 clientSecret = "xyzzyplugh"
20 providerID = "providerID"
23func testOptions() *options.Options {
24 o := options.NewOptions()
25 o.UpstreamServers.Upstreams = append(o.UpstreamServers.Upstreams, options.Upstream{
28 URI: "http://127.0.0.1:8080/",
30 o.Cookie.Secret = cookieSecret
31 o.Providers[0].ID = providerID
32 o.Providers[0].ClientID = clientID
33 o.Providers[0].ClientSecret = clientSecret
34 o.EmailDomains = []string{"*"}
38func errorMsg(msgs []string) string {
39 result := make([]string, 0)
40 result = append(result, "invalid configuration:")
41 result = append(result, msgs...)
42 return strings.Join(result, "\n ")
45func TestNewOptions(t *testing.T) {
46 o := options.NewOptions()
47 o.EmailDomains = []string{"*"}
49 assert.NotEqual(t, nil, err)
51 expected := errorMsg([]string{
52 "missing setting: cookie-secret or cookie-secret-file",
53 "provider has empty id: ids are required for all providers",
54 "provider missing setting: client-id",
55 "missing setting: client-secret or client-secret-file"})
56 assert.Equal(t, expected, err.Error())
59func TestGoogleGroupOptionsWithoutServiceAccountJSON(t *testing.T) {
61 o.Providers[0].GoogleConfig.AdminEmail = "admin@example.com"
63 assert.NotEqual(t, nil, err)
65 expected := errorMsg([]string{
66 "missing setting: google-service-account-json or google-use-application-default-credentials"})
67 assert.Equal(t, expected, err.Error())
70func TestGoogleGroupOptionsWithoutAdminEmail(t *testing.T) {
72 o.Providers[0].GoogleConfig.UseApplicationDefaultCredentials = ptr.To(true)
74 assert.NotEqual(t, nil, err)
76 expected := errorMsg([]string{
77 "missing setting: google-admin-email"})
78 assert.Equal(t, expected, err.Error())
81func TestGoogleGroupOptionsWithoutGroups(t *testing.T) {
83 // Set admin email and application default credentials but no groups - should still require them
84 o.Providers[0].GoogleConfig.AdminEmail = "admin@example.com"
85 o.Providers[0].GoogleConfig.UseApplicationDefaultCredentials = ptr.To(true)
87 // Should pass validation since google-group is now optional
88 assert.Equal(t, nil, err)
91func TestGoogleGroupInvalidFile(t *testing.T) {
93 o.Providers[0].GoogleConfig.Groups = []string{"test_group"}
94 o.Providers[0].GoogleConfig.AdminEmail = "admin@example.com"
95 o.Providers[0].GoogleConfig.ServiceAccountJSON = "file_doesnt_exist.json"
97 assert.NotEqual(t, nil, err)
99 expected := errorMsg([]string{
100 "Google credentials file not found: file_doesnt_exist.json",
102 assert.Equal(t, expected, err.Error())
105func TestInitializedOptions(t *testing.T) {
107 assert.Equal(t, nil, Validate(o))
110// Note that it's not worth testing nonparseable URLs, since url.Parse()
111// seems to parse damn near anything.
112func TestRedirectURL(t *testing.T) {
114 o.RawRedirectURL = "https://myhost.com/oauth2/callback"
115 assert.Equal(t, nil, Validate(o))
116 expected := &url.URL{
117 Scheme: "https", Host: "myhost.com", Path: "/oauth2/callback"}
118 assert.Equal(t, expected, o.GetRedirectURL())
121func TestCookieRefreshMustBeLessThanCookieExpire(t *testing.T) {
123 assert.Equal(t, nil, Validate(o))
125 o.Cookie.Secret = "0123456789abcdef"
126 o.Cookie.Refresh = o.Cookie.Expire
127 assert.NotEqual(t, nil, Validate(o))
129 o.Cookie.Refresh -= time.Duration(1)
130 assert.Equal(t, nil, Validate(o))
133func TestBase64CookieSecret(t *testing.T) {
135 assert.Equal(t, nil, Validate(o))
137 // 32 byte, base64 (urlsafe) encoded key
138 o.Cookie.Secret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ="
139 assert.Equal(t, nil, Validate(o))
141 // 32 byte, base64 (urlsafe) encoded key, w/o padding
142 o.Cookie.Secret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ"
143 assert.Equal(t, nil, Validate(o))
145 // 24 byte, base64 (urlsafe) encoded key
146 o.Cookie.Secret = "Kp33Gj-GQmYtz4zZUyUDdqQKx5_Hgkv3"
147 assert.Equal(t, nil, Validate(o))
149 // 16 byte, base64 (urlsafe) encoded key
150 o.Cookie.Secret = "LFEqZYvYUwKwzn0tEuTpLA=="
151 assert.Equal(t, nil, Validate(o))
153 // 16 byte, base64 (urlsafe) encoded key, w/o padding
154 o.Cookie.Secret = "LFEqZYvYUwKwzn0tEuTpLA"
155 assert.Equal(t, nil, Validate(o))
158func TestValidateSignatureKey(t *testing.T) {
160 o.SignatureKey = "sha1:secret"
161 assert.Equal(t, nil, Validate(o))
162 assert.Equal(t, o.GetSignatureData().Hash, crypto.SHA1)
163 assert.Equal(t, o.GetSignatureData().Key, "secret")
166func TestValidateSignatureKeyInvalidSpec(t *testing.T) {
168 o.SignatureKey = "invalid spec"
170 assert.Equal(t, err.Error(), "invalid configuration:\n"+
171 " invalid signature hash:key spec: "+o.SignatureKey)
174func TestValidateSignatureKeyUnsupportedAlgorithm(t *testing.T) {
176 o.SignatureKey = "unsupported:default secret"
178 assert.Equal(t, err.Error(), "invalid configuration:\n"+
179 " unsupported signature hash algorithm: "+o.SignatureKey)
182func TestGCPHealthcheck(t *testing.T) {
184 o.GCPHealthChecks = true
185 assert.Equal(t, nil, Validate(o))
188func TestRealClientIPHeader(t *testing.T) {
189 // Ensure nil if ReverseProxy not set.
191 o.RealClientIPHeader = "X-Real-IP"
192 assert.Equal(t, nil, Validate(o))
193 assert.Nil(t, o.GetRealClientIPParser())
195 // Ensure simple use case works.
197 o.ReverseProxy = true
198 o.RealClientIPHeader = "X-Forwarded-For"
199 assert.Equal(t, nil, Validate(o))
200 assert.NotNil(t, o.GetRealClientIPParser())
202 // Ensure unknown header format process an error.
204 o.ReverseProxy = true
205 o.RealClientIPHeader = "Forwarded"
207 assert.NotEqual(t, nil, err)
208 expected := errorMsg([]string{
209 "real_client_ip_header (Forwarded) not accepted parameter value: the http header key (Forwarded) is either invalid or unsupported",
211 assert.Equal(t, expected, err.Error())
212 assert.Nil(t, o.GetRealClientIPParser())
214 // Ensure invalid header format produces an error.
216 o.ReverseProxy = true
217 o.RealClientIPHeader = "!934invalidheader-23:"
219 assert.NotEqual(t, nil, err)
220 expected = errorMsg([]string{
221 "real_client_ip_header (!934invalidheader-23:) not accepted parameter value: the http header key (!934invalidheader-23:) is either invalid or unsupported",
223 assert.Equal(t, expected, err.Error())
224 assert.Nil(t, o.GetRealClientIPParser())
227func TestProviderCAFilesError(t *testing.T) {
228 file, err := os.CreateTemp("", "absent.*.crt")
229 assert.NoError(t, err)
230 assert.NoError(t, file.Close())
231 assert.NoError(t, os.Remove(file.Name()))
234 o.Providers[0].CAFiles = append(o.Providers[0].CAFiles, file.Name())
237 assert.Contains(t, err.Error(), "unable to load provider CA file(s)")
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