Skip to content

Commit b18e732

Browse files
committed
support redis acl
Signed-off-by: my036811 <miner.yang@broadcom.com>
1 parent 0c62ec3 commit b18e732

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

configuration/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ type Configuration struct {
174174
// Password string to use when making a connection.
175175
Password string `yaml:"password,omitempty"`
176176

177+
// Username string to use when making a connection.
178+
Username string `yaml:"username,omitempty"`
179+
177180
// DB specifies the database to connect to on the redis instance.
178181
DB int `yaml:"db,omitempty"`
179182

registry/handlers/app.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/FZambia/sentinel"
21+
"github.com/distribution/distribution/v3/configuration"
2122
"github.com/distribution/reference"
2223
"github.com/docker/distribution"
2324
"github.com/docker/distribution/configuration"
@@ -518,13 +519,15 @@ func (app *App) configureRedis(configuration *configuration.Configuration) {
518519
redisOptions = append(redisOptions, redis.DialUseTLS(true))
519520
}
520521

522+
sentinelOptions := redisOptions
523+
521524
if configuration.Redis.SentinelMasterSet != "" {
522525
sntnl := &sentinel.Sentinel{
523526
Addrs: strings.Split(configuration.Redis.Addr, ","),
524527
MasterName: configuration.Redis.SentinelMasterSet,
525528
Dial: func(addr string) (redis.Conn, error) {
526529
c, err := redis.Dial("tcp", addr,
527-
redisOptions...)
530+
sentinelOptions...)
528531
if err != nil {
529532
return nil, err
530533
}
@@ -553,6 +556,16 @@ func (app *App) configureRedis(configuration *configuration.Configuration) {
553556
}
554557
}
555558

559+
// Parse auth configurations only for master if service deployed as sentinel mode
560+
username := configuration.Redis.Username
561+
password := configuration.Redis.Password
562+
if password != "" {
563+
if username != "" {
564+
redisOptions = append(redisOptions, redis.DialUsername(username))
565+
}
566+
redisOptions = append(redisOptions, redis.DialPassword(password))
567+
}
568+
556569
pool := &redis.Pool{
557570
Dial: func() (redis.Conn, error) {
558571
// TODO(stevvooe): Yet another use case for contextual timing.
@@ -580,16 +593,6 @@ func (app *App) configureRedis(configuration *configuration.Configuration) {
580593
done(err)
581594
return nil, err
582595
}
583-
584-
// authorize the connection
585-
if configuration.Redis.Password != "" {
586-
if _, err = conn.Do("AUTH", configuration.Redis.Password); err != nil {
587-
defer conn.Close()
588-
done(err)
589-
return nil, err
590-
}
591-
}
592-
593596
// select the database to use
594597
if configuration.Redis.DB != 0 {
595598
if _, err = conn.Do("SELECT", configuration.Redis.DB); err != nil {

0 commit comments

Comments
 (0)