Add SqlServer MSI Support (#591)

* MSI Auth for SQL Server

Add MSI Auth option to SQL Server connection. Default if no password is provided in connection string.

* Parse resource endpoint from server for msi

update host name parsing to get just the resource endpoint for msi

* Update go-mssqldb

update go-mssqldb to resolve panic issue referenced here: https://github.com/denisenkom/go-mssqldb/pull/642

* Update sqlserver.go

switch from deprecated methods. NewServicePrincipalTokenFromManagedIdentity calls the two methods that are deprecated for us

* Update sqlserver.go

add useMsi param instead of looking for nil password

* Update sqlserver readme

Update sqlserver readme for msi auth. make useMsi a bit safer

* Update sqlserver.go

remove comment

* Update database/sqlserver/README.md

Co-authored-by: Keegan Campbell <[email protected]>

* Update sqlserver.go

refactor resource uri logic into its own method

* Update sqlserver_test.go

add tests for msi connection. can only test whether it fails with useMsi= true, or succeeds with useMsi=false

* Update sqlserver.go

check msi.EnsureFresh return value

* Return error for multiple auth and move query filter

move migrate.FilterCustomQuery(purl).String() into one line out of if/else. return error if both useMsi=true and password are passed

* Update README.md

update readme with warning for useMsi

* Update sqlserver_test.go

Update TestMsiFalse test case as now it should fail when useMsi=false and no password is provided

Co-authored-by: Keegan Campbell <[email protected]>
This commit is contained in:
Sam Fox
2021-07-01 23:08:44 -07:00
committed by GitHub
co-authored by Keegan Campbell
parent bef0635743
commit dd30be4433
6 changed files with 153 additions and 20 deletions
+71
View File
@@ -38,6 +38,14 @@ func msConnectionString(host, port string) string {
return fmt.Sprintf("sqlserver://sa:%v@%v:%v?database=master", saPassword, host, port)
}
func msConnectionStringMsiWithPassword(host, port string, useMsi bool) string {
return fmt.Sprintf("sqlserver://sa:%v@%v:%v?database=master&useMsi=%t", saPassword, host, port, useMsi)
}
func msConnectionStringMsi(host, port string, useMsi bool) string {
return fmt.Sprintf("sqlserver://sa@%v:%v?database=master&useMsi=%t", host, port, useMsi)
}
func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
ip, port, err := c.Port(defaultPort)
if err != nil {
@@ -218,3 +226,66 @@ func TestLockWorks(t *testing.T) {
}
})
}
func TestMsiTrue(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(defaultPort)
if err != nil {
t.Fatal(err)
}
addr := msConnectionStringMsi(ip, port, true)
p := &SQLServer{}
_, err = p.Open(addr)
if err == nil {
t.Fatal("MSI should fail when not running in an Azure context.")
}
})
}
func TestOpenWithPasswordAndMSI(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(defaultPort)
if err != nil {
t.Fatal(err)
}
addr := msConnectionStringMsiWithPassword(ip, port, true)
p := &SQLServer{}
_, err = p.Open(addr)
if err == nil {
t.Fatal("Open should fail when both password and useMsi=true are passed.")
}
addr = msConnectionStringMsiWithPassword(ip, port, false)
p = &SQLServer{}
d, err := p.Open(addr)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := d.Close(); err != nil {
t.Error(err)
}
}()
dt.Test(t, d, []byte("SELECT 1"))
})
}
func TestMsiFalse(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(defaultPort)
if err != nil {
t.Fatal(err)
}
addr := msConnectionStringMsi(ip, port, false)
p := &SQLServer{}
_, err = p.Open(addr)
if err == nil {
t.Fatal("Open should fail since no password was passed and useMsi is false.")
}
})
}