Skip to content

fix: use authenticated urls for pubsub #14261

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 25 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
comments
  • Loading branch information
f0ssel committed Aug 14, 2024
commit 88d50b85e937bbfa777b5660cddd00b6dd87d0b8
3 changes: 2 additions & 1 deletion coderd/database/awsiamrds/awsiamrds.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (d *awsIamRdsDriver) Open(name string) (driver.Conn, error) {
return conn, nil
}

// Connector returns a driver.Connector that fetches a new authentication token for each connection.
func (d *awsIamRdsDriver) Connector(name string) (driver.Connector, error) {
connector := &connector{
url: name,
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
return nc.Connect(ctx)
}

func (c *connector) Driver() driver.Driver {
func (*connector) Driver() driver.Driver {
return &pq.Driver{}
}

Expand Down
2 changes: 2 additions & 0 deletions coderd/database/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/lib/pq"
)

// ConnectorCreator can create a driver.Connector.
type ConnectorCreator interface {
Connector(name string) (driver.Connector, error)
}

// DialerConnector can create a driver.Connector and set a pq.Dialer.
type DialerConnector interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Connect(context.Context) (driver.Conn, error)
Dialer(dialer pq.Dialer)
Expand Down
11 changes: 6 additions & 5 deletions coderd/database/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,16 @@ func (p *PGPubsub) startListener(ctx context.Context, connectURL string) error {

// Create a custom connector if the database driver supports it.
connectorCreator, ok := p.db.Driver().(database.ConnectorCreator)
if !ok {
connector, err = pq.NewConnector(connectURL)
if ok {
connector, err = connectorCreator.Connector(connectURL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't get hit in the package unit tests. A good way to test it would be to create a pq Driver wrapper that we can control.

I'd like to see a test where we start pubsub with the wrapped driver, do some pub'ing and sub'ing, then kill the connection and verify that the pubsub / pq.Listener reconnects automatically. That would give a nice test of the pq changes you made as well.

if err != nil {
return xerrors.Errorf("create pq connector: %w", err)
return xerrors.Errorf("create custom connector: %w", err)
}
} else {
connector, err = connectorCreator.Connector(connectURL)
// use the default pq connector otherwise
connector, err = pq.NewConnector(connectURL)
if err != nil {
return xerrors.Errorf("create custom connector: %w", err)
return xerrors.Errorf("create pq connector: %w", err)
}
}

Expand Down