From fd2dcf7e5ffbfea9d57f6a2e2f9cc3bd4415504d Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Mon, 26 Jun 2023 14:53:50 -0500 Subject: [PATCH 1/4] Allow for local dns resolution with a custom dialer --- protocol.go | 8 ++++++++ tds_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/protocol.go b/protocol.go index 755075b7..615727ea 100644 --- a/protocol.go +++ b/protocol.go @@ -51,6 +51,14 @@ func (t tcpDialer) DialSqlConnection(ctx context.Context, c *Connector, p *msdsn var ips []net.IP ip := net.ParseIP(p.Host) if ip == nil { + // if the dialer has been updated, the dialer may be proxying to a different network, and so the + // dialer should be used to connect so the DNS is resolved within the right network + if c != nil && c.Dialer != nil { + d := c.getDialer(p) + addr := net.JoinHostPort(p.Host, strconv.Itoa(int(resolveServerPort(p.Port)))) + return d.DialContext(ctx, "tcp", addr) + } + ips, err = net.LookupIP(p.Host) if err != nil { return diff --git a/tds_test.go b/tds_test.go index 94d1be65..38ede43d 100644 --- a/tds_test.go +++ b/tds_test.go @@ -971,3 +971,44 @@ func versionToHexString(v uint32) string { binary.LittleEndian.PutUint32(b, v) return hex.EncodeToString(b) } + +func TestDialSqlConnectionCustomDialer(t *testing.T) { + tl := testLogger{t: t} + defer tl.StopLogging() + SetLogger(&tl) + + params := msdsn.Config{ + Host: "nonexistant-dns.svc.cluster.local", + } + connector, err := NewConnector(params.URL().String()) + if err != nil { + t.Error(err) + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + // if a dialer is specified, the dialer should be used to make the connection + mock := NewMockTransportDialer( + []string{}, + []string{}, + ) + connector.Dialer = mock + if mock.count != 0 { + t.Error("expecting no connections") + } + sqlDialer, _ := msdsn.ProtocolDialers["tcp"].(MssqlProtocolDialer) + conn, err := sqlDialer.DialSqlConnection(ctx, connector, ¶ms) + if err != nil { + t.Error(err) + } + + if mock.count != 1 { + t.Error("expecting 1 connection") + } + + err = conn.Close() + if err != nil { + t.Error(err) + } +} From a9be1e059ccf5b7695763956e469346696f785f3 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Tue, 27 Jun 2023 15:07:07 -0500 Subject: [PATCH 2/4] Use a new dialer type --- mssql.go | 11 ++++++++++- protocol.go | 8 ++++---- tds_login_test.go | 13 +++++++++++++ tds_test.go | 16 ++++++++++++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/mssql.go b/mssql.go index a875e8a4..23a271d2 100644 --- a/mssql.go +++ b/mssql.go @@ -194,7 +194,9 @@ type Connector struct { // SessionInitSQL is empty. SessionInitSQL string - // Dialer sets a custom dialer for all network operations. + // Dialer sets a custom dialer for all network operations, except DNS resolution unless + // the dialer implements the HostDialer. + // // If Dialer is not set, normal net dialers are used. Dialer Dialer } @@ -203,6 +205,13 @@ type Dialer interface { DialContext(ctx context.Context, network string, addr string) (net.Conn, error) } +// HostDialer should be used if the dialer is proxying requests to a different network +// and DNS should be resolved in that other network +type HostDialer interface { + Dialer + HostName() string +} + func (c *Connector) getDialer(p *msdsn.Config) Dialer { if c != nil && c.Dialer != nil { return c.Dialer diff --git a/protocol.go b/protocol.go index 615727ea..f7635d50 100644 --- a/protocol.go +++ b/protocol.go @@ -51,10 +51,10 @@ func (t tcpDialer) DialSqlConnection(ctx context.Context, c *Connector, p *msdsn var ips []net.IP ip := net.ParseIP(p.Host) if ip == nil { - // if the dialer has been updated, the dialer may be proxying to a different network, and so the - // dialer should be used to connect so the DNS is resolved within the right network - if c != nil && c.Dialer != nil { - d := c.getDialer(p) + // if the custom dialer is a host dialer, the DNS is resolved within the network + // the dialer is sending the request to, rather than the one the driver is running on + d := c.getDialer(p) + if _, ok := d.(HostDialer); ok { addr := net.JoinHostPort(p.Host, strconv.Itoa(int(resolveServerPort(p.Port)))) return d.DialContext(ctx, "tcp", addr) } diff --git a/tds_login_test.go b/tds_login_test.go index 70a6b293..780ae589 100644 --- a/tds_login_test.go +++ b/tds_login_test.go @@ -43,6 +43,19 @@ func (d *MockTransportDialer) DialContext(ctx context.Context, network string, a return d.client, nil } +type MockHostTransportDialer struct { + Dialer *MockTransportDialer + Host string +} + +func (m MockHostTransportDialer) DialContext(ctx context.Context, network string, addr string) (conn net.Conn, err error) { + return m.Dialer.DialContext(ctx, network, addr) +} + +func (m MockHostTransportDialer) HostName() string { + return m.Host +} + func testLoginSequenceServer(result chan error, conn net.Conn, expectedPackets, responsePackets []string) { defer func() { conn.Close() diff --git a/tds_test.go b/tds_test.go index 38ede43d..3c3fb074 100644 --- a/tds_test.go +++ b/tds_test.go @@ -988,12 +988,16 @@ func TestDialSqlConnectionCustomDialer(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() - // if a dialer is specified, the dialer should be used to make the connection + // if a host dialer is specified, the dialer should be used to resolve the DNS mock := NewMockTransportDialer( []string{}, []string{}, ) - connector.Dialer = mock + connector.Dialer = MockHostTransportDialer{ + Dialer: mock, + Host: params.Host, + } + if mock.count != 0 { t.Error("expecting no connections") } @@ -1011,4 +1015,12 @@ func TestDialSqlConnectionCustomDialer(t *testing.T) { if err != nil { t.Error(err) } + + // if it is not a host dialer, the dialer should not be used to resolve DNS + connector.Dialer = mock + sqlDialer, _ = msdsn.ProtocolDialers["tcp"].(MssqlProtocolDialer) + _, err = sqlDialer.DialSqlConnection(ctx, connector, ¶ms) + if !strings.Contains(err.Error(), "no such host") { + t.Error(fmt.Errorf("dialer should not be used to resolve dns if not a host dialer")) + } } From 9a3698bba25f22097388be8ec239fe996541f7ed Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Thu, 29 Jun 2023 16:03:06 -0500 Subject: [PATCH 3/4] fix unit test --- tds_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tds_test.go b/tds_test.go index 3c3fb074..448491b8 100644 --- a/tds_test.go +++ b/tds_test.go @@ -1020,7 +1020,7 @@ func TestDialSqlConnectionCustomDialer(t *testing.T) { connector.Dialer = mock sqlDialer, _ = msdsn.ProtocolDialers["tcp"].(MssqlProtocolDialer) _, err = sqlDialer.DialSqlConnection(ctx, connector, ¶ms) - if !strings.Contains(err.Error(), "no such host") { + if err == nil { t.Error(fmt.Errorf("dialer should not be used to resolve dns if not a host dialer")) } } From d376e9e8754fa265fec42ed24cf1aa2108cf3cf4 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Thu, 29 Jun 2023 16:40:42 -0500 Subject: [PATCH 4/4] Add changelog & readme --- CHANGELOG.md | 6 ++++++ README.md | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 433a3846..7b954d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.2.0 + +### Features + +* A connector's dialer can now be used to resolve DNS if the dialer implements the `HostDialer` interface + ## 1.0.0 ### Features diff --git a/README.md b/README.md index 03162f3e..48dd471a 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ Other supported formats are listed below. If no pipe name can be derived from the DSN, connection attempts will first query the SQL Browser service to find the pipe name for the instance. +### DNS Resolution through a Custom Dialer + +Custom Dialers can be used to resolve DNS if the Connection's Dialer implements the `HostDialer` interface. This is helpful when the dialer is proxying requests to a different, private network and the DNS record is local to the private network. + ### Protocol configuration To force a specific protocol for the connection there two several options: