From bbbcbd2b23989af3936d293ea1f889cadb25c4e0 Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Sat, 17 Jun 2023 20:43:45 -0400 Subject: [PATCH] Add example for using Redis with TLS --- redis/README.md | 12 +++++++++++- redis/redis_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/redis/README.md b/redis/README.md index 5de46b83..6d22bae9 100644 --- a/redis/README.md +++ b/redis/README.md @@ -35,7 +35,7 @@ Import the storage package. import "github.com/gofiber/storage/redis/v2" ``` -You can use the following possibilities to create a storage: +You can use the one of the following options to create a Redis Storage: ```go // Initialize default config store := redis.New() @@ -63,6 +63,16 @@ store := redis.New(redis.Config{ Addrs: []string{":6379", ":6380"}, }) +// Create a client with support for TLS +tlsCfg := &tls.Config{ + MinVersion: tls.VersionTLS12, +} +store = redis.New(redis.Config{ + URL: "redis://:@127.0.0.1:6379/", + TLSConfig: tlsCfg, + Reset: false, +}) + // or just the url with all information store = redis.New(redis.Config{ URL: "redis://:@127.0.0.1:6379/", diff --git a/redis/redis_test.go b/redis/redis_test.go index 883de496..ee6f3b56 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -149,6 +149,35 @@ func Test_Redis_Initalize_WithURL(t *testing.T) { utils.AssertEqual(t, nil, testStoreUrl.Close()) } +func Test_Redis_Initalize_WithURL_TLS_Without_x509Pair(t *testing.T) { + tlsCfg := &tls.Config{ + MinVersion: tls.VersionTLS12, + InsecureSkipVerify: true, + } + + testStoreUrl := New(Config{ + URL: "redis://localhost:6380", + TLSConfig: tlsCfg, + }) + + var ( + key = "clark" + val = []byte("kent") + ) + + err = testStoreUrl.Set(key, val, 0) + utils.AssertEqual(t, nil, err) + + result, err := testStoreUrl.Get(key) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, val, result) + + err = testStoreUrl.Delete(key) + utils.AssertEqual(t, nil, err) + + utils.AssertEqual(t, nil, testStoreUrl.Close()) +} + func Test_Redis_Initalize_WithURL_TLS(t *testing.T) { cer, err := tls.LoadX509KeyPair("./tests/tls/client.crt", "./tests/tls/client.key") if err != nil {