Skip to content

Commit 521488d

Browse files
authored
feat: Add simple TLS support in Go RedisOnlineStore (#2860)
* Add simple TLS support in RedisOnlineStore Per the go-redis docs: 'To enable TLS/SSL, you need to provide an empty tls.Config.' https://redis.uptrace.dev/guide/go-redis.html#using-tls This will allow users to append ',ssl=True' to their connection string in order to enable TLS/SSL support in the Redis client Signed-off-by: William Horton <[email protected]> * Add tests of Redis config from connection string Signed-off-by: William Horton <[email protected]>
1 parent 2800e37 commit 521488d

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

go/internal/feast/onlinestore/redisonlinestore.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package onlinestore
22

33
import (
44
"context"
5+
"crypto/tls"
56
"encoding/binary"
67
"errors"
78
"fmt"
@@ -43,6 +44,7 @@ func NewRedisOnlineStore(project string, onlineStoreConfig map[string]interface{
4344

4445
var address []string
4546
var password string
47+
var tlsConfig *tls.Config
4648
var db int // Default to 0
4749

4850
// Parse redis_type and write it into conf.t
@@ -69,8 +71,12 @@ func NewRedisOnlineStore(project string, onlineStoreConfig map[string]interface{
6971
if kv[0] == "password" {
7072
password = kv[1]
7173
} else if kv[0] == "ssl" {
72-
// TODO (woop): Add support for TLS/SSL
73-
// ssl = kv[1] == "true"
74+
result, err := strconv.ParseBool(kv[1])
75+
if err != nil {
76+
return nil, err
77+
} else if result {
78+
tlsConfig = &tls.Config{}
79+
}
7480
} else if kv[0] == "db" {
7581
db, err = strconv.Atoi(kv[1])
7682
if err != nil {
@@ -87,9 +93,10 @@ func NewRedisOnlineStore(project string, onlineStoreConfig map[string]interface{
8793

8894
if t == redisNode {
8995
store.client = redis.NewClient(&redis.Options{
90-
Addr: address[0],
91-
Password: password, // No password set
92-
DB: db,
96+
Addr: address[0],
97+
Password: password, // No password set
98+
DB: db,
99+
TLSConfig: tlsConfig,
93100
})
94101
} else {
95102
return nil, errors.New("only single node Redis is supported at this time")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package onlinestore
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestNewRedisOnlineStore(t *testing.T) {
10+
var config = map[string]interface{}{
11+
"connection_string": "redis://localhost:6379",
12+
}
13+
store, err := NewRedisOnlineStore("test", config)
14+
assert.Nil(t, err)
15+
var opts = store.client.Options()
16+
assert.Equal(t, opts.Addr, "redis://localhost:6379")
17+
assert.Equal(t, opts.Password, "")
18+
assert.Equal(t, opts.DB, 0)
19+
assert.Nil(t, opts.TLSConfig)
20+
}
21+
22+
func TestNewRedisOnlineStoreWithPassword(t *testing.T) {
23+
var config = map[string]interface{}{
24+
"connection_string": "redis://localhost:6379,password=secret",
25+
}
26+
store, err := NewRedisOnlineStore("test", config)
27+
assert.Nil(t, err)
28+
var opts = store.client.Options()
29+
assert.Equal(t, opts.Addr, "redis://localhost:6379")
30+
assert.Equal(t, opts.Password, "secret")
31+
}
32+
33+
func TestNewRedisOnlineStoreWithDB(t *testing.T) {
34+
var config = map[string]interface{}{
35+
"connection_string": "redis://localhost:6379,db=1",
36+
}
37+
store, err := NewRedisOnlineStore("test", config)
38+
assert.Nil(t, err)
39+
var opts = store.client.Options()
40+
assert.Equal(t, opts.Addr, "redis://localhost:6379")
41+
assert.Equal(t, opts.DB, 1)
42+
}
43+
44+
func TestNewRedisOnlineStoreWithSsl(t *testing.T) {
45+
var config = map[string]interface{}{
46+
"connection_string": "redis://localhost:6379,ssl=true",
47+
}
48+
store, err := NewRedisOnlineStore("test", config)
49+
assert.Nil(t, err)
50+
var opts = store.client.Options()
51+
assert.Equal(t, opts.Addr, "redis://localhost:6379")
52+
assert.NotNil(t, opts.TLSConfig)
53+
}

0 commit comments

Comments
 (0)