Skip to content

Commit 3aea406

Browse files
authored
feat: cache: redis: tls: allow enabling without client certs (#556)
1 parent cad3e77 commit 3aea406

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

cache/async_cache_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,45 @@ func TestAsyncCache_RedisCache_TLS(t *testing.T) {
299299
}
300300
}
301301

302+
func TestAsyncCache_RedisCache_ServerOnlyTLS(t *testing.T) {
303+
serverCfg := config.TLS{
304+
CertFile: "../testdata/example.com.cert",
305+
KeyFile: "../testdata/example.com.key",
306+
}
307+
308+
clientCfg := config.TLS{
309+
InsecureSkipVerify: true,
310+
}
311+
312+
tlsServerConfig, err := serverCfg.BuildTLSConfig(nil)
313+
if err != nil {
314+
t.Fatalf("could not build tls config: %s", err)
315+
}
316+
s := miniredis.NewMiniRedis()
317+
if err := s.StartTLS(tlsServerConfig); err != nil {
318+
t.Fatalf("could not start miniredis: %s", err.Error())
319+
// not reached
320+
}
321+
t.Cleanup(s.Close)
322+
323+
var redisCfg = config.Cache{
324+
Name: "test",
325+
Mode: "redis",
326+
Redis: config.RedisCacheConfig{
327+
EnableTLS: true,
328+
TLS: clientCfg,
329+
Addresses: []string{s.Addr()},
330+
},
331+
Expire: config.Duration(cacheTTL),
332+
MaxPayloadSize: config.ByteSize(100000000),
333+
}
334+
335+
_, err = NewAsyncCache(redisCfg, 1*time.Second)
336+
if err != nil {
337+
t.Fatalf("could not instanciate redis async cache because of the following error: %s", err.Error())
338+
}
339+
}
340+
302341
func TestAsyncCache_RedisCache_wrong_instantiation(t *testing.T) {
303342
var redisCfg = config.Cache{
304343
Name: "test",

clients/redis.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func NewRedisClient(cfg config.RedisCacheConfig) (redis.UniversalClient, error)
2222
options.DB = cfg.DBIndex
2323
}
2424

25-
if len(cfg.CertFile) != 0 || len(cfg.KeyFile) != 0 {
25+
// maintain backwards compatibility in case of non-presence of enable_tls
26+
if len(cfg.CertFile) != 0 || len(cfg.KeyFile) != 0 || cfg.EnableTLS {
2627
tlsConfig, err := cfg.TLS.BuildTLSConfig(nil)
2728
if err != nil {
2829
return nil, err

config/config.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,10 @@ func (c *TLS) BuildTLSConfig(acm *autocert.Manager) (*tls.Config, error) {
367367
c.CertFile, c.KeyFile, err)
368368
}
369369
tlsCfg.Certificates = []tls.Certificate{cert}
370-
} else {
371-
if acm == nil {
372-
return nil, fmt.Errorf("autocert manager is not configured")
373-
}
370+
} else if acm != nil {
374371
tlsCfg.GetCertificate = acm.GetCertificate
375372
}
373+
376374
return &tlsCfg, nil
377375
}
378376

@@ -965,7 +963,8 @@ type FileSystemCacheConfig struct {
965963
}
966964

967965
type RedisCacheConfig struct {
968-
TLS `yaml:",inline"`
966+
TLS `yaml:",inline"`
967+
EnableTLS bool `yaml:"enable_tls,omitempty"`
969968

970969
Username string `yaml:"username,omitempty"`
971970
Password string `yaml:"password,omitempty"`

docs/src/content/docs/configuration/default.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,22 @@ caches:
6767
# Applicable for cache mode: redis
6868
# You should use multiple addresses only if they all belong to the same redis cluster.
6969
redis:
70-
# Paths to TLS cert and key files for the redis server.
71-
# If you change the cert & key files while chproxy is running, you have to restart chproxy so that it loads them.
72-
# Triggering a SIGHUP signal won't work as for the rest of the configuration.
73-
cert_file: "redis tls cert file path"
74-
key_file: "redis tls key file apth"
75-
# Allow to skip the verification of the redis server certificate.
76-
insecure_skip_verify: true
77-
7870
addresses:
7971
- "localhost:16379"
8072
username: "user"
8173
password: "pass"
74+
75+
# TLS: For backwards compatibility, having a non-empty cert_file and key_file also enables TLS configuration.
76+
enable_tls: false
77+
78+
# TLS: Switch to true to disable server certificate validation ( e.g. when using self-signed certificates )
79+
insecure_skip_verify: false
80+
81+
# TLS: Paths to cert and key file for client-side X.509/mTLS authentication.
82+
# Reload is NOT automatic : SIGHUP insufficient, chproxy must be restarted.
83+
cert_file: "path to of tls client certificate to present to redis conn"
84+
key_file: "path to of tls client cert key to present to redis conn"
85+
8286
expire: 10s
8387

8488
# Optional network lists, might be used as values for `allowed_networks`.

0 commit comments

Comments
 (0)