Skip to content

Commit

Permalink
Merge pull request #982 from gofiber/issue-981
Browse files Browse the repository at this point in the history
Support for using options.TLSConfig if returned by redis.ParseURL()
  • Loading branch information
ReneWerner87 authored Sep 13, 2023
2 parents a21d4da + 1af49ff commit 54245b2
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 311 deletions.
6 changes: 5 additions & 1 deletion .github/scripts/gen-test-certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# ./tls/client.{crt,key} A certificate restricted for SSL client usage.
# ./tls/server.{crt,key} A certificate restricted for SSL server usage.

set -e

generate_cert() {
local name=$1
local cn="$2"
Expand Down Expand Up @@ -44,14 +46,16 @@ cat > ./tls/openssl.cnf <<_END_
[ server_cert ]
keyUsage = digitalSignature, keyEncipherment
nsCertType = server
subjectAltName = DNS:localhost"
[ client_cert ]
keyUsage = digitalSignature, keyEncipherment
nsCertType = client
_END_

generate_cert server "Server-only" "-extfile ./tls/openssl.cnf -extensions server_cert"
generate_cert client "Client-only" "-extfile ./tls/openssl.cnf -extensions client_cert"
generate_cert redis "Generic-cert"
generate_cert redis "localhost" "-extfile ./tls/openssl.cnf -extensions server_cert"

# List generated certs
ls -la ./tls
Expand Down
25 changes: 20 additions & 5 deletions .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,35 @@ jobs:
- name: Fetch Repository
uses: actions/checkout@v4

- name: Generate TLS certs
run: ./.github/scripts/gen-test-certs.sh

- name: Add Custom CA cert
run: sudo cp /home/runner/work/storage/storage/tls/ca.crt /usr/local/share/ca-certificates/custom.crt

- name: Trust Custom CA Cert
run: sudo update-ca-certificates

- name: Setup Redis
uses: shogo82148/actions-setup-redis@v1
with:
redis-version: ${{ matrix.redis }}
auto-start: 'false'
redis-port: '6379'
redis-tls-port: '6380'

- name: Run Redis
run: |
redis-server --tls-port 6380 --port 6379 \
--tls-cert-file ./redis/tests/tls/redis.crt \
--tls-key-file ./redis/tests/tls/redis.key \
--tls-ca-cert-file ./redis/tests/tls/ca.crt&
--tls-cert-file /home/runner/work/storage/storage/tls/redis.crt \
--tls-key-file /home/runner/work/storage/storage/tls/redis.key \
--tls-ca-cert-file /home/runner/work/storage/storage/tls/ca.crt &
- name: Run Redis instance with MTLS disabled
run: |
redis-server --tls-port 16380 --port 16379 \
--tls-cert-file /home/runner/work/storage/storage/tls/redis.crt \
--tls-key-file /home/runner/work/storage/storage/tls/redis.key \
--tls-ca-cert-file /home/runner/work/storage/storage/tls/ca.crt \
--tls-auth-clients no &
- name: Setup Redis Cluster
uses: vishnudxb/[email protected]
Expand Down
5 changes: 5 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func New(config ...Config) *Storage {
cfg.Password = options.Password
cfg.Database = options.DB
cfg.Addrs = []string{options.Addr}

// If cfg.TLSConfig is not provided, and options returns one, use it.
if cfg.TLSConfig == nil && options.TLSConfig != nil {
cfg.TLSConfig = options.TLSConfig
}
} else if len(cfg.Addrs) == 0 {
// Fallback to Host and Port values if Addrs is empty
cfg.Addrs = []string{fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)}
Expand Down
68 changes: 67 additions & 1 deletion redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func Test_Redis_Initalize_WithURL(t *testing.T) {
}

func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
cer, err := tls.LoadX509KeyPair("./tests/tls/client.crt", "./tests/tls/client.key")
cer, err := tls.LoadX509KeyPair("/home/runner/work/storage/storage/tls/client.crt", "/home/runner/work/storage/storage/tls/client.key")
if err != nil {
log.Println(err)
return
Expand Down Expand Up @@ -188,6 +188,72 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
require.Nil(t, testStoreUrl.Close())
}

func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) {
cer, err := tls.LoadX509KeyPair("/home/runner/work/storage/storage/tls/client.crt", "/home/runner/work/storage/storage/tls/client.key")
if err != nil {
log.Println(err)
return
}
tlsCfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
InsecureSkipVerify: false,
Certificates: []tls.Certificate{cer},
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}

testStoreUrl := New(Config{
URL: "redis://localhost:6380",
TLSConfig: tlsCfg,
})

var (
key = "clark"
val = []byte("kent")
)

err = testStoreUrl.Set(key, val, 0)
require.NoError(t, err)

result, err := testStoreUrl.Get(key)
require.NoError(t, err)
require.Equal(t, val, result)

err = testStoreUrl.Delete(key)
require.NoError(t, err)

require.Nil(t, testStoreUrl.Close())
}

func Test_Redis_Initalize_With_Secure_URL(t *testing.T) {
testStoreUrl := New(Config{
URL: "rediss://localhost:16380",
})

var (
key = "clark"
val = []byte("kent")
)

err := testStoreUrl.Set(key, val, 0)
require.NoError(t, err)

result, err := testStoreUrl.Get(key)
require.NoError(t, err)
require.Equal(t, val, result)

err = testStoreUrl.Delete(key)
require.NoError(t, err)

require.Nil(t, testStoreUrl.Close())
}

func Test_Redis_Universal_Addrs(t *testing.T) {
// This should failover and create a Single Node connection.
testStoreUniversal := New(Config{
Expand Down
57 changes: 0 additions & 57 deletions redis/tests/gen-test-certs.sh

This file was deleted.

29 changes: 0 additions & 29 deletions redis/tests/tls/ca.crt

This file was deleted.

51 changes: 0 additions & 51 deletions redis/tests/tls/ca.key

This file was deleted.

1 change: 0 additions & 1 deletion redis/tests/tls/ca.txt

This file was deleted.

24 changes: 0 additions & 24 deletions redis/tests/tls/client.crt

This file was deleted.

27 changes: 0 additions & 27 deletions redis/tests/tls/client.key

This file was deleted.

6 changes: 0 additions & 6 deletions redis/tests/tls/openssl.cnf

This file was deleted.

23 changes: 0 additions & 23 deletions redis/tests/tls/redis.crt

This file was deleted.

8 changes: 0 additions & 8 deletions redis/tests/tls/redis.dh

This file was deleted.

Loading

0 comments on commit 54245b2

Please sign in to comment.