From 9e7291903f683e693c5aee5197b2d54b7e3bc8a2 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Wed, 22 Jul 2020 13:28:45 +0800 Subject: [PATCH 1/5] support redis tls --- database/gredis/gredis.go | 4 ++++ database/gredis/gredis_config.go | 6 +++++ database/gredis/gredis_z_unit_test.go | 32 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/database/gredis/gredis.go b/database/gredis/gredis.go index 2a2b008ba..a2fbf462f 100644 --- a/database/gredis/gredis.go +++ b/database/gredis/gredis.go @@ -46,6 +46,8 @@ type Config struct { IdleTimeout time.Duration // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0) MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0) ConnectTimeout time.Duration // Dial connection timeout. + TLS bool //support tls + TLSSkipVerify bool //tls skip verify } // Pool statistics. @@ -102,6 +104,8 @@ func New(config Config) *Redis { "tcp", fmt.Sprintf("%s:%d", config.Host, config.Port), redis.DialConnectTimeout(config.ConnectTimeout), + redis.DialUseTLS(config.TLS), + redis.DialTLSSkipVerify(config.TLSSkipVerify), ) if err != nil { return nil, err diff --git a/database/gredis/gredis_config.go b/database/gredis/gredis_config.go index eab96d73d..c8c2b9b01 100644 --- a/database/gredis/gredis_config.go +++ b/database/gredis/gredis_config.go @@ -110,6 +110,12 @@ func ConfigFromStr(str string) (config Config, err error) { if v, ok := parse["maxConnLifetime"]; ok { config.MaxConnLifetime = gconv.Duration(v) * time.Second } + if v, ok := parse["tls"]; ok { + config.TLS = gconv.Bool(v) + } + if v, ok := parse["skipVerify"]; ok { + config.TLSSkipVerify = gconv.Bool(v) + } return } array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str) diff --git a/database/gredis/gredis_z_unit_test.go b/database/gredis/gredis_z_unit_test.go index 23e5804f0..50bef9473 100644 --- a/database/gredis/gredis_z_unit_test.go +++ b/database/gredis/gredis_z_unit_test.go @@ -27,6 +27,14 @@ var ( Port: 6379, Db: 1, } + + tlsConfig = gredis.Config{ + Host: "127.0.0.1", + Port: 6379, + Db: 1, + TLS: true, + TLSSkipVerify: true, + } ) func Test_NewClose(t *testing.T) { @@ -382,3 +390,27 @@ func Test_Auto_MarshalSlice(t *testing.T) { t.Assert(users2, users1) }) } + +func Test_Conn_TLS(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + redis := gredis.New(tlsConfig) + defer redis.Close() + conn := redis.Conn() + defer conn.Close() + + key := gconv.String(gtime.TimestampNano()) + value := []byte("v") + r, err := conn.Do("SET", key, value) + t.Assert(err, nil) + + r, err = conn.Do("GET", key) + t.Assert(err, nil) + t.Assert(r, value) + + _, err = conn.Do("DEL", key) + t.Assert(err, nil) + r, err = conn.Do("GET", key) + t.Assert(err, nil) + t.Assert(r, nil) + }) +} From 208bdffdf7c23ce0f2980682479ca3baa7df1d20 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Wed, 22 Jul 2020 14:02:21 +0800 Subject: [PATCH 2/5] update comment --- database/gredis/gredis.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gredis/gredis.go b/database/gredis/gredis.go index a2fbf462f..45de72de1 100644 --- a/database/gredis/gredis.go +++ b/database/gredis/gredis.go @@ -46,8 +46,8 @@ type Config struct { IdleTimeout time.Duration // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0) MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0) ConnectTimeout time.Duration // Dial connection timeout. - TLS bool //support tls - TLSSkipVerify bool //tls skip verify + TLS bool // Specifies the config to use when a TLS connection is dialed. + TLSSkipVerify bool // Disables server name verification when connecting over TLS } // Pool statistics. From 646280a6a998a79179ffcdf5086ee2859c2b3cb8 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Wed, 22 Jul 2020 15:08:32 +0800 Subject: [PATCH 3/5] remove tls unit test case --- database/gredis/gredis_z_unit_test.go | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/database/gredis/gredis_z_unit_test.go b/database/gredis/gredis_z_unit_test.go index 50bef9473..3270af08f 100644 --- a/database/gredis/gredis_z_unit_test.go +++ b/database/gredis/gredis_z_unit_test.go @@ -28,6 +28,7 @@ var ( Db: 1, } + //demo for tls config tlsConfig = gredis.Config{ Host: "127.0.0.1", Port: 6379, @@ -390,27 +391,3 @@ func Test_Auto_MarshalSlice(t *testing.T) { t.Assert(users2, users1) }) } - -func Test_Conn_TLS(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - redis := gredis.New(tlsConfig) - defer redis.Close() - conn := redis.Conn() - defer conn.Close() - - key := gconv.String(gtime.TimestampNano()) - value := []byte("v") - r, err := conn.Do("SET", key, value) - t.Assert(err, nil) - - r, err = conn.Do("GET", key) - t.Assert(err, nil) - t.Assert(r, value) - - _, err = conn.Do("DEL", key) - t.Assert(err, nil) - r, err = conn.Do("GET", key) - t.Assert(err, nil) - t.Assert(r, nil) - }) -} From 8bac0614f578cd4d3cedeb86e0f9566bcd685368 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Wed, 22 Jul 2020 15:13:40 +0800 Subject: [PATCH 4/5] format code --- database/gredis/gredis.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gredis/gredis.go b/database/gredis/gredis.go index 45de72de1..9b7d2d173 100644 --- a/database/gredis/gredis.go +++ b/database/gredis/gredis.go @@ -46,8 +46,8 @@ type Config struct { IdleTimeout time.Duration // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0) MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0) ConnectTimeout time.Duration // Dial connection timeout. - TLS bool // Specifies the config to use when a TLS connection is dialed. - TLSSkipVerify bool // Disables server name verification when connecting over TLS + TLS bool // Specifies the config to use when a TLS connection is dialed. + TLSSkipVerify bool // Disables server name verification when connecting over TLS } // Pool statistics. From 2798fa4444040e04135e6cb8e8aae4d95d48a783 Mon Sep 17 00:00:00 2001 From: xbkaishui Date: Wed, 22 Jul 2020 15:27:00 +0800 Subject: [PATCH 5/5] revert unit test --- database/gredis/gredis_z_unit_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/database/gredis/gredis_z_unit_test.go b/database/gredis/gredis_z_unit_test.go index 3270af08f..23e5804f0 100644 --- a/database/gredis/gredis_z_unit_test.go +++ b/database/gredis/gredis_z_unit_test.go @@ -27,15 +27,6 @@ var ( Port: 6379, Db: 1, } - - //demo for tls config - tlsConfig = gredis.Config{ - Host: "127.0.0.1", - Port: 6379, - Db: 1, - TLS: true, - TLSSkipVerify: true, - } ) func Test_NewClose(t *testing.T) {