-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add passwordCommand option for SQL datastore credentials #9879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package sqlplugin | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql/driver" | ||
| ) | ||
|
|
||
| // RefreshingConnector is a driver.Connector that calls buildDSN on every | ||
| // Connect, so each new physical connection gets a fresh credential. This is | ||
| // used when passwordCommand is configured to fetch short-lived tokens. | ||
| type RefreshingConnector struct { | ||
| buildDSN func() (string, error) | ||
| newConnector func(dsn string) (driver.Connector, error) | ||
| driver driver.Driver | ||
| } | ||
|
|
||
| func NewRefreshingConnector( | ||
| buildDSN func() (string, error), | ||
| newConnector func(dsn string) (driver.Connector, error), | ||
| d driver.Driver, | ||
| ) *RefreshingConnector { | ||
| return &RefreshingConnector{ | ||
| buildDSN: buildDSN, | ||
| newConnector: newConnector, | ||
| driver: d, | ||
| } | ||
| } | ||
|
|
||
| func (c *RefreshingConnector) Connect(ctx context.Context) (driver.Conn, error) { | ||
| dsn, err := c.buildDSN() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| connector, err := c.newConnector(dsn) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return connector.Connect(ctx) | ||
| } | ||
|
|
||
| func (c *RefreshingConnector) Driver() driver.Driver { | ||
| return c.driver | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package sqlplugin | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql/driver" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type fakeConn struct{} | ||
|
|
||
| func (fakeConn) Prepare(string) (driver.Stmt, error) { return nil, nil } | ||
| func (fakeConn) Close() error { return nil } | ||
| func (fakeConn) Begin() (driver.Tx, error) { return nil, nil } | ||
|
|
||
| type fakeConnector struct{ dsn string } | ||
|
|
||
| func (c *fakeConnector) Connect(context.Context) (driver.Conn, error) { return fakeConn{}, nil } | ||
| func (c *fakeConnector) Driver() driver.Driver { return nil } | ||
|
|
||
| func TestRefreshingConnector_CallsBuildDSNOnEachConnect(t *testing.T) { | ||
| callCount := 0 | ||
| c := NewRefreshingConnector( | ||
| func() (string, error) { | ||
| callCount++ | ||
| return "dsn-" + string(rune('0'+callCount)), nil | ||
| }, | ||
| func(dsn string) (driver.Connector, error) { | ||
| return &fakeConnector{dsn: dsn}, nil | ||
| }, | ||
| nil, | ||
| ) | ||
|
|
||
| for range 3 { | ||
| _, err := c.Connect(context.Background()) | ||
| require.NoError(t, err) | ||
| } | ||
| require.Equal(t, 3, callCount) | ||
| } | ||
|
|
||
| func TestRefreshingConnector_PropagatesBuildDSNError(t *testing.T) { | ||
| expectedErr := errors.New("token expired") | ||
| c := NewRefreshingConnector( | ||
| func() (string, error) { | ||
| return "", expectedErr | ||
| }, | ||
| func(dsn string) (driver.Connector, error) { | ||
| t.Fatal("NewConnector should not be called when BuildDSN fails") | ||
| return nil, nil | ||
| }, | ||
| nil, | ||
| ) | ||
|
|
||
| _, err := c.Connect(context.Background()) | ||
| require.ErrorIs(t, err, expectedErr) | ||
| } | ||
|
|
||
| func TestRefreshingConnector_PropagatesNewConnectorError(t *testing.T) { | ||
| expectedErr := errors.New("bad dsn") | ||
| c := NewRefreshingConnector( | ||
| func() (string, error) { | ||
| return "some-dsn", nil | ||
| }, | ||
| func(dsn string) (driver.Connector, error) { | ||
| return nil, expectedErr | ||
| }, | ||
| nil, | ||
| ) | ||
|
|
||
| _, err := c.Connect(context.Background()) | ||
| require.ErrorIs(t, err, expectedErr) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.