@@ -1,10 +1,6 @@
11package redis
22
33import (
4− "bytes"
5− "crypto/tls"
6− "encoding/pem"
7− "os"
84 "time"
95
106 "github.com/Bose/minisentinel"
@@ -13,43 +9,15 @@ import (
139 sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
1410 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/persistence"
1511 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/sessions/tests"
16− "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util"
17− . "github.com/onsi/ginkgo"
12+ . "github.com/onsi/ginkgo/v2"
1813 . "github.com/onsi/gomega"
1914)
2015
21−const redisUsername = "testuser"
22−const redisPassword = "0123456789abcdefghijklmnopqrstuv"
23−
24−var (
25− cert tls.Certificate
26− caPath string
16+const (
17+ redisUsername = "testuser"
18+ redisPassword = "0123456789abcdefghijklmnopqrstuv"
2719)
2820
29−var _ = BeforeSuite(func() {
30− var err error
31− certBytes, keyBytes, err := util.GenerateCert("127.0.0.1")
32− Expect(err).ToNot(HaveOccurred())
33− certOut := new(bytes.Buffer)
34− Expect(pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})).To(Succeed())
35− certData := certOut.Bytes()
36− keyOut := new(bytes.Buffer)
37− Expect(pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})).To(Succeed())
38− cert, err = tls.X509KeyPair(certData, keyOut.Bytes())
39− Expect(err).ToNot(HaveOccurred())
40−
41− certFile, err := os.CreateTemp("", "cert.*.pem")
42− Expect(err).ToNot(HaveOccurred())
43− caPath = certFile.Name()
44− _, err = certFile.Write(certData)
45− defer certFile.Close()
46− Expect(err).ToNot(HaveOccurred())
47−})
48−
49−var _ = AfterSuite(func() {
50− Expect(os.Remove(caPath)).ToNot(HaveOccurred())
51−})
52−
5321var _ = Describe("Redis SessionStore Tests", func() {
5422 // helper interface to allow us to close client connections
5523 // All non-nil redis clients should implement this
@@ -79,11 +47,12 @@ var _ = Describe("Redis SessionStore Tests", func() {
7947 }
8048 })
8149
50+ const redisProtocol = "redis://"
8251 tests.RunSessionStoreTests(
8352 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
8453 // Set the connection URL
8554 opts.Type = options.RedisSessionStoreType
86− opts.Redis.ConnectionURL = "redis://" + mr.Addr()
55+ opts.Redis.ConnectionURL = redisProtocol + mr.Addr()
8756
8857 // Capture the session store so that we can close the client
8958 var err error
@@ -104,14 +73,10 @@ var _ = Describe("Redis SessionStore Tests", func() {
10473 Expect(ms.Start()).To(Succeed())
10574 })
10675
107− AfterEach(func() {
108− ms.Close()
109− })
110−
11176 tests.RunSessionStoreTests(
11277 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
11378 // Set the sentinel connection URL
114− sentinelAddr := "redis://" + ms.Addr()
79+ sentinelAddr := redisProtocol + ms.Addr()
11580 opts.Type = options.RedisSessionStoreType
11681 opts.Redis.SentinelConnectionURLs = []string{sentinelAddr}
11782 opts.Redis.UseSentinel = true
@@ -132,7 +97,7 @@ var _ = Describe("Redis SessionStore Tests", func() {
13297 Context("with cluster", func() {
13398 tests.RunSessionStoreTests(
13499 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
135− clusterAddr := "redis://" + mr.Addr()
100+ clusterAddr := redisProtocol + mr.Addr()
136101 opts.Type = options.RedisSessionStoreType
137102 opts.Redis.ClusterConnectionURLs = []string{clusterAddr}
138103 opts.Redis.UseCluster = true
@@ -162,7 +127,7 @@ var _ = Describe("Redis SessionStore Tests", func() {
162127 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
163128 // Set the connection URL
164129 opts.Type = options.RedisSessionStoreType
165− opts.Redis.ConnectionURL = "redis://" + mr.Addr()
130+ opts.Redis.ConnectionURL = redisProtocol + mr.Addr()
166131 opts.Redis.Password = redisPassword
167132
168133 // Capture the session store so that we can close the client
@@ -184,14 +149,10 @@ var _ = Describe("Redis SessionStore Tests", func() {
184149 Expect(ms.Start()).To(Succeed())
185150 })
186151
187− AfterEach(func() {
188− ms.Close()
189− })
190−
191152 tests.RunSessionStoreTests(
192153 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
193154 // Set the sentinel connection URL
194− sentinelAddr := "redis://" + ms.Addr()
155+ sentinelAddr := redisProtocol + ms.Addr()
195156 opts.Type = options.RedisSessionStoreType
196157 opts.Redis.SentinelConnectionURLs = []string{sentinelAddr}
197158 opts.Redis.UseSentinel = true
@@ -213,7 +174,7 @@ var _ = Describe("Redis SessionStore Tests", func() {
213174 Context("with cluster", func() {
214175 tests.RunSessionStoreTests(
215176 func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
216− clusterAddr := "redis://" + mr.Addr()
177+ clusterAddr := redisProtocol + mr.Addr()
217178 opts.Type = options.RedisSessionStoreType
218179 opts.Redis.ClusterConnectionURLs = []string{clusterAddr}
219180 opts.Redis.UseCluster = true
@@ -281,190 +242,4 @@ var _ = Describe("Redis SessionStore Tests", func() {
281242 )
282243 })
283244 })
284−
285− Context("with TLS connection", func() {
286− BeforeEach(func() {
287− mr.Close()
288−
289− var err error
290− mr, err = miniredis.RunTLS(&tls.Config{Certificates: []tls.Certificate{cert}})
291− Expect(err).ToNot(HaveOccurred())
292− })
293− AfterEach(func() {
294− mr.Close()
295−
296− var err error
297− mr, err = miniredis.Run()
298− Expect(err).ToNot(HaveOccurred())
299− })
300−
301− Context("with standalone", func() {
302− tests.RunSessionStoreTests(
303− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
304− // Set the connection URL
305− opts.Type = options.RedisSessionStoreType
306− opts.Redis.ConnectionURL = "rediss://" + mr.Addr()
307− opts.Redis.CAPath = caPath
308−
309− // Capture the session store so that we can close the client
310− ss, err := NewRedisSessionStore(opts, cookieOpts)
311− return ss, err
312− },
313− func(d time.Duration) error {
314− mr.FastForward(d)
315− return nil
316− },
317− )
318− })
319−
320− Context("with cluster", func() {
321− tests.RunSessionStoreTests(
322− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
323− clusterAddr := "rediss://" + mr.Addr()
324− opts.Type = options.RedisSessionStoreType
325− opts.Redis.ClusterConnectionURLs = []string{clusterAddr}
326− opts.Redis.UseCluster = true
327− opts.Redis.CAPath = caPath
328−
329− // Capture the session store so that we can close the client
330− var err error
331− ss, err = NewRedisSessionStore(opts, cookieOpts)
332− return ss, err
333− },
334− func(d time.Duration) error {
335− mr.FastForward(d)
336− return nil
337− },
338− )
339− })
340− })
341−
342− Context("with insecure TLS connection", func() {
343− BeforeEach(func() {
344− mr.Close()
345−
346− var err error
347− mr, err = miniredis.RunTLS(&tls.Config{Certificates: []tls.Certificate{cert}})
348− Expect(err).ToNot(HaveOccurred())
349− })
350− AfterEach(func() {
351− mr.Close()
352−
353− var err error
354− mr, err = miniredis.Run()
355− Expect(err).ToNot(HaveOccurred())
356− })
357−
358− Context("with standalone", func() {
359− tests.RunSessionStoreTests(
360− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
361− // Set the connection URL
362− opts.Type = options.RedisSessionStoreType
363− opts.Redis.ConnectionURL = "rediss://" + mr.Addr()
364− opts.Redis.InsecureSkipTLSVerify = true
365−
366− // Capture the session store so that we can close the client
367− ss, err := NewRedisSessionStore(opts, cookieOpts)
368− return ss, err
369− },
370− func(d time.Duration) error {
371− mr.FastForward(d)
372− return nil
373− },
374− )
375− })
376−
377− Context("with cluster", func() {
378− tests.RunSessionStoreTests(
379− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
380− clusterAddr := "rediss://" + mr.Addr()
381− opts.Type = options.RedisSessionStoreType
382− opts.Redis.ClusterConnectionURLs = []string{clusterAddr}
383− opts.Redis.UseCluster = true
384− opts.Redis.InsecureSkipTLSVerify = true
385−
386− // Capture the session store so that we can close the client
387− var err error
388− ss, err = NewRedisSessionStore(opts, cookieOpts)
389− return ss, err
390− },
391− func(d time.Duration) error {
392− mr.FastForward(d)
393− return nil
394− },
395− )
396− })
397− })
398−
399− Context("with custom CA path", func() {
400− BeforeEach(func() {
401− mr.Close()
402−
403− var err error
404− mr, err = miniredis.RunTLS(&tls.Config{Certificates: []tls.Certificate{cert}})
405− Expect(err).ToNot(HaveOccurred())
406− })
407−
408− AfterEach(func() {
409− mr.Close()
410−
411− var err error
412− mr, err = miniredis.Run()
413− Expect(err).ToNot(HaveOccurred())
414− })
415−
416− tests.RunSessionStoreTests(
417− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
418− // Set the connection URL
419− opts.Type = options.RedisSessionStoreType
420− opts.Redis.ConnectionURL = "redis://" + mr.Addr()
421− opts.Redis.CAPath = caPath
422−
423− // Capture the session store so that we can close the client
424− var err error
425− ss, err = NewRedisSessionStore(opts, cookieOpts)
426− return ss, err
427− },
428− func(d time.Duration) error {
429− mr.FastForward(d)
430− return nil
431− },
432− )
433− })
434−
435− Context("with insecure TLS connection", func() {
436− BeforeEach(func() {
437− mr.Close()
438−
439− var err error
440− mr, err = miniredis.RunTLS(&tls.Config{Certificates: []tls.Certificate{cert}})
441− Expect(err).ToNot(HaveOccurred())
442− })
443−
444− AfterEach(func() {
445− mr.Close()
446−
447− var err error
448− mr, err = miniredis.Run()
449− Expect(err).ToNot(HaveOccurred())
450− })
451−
452− tests.RunSessionStoreTests(
453− func(opts *options.SessionOptions, cookieOpts *options.Cookie) (sessionsapi.SessionStore, error) {
454− // Set the connection URL
455− opts.Type = options.RedisSessionStoreType
456− opts.Redis.ConnectionURL = "redis://127.0.0.1:" + mr.Port() // func (*Miniredis) StartTLS listens on 127.0.0.1
457− opts.Redis.InsecureSkipTLSVerify = true
458−
459− // Capture the session store so that we can close the client
460− var err error
461− ss, err = NewRedisSessionStore(opts, cookieOpts)
462− return ss, err
463− },
464− func(d time.Duration) error {
465− mr.FastForward(d)
466− return nil
467− },
468− )
469− })
470245})