Skip to content

Commit

Permalink
Add support for individual passwords for ring shards
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Benford committed Jul 17, 2019
1 parent 850dea5 commit 9169633
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type RingOptions struct {
// Map of name => host:port addresses of ring shards.
Addrs map[string]string

// Map of name => password of ring shards, to allow different shards to have
// different passwords. It will be ignored if the Password field is set.
Passwords map[string]string

// Frequency of PING commands sent to check shards availability.
// Shard is considered down after 3 subsequent failed checks.
HeartbeatFrequency time.Duration
Expand Down Expand Up @@ -98,12 +102,12 @@ func (opt *RingOptions) init() {
}
}

func (opt *RingOptions) clientOptions() *Options {
func (opt *RingOptions) clientOptions(shard string) *Options {
return &Options{
OnConnect: opt.OnConnect,

DB: opt.DB,
Password: opt.Password,
Password: opt.getPassword(shard),

DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout,
Expand All @@ -118,6 +122,13 @@ func (opt *RingOptions) clientOptions() *Options {
}
}

func (opt *RingOptions) getPassword(shard string) string {
if opt.Password == "" {
return opt.Passwords[shard]
}
return opt.Password
}

//------------------------------------------------------------------------------

type ringShard struct {
Expand Down Expand Up @@ -365,7 +376,7 @@ func NewRing(opt *RingOptions) *Ring {
ring.cmdsInfoCache = newCmdsInfoCache(ring.cmdsInfo)

for name, addr := range opt.Addrs {
clopt := opt.clientOptions()
clopt := opt.clientOptions(name)
clopt.Addr = addr
ring.shards.Add(name, NewClient(clopt))
}
Expand Down
23 changes: 23 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ var _ = Describe("Redis Ring", func() {
Expect(ringShard2.Info().Val()).To(ContainSubstring("keys=100"))
})
})

Describe("shard passwords", func() {
It("can be initialized with a single password, used for all shards", func() {
opts := redisRingOptions()
opts.Password = "password"
ring = redis.NewRing(opts)

err := ring.Ping().Err()
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
})

It("can be initialized with a passwords map, one for each shard", func() {
opts := redisRingOptions()
opts.Passwords = map[string]string{
"ringShardOne": "password1",
"ringShardTwo": "password2",
}
ring = redis.NewRing(opts)

err := ring.Ping().Err()
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
})
})
})

var _ = Describe("empty Redis Ring", func() {
Expand Down

0 comments on commit 9169633

Please sign in to comment.