Skip to content

Commit 56a3dbc

Browse files
jan-barjanbar
and
janbar
authored
feat: provide a username and password callback method, so that the plaintext username and password will not be stored in the memory, and the username and password will only be generated once when the CredentialsProvider is called. After the method is executed, the username and password strings on the stack will be released. (#2097)
Co-authored-by: janbar <[email protected]>
1 parent 2465baa commit 56a3dbc

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

options.go

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type Options struct {
5151
// or the User Password when connecting to a Redis 6.0 instance, or greater,
5252
// that is using the Redis ACL system.
5353
Password string
54+
// CredentialsProvider allows the username and password to be updated
55+
// before reconnecting. It should return the current username and password.
56+
CredentialsProvider func() (username string, password string)
5457

5558
// Database to be selected after connecting to the server.
5659
DB int

redis.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
217217
}
218218
cn.Inited = true
219219

220-
if c.opt.Password == "" &&
220+
username, password := c.opt.Username, c.opt.Password
221+
if c.opt.CredentialsProvider != nil {
222+
username, password = c.opt.CredentialsProvider()
223+
}
224+
225+
if password == "" &&
221226
c.opt.DB == 0 &&
222227
!c.opt.readOnly &&
223228
c.opt.OnConnect == nil {
@@ -228,11 +233,11 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
228233
conn := newConn(ctx, c.opt, connPool)
229234

230235
_, err := conn.Pipelined(ctx, func(pipe Pipeliner) error {
231-
if c.opt.Password != "" {
232-
if c.opt.Username != "" {
233-
pipe.AuthACL(ctx, c.opt.Username, c.opt.Password)
236+
if password != "" {
237+
if username != "" {
238+
pipe.AuthACL(ctx, username, password)
234239
} else {
235-
pipe.Auth(ctx, c.opt.Password)
240+
pipe.Auth(ctx, password)
236241
}
237242
}
238243

0 commit comments

Comments
 (0)