Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mssql): retire the go-mssqldb fork and stop sending credentials twice (#21879)
Danny Xudc46975199ae
diff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.goindex 21ca002ec0..a4f3c5f2cf 100644--- a/backend/plugin/db/mssql/mssql.go+++ b/backend/plugin/db/mssql/mssql.go@@ -8,6 +8,7 @@ import ("io""log/slog""maps"+ "net""net/url""os""slices"@@ -85,17 +86,23 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionC// written to disk, or empty.//// go-mssqldb matches parameter keys case-insensitively and accepts each key once, so Extra-// Parameters merge by lowercased key. They override the app name and tlsmin defaults, never the-// database or the Azure credential. With TLS mode TLS, the TLS section sets a floor that Extra-// Parameters may raise but not lower: encrypt=true unless they ask for encrypt=strict, and-// TrustServerCertificate=false when Verify server certificate is on or they ask for it. The-// section's CA replaces a certificate parameter. With TLS mode Disabled, Extra Parameters apply as-// given over a TrustServerCertificate=true default.+// Parameters merge by lowercased key. The URL form of a DSN already derives user id and password+// from the userinfo and server and port from the host, and the driver rejects a second value for+// any of them, so the credentials travel in the query and the server and port in the host, each+// once. A named instance has to go in the path, because the host cannot hold the backslash of+// Server=host\instance. Extra Parameters override the app name, tlsmin, credentials, server and+// port, never the database or the Azure credential. With TLS mode TLS, the TLS section sets a+// floor that Extra Parameters may raise but not lower: encrypt=true unless they ask for+// encrypt=strict, and TrustServerCertificate=false when Verify server certificate is on or they+// ask for it. The section's CA replaces a certificate parameter. With TLS mode Disabled, Extra+// Parameters apply as given over a TrustServerCertificate=true default.func connectionDSN(ds *storepb.DataSource, password, database, caFile string) (string, string) {params := map[string]string{msdsn.AppName: "bytebase",// Older SQL Server versions offer only TLS 1.0: https://github.com/microsoft/go-mssqldb/issues/33.- msdsn.TLSMin: "1.0",+ msdsn.TLSMin: "1.0",+ msdsn.UserID: ds.Username,+ msdsn.Password: password,}extra := ds.GetExtraConnectionParameters()// Sorted, so keys that differ only in case resolve the same way every time.@@ -126,22 +133,40 @@ func connectionDSN(ds *storepb.DataSource, password, database, caFile string) (sparams["fedauth"] = azuread.ActiveDirectoryServicePrincipalparams[msdsn.UserID] = fmt.Sprintf("%s@%s", credential.ClientId, credential.TenantId)params[msdsn.Password] = credential.ClientSecret- password = ""} else {params["fedauth"] = azuread.ActiveDirectoryDefault}}+ host, port := ds.Host, ds.Port+ if server, ok := params[msdsn.Server]; ok {+ delete(params, msdsn.Server)+ host = server+ }+ if value, ok := params[msdsn.Port]; ok {+ delete(params, msdsn.Port)+ port = value+ }+ instance := ""+ if before, after, ok := strings.Cut(host, `\`); ok {+ host, instance = before, after+ }+ // net.JoinHostPort brackets an IPv6 literal, which the URL host needs and the driver strips+ // back off, so a value that already carries brackets must not get a second pair.+ host = strings.TrimSuffix(strings.TrimPrefix(host, "["), "]")+query := url.Values{}for key, value := range params {query.Set(key, value)}u := &url.URL{Scheme: "sqlserver",- User: url.UserPassword(ds.Username, password),- Host: fmt.Sprintf("%s:%s", ds.Host, ds.Port),+ Host: net.JoinHostPort(host, port),RawQuery: query.Encode(),}+ if instance != "" {+ u.Path = "/" + instance+ }return driverName, u.String()}diff --git a/backend/plugin/db/mssql/mssql_test.go b/backend/plugin/db/mssql/mssql_test.goindex a20d92a052..f9c8717b3b 100644--- a/backend/plugin/db/mssql/mssql_test.go+++ b/backend/plugin/db/mssql/mssql_test.go@@ -38,8 +38,6 @@ func TestConnectionDSNAppliesTLSSettings(t *testing.T) {}sectionCA, sectionPool := writeCA("section.pem")extraCA, extraPool := writeCA("extra.pem")- systemPool, err := x509.SystemCertPool()- require.NoError(t, err)for _, tc := range []struct {name string@@ -48,26 +46,29 @@ func TestConnectionDSNAppliesTLSSettings(t *testing.T) {caFile stringextra map[string]stringencryption msdsn.Encryption- // roots verify the server certificate; nil means any certificate is accepted.- roots *x509.CertPool+ // roots verify the server certificate; nil means any certificate is accepted,+ // unless systemRoots asks for the host's trust store, which crypto/tls takes+ // from an unset RootCAs.+ roots *x509.CertPool+ systemRoots bool}{{name: "disabled", encryption: msdsn.EncryptionOff},{name: "disabled ignores verify", verify: true, encryption: msdsn.EncryptionOff},{name: "tls", useSSL: true, encryption: msdsn.EncryptionRequired},{name: "tls with a CA", useSSL: true, caFile: sectionCA, encryption: msdsn.EncryptionRequired},{name: "tls verified against the CA", useSSL: true, verify: true, caFile: sectionCA, encryption: msdsn.EncryptionRequired, roots: sectionPool},- {name: "tls verified against system trust", useSSL: true, verify: true, encryption: msdsn.EncryptionRequired, roots: systemPool},+ {name: "tls verified against system trust", useSSL: true, verify: true, encryption: msdsn.EncryptionRequired, systemRoots: true},{name: "extra parameters cannot turn verification off", useSSL: true, verify: true, caFile: sectionCA, extra: map[string]string{"TrustServerCertificate": "true"}, encryption: msdsn.EncryptionRequired, roots: sectionPool},{name: "extra parameters cannot make encryption optional", useSSL: true, extra: map[string]string{"encrypt": "false"}, encryption: msdsn.EncryptionRequired},{name: "extra parameters cannot disable encryption", useSSL: true, extra: map[string]string{"Encrypt": "DISABLE"}, encryption: msdsn.EncryptionRequired},- {name: "extra parameters can require strict encryption", useSSL: true, extra: map[string]string{"Encrypt": "Strict"}, encryption: msdsn.EncryptionStrict, roots: systemPool},+ {name: "extra parameters can require strict encryption", useSSL: true, extra: map[string]string{"Encrypt": "Strict"}, encryption: msdsn.EncryptionStrict, systemRoots: true},{name: "extra parameters can turn verification on", useSSL: true, caFile: sectionCA, extra: map[string]string{"trustservercertificate": "false"}, encryption: msdsn.EncryptionRequired, roots: sectionPool},{name: "the CA replaces a certificate parameter", useSSL: true, verify: true, caFile: sectionCA, extra: map[string]string{"Certificate": extraCA}, encryption: msdsn.EncryptionRequired, roots: sectionPool},{name: "a certificate parameter replaces system trust", useSSL: true, verify: true, extra: map[string]string{"certificate": extraCA}, encryption: msdsn.EncryptionRequired, roots: extraPool},{name: "disabled applies encrypt", extra: map[string]string{"encrypt": "true"}, encryption: msdsn.EncryptionRequired},- {name: "disabled applies TrustServerCertificate", extra: map[string]string{"TrustServerCertificate": "false"}, encryption: msdsn.EncryptionOff, roots: systemPool},+ {name: "disabled applies TrustServerCertificate", extra: map[string]string{"TrustServerCertificate": "false"}, encryption: msdsn.EncryptionOff, systemRoots: true},{name: "disabled applies encrypt=disable", extra: map[string]string{"encrypt": "disable"}, encryption: msdsn.EncryptionDisabled},} {t.Run(tc.name, func(t *testing.T) {@@ -88,9 +89,14 @@ func TestConnectionDSNAppliesTLSSettings(t *testing.T) {require.Nil(t, cfg.TLSConfig)return}- require.Equal(t, tc.roots == nil, cfg.TLSConfig.InsecureSkipVerify)- if tc.roots != nil {+ verified := tc.roots != nil || tc.systemRoots+ require.Equal(t, !verified, cfg.TLSConfig.InsecureSkipVerify)+ if verified {require.Equal(t, "db.example.com", cfg.TLSConfig.ServerName)+ }+ if tc.systemRoots {+ require.Nil(t, cfg.TLSConfig.RootCAs, "verified against roots other than the host's")+ } else if tc.roots != nil {require.True(t, tc.roots.Equal(cfg.TLSConfig.RootCAs), "verified against the wrong roots")}})@@ -113,6 +119,12 @@ func TestConnectionDSNMergesExtraParameters(t *testing.T) {driverName: "sqlserver",want: map[string]string{"app name": "bytebase", "tlsmin": "1.0", "database": "db1", "user id": "bytebase", "password": "password"},},+ {+ name: "extra parameters override the credentials",+ ds: &storepb.DataSource{Username: "bytebase", ExtraConnectionParameters: map[string]string{"User ID": "other", "Password": "other"}},+ driverName: "sqlserver",+ want: map[string]string{"user id": "other", "password": "other"},+ },{name: "extra parameters override the app name and tlsmin",ds: &storepb.DataSource{ExtraConnectionParameters: map[string]string{"App Name": "etl", "TLSMin": "1.2"}},@@ -161,6 +173,64 @@ func TestConnectionDSNMergesExtraParameters(t *testing.T) {}}+// A named instance is only reachable through Extra Parameters, because the Host field has no room+// for the backslash of Server=host\instance.+func TestConnectionDSNCarriesServerAndPort(t *testing.T) {+ for _, tc := range []struct {+ name string+ ds *storepb.DataSource+ wantHost string+ wantPort uint64+ wantInstance string+ }{+ {+ name: "the server and port come from Bytebase",+ ds: &storepb.DataSource{Host: "db.example.com", Port: "1433"},+ wantHost: "db.example.com",+ wantPort: 1433,+ },+ {+ name: "extra parameters override the server and port",+ ds: &storepb.DataSource{Host: "db.example.com", Port: "1433", ExtraConnectionParameters: map[string]string{"Server": "other.example.com", "Port": "1444"}},+ wantHost: "other.example.com",+ wantPort: 1444,+ },+ {+ name: "an IPv6 server",+ ds: &storepb.DataSource{Host: "db.example.com", Port: "1433", ExtraConnectionParameters: map[string]string{"Server": "2001:db8::1"}},+ wantHost: "2001:db8::1",+ wantPort: 1433,+ },+ {+ name: "an IPv6 server already in brackets",+ ds: &storepb.DataSource{Host: "db.example.com", Port: "1433", ExtraConnectionParameters: map[string]string{"Server": "[2001:db8::1]"}},+ wantHost: "2001:db8::1",+ wantPort: 1433,+ },+ {+ name: "an IPv6 host",+ ds: &storepb.DataSource{Host: "2001:db8::1", Port: "1433"},+ wantHost: "2001:db8::1",+ wantPort: 1433,+ },+ {+ name: "extra parameters reach a named instance",+ ds: &storepb.DataSource{Host: "db.example.com", ExtraConnectionParameters: map[string]string{"Server": `other.example.com\SQLEXPRESS`}},+ wantHost: "other.example.com",+ wantInstance: "SQLEXPRESS",+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ _, dsn := connectionDSN(tc.ds, "password", "", "")+ cfg, err := msdsn.Parse(dsn)+ require.NoError(t, err)+ require.Equal(t, tc.wantHost, cfg.Host)+ require.Equal(t, tc.wantPort, cfg.Port)+ require.Equal(t, tc.wantInstance, cfg.Instance)+ })+ }+}+func TestOpenAppliesTLSSettings(t *testing.T) {t.Parallel()ctx := context.Background()diff --git a/go.mod b/go.modindex 50fddb0e51..af30b19667 100644--- a/go.mod+++ b/go.mod@@ -373,8 +373,6 @@ replace (github.com/jackc/pgx/v5 => github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4- github.com/microsoft/go-mssqldb => github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898-github.com/pingcap/tidb => github.com/bytebase/tidb v0.0.0-20251104040057-d29df9dd1b3bgithub.com/pingcap/tidb/pkg/parser => github.com/bytebase/tidb/pkg/parser v0.0.0-20251104040057-d29df9dd1b3bdiff --git a/go.sum b/go.sumindex db7a99ba66..c3a6758aee 100644--- a/go.sum+++ b/go.sum@@ -70,8 +70,8 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.13.0 h1:Terj1CMW4+NY5CpWjSRkqLgithub.com/Azure/azure-sdk-for-go/sdk/internal v1.13.0/go.mod h1:7Ut2Qw9vcwpLW5Q4MBy43uSxXS7tK2yjs94OJ6j5thE=github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.0 h1:LR0kAX9ykz8G4YgLCaRDVJ3+n43R8MneB5dTy2konZo=github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.0/go.mod h1:DWAciXemNf++PQJLeXUB4HHH5OpsAh12HZnu2wXE1jA=-github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.1 h1:MyVTgWR8qd/Jw1Le0NZebGBUCLbtak3bJ3z1OlqZBpw=-github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.0.1/go.mod h1:GpPjLhVR9dnUoJMyHWSPy71xY9/lcmpzIPZXmF0FCVY=+github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.5.0 h1:MaKvxE6D0KkjOg6Wd9M00iqP5PR0kUxCfiezes4JweM=+github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.5.0/go.mod h1:i2h9fsTFKZorh8RdV2IcSUf/Qj98GlTkrTvUbX/s8as=github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.5.1 h1:f4X/okTwHAfzJamPo8hyStXinb4sCTK36gMZj+KLQdw=github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.5.1/go.mod h1:sEd11NnHY3lizFx/zf5u7x5qNjhF0t5VZM0irRHKXVU=github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.2.0 h1:nCYfgcSyHZXJI8J0IWE5MsCGlb2xp9fJiXyxWgmOFg4=@@ -255,8 +255,6 @@ github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20260424030223-eacgithub.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20260424030223-eac0db373bca/go.mod h1:OwIkGuhFwOV7zicy7lmJl3MRqhkf2OmCSX8NMFEPm4A=github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5 h1:mL04uKgVhYjXsZTvVTk1GbDz0qzzn8Jv+t24fT7/ydM=github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5/go.mod h1:FwG3wgXv1mXHLEXEcgvgM7KIrefWrwYHI44+BLOVO2c=-github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898 h1:0ggqA9XL4yvtePPvRDRYMed3jtdNk4s0o8AMW8uJPSA=-github.com/bytebase/go-mssqldb v0.0.0-20240801091126-3ff3ca07d898/go.mod h1:2KhUz4GSLcdJGXSJMXAf2BjmakcgoOkNyPpHdGAxRpY=github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735 h1:v8y6mXeHmYVsdM8cHul7kXSbOuDPL7yN1XSRMu1zo48=github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735/go.mod h1:vTHjcvfn8Pmf65WjnV1ytTCYyC7xNhFjrniv4YWvtA8=github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=@@ -713,6 +711,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEgithub.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=+github.com/microsoft/go-mssqldb v1.11.2 h1:FCgeBIK8um2+X4tbun6Q71N1KsfyCDPKY41e1yGVjSE=+github.com/microsoft/go-mssqldb v1.11.2/go.mod h1:CYgwG5AMXFojbjTg+GNP5G/y6uz1BhTyZaPqQWzkGnQ=github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpspGNG7Z948v4n35fFGB3RR3G/ry4FWs=github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 h1:+n/aFZefKZp7spd8DFdX7uMikMLXX4oubIzJF4kv/wI=