Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 4c5d2e8

Browse files
committed
feat: support Redis TLS
Signed-off-by: knqyf263 <[email protected]>
1 parent 47f8aab commit 4c5d2e8

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ dev:
3939
debug:
4040
skaffold debug --tolerate-failures-until-deadline=true
4141

42-
run: export SCANNER_TRIVY_CACHE_DIR = $(TMPDIR)harbor-scanner-trivy/.cache/trivy
43-
run: export SCANNER_TRIVY_REPORTS_DIR=$(TMPDIR)harbor-scanner-trivy/.cache/reports
42+
run: export SCANNER_TRIVY_CACHE_DIR = $(TMPDIR)/harbor-scanner-trivy/.cache/trivy
43+
run: export SCANNER_TRIVY_REPORTS_DIR=$(TMPDIR)/harbor-scanner-trivy/.cache/reports
4444
run: export SCANNER_LOG_LEVEL=debug
4545
run:
4646
@mkdir -p $(SCANNER_TRIVY_CACHE_DIR) $(SCANNER_TRIVY_REPORTS_DIR)

pkg/etc/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type RedisPool struct {
7373
ConnectionTimeout time.Duration `env:"SCANNER_REDIS_POOL_CONNECTION_TIMEOUT" envDefault:"1s"`
7474
ReadTimeout time.Duration `env:"SCANNER_REDIS_POOL_READ_TIMEOUT" envDefault:"1s"`
7575
WriteTimeout time.Duration `env:"SCANNER_REDIS_POOL_WRITE_TIMEOUT" envDefault:"1s"`
76+
CACert string `env:"SCANNER_REDIS_CA_CERT" envDefault:""` // For private certs
7677
}
7778

7879
func LogLevel() slog.Level {

pkg/redisx/pool.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package redisx
22

33
import (
44
"context"
5+
"crypto/x509"
56
"fmt"
67
"log/slog"
78
"net/url"
9+
"os"
810
"strconv"
911
"strings"
1012

@@ -25,7 +27,7 @@ func NewClient(config etc.RedisPool) (*redis.Client, error) {
2527
}
2628

2729
switch configURL.Scheme {
28-
case "redis":
30+
case "redis", "rediss":
2931
return newInstancePool(config)
3032
case "redis+sentinel":
3133
return newSentinelPool(configURL, config)
@@ -36,7 +38,7 @@ func NewClient(config etc.RedisPool) (*redis.Client, error) {
3638

3739
// redis://user:password@host:port/db-number
3840
func newInstancePool(config etc.RedisPool) (*redis.Client, error) {
39-
// TODO: Ask the Harbor team about why they use "idle_timeout_seconds" instead of "idle_timeout".
41+
// redigo uses "idle_timeout_seconds" for the idle timeout configuration
4042
config.URL = strings.ReplaceAll(config.URL, "idle_timeout_seconds", "idle_timeout")
4143

4244
slog.Debug("Constructing connection pool for Redis", slog.String("url", config.URL))
@@ -53,6 +55,17 @@ func newInstancePool(config etc.RedisPool) (*redis.Client, error) {
5355
return nil
5456
}
5557

58+
if options.TLSConfig != nil && config.CACert != "" {
59+
// Load the CA certificate
60+
caCert, err := os.ReadFile(config.CACert)
61+
if err != nil {
62+
return nil, xerrors.Errorf("unable to read CA certificate: %s", err)
63+
}
64+
caCertPool := x509.NewCertPool()
65+
caCertPool.AppendCertsFromPEM(caCert)
66+
options.TLSConfig.RootCAs = caCertPool
67+
}
68+
5669
return redis.NewClient(options), nil
5770
}
5871

pkg/redisx/pool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212

1313
func TestGetPool(t *testing.T) {
1414

15-
t.Run("Should return error when configured to connect to secure redis", func(t *testing.T) {
15+
t.Run("Should not return error when configured to connect to secure redis", func(t *testing.T) {
1616
_, err := NewClient(etc.RedisPool{
1717
URL: "rediss://hostname:6379",
1818
})
19-
assert.EqualError(t, err, "invalid redis URL scheme: rediss")
19+
assert.NoError(t, err)
2020
})
2121

2222
t.Run("Should return error when configured with unsupported url scheme", func(t *testing.T) {

0 commit comments

Comments
 (0)