Skip to content

Commit 61e5e01

Browse files
authored
feat: add TLS configuration support for Redis connections (#31)
- Add TLS configuration option to the options struct - Introduce `WithTLS` option function to configure Redis connection with TLS - Introduce `WithSkipTLSVerify` option function to allow skipping TLS certificate verification - Update `NewWorker` function to include TLS configuration in Redis connection settings Signed-off-by: appleboy <[email protected]>
1 parent ca15191 commit 61e5e01

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

options.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redisdb
22

33
import (
44
"context"
5+
"crypto/tls"
56

67
"github.com/golang-queue/queue"
78
"github.com/golang-queue/queue/core"
@@ -23,6 +24,7 @@ type options struct {
2324
cluster bool
2425
sentinel bool
2526
masterName string
27+
tls *tls.Config
2628
}
2729

2830
// WithAddr setup the addr of redis
@@ -53,6 +55,38 @@ func WithSentinel(enable bool) Option {
5355
}
5456
}
5557

58+
// WithTLS is an option function that configures the Redis connection to use TLS.
59+
// It sets the ServerName to the address of the Redis server and enforces a minimum
60+
// TLS version of 1.2.
61+
func WithTLS() Option {
62+
return func(w *options) {
63+
w.tls = &tls.Config{
64+
MinVersion: tls.VersionTLS12,
65+
}
66+
if w.addr != "" {
67+
w.tls.ServerName = w.addr
68+
}
69+
}
70+
}
71+
72+
// WithSkipTLSVerify returns an Option that configures the TLS settings to skip
73+
// verification of the server's certificate. This is useful for connecting to
74+
// servers with self-signed certificates or when certificate verification is
75+
// not required. Use this option with caution as it makes the connection
76+
// susceptible to man-in-the-middle attacks.
77+
func WithSkipTLSVerify() Option {
78+
return func(w *options) {
79+
if w.tls == nil {
80+
w.tls = &tls.Config{
81+
InsecureSkipVerify: true, //nolint: gosec
82+
83+
}
84+
return
85+
}
86+
w.tls.InsecureSkipVerify = true
87+
}
88+
}
89+
5690
// WithMasterName sentinel master name
5791
func WithMasterName(masterName string) Option {
5892
return func(w *options) {

redis.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ func NewWorker(opts ...Option) *Worker {
4141
}
4242

4343
options := &redis.Options{
44-
Addr: w.opts.addr,
45-
Username: w.opts.username,
46-
Password: w.opts.password,
47-
DB: w.opts.db,
44+
Addr: w.opts.addr,
45+
Username: w.opts.username,
46+
Password: w.opts.password,
47+
DB: w.opts.db,
48+
TLSConfig: w.opts.tls,
4849
}
4950
w.rdb = redis.NewClient(options)
5051

@@ -58,9 +59,10 @@ func NewWorker(opts ...Option) *Worker {
5859

5960
if w.opts.cluster {
6061
w.rdb = redis.NewClusterClient(&redis.ClusterOptions{
61-
Addrs: strings.Split(w.opts.addr, ","),
62-
Username: w.opts.username,
63-
Password: w.opts.password,
62+
Addrs: strings.Split(w.opts.addr, ","),
63+
Username: w.opts.username,
64+
Password: w.opts.password,
65+
TLSConfig: w.opts.tls,
6466
})
6567
}
6668

@@ -71,6 +73,7 @@ func NewWorker(opts ...Option) *Worker {
7173
Username: w.opts.username,
7274
Password: w.opts.password,
7375
DB: w.opts.db,
76+
TLSConfig: w.opts.tls,
7477
})
7578
}
7679

0 commit comments

Comments
 (0)