Skip to content

Commit aa4ce90

Browse files
committed
Fix tls/password usage when selecting a db, add new acceptance test
1 parent 3944fa4 commit aa4ce90

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

acceptance-tests/acceptance.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
[ "$status" -eq 0 ]
1717
}
1818

19+
@test "Pass when using a non-default db, and a password" {
20+
run tests/select-db-with-password.sh
21+
[ "$status" -eq 0 ]
22+
}
23+
1924
# https://github.com/yannh/redis-dump-go/issues/11
2025
# https://github.com/yannh/redis-dump-go/issues/18
2126
@test "Pass when importing a ZSET with 1M entries" {

acceptance-tests/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ echo "-> Waiting for Redis to start..."
77
timeout 30 sh -c 'until redis-cli -h redis -p 6379 PING >/dev/null; do sleep 1; done'
88

99
echo "-> Running acceptance tests..."
10-
bats --tap acceptance.bats
10+
bats --tap acceptance.bats --verbose-run

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ services:
55
image: "redis:alpine"
66
ports:
77
- "6379:6379"
8+
redis_secure:
9+
image: "redis:alpine"
10+
volumes:
11+
- ./acceptance-tests/redis-confs:/usr/local/etc/redis
12+
ports:
13+
- target: 6380
14+
published: 6380
15+
protocol: tcp
16+
mode: host
17+
entrypoint: ["/usr/local/bin/redis-server", "/usr/local/etc/redis/with_password.conf"]
818
tests:
919
image: "alpine:latest"
1020
volumes:
@@ -13,5 +23,6 @@ services:
1323
- ./bin/redis-dump-go:/redis-dump-go
1424
depends_on:
1525
- "redis"
26+
- "redis_secure"
1627
working_dir: /acceptance-tests
1728
entrypoint: ["/acceptance-tests/entrypoint.sh"]

pkg/redisdump/redisdump.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,17 @@ func parseKeyspaceInfo(keyspaceInfo string) ([]uint8, error) {
263263
return dbs, nil
264264
}
265265

266-
func getDBIndexes(redisURL string) ([]uint8, error) {
267-
client, err := radix.NewPool("tcp", redisURL, 1)
266+
func getDBIndexes(redisURL string, redisPassword string, tlsHandler *TlsHandler) ([]uint8, error) {
267+
customConnFunc := func(network, addr string) (radix.Conn, error) {
268+
dialOpts, err := redisDialOpts(redisPassword, tlsHandler, nil)
269+
if err != nil {
270+
return nil, err
271+
}
272+
273+
return radix.Dial(network, addr, dialOpts...)
274+
}
275+
276+
client, err := radix.NewPool("tcp", redisURL, 1, radix.PoolConnFunc(customConnFunc))
268277
if err != nil {
269278
return nil, err
270279
}
@@ -331,7 +340,7 @@ func RedisURL(redisHost string, redisPort string) string {
331340
return fmt.Sprintf("redis://%s:%s", redisHost, redisPort)
332341
}
333342

334-
func redisDialOpts(redisPassword string, tlsHandler *TlsHandler, db uint8) ([]radix.DialOpt, error) {
343+
func redisDialOpts(redisPassword string, tlsHandler *TlsHandler, db *uint8) ([]radix.DialOpt, error) {
335344
dialOpts := []radix.DialOpt{
336345
radix.DialTimeout(5 * time.Minute),
337346
}
@@ -346,7 +355,9 @@ func redisDialOpts(redisPassword string, tlsHandler *TlsHandler, db uint8) ([]ra
346355
dialOpts = append(dialOpts, radix.DialUseTLS(tlsCfg))
347356
}
348357

349-
dialOpts = append(dialOpts, radix.DialSelectDB(int(db)))
358+
if db != nil {
359+
dialOpts = append(dialOpts, radix.DialSelectDB(int(*db)))
360+
}
350361

351362
return dialOpts, nil
352363
}
@@ -371,7 +382,7 @@ func DumpDB(redisHost string, redisPort int, redisPassword string, tlsHandler *T
371382

372383
redisURL := RedisURL(redisHost, fmt.Sprint(redisPort))
373384
customConnFunc := func(network, addr string) (radix.Conn, error) {
374-
dialOpts, err := redisDialOpts(redisPassword, tlsHandler, *db)
385+
dialOpts, err := redisDialOpts(redisPassword, tlsHandler, db)
375386
if err != nil {
376387
return nil, err
377388
}
@@ -411,7 +422,7 @@ func DumpDB(redisHost string, redisPort int, redisPassword string, tlsHandler *T
411422
// are regularly sent to the channel progressNotifications
412423
func DumpServer(redisHost string, redisPort int, redisPassword string, tlsHandler *TlsHandler, filter string, nWorkers int, withTTL bool, batchSize int, noscan bool, logger *log.Logger, serializer func([]string) string, progress chan<- ProgressNotification) error {
413424
url := RedisURL(redisHost, fmt.Sprint(redisPort))
414-
dbs, err := getDBIndexes(url)
425+
dbs, err := getDBIndexes(url, redisPassword, tlsHandler)
415426
if err != nil {
416427
return err
417428
}

pkg/redisdump/redisdump_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func TestRedisDialOpts(t *testing.T) {
261261
nil,
262262
},
263263
} {
264-
dOpts, err := redisDialOpts(testCase.redisPassword, testCase.tlsHandler, testCase.db)
264+
dOpts, err := redisDialOpts(testCase.redisPassword, testCase.tlsHandler, &testCase.db)
265265
if err != testCase.err {
266266
t.Errorf("expected error to be %+v, got %+v", testCase.err, err)
267267
}

0 commit comments

Comments
 (0)