Skip to content

Commit 7fe6abd

Browse files
committed
feat: add TLS configuration options and functions to options struct
- Add TLS configuration option to `options` struct - Introduce `WithTLS` function to configure TLS with a minimum version of 1.2 - Introduce `WithSkipTLSVerify` function to allow skipping TLS certificate verification - Update `NewWorker` function to use TLS configuration from options Signed-off-by: appleboy <[email protected]>
1 parent 74b703f commit 7fe6abd

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

options.go

Lines changed: 30 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
"time"
67

78
"github.com/golang-queue/queue"
@@ -25,6 +26,7 @@ type options struct {
2526
consumer string
2627
maxLength int64
2728
blockTime time.Duration
29+
tls *tls.Config
2830
}
2931

3032
// WithAddr setup the addr of redis
@@ -121,6 +123,34 @@ func WithLogger(l queue.Logger) Option {
121123
}
122124
}
123125

126+
// WithTLS returns an Option that configures the use of TLS for the connection.
127+
// It sets the minimum TLS version to TLS 1.2.
128+
func WithTLS() Option {
129+
return func(w *options) {
130+
w.tls = &tls.Config{
131+
MinVersion: tls.VersionTLS12,
132+
}
133+
}
134+
}
135+
136+
// WithSkipTLSVerify returns an Option that configures the TLS settings to skip
137+
// verification of the server's certificate. This is useful for connecting to
138+
// servers with self-signed certificates or when certificate verification is
139+
// not required. Use this option with caution as it makes the connection
140+
// susceptible to man-in-the-middle attacks.
141+
func WithSkipTLSVerify() Option {
142+
return func(w *options) {
143+
if w.tls == nil {
144+
w.tls = &tls.Config{
145+
InsecureSkipVerify: true, //nolint: gosec
146+
147+
}
148+
return
149+
}
150+
w.tls.InsecureSkipVerify = true
151+
}
152+
}
153+
124154
func newOptions(opts ...Option) options {
125155
defaultOpts := options{
126156
addr: "127.0.0.1:6379",

redis.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ func NewWorker(opts ...Option) *Worker {
5050
} else if w.opts.addr != "" {
5151
if w.opts.cluster {
5252
w.rdb = redis.NewClusterClient(&redis.ClusterOptions{
53-
Addrs: strings.Split(w.opts.addr, ","),
54-
Username: w.opts.username,
55-
Password: w.opts.password,
53+
Addrs: strings.Split(w.opts.addr, ","),
54+
Username: w.opts.username,
55+
Password: w.opts.password,
56+
TLSConfig: w.opts.tls,
5657
})
5758
} else {
5859
options := &redis.Options{
59-
Addr: w.opts.addr,
60-
Username: w.opts.username,
61-
Password: w.opts.password,
62-
DB: w.opts.db,
60+
Addr: w.opts.addr,
61+
Username: w.opts.username,
62+
Password: w.opts.password,
63+
DB: w.opts.db,
64+
TLSConfig: w.opts.tls,
6365
}
6466
w.rdb = redis.NewClient(options)
6567
}

0 commit comments

Comments
 (0)