Skip to content

Commit 17a13a7

Browse files
authored
feat: add TLS option for redis adapter creation (#38)
* Add TLS option for redis adapter creation * Add redis dial option to use tls * Add TLS option for create adapter with options * Improve unit tests * Improve according to reviews
1 parent 0cd40e5 commit 17a13a7

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
func main() {
20+
// Direct Initialization:
2021
// Initialize a Redis adapter and use it in a Casbin enforcer:
2122
a, _ := redisadapter.NewAdapter("tcp", "127.0.0.1:6379") // Your Redis network and address.
2223

@@ -29,6 +30,15 @@ func main() {
2930
// Use the following if you use Redis connections pool
3031
// pool := &redis.Pool{}
3132
// a, err := redisadapter.NewAdapterWithPool(pool)
33+
34+
// Initialization with different user options:
35+
// Use the following if you use Redis with passowrd like "123":
36+
// a, err := redisadapter.NewAdapterWithOption(redisadapter.WithNetwork("tcp"), redisadapter.WithAddress("127.0.0.1:6379"), redisadapter.WithPassword("123"))
37+
38+
// Use the following if you use Redis with username, password, and TLS option:
39+
// var clientTLSConfig tls.Config
40+
// ...
41+
// a, err := redisadapter.NewAdapterWithOption(redisadapter.WithNetwork("tcp"), redisadapter.WithAddress("127.0.0.1:6379"), redisadapter.WithUsername("testAccount"), redisadapter.WithPassword("123456"), redisadapter.WithTls(&clientTLSConfig))
3242

3343
e := casbin.NewEnforcer("examples/rbac_model.conf", a)
3444

adapter.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package redisadapter
1616

1717
import (
1818
"bytes"
19+
"crypto/tls"
1920
"encoding/json"
2021
"errors"
2122
"fmt"
@@ -46,6 +47,7 @@ type Adapter struct {
4647
key string
4748
username string
4849
password string
50+
tlsConfig *tls.Config
4951
conn redis.Conn
5052
isFiltered bool
5153
}
@@ -106,7 +108,7 @@ func NewAdapterWithPool(pool *redis.Pool) (*Adapter, error) {
106108

107109
type Option func(*Adapter)
108110

109-
func NewAdpaterWithOption(options ...Option) (*Adapter, error) {
111+
func NewAdapterWithOption(options ...Option) (*Adapter, error) {
110112
a := &Adapter{}
111113
for _, option := range options {
112114
option(a)
@@ -149,24 +151,31 @@ func WithKey(key string) Option {
149151
}
150152
}
151153

154+
func WithTls(tlsConfig *tls.Config) Option {
155+
return func(a *Adapter) {
156+
a.tlsConfig = tlsConfig
157+
}
158+
}
159+
152160
func (a *Adapter) open() error {
153161
//redis.Dial("tcp", "127.0.0.1:6379")
162+
useTls := a.tlsConfig != nil
154163
if a.username != "" {
155-
conn, err := redis.Dial(a.network, a.address, redis.DialUsername(a.username), redis.DialPassword(a.password))
164+
conn, err := redis.Dial(a.network, a.address, redis.DialUsername(a.username), redis.DialPassword(a.password), redis.DialTLSConfig(a.tlsConfig), redis.DialUseTLS(useTls))
156165
if err != nil {
157166
return err
158167
}
159168

160169
a.conn = conn
161170
} else if a.password == "" {
162-
conn, err := redis.Dial(a.network, a.address)
171+
conn, err := redis.Dial(a.network, a.address, redis.DialTLSConfig(a.tlsConfig), redis.DialUseTLS(useTls))
163172
if err != nil {
164173
return err
165174
}
166175

167176
a.conn = conn
168177
} else {
169-
conn, err := redis.Dial(a.network, a.address, redis.DialPassword(a.password))
178+
conn, err := redis.Dial(a.network, a.address, redis.DialPassword(a.password), redis.DialTLSConfig(a.tlsConfig), redis.DialUseTLS(useTls))
170179
if err != nil {
171180
return err
172181
}

adapter_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,20 @@ func TestAdapters(t *testing.T) {
361361

362362
// Use the following if you use Redis with a account
363363
// a, err := NewAdapterWithUser("tcp", "127.0.0.1:6379", "testaccount", "userpass")
364+
testSaveLoad(t, a)
365+
testAutoSave(t, a)
366+
testFilteredPolicy(t, a)
367+
testAddPolicies(t, a)
368+
testRemovePolicies(t, a)
369+
testUpdatePolicies(t, a)
370+
testUpdateFilteredPolicies(t, a)
371+
}
372+
373+
func TestAdapterWithOption(t *testing.T) {
374+
a, _ := NewAdapterWithOption(WithNetwork("tcp"), WithAddress("127.0.0.1:6379"))
375+
// User the following if use TLS to connect to redis
376+
// var clientTLSConfig tls.Config
377+
// a, err := NewAdapterWithOption(WithTls(&clientTLSConfig))
364378

365379
testSaveLoad(t, a)
366380
testAutoSave(t, a)

0 commit comments

Comments
 (0)