Skip to content

Options.CredentialsProvider should support Context and returning an error #2681

Closed
@maerlyn5

Description

Expected Behavior

The interface for Options.CredentialsProvider should be func(ctx context.Context) (username string, password string, err error)

baseClient.initConn should return any error from Options.CredentialsProvider

Current Behavior

Options.CredentialsProvider doesn't support context and doesn't pass errors down the call chain.

Context (Environment)

I want to use Options.CredentialsProvider to implement dynamic password fetching from cloud providers. e.g. #2343

Detailed Description

Looking at the original commit #2097, the intent of CredentialsProvider seems to be to memory scrub plaintext passwords, but now people want to use it to dynamically fetch passwords from cloud providers. As the library generally supports Context, i'll skip the explanation for why its idiomatic and appropriate to pass context into code that is intended to make network requests. This would be a breaking change and require a version increment.

Possible Implementation

CredentialsProvider func(ctx context.Context) (username string, password string, err error)

func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
	if cn.Inited {
		return nil
	}
	cn.Inited = true
	username, password := c.opt.Username, c.opt.Password
	if c.opt.CredentialsProvider != nil {
		var credentialsProviderErr error
		username, password, credentialsProviderErr = c.opt.CredentialsProvider(ctx)
		if credentialsProviderErr != nil {
			return credentialsProviderErr
		}
	}
..
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions