git.iko.soy/typeable/oauth2-proxy
on master help sign in
Switch to in session store initialisation
commit6e1b3b9660bf7968d175fcfdeffb677ad3a2f4fe authorJoel Speed <joel.speed@hotmail.co.uk> dateSun 28 Jun 2020, 12:44 (6 years ago) parent7784639
6 files changed+48−35
oauthproxy_test.go+2−7 pkg/sessions/cookie/session_store.go+6−1 pkg/sessions/redis/redis_store.go+6−1 pkg/sessions/redis/redis_store_test.go+6−6 pkg/sessions/session_store.go+2−7 pkg/sessions/session_store_test.go+26−13
oauthproxy_test.go+2−7
@@ -22,7 +22,6 @@ import (
2222 "github.com/mbland/hmacauth"
2323 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
2424 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
25− "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
2625 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
2726 "github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/cookie"
2827 "github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
@@ -1605,9 +1604,7 @@ func TestClearSplitCookie(t *testing.T) {
16051604 opts.Cookie.Secret = base64CookieSecret
16061605 opts.Cookie.Name = "oauth2"
16071606 opts.Cookie.Domains = []string{"abc"}
1608− cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(opts.Cookie.Secret))
1609− assert.Equal(t, nil, err)
1610− store, err := cookie.NewCookieSessionStore(&opts.Session, &opts.Cookie, cipher)
1607+ store, err := cookie.NewCookieSessionStore(&opts.Session, &opts.Cookie)
16111608 assert.Equal(t, nil, err)
16121609 p := OAuthProxy{CookieName: opts.Cookie.Name, CookieDomains: opts.Cookie.Domains, sessionStore: store}
16131610 var rw = httptest.NewRecorder()
@@ -1636,9 +1633,7 @@ func TestClearSingleCookie(t *testing.T) {
16361633 opts := baseTestOptions()
16371634 opts.Cookie.Name = "oauth2"
16381635 opts.Cookie.Domains = []string{"abc"}
1639− cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(opts.Cookie.Secret))
1640− assert.Equal(t, nil, err)
1641− store, err := cookie.NewCookieSessionStore(&opts.Session, &opts.Cookie, cipher)
1636+ store, err := cookie.NewCookieSessionStore(&opts.Session, &opts.Cookie)
16421637 assert.Equal(t, nil, err)
16431638 p := OAuthProxy{CookieName: opts.Cookie.Name, CookieDomains: opts.Cookie.Domains, sessionStore: store}
16441639 var rw = httptest.NewRecorder()
pkg/sessions/cookie/session_store.go+6−1
@@ -126,7 +126,12 @@ func (s *SessionStore) makeCookie(req *http.Request, name string, value string,
126126
127127// NewCookieSessionStore initialises a new instance of the SessionStore from
128128// the configuration given
129−func NewCookieSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions, cipher encryption.Cipher) (sessions.SessionStore, error) {
129+func NewCookieSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
130+ cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(cookieOpts.Secret))
131+ if err != nil {
132+ return nil, fmt.Errorf("error initialising cipher: %v", err)
133+ }
134+
130135 return &SessionStore{
131136 CookieCipher: cipher,
132137 CookieOptions: cookieOpts,
pkg/sessions/redis/redis_store.go+6−1
@@ -39,7 +39,12 @@ type SessionStore struct {
3939
4040// NewRedisSessionStore initialises a new instance of the SessionStore from
4141// the configuration given
42−func NewRedisSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions, cipher encryption.Cipher) (sessions.SessionStore, error) {
42+func NewRedisSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
43+ cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(cookieOpts.Secret))
44+ if err != nil {
45+ return nil, fmt.Errorf("error initialising cipher: %v", err)
46+ }
47+
4348 client, err := newRedisCmdable(opts.Redis)
4449 if err != nil {
4550 return nil, fmt.Errorf("error constructing redis client: %v", err)
pkg/sessions/redis/redis_store_test.go+6−6
@@ -11,7 +11,6 @@ import (
1111 "github.com/alicebob/miniredis/v2"
1212 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
1313 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
14− "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
1514 "github.com/stretchr/testify/assert"
1615 "github.com/stretchr/testify/require"
1716)
@@ -21,9 +20,6 @@ func TestRedisStore(t *testing.T) {
2120 _, err := rand.Read(secret)
2221 assert.NoError(t, err)
2322
24− cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(string(secret)))
25− assert.NoError(t, err)
26−
2723 t.Run("save session on redis standalone", func(t *testing.T) {
2824 redisServer, err := miniredis.Run()
2925 require.NoError(t, err)
@@ -34,7 +30,9 @@ func TestRedisStore(t *testing.T) {
3430 Host: redisServer.Addr(),
3531 }
3632 opts.Session.Redis.ConnectionURL = redisURL.String()
37− redisStore, err := NewRedisSessionStore(&opts.Session, &opts.Cookie, cipher)
33+
34+ opts.Cookie.Secret = string(secret)
35+ redisStore, err := NewRedisSessionStore(&opts.Session, &opts.Cookie)
3836 require.NoError(t, err)
3937 err = redisStore.Save(
4038 httptest.NewRecorder(),
@@ -58,7 +56,9 @@ func TestRedisStore(t *testing.T) {
5856 opts.Session.Redis.SentinelConnectionURLs = []string{sentinelURL.String()}
5957 opts.Session.Redis.UseSentinel = true
6058 opts.Session.Redis.SentinelMasterName = sentinel.MasterInfo().Name
61− redisStore, err := NewRedisSessionStore(&opts.Session, &opts.Cookie, cipher)
59+
60+ opts.Cookie.Secret = string(secret)
61+ redisStore, err := NewRedisSessionStore(&opts.Session, &opts.Cookie)
6262 require.NoError(t, err)
6363 err = redisStore.Save(
6464 httptest.NewRecorder(),
pkg/sessions/session_store.go+2−7
@@ -5,22 +5,17 @@ import (
55
66 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
77 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
8− "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
98 "github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/cookie"
109 "github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/redis"
1110)
1211
1312// NewSessionStore creates a SessionStore from the provided configuration
1413func NewSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
15− cipher, err := encryption.NewBase64Cipher(encryption.NewCFBCipher, encryption.SecretBytes(cookieOpts.Secret))
16− if err != nil {
17− return nil, fmt.Errorf("error initialising cipher: %v", err)
18− }
1914 switch opts.Type {
2015 case options.CookieSessionStoreType:
21− return cookie.NewCookieSessionStore(opts, cookieOpts, cipher)
16+ return cookie.NewCookieSessionStore(opts, cookieOpts)
2217 case options.RedisSessionStoreType:
23− return redis.NewRedisSessionStore(opts, cookieOpts, cipher)
18+ return redis.NewRedisSessionStore(opts, cookieOpts)
2419 default:
2520 return nil, fmt.Errorf("unknown session store type '%s'", opts.Type)
2621 }
pkg/sessions/session_store_test.go+26−13
@@ -417,6 +417,19 @@ var _ = Describe("NewSessionStore", func() {
417417 Context("the cookie.SessionStore", func() {
418418 RunSessionTests(false)
419419 })
420+
421+ Context("with an invalid cookie secret", func() {
422+ BeforeEach(func() {
423+ cookieOpts.Secret = "invalid"
424+ })
425+
426+ It("returns an error", func() {
427+ ss, err := sessions.NewSessionStore(opts, cookieOpts)
428+ Expect(err).To(HaveOccurred())
429+ Expect(err.Error()).To(Equal("error initialising cipher: crypto/aes: invalid key size 7"))
430+ Expect(ss).To(BeNil())
431+ })
432+ })
420433 })
421434
422435 Context("with type 'redis'", func() {
@@ -441,30 +454,30 @@ var _ = Describe("NewSessionStore", func() {
441454 Context("the redis.SessionStore", func() {
442455 RunSessionTests(true)
443456 })
444− })
445457
446− Context("with an invalid type", func() {
447− BeforeEach(func() {
448− opts.Type = "invalid-type"
449− })
458+ Context("with an invalid cookie secret", func() {
459+ BeforeEach(func() {
460+ cookieOpts.Secret = "invalid"
461+ })
450462
451− It("returns an error", func() {
452− ss, err := sessions.NewSessionStore(opts, cookieOpts)
453− Expect(err).To(HaveOccurred())
454− Expect(err.Error()).To(Equal("unknown session store type 'invalid-type'"))
455− Expect(ss).To(BeNil())
463+ It("returns an error", func() {
464+ ss, err := sessions.NewSessionStore(opts, cookieOpts)
465+ Expect(err).To(HaveOccurred())
466+ Expect(err.Error()).To(Equal("error initialising cipher: crypto/aes: invalid key size 7"))
467+ Expect(ss).To(BeNil())
468+ })
456469 })
457470 })
458471
459− Context("with an invalid cookie secret", func() {
472+ Context("with an invalid type", func() {
460473 BeforeEach(func() {
461− cookieOpts.Secret = "invalid"
474+ opts.Type = "invalid-type"
462475 })
463476
464477 It("returns an error", func() {
465478 ss, err := sessions.NewSessionStore(opts, cookieOpts)
466479 Expect(err).To(HaveOccurred())
467− Expect(err.Error()).To(Equal("error initialising cipher: crypto/aes: invalid key size 7"))
480+ Expect(err.Error()).To(Equal("unknown session store type 'invalid-type'"))
468481 Expect(ss).To(BeNil())
469482 })
470483 })