MSSQL: Add support for MI authentication to MSSQL (#73597)
* Add support for MI authentication to MSSQL This adds support for managed identity authentication for MSSQL managed instances running in Azure. Co-authored-by: baldm0mma <jev.forsberg@grafana.com>
This commit is contained in:
co-authored by
baldm0mma
parent
3ccfa5620f
commit
579709c7a6
+19
-5
@@ -5,18 +5,19 @@ import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
mssql "github.com/grafana/go-mssqldb"
|
||||
sdkproxy "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
|
||||
"github.com/grafana/grafana/pkg/tsdb/sqleng"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
mssql "github.com/microsoft/go-mssqldb"
|
||||
"golang.org/x/net/proxy"
|
||||
"xorm.io/core"
|
||||
)
|
||||
|
||||
// createMSSQLProxyDriver creates and registers a new sql driver that uses a mssql connector and updates the dialer to
|
||||
// route connections through the secure socks proxy
|
||||
func createMSSQLProxyDriver(cnnstr string, opts *sdkproxy.Options) (string, error) {
|
||||
func createMSSQLProxyDriver(cnnstr string, hostName string, opts *sdkproxy.Options) (string, error) {
|
||||
sqleng.XormDriverMu.Lock()
|
||||
defer sqleng.XormDriverMu.Unlock()
|
||||
|
||||
@@ -34,7 +35,7 @@ func createMSSQLProxyDriver(cnnstr string, opts *sdkproxy.Options) (string, erro
|
||||
return "", err
|
||||
}
|
||||
|
||||
driver, err := newMSSQLProxyDriver(connector, opts)
|
||||
driver, err := newMSSQLProxyDriver(connector, hostName, opts)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -45,6 +46,19 @@ func createMSSQLProxyDriver(cnnstr string, opts *sdkproxy.Options) (string, erro
|
||||
return driverName, nil
|
||||
}
|
||||
|
||||
type HostTransportDialer struct {
|
||||
Dialer proxy.ContextDialer
|
||||
Host string
|
||||
}
|
||||
|
||||
func (m HostTransportDialer) DialContext(ctx context.Context, network string, addr string) (conn net.Conn, err error) {
|
||||
return m.Dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
func (m HostTransportDialer) HostName() string {
|
||||
return m.Host
|
||||
}
|
||||
|
||||
// mssqlProxyDriver is a regular mssql driver with an updated dialer.
|
||||
// This is needed because there is no way to save a dialer to the mssql driver in xorm
|
||||
type mssqlProxyDriver struct {
|
||||
@@ -56,7 +70,7 @@ var _ core.Driver = (*mssqlProxyDriver)(nil)
|
||||
|
||||
// newMSSQLProxyDriver updates the dialer for a mssql connector with a dialer that proxys connections through the secure socks proxy
|
||||
// and returns a new mssql driver to register
|
||||
func newMSSQLProxyDriver(connector *mssql.Connector, opts *sdkproxy.Options) (*mssqlProxyDriver, error) {
|
||||
func newMSSQLProxyDriver(connector *mssql.Connector, hostName string, opts *sdkproxy.Options) (*mssqlProxyDriver, error) {
|
||||
dialer, err := sdkproxy.Cli.NewSecureSocksProxyContextDialer(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -67,7 +81,7 @@ func newMSSQLProxyDriver(connector *mssql.Connector, opts *sdkproxy.Options) (*m
|
||||
return nil, errors.New("unable to cast socks proxy dialer to context proxy dialer")
|
||||
}
|
||||
|
||||
connector.Dialer = contextDialer
|
||||
connector.Dialer = HostTransportDialer{contextDialer, hostName}
|
||||
return &mssqlProxyDriver{c: connector}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user