pgxpool.ConnectConfig is not able to connect over ssh tunnel
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
**Describe the bug**
We are not able to connect to our postgres database over ssh tunnel using pgxpool.ConnectConfig . The code is at https://go.dev/play/p/YhQVtd6OKy1 . The same configuration works with pgx.NewConnPool(connPoolConfig).
**To Reproduce**
Steps to reproduce the behavior:
It is reproducible with a tunnel on ec2 connecting to rds. It is not reproducible on localhost.
If possible, please provide runnable example such as:
```go
package main
import (
"context"
"fmt"
"net"
"os"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"golang.org/x/crypto/ssh"
)
func getAuthMethods() []ssh.AuthMethod {
callback := func() ([]ssh.Signer, error) {
key, err := os.ReadFile("/tmp/ssh-key")
if err != nil {
panic(fmt.Sprintf("unable to read private key: %v", err))
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return []ssh.Signer{signer}, nil
}
return []ssh.AuthMethod{ssh.PublicKeysCallback(callback)}
}
type noDeadlineConn struct{ net.Conn }
func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }
func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }
func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }
func main() {
sshHost := "127.0.0.1" // SSH Server Hostname/IP
sshPort := 22 // SSH Port
sshUser := "user" // SSH Username
sshPass := "" // Empty string for no password
sshConfig := &ssh.ClientConfig{
User: sshUser,
Auth: getAuthMethods(),
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //this line
}
if sshPass != "" {
sshConfig.Auth = append(sshConfig.Auth, ssh.PasswordCallback(func() (string, error) {
return sshPass, nil
}))
}
if sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", sshHost, sshPort), sshConfig); err == nil {
defer sshcon.Close()
config, err := pgxpool.ParseConfig("user=test password=test host=test.us-east-1.rds.amazonaws.com port=5432 dbname=test sslmode=disable pool_max_conns=10")
if err != nil {
fmt.Println(err)
return
}
config.ConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := sshcon.Dial(network, addr)
return &noDeadlineConn{Conn: conn}, err
//return conn, err
}
pool, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
fmt.Println("error")
fmt.Println(err)
return
}
fmt.Printf("Successfully connected to the db\n")
if rows, err := pool.Query(context.Background(), "SELECT id, name FROM holidays ORDER BY id asc"); err == nil {
for rows.Next() {
var id int64
var name string
rows.Scan(&id, &name)
fmt.Printf("ID: %d Name: %s\n", id, name)
}
rows.Close()
} else {
fmt.Println(err)
}
pool.Close()
} else {
fmt.Println(err)
}
}
```
**Expected behavior**
The connection is established.
**Actual behavior**
go run .
error
failed to connect to `host=test.us-east-1.rds.amazonaws.com user=test database=test`: dial error (ssh: rejected: connect failed (Connection timed out))
**Version**
- Go: `$ go version` -> go version go1.20.5 linux/amd64
- PostgreSQL: We are not able to connect
- pgx: `$ grep 'github.com/jackc/pgx/v[0-9]' go.mod` -> v4.17.2 and v4.18.1
**Additional context**
Add any other context about the problem here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied runnable example and compare pgxpool.ConnectConfig with pgx.NewConnPool, focusing on pgxpool.ParseConfig, ConnConfig.DialFunc, and the ssh connection path. Run the example against the EC2-to-RDS tunnel and inspect why the pool reports an SSH connection timeout. Done means the same configured tunnel establishes a pgxpool connection successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100