Skip to content

Commit 3ec0f5f

Browse files
tangxinfamattklein123
authored andcommitted
fix: support auth without tls (#116)
Signed-off-by: tangxinfa <[email protected]>
1 parent 9517071 commit 3ec0f5f

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ before_script:
88
- redis-server --port 6380 &
99
- redis-server --port 6381 --requirepass password123 &
1010
- redis-server --port 6382 --requirepass password123 &
11+
- redis-server --port 6384 --requirepass password123 &
12+
- redis-server --port 6385 --requirepass password123 &
1113
script: make check_format tests

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ Ratelimit uses Redis as its caching layer. Ratelimit supports two operation mode
387387
1. One Redis server for all limits.
388388
1. Two Redis instances: one for per second limits and another one for all other limits.
389389

390-
As well Ratelimit supports TLS connections and authentication over TLS connections. These can be configured using the following environment variables:
390+
As well Ratelimit supports TLS connections and authentication. These can be configured using the following environment variables:
391391

392392
1. `REDIS_TLS` & `REDIS_PERSECOND_TLS`: set to `"true"` to enable a TLS connection for the specific connection type.
393-
1. `REDIS_AUTH` & `REDIS_PERSECOND_AUTH`: set to `"password"` to enable authentication to the redis host. This requires TLS to be enabled as well for the specific connection.
393+
1. `REDIS_AUTH` & `REDIS_PERSECOND_AUTH`: set to `"password"` to enable authentication to the redis host.
394394

395395
## One Redis Instance
396396

src/redis/driver_impl.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis
22

33
import (
44
"crypto/tls"
5+
"net"
56

67
stats "github.com/lyft/gostats"
78
"github.com/lyft/ratelimit/src/assert"
@@ -66,19 +67,16 @@ func (this *poolImpl) Put(c Connection) {
6667
}
6768
}
6869

69-
func NewPoolImpl(scope stats.Scope, socketType string, url string, poolSize int) Pool {
70-
logger.Warnf("connecting to redis on %s %s with pool size %d", socketType, url, poolSize)
71-
pool, err := pool.New(socketType, url, poolSize)
72-
checkError(err)
73-
return &poolImpl{
74-
pool: pool,
75-
stats: newPoolStats(scope)}
76-
}
77-
78-
func NewAuthTLSPoolImpl(scope stats.Scope, auth string, url string, poolSize int) Pool {
79-
logger.Warnf("connecting to redis on tls %s with pool size %d", url, poolSize)
70+
func NewPoolImpl(scope stats.Scope, useTls bool, auth string, url string, poolSize int) Pool {
71+
logger.Warnf("connecting to redis on %s with pool size %d", url, poolSize)
8072
df := func(network, addr string) (*redis.Client, error) {
81-
conn, err := tls.Dial("tcp", addr, &tls.Config{})
73+
var conn net.Conn
74+
var err error
75+
if useTls {
76+
conn, err = tls.Dial("tcp", addr, &tls.Config{})
77+
} else {
78+
conn, err = net.Dial("tcp", addr)
79+
}
8280
if err != nil {
8381
return nil, err
8482
}
@@ -88,7 +86,7 @@ func NewAuthTLSPoolImpl(scope stats.Scope, auth string, url string, poolSize int
8886
return nil, err
8987
}
9088
if auth != "" {
91-
logger.Warnf("enabling authentication to redis on tls %s", url)
89+
logger.Warnf("enabling authentication to redis on %s", url)
9290
if err = client.Cmd("AUTH", auth).Err; err != nil {
9391
client.Close()
9492
return nil, err

src/service_cmd/runner/runner.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,10 @@ func (runner *Runner) Run() {
4747

4848
var perSecondPool redis.Pool
4949
if s.RedisPerSecond {
50-
if s.RedisPerSecondAuth != "" || s.RedisPerSecondTls {
51-
perSecondPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
52-
} else {
53-
perSecondPool = redis.NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisSocketType, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
54-
}
55-
50+
perSecondPool = redis.NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondTls, s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
5651
}
5752
var otherPool redis.Pool
58-
if s.RedisAuth != "" || s.RedisTls {
59-
otherPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisAuth, s.RedisUrl, s.RedisPoolSize)
60-
} else {
61-
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisSocketType, s.RedisUrl, s.RedisPoolSize)
62-
}
53+
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize)
6354

6455
var localCache *freecache.Cache
6556
if s.LocalCacheSizeInBytes != 0 {

test/integration/integration_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,43 @@ func TestBasicTLSConfig(t *testing.T) {
5757
t.Run("WithPerSecondRedisTLSWithLocalCache", testBasicConfigAuthTLS("18089", "true", "1000"))
5858
}
5959

60+
func TestBasicAuthConfig(t *testing.T) {
61+
t.Run("WithoutPerSecondRedisAuth", testBasicConfigAuth("8091", "false", "0"))
62+
t.Run("WithPerSecondRedisAuth", testBasicConfigAuth("8093", "true", "0"))
63+
t.Run("WithoutPerSecondRedisAuthWithLocalCache", testBasicConfigAuth("18091", "false", "1000"))
64+
t.Run("WithPerSecondRedisAuthWithLocalCache", testBasicConfigAuth("18093", "true", "1000"))
65+
}
66+
6067
func testBasicConfigAuthTLS(grpcPort, perSecond string, local_cache_size string) func(*testing.T) {
6168
os.Setenv("REDIS_PERSECOND_URL", "localhost:16382")
6269
os.Setenv("REDIS_URL", "localhost:16381")
6370
os.Setenv("REDIS_AUTH", "password123")
71+
os.Setenv("REDIS_TLS", "true")
6472
os.Setenv("REDIS_PERSECOND_AUTH", "password123")
73+
os.Setenv("REDIS_PERSECOND_TLS", "true")
6574
return testBasicBaseConfig(grpcPort, perSecond, local_cache_size)
6675
}
6776

6877
func testBasicConfig(grpcPort, perSecond string, local_cache_size string) func(*testing.T) {
6978
os.Setenv("REDIS_PERSECOND_URL", "localhost:6380")
7079
os.Setenv("REDIS_URL", "localhost:6379")
80+
os.Setenv("REDIS_AUTH", "")
7181
os.Setenv("REDIS_TLS", "false")
82+
os.Setenv("REDIS_PERSECOND_AUTH", "")
7283
os.Setenv("REDIS_PERSECOND_TLS", "false")
7384
return testBasicBaseConfig(grpcPort, perSecond, local_cache_size)
7485
}
7586

87+
func testBasicConfigAuth(grpcPort, perSecond string, local_cache_size string) func(*testing.T) {
88+
os.Setenv("REDIS_PERSECOND_URL", "localhost:6385")
89+
os.Setenv("REDIS_URL", "localhost:6384")
90+
os.Setenv("REDIS_TLS", "false")
91+
os.Setenv("REDIS_AUTH", "password123")
92+
os.Setenv("REDIS_PERSECOND_TLS", "false")
93+
os.Setenv("REDIS_PERSECOND_AUTH", "password123")
94+
return testBasicBaseConfig(grpcPort, perSecond, local_cache_size)
95+
}
96+
7697
func getCacheKey(cacheKey string, enableLocalCache bool) string {
7798
if enableLocalCache {
7899
return cacheKey + "_local"
@@ -214,7 +235,9 @@ func testBasicConfigLegacy(local_cache_size string) func(*testing.T) {
214235
os.Setenv("REDIS_PERSECOND_URL", "localhost:6380")
215236
os.Setenv("REDIS_URL", "localhost:6379")
216237
os.Setenv("REDIS_TLS", "false")
238+
os.Setenv("REDIS_AUTH", "")
217239
os.Setenv("REDIS_PERSECOND_TLS", "false")
240+
os.Setenv("REDIS_PERSECOND_AUTH", "")
218241
os.Setenv("LOCAL_CACHE_SIZE", local_cache_size)
219242
local_cache_size_val, _ := strconv.Atoi(local_cache_size)
220243
enable_local_cache := local_cache_size_val > 0

0 commit comments

Comments
 (0)