Skip to content

Commit 860c66b

Browse files
fix: use ActiveDirectoryDefault for -G with no username (#754)
1 parent 1d6f9ff commit 860c66b

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

cmd/sqlcmd/sqlcmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ func (a SQLCmdArguments) authenticationMethod(hasPassword bool) string {
207207
if a.UseAad {
208208
switch {
209209
case a.UserName == "":
210-
return azuread.ActiveDirectoryIntegrated
210+
// Default walks the Azure credential chain; works in non-interactive shells (issue #699).
211+
return azuread.ActiveDirectoryDefault
211212
case hasPassword:
212213
return azuread.ActiveDirectoryPassword
213214
default:

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,26 @@ func TestConditionsForPasswordPrompt(t *testing.T) {
525525
}
526526
}
527527

528+
func TestAuthenticationMethodForUseAad(t *testing.T) {
529+
tests := []struct {
530+
name string
531+
userName string
532+
hasPassword bool
533+
expected string
534+
}{
535+
{"-G no username picks Default", "", false, azuread.ActiveDirectoryDefault},
536+
{"-G no username, password ignored", "", true, azuread.ActiveDirectoryDefault},
537+
{"-G with username, no password picks Interactive", "user@contoso", false, azuread.ActiveDirectoryInteractive},
538+
{"-G with username and password picks Password", "user@contoso", true, azuread.ActiveDirectoryPassword},
539+
}
540+
for _, tc := range tests {
541+
t.Run(tc.name, func(t *testing.T) {
542+
a := SQLCmdArguments{UseAad: true, UserName: tc.userName}
543+
assert.Equal(t, tc.expected, a.authenticationMethod(tc.hasPassword))
544+
})
545+
}
546+
}
547+
528548
func TestStartupScript(t *testing.T) {
529549
o, err := os.CreateTemp("", "sqlcmdmain")
530550
assert.NoError(t, err, "os.CreateTemp")

0 commit comments

Comments
 (0)