Files
teleport/lib/client/conntest/database/sqlserver.go
T
2023-12-01 17:48:14 +00:00

83 lines
2.5 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package database
import (
"context"
"database/sql"
"net"
"net/url"
"strconv"
"strings"
"github.com/gravitational/trace"
_ "github.com/microsoft/go-mssqldb"
"github.com/gravitational/teleport/lib/defaults"
)
// SQLServerPinger implements the DatabasePinger interface for the SQL Server
// protocol.
type SQLServerPinger struct{}
// Ping tests the connection to the Database with a simple request.
func (p *SQLServerPinger) Ping(ctx context.Context, params PingParams) error {
if err := params.CheckAndSetDefaults(defaults.ProtocolPostgres); err != nil {
return trace.Wrap(err)
}
query := url.Values{}
query.Add("database", params.DatabaseName)
u := &url.URL{
Scheme: "sqlserver",
User: url.User(params.Username),
Host: net.JoinHostPort(params.Host, strconv.Itoa(params.Port)),
RawQuery: query.Encode(),
}
db, err := sql.Open("sqlserver", u.String())
if err != nil {
return trace.Wrap(err)
}
defer db.Close()
err = db.PingContext(ctx)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// IsConnectionRefusedError returns whether the error is referring to a connection refused.
func (p *SQLServerPinger) IsConnectionRefusedError(err error) bool {
return strings.Contains(err.Error(), "unable to open tcp connection with host")
}
// IsInvalidDatabaseUserError returns whether the error is referring to an invalid (non-existent) user.
func (p *SQLServerPinger) IsInvalidDatabaseUserError(err error) bool {
return strings.Contains(err.Error(), "authentication failed")
}
// IsInvalidDatabaseNameError returns whether the error is referring to an invalid (non-existent) database name.
func (p *SQLServerPinger) IsInvalidDatabaseNameError(err error) bool {
return strings.Contains(err.Error(), "Cannot open database")
}