Skip to content

Commit 0383d08

Browse files
dcherubiniofekshenawandyakov
authored
feat(client): Add CredentialsProvider field to UniversalOptions (#2927)
* Add CredentialsProvider field to universal client * fix(options): Add credentials providers to universal options and pass to client options * chore(ring): Add missing fields in building clientOptions --------- Co-authored-by: ofekshenawa <[email protected]> Co-authored-by: Nedyalko Dyakov <[email protected]> Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent fa475cb commit 0383d08

File tree

3 files changed

+102
-27
lines changed

3 files changed

+102
-27
lines changed

ring.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/cespare/xxhash/v2"
1515
"github.com/dgryski/go-rendezvous" //nolint
16+
"github.com/redis/go-redis/v9/auth"
1617

1718
"github.com/redis/go-redis/v9/internal"
1819
"github.com/redis/go-redis/v9/internal/hashtag"
@@ -73,7 +74,24 @@ type RingOptions struct {
7374
Protocol int
7475
Username string
7576
Password string
76-
DB int
77+
// CredentialsProvider allows the username and password to be updated
78+
// before reconnecting. It should return the current username and password.
79+
CredentialsProvider func() (username string, password string)
80+
81+
// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
82+
// done to maintain API compatibility. In the future,
83+
// there might be a merge between CredentialsProviderContext and CredentialsProvider.
84+
// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
85+
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
86+
87+
// StreamingCredentialsProvider is used to retrieve the credentials
88+
// for the connection from an external source. Those credentials may change
89+
// during the connection lifetime. This is useful for managed identity
90+
// scenarios where the credentials are retrieved from an external source.
91+
//
92+
// Currently, this is a placeholder for the future implementation.
93+
StreamingCredentialsProvider auth.StreamingCredentialsProvider
94+
DB int
7795

7896
MaxRetries int
7997
MinRetryBackoff time.Duration
@@ -154,10 +172,13 @@ func (opt *RingOptions) clientOptions() *Options {
154172
Dialer: opt.Dialer,
155173
OnConnect: opt.OnConnect,
156174

157-
Protocol: opt.Protocol,
158-
Username: opt.Username,
159-
Password: opt.Password,
160-
DB: opt.DB,
175+
Protocol: opt.Protocol,
176+
Username: opt.Username,
177+
Password: opt.Password,
178+
CredentialsProvider: opt.CredentialsProvider,
179+
CredentialsProviderContext: opt.CredentialsProviderContext,
180+
StreamingCredentialsProvider: opt.StreamingCredentialsProvider,
181+
DB: opt.DB,
161182

162183
MaxRetries: -1,
163184

sentinel.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313
"time"
1414

15+
"github.com/redis/go-redis/v9/auth"
1516
"github.com/redis/go-redis/v9/internal"
1617
"github.com/redis/go-redis/v9/internal/pool"
1718
"github.com/redis/go-redis/v9/internal/rand"
@@ -60,7 +61,24 @@ type FailoverOptions struct {
6061
Protocol int
6162
Username string
6263
Password string
63-
DB int
64+
// CredentialsProvider allows the username and password to be updated
65+
// before reconnecting. It should return the current username and password.
66+
CredentialsProvider func() (username string, password string)
67+
68+
// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
69+
// done to maintain API compatibility. In the future,
70+
// there might be a merge between CredentialsProviderContext and CredentialsProvider.
71+
// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
72+
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
73+
74+
// StreamingCredentialsProvider is used to retrieve the credentials
75+
// for the connection from an external source. Those credentials may change
76+
// during the connection lifetime. This is useful for managed identity
77+
// scenarios where the credentials are retrieved from an external source.
78+
//
79+
// Currently, this is a placeholder for the future implementation.
80+
StreamingCredentialsProvider auth.StreamingCredentialsProvider
81+
DB int
6482

6583
MaxRetries int
6684
MinRetryBackoff time.Duration
@@ -107,10 +125,13 @@ func (opt *FailoverOptions) clientOptions() *Options {
107125
Dialer: opt.Dialer,
108126
OnConnect: opt.OnConnect,
109127

110-
DB: opt.DB,
111-
Protocol: opt.Protocol,
112-
Username: opt.Username,
113-
Password: opt.Password,
128+
DB: opt.DB,
129+
Protocol: opt.Protocol,
130+
Username: opt.Username,
131+
Password: opt.Password,
132+
CredentialsProvider: opt.CredentialsProvider,
133+
CredentialsProviderContext: opt.CredentialsProviderContext,
134+
StreamingCredentialsProvider: opt.StreamingCredentialsProvider,
114135

115136
MaxRetries: opt.MaxRetries,
116137
MinRetryBackoff: opt.MinRetryBackoff,
@@ -187,9 +208,12 @@ func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
187208
Dialer: opt.Dialer,
188209
OnConnect: opt.OnConnect,
189210

190-
Protocol: opt.Protocol,
191-
Username: opt.Username,
192-
Password: opt.Password,
211+
Protocol: opt.Protocol,
212+
Username: opt.Username,
213+
Password: opt.Password,
214+
CredentialsProvider: opt.CredentialsProvider,
215+
CredentialsProviderContext: opt.CredentialsProviderContext,
216+
StreamingCredentialsProvider: opt.StreamingCredentialsProvider,
193217

194218
MaxRedirects: opt.MaxRetries,
195219

universal.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"crypto/tls"
66
"net"
77
"time"
8+
9+
"github.com/redis/go-redis/v9/auth"
810
)
911

1012
// UniversalOptions information is required by UniversalClient to establish
@@ -26,9 +28,27 @@ type UniversalOptions struct {
2628
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
2729
OnConnect func(ctx context.Context, cn *Conn) error
2830

29-
Protocol int
30-
Username string
31-
Password string
31+
Protocol int
32+
Username string
33+
Password string
34+
// CredentialsProvider allows the username and password to be updated
35+
// before reconnecting. It should return the current username and password.
36+
CredentialsProvider func() (username string, password string)
37+
38+
// CredentialsProviderContext is an enhanced parameter of CredentialsProvider,
39+
// done to maintain API compatibility. In the future,
40+
// there might be a merge between CredentialsProviderContext and CredentialsProvider.
41+
// There will be a conflict between them; if CredentialsProviderContext exists, we will ignore CredentialsProvider.
42+
CredentialsProviderContext func(ctx context.Context) (username string, password string, err error)
43+
44+
// StreamingCredentialsProvider is used to retrieve the credentials
45+
// for the connection from an external source. Those credentials may change
46+
// during the connection lifetime. This is useful for managed identity
47+
// scenarios where the credentials are retrieved from an external source.
48+
//
49+
// Currently, this is a placeholder for the future implementation.
50+
StreamingCredentialsProvider auth.StreamingCredentialsProvider
51+
3252
SentinelUsername string
3353
SentinelPassword string
3454

@@ -96,9 +116,12 @@ func (o *UniversalOptions) Cluster() *ClusterOptions {
96116
Dialer: o.Dialer,
97117
OnConnect: o.OnConnect,
98118

99-
Protocol: o.Protocol,
100-
Username: o.Username,
101-
Password: o.Password,
119+
Protocol: o.Protocol,
120+
Username: o.Username,
121+
Password: o.Password,
122+
CredentialsProvider: o.CredentialsProvider,
123+
CredentialsProviderContext: o.CredentialsProviderContext,
124+
StreamingCredentialsProvider: o.StreamingCredentialsProvider,
102125

103126
MaxRedirects: o.MaxRedirects,
104127
ReadOnly: o.ReadOnly,
@@ -147,10 +170,14 @@ func (o *UniversalOptions) Failover() *FailoverOptions {
147170
Dialer: o.Dialer,
148171
OnConnect: o.OnConnect,
149172

150-
DB: o.DB,
151-
Protocol: o.Protocol,
152-
Username: o.Username,
153-
Password: o.Password,
173+
DB: o.DB,
174+
Protocol: o.Protocol,
175+
Username: o.Username,
176+
Password: o.Password,
177+
CredentialsProvider: o.CredentialsProvider,
178+
CredentialsProviderContext: o.CredentialsProviderContext,
179+
StreamingCredentialsProvider: o.StreamingCredentialsProvider,
180+
154181
SentinelUsername: o.SentinelUsername,
155182
SentinelPassword: o.SentinelPassword,
156183

@@ -199,10 +226,13 @@ func (o *UniversalOptions) Simple() *Options {
199226
Dialer: o.Dialer,
200227
OnConnect: o.OnConnect,
201228

202-
DB: o.DB,
203-
Protocol: o.Protocol,
204-
Username: o.Username,
205-
Password: o.Password,
229+
DB: o.DB,
230+
Protocol: o.Protocol,
231+
Username: o.Username,
232+
Password: o.Password,
233+
CredentialsProvider: o.CredentialsProvider,
234+
CredentialsProviderContext: o.CredentialsProviderContext,
235+
StreamingCredentialsProvider: o.StreamingCredentialsProvider,
206236

207237
MaxRetries: o.MaxRetries,
208238
MinRetryBackoff: o.MinRetryBackoff,

0 commit comments

Comments
 (0)