Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Redis server TLS support #104

Merged
merged 8 commits into from
Nov 20, 2024
20 changes: 20 additions & 0 deletions kv/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kv

import (
"crypto/tls"
"time"
)

Expand All @@ -26,6 +27,12 @@ type Config struct {
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
IdleCheckFreq time.Duration `mapstructure:"idle_check_freq"`
ReadOnly bool `mapstructure:"read_only"`
TLSConfig TLSConfig `mapstructure:"tls"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a pointer: *TLSConfig to distinguish between empty and non set values.

}

type TLSConfig struct {
MinVersion string `mapstructure:"min_version"`
CaFile string `mapstructure:"ca_file"`
}

// InitDefaults initializing fill config with default values
Expand All @@ -34,3 +41,16 @@ func (s *Config) InitDefaults() {
s.Addrs = []string{"127.0.0.1:6379"} // default addr is pointing to local storage
}
}

func (t *TLSConfig) TLSVersion() uint16 {
switch t.MinVersion {
case "1.0":
return tls.VersionTLS10
case "1.1":
return tls.VersionTLS11
case "1.2":
return tls.VersionTLS12
default:
return tls.VersionTLS13
}
}
39 changes: 37 additions & 2 deletions kv/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import (
"context"
"crypto/tls"
"crypto/x509"
stderr "errors"
"os"
"strings"
"time"
"unsafe"
Expand Down Expand Up @@ -53,7 +56,7 @@

d.cfg.InitDefaults()

d.universalClient = redis.NewUniversalClient(&redis.UniversalOptions{
redisOptions := &redis.UniversalOptions{
Addrs: d.cfg.Addrs,
DB: d.cfg.DB,
Username: d.cfg.Username,
Expand All @@ -74,7 +77,39 @@
RouteByLatency: d.cfg.RouteByLatency,
RouteRandomly: d.cfg.RouteRandomly,
MasterName: d.cfg.MasterName,
})
}

tlsConfig := &tls.Config{}

Check failure on line 82 in kv/driver.go

View workflow job for this annotation

GitHub Actions / Golang-CI (lint)

G402: TLS MinVersion too low. (gosec)
rustatian marked this conversation as resolved.
Show resolved Hide resolved
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
rustatian marked this conversation as resolved.
Show resolved Hide resolved

if d.cfg.TLSConfig.MinVersion != "" {
tlsConfig.MinVersion = d.cfg.TLSConfig.TLSVersion()
}
rustatian marked this conversation as resolved.
Show resolved Hide resolved

if d.cfg.TLSConfig.CaFile != "" {
if _, crtExistErr := os.Stat(d.cfg.TLSConfig.CaFile); crtExistErr != nil {
return nil, errors.E(op, crtExistErr)
}

bytes, crtReadErr := os.ReadFile(d.cfg.TLSConfig.CaFile)
if crtReadErr != nil {
return nil, errors.E(op, crtReadErr)
}

if !rootCAs.AppendCertsFromPEM(bytes) {
return nil, errors.E(op, errors.Errorf("failed to append certs from PEM file: %s", d.cfg.TLSConfig.CaFile))
}
rustatian marked this conversation as resolved.
Show resolved Hide resolved
}

if d.cfg.TLSConfig.CaFile != "" || d.cfg.TLSConfig.MinVersion != "" {
tlsConfig.RootCAs = rootCAs
redisOptions.TLSConfig = tlsConfig
}

d.universalClient = redis.NewUniversalClient(redisOptions)

err = redisotel.InstrumentMetrics(d.universalClient)
if err != nil {
Expand Down
Loading